blob: 5184f80bf1941b0e36c05483bb173c2be4343c89 [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
1041 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001042 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001043 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001044 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001045 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001046 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001047
Joe Onorato3a8820b2009-11-10 15:06:42 -08001048 final View v = getWindow().peekDecorView();
1049 if (v != null && v.getWindowToken() != null) {
1050 InputMethodManager imm = (InputMethodManager)getSystemService(
1051 INPUT_METHOD_SERVICE);
1052 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054 }
1055 }
1056
1057 @Override
1058 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1059 // Do not call super here
1060 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001061
1062 if (mHomeCustomizationDrawer != null) {
1063 String cur = savedInstanceState.getString("currentTab");
1064 if (cur != null) {
1065 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1066 }
1067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 }
1069
1070 @Override
1071 protected void onSaveInstanceState(Bundle outState) {
1072 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1073
1074 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1075 if (folders.size() > 0) {
1076 final int count = folders.size();
1077 long[] ids = new long[count];
1078 for (int i = 0; i < count; i++) {
1079 final FolderInfo info = folders.get(i).getInfo();
1080 ids[i] = info.id;
1081 }
1082 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1083 } else {
1084 super.onSaveInstanceState(outState);
1085 }
1086
Joe Onoratofb0ca672009-09-14 17:55:46 -04001087 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001088 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091
1092 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1093 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1094 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1095
1096 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1097 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1098 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1099 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1100 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1101 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1102 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1103 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001104 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 }
1106
1107 if (mFolderInfo != null && mWaitingForResult) {
1108 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1109 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1110 }
Michael Jurka946ad472010-07-09 18:05:18 -07001111
1112 if (mHomeCustomizationDrawer != null) {
1113 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1114 if (currentTabTag != null) {
1115 outState.putString("currentTab", currentTabTag);
1116 }
1117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119
1120 @Override
1121 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001125 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001127 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129
1130 TextKeyListener.getInstance().release();
1131
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001135
1136 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001137
Patrick Dubroyab962b72010-07-26 12:10:10 -07001138 // Some launcher layouts don't have a previous and next view
1139 if (mPreviousView != null) {
1140 dismissPreview(mPreviousView);
1141 }
1142 if (mNextView != null) {
1143 dismissPreview(mNextView);
1144 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001145
1146 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
1148
1149 @Override
1150 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001151 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 super.startActivityForResult(intent, requestCode);
1153 }
1154
1155 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001156 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001158
Joe Onorato7bb17492009-09-24 17:51:01 -07001159 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Karl Rosaen138a0412009-04-23 19:00:21 -07001161 if (initialQuery == null) {
1162 // Use any text typed in the launcher as the initial query
1163 initialQuery = getTypedText();
1164 clearTypedText();
1165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 if (appSearchData == null) {
1167 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001168 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
Romain Guycbb89e42009-06-08 15:52:54 -07001170
Karl Rosaen138a0412009-04-23 19:00:21 -07001171 final SearchManager searchManager =
1172 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001173 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001174 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176
1177 @Override
1178 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 if (isWorkspaceLocked()) {
1180 return false;
1181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182
1183 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1186 .setIcon(android.R.drawable.ic_menu_add)
1187 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001188 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 .setIcon(android.R.drawable.ic_menu_gallery)
1190 .setAlphabeticShortcut('W');
1191 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1192 .setIcon(android.R.drawable.ic_search_category_default)
1193 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1194 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1195 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1196 .setAlphabeticShortcut('N');
1197
1198 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001199 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1200 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
1202 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1203 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1204 .setIntent(settings);
1205
1206 return true;
1207 }
1208
1209 @Override
1210 public boolean onPrepareOptionsMenu(Menu menu) {
1211 super.onPrepareOptionsMenu(menu);
1212
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213 // If all apps is animating, don't show the menu, because we don't know
1214 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001215 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 return false;
1217 }
1218
1219 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001220 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1222 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1223
1224 // Disable add if the workspace is full.
1225 if (visible) {
1226 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1227 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
1230 return true;
1231 }
1232
Michael Jurka0e260592010-06-30 17:07:39 -07001233 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1234 // using the settings menu to add an item, something similar happens in showAddDialog
1235 public void prepareAddItemFromHomeCustomizationDrawer() {
1236 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1237 mAddItemCellInfo = mMenuAddInfo;
1238 }
1239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 @Override
1241 public boolean onOptionsItemSelected(MenuItem item) {
1242 switch (item.getItemId()) {
1243 case MENU_ADD:
1244 addItems();
1245 return true;
1246 case MENU_WALLPAPER_SETTINGS:
1247 startWallpaper();
1248 return true;
1249 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001250 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 return true;
1252 case MENU_NOTIFICATIONS:
1253 showNotifications();
1254 return true;
1255 }
1256
1257 return super.onOptionsItemSelected(item);
1258 }
Romain Guycbb89e42009-06-08 15:52:54 -07001259
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 /**
1261 * Indicates that we want global search for this activity by setting the globalSearch
1262 * argument for {@link #startSearch} to true.
1263 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001265 @Override
1266 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001267 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001268 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001269 }
1270
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 public boolean isWorkspaceLocked() {
1272 return mWorkspaceLoading || mWaitingForResult;
1273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001276 if (LauncherApplication.isScreenXLarge()) {
1277 // Animate the widget chooser up from the bottom of the screen
1278 if (!isCustomizationDrawerVisible()) {
1279 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001280 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001281 }
1282 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001283 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001284 showAddDialog(mMenuAddInfo);
1285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
Michael Jurkaaf442092010-06-10 17:01:57 -07001288 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1289 mAddItemCellInfo = cellInfo;
1290 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1291 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1292 addAppWidgetImpl(appWidgetId);
1293 }
1294
1295 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001296 // TODO: catch bad widget exception when sent
1297 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001298 // TODO: Is this log message meaningful?
1299 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1300 addAppWidgetImpl(appWidgetId);
1301 }
1302
1303 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001304 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305
Bjorn Bringert7984c942009-12-09 15:38:25 +00001306 if (appWidget.configure != null) {
1307 // Launch over to configure widget, if needed
1308 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1309 intent.setComponent(appWidget.configure);
1310 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1311
Romain Guy8e633c52010-03-04 12:51:36 -08001312 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001314 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001315 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317 }
Romain Guycbb89e42009-06-08 15:52:54 -07001318
Joe Onoratodeb98af2010-02-19 14:59:39 -08001319 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001320 // Handle case where user selected "Applications"
1321 String applicationName = getResources().getString(R.string.group_applications);
1322 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001324 if (applicationName != null && applicationName.equals(shortcutName)) {
1325 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1326 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001327
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001328 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1329 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001330 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001331 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001332 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
1336 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001337 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001338 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001339 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001340
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001341 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001342 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001343 } else {
1344 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 UserFolderInfo folderInfo = new UserFolderInfo();
1350 folderInfo.title = getText(R.string.folder_name);
1351
1352 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1353 cellInfo.screen = mWorkspace.getCurrentScreen();
1354 if (!findSingleSlot(cellInfo)) return;
1355
1356 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 LauncherModel.addItemToDatabase(this, folderInfo,
1358 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
1362 // Create the view
1363 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1364 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1365 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 void removeFolder(FolderInfo folder) {
1370 mFolders.remove(folder.id);
1371 }
1372
1373 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 cellInfo.screen = mWorkspace.getCurrentScreen();
1375 if (!findSingleSlot(cellInfo)) return;
1376
1377 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1378
1379 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1381 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001382 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1383 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385 }
1386
1387 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1388 CellLayout.CellInfo cellInfo, boolean notify) {
1389
1390 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1391 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1392
1393 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 Intent.ShortcutIconResource iconResource = null;
1395
1396 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1397 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1398 try {
1399 iconResource = (Intent.ShortcutIconResource) extra;
1400 final PackageManager packageManager = context.getPackageManager();
1401 Resources resources = packageManager.getResourcesForApplication(
1402 iconResource.packageName);
1403 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1404 icon = resources.getDrawable(id);
1405 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001406 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408 }
1409
1410 if (icon == null) {
1411 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1412 }
1413
1414 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001415 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 info.title = name;
1417 info.iconResource = iconResource;
1418 info.uri = data.getData();
1419 info.baseIntent = baseIntent;
1420 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1421 LiveFolders.DISPLAY_MODE_GRID);
1422
1423 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1424 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426
1427 return info;
1428 }
1429
1430 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1431 final int[] xy = new int[2];
1432 if (findSlot(cellInfo, xy, 1, 1)) {
1433 cellInfo.cellX = xy[0];
1434 cellInfo.cellY = xy[1];
1435 return true;
1436 }
1437 return false;
1438 }
1439
1440 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1441 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1442 boolean[] occupied = mSavedState != null ?
1443 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1444 cellInfo = mWorkspace.findAllVacantCells(occupied);
1445 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1446 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1447 return false;
1448 }
1449 }
1450 return true;
1451 }
1452
1453 private void showNotifications() {
1454 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1455 if (statusBar != null) {
1456 statusBar.expand();
1457 }
1458 }
1459
1460 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001461 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001463 Intent chooser = Intent.createChooser(pickWallpaper,
1464 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001465 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1466 // Removed in Eclair MR1
1467// WallpaperManager wm = (WallpaperManager)
1468// getSystemService(Context.WALLPAPER_SERVICE);
1469// WallpaperInfo wi = wm.getWallpaperInfo();
1470// if (wi != null && wi.getSettingsActivity() != null) {
1471// LabeledIntent li = new LabeledIntent(getPackageName(),
1472// R.string.configure_wallpaper, 0);
1473// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1474// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1475// }
Mike Clerona0618e42009-10-22 13:55:21 -07001476 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001479 /**
1480 * Registers various content observers. The current implementation registers
1481 * only a favorites observer to keep track of the favorites applications.
1482 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001483 private void registerContentObservers() {
1484 ContentResolver resolver = getContentResolver();
1485 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1486 true, mWidgetObserver);
1487 }
1488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 @Override
1490 public boolean dispatchKeyEvent(KeyEvent event) {
1491 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1492 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001494 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001495 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001496 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001497 dumpState();
1498 return true;
1499 }
1500 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001501 }
1502 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1503 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001504 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 return true;
1506 }
1507 }
1508
1509 return super.dispatchKeyEvent(event);
1510 }
1511
Joe Onorato88ec0992009-11-19 13:16:06 -08001512 @Override
1513 public void onBackPressed() {
1514 if (isAllAppsVisible()) {
1515 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001516 } else if (isCustomizationDrawerVisible()) {
1517 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001518 } else {
1519 closeFolder();
1520 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001521 // Some launcher layouts don't have a previous and next view
1522 if (mPreviousView != null) {
1523 dismissPreview(mPreviousView);
1524 dismissPreview(mNextView);
1525 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001526 }
1527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 private void closeFolder() {
1529 Folder folder = mWorkspace.getOpenFolder();
1530 if (folder != null) {
1531 closeFolder(folder);
1532 }
1533 }
1534
1535 void closeFolder(Folder folder) {
1536 folder.getInfo().opened = false;
1537 ViewGroup parent = (ViewGroup) folder.getParent();
1538 if (parent != null) {
1539 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001540 if (folder instanceof DropTarget) {
1541 // Live folders aren't DropTargets.
1542 mDragController.removeDropTarget((DropTarget)folder);
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545 folder.onClose();
1546 }
1547
1548 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001549 * Re-listen when widgets are reset.
1550 */
1551 private void onAppWidgetReset() {
1552 mAppWidgetHost.startListening();
1553 }
1554
1555 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1557 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 private void unbindDesktopItems() {
1560 for (ItemInfo item: mDesktopItems) {
1561 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 /**
1566 * Launches the intent referred by the clicked shortcut.
1567 *
1568 * @param v The view representing the clicked shortcut.
1569 */
1570 public void onClick(View v) {
1571 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001572 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001574 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001575 int[] pos = new int[2];
1576 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001577 intent.setSourceBounds(new Rect(pos[0], pos[1],
1578 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001579 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 } else if (tag instanceof FolderInfo) {
1581 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001582 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001583 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001584 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001585 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001586 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589 }
1590
Michael Jurka0e260592010-06-30 17:07:39 -07001591 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001592 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001593 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001594 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001595 return false;
1596 }
1597
Michael Jurkaaf442092010-06-10 17:01:57 -07001598 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001599 * Event handler for the search button
1600 *
1601 * @param v The view that was clicked.
1602 */
1603 public void onClickSearchButton(View v) {
1604 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1605 View button = findViewById(R.id.search_button);
1606 i.setSourceBounds(
1607 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1608 startActivity(i);
1609 }
1610
1611 /**
1612 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001613 * enters home screen customization mode.
1614 *
1615 * @param v The view that was clicked.
1616 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001617 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001618 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001619 }
1620
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001621 /**
1622 * Event handler for the "grid" button that appears on the home screen, which
1623 * enters all apps mode.
1624 *
1625 * @param v The view that was clicked.
1626 */
1627 public void onClickAllAppsButton(View v) {
1628 showAllApps(true);
1629 }
1630
Joe Onoratof984e852010-03-25 09:47:45 -07001631 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1633 try {
1634 startActivity(intent);
1635 } catch (ActivityNotFoundException e) {
1636 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001637 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 } catch (SecurityException e) {
1639 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001640 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001642 "or use the exported attribute for this activity. "
1643 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 }
1645 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001646
Romain Guy8e633c52010-03-04 12:51:36 -08001647 void startActivityForResultSafely(Intent intent, int requestCode) {
1648 try {
1649 startActivityForResult(intent, requestCode);
1650 } catch (ActivityNotFoundException e) {
1651 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1652 } catch (SecurityException e) {
1653 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1654 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1655 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1656 "or use the exported attribute for this activity.", e);
1657 }
1658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659
1660 private void handleFolderClick(FolderInfo folderInfo) {
1661 if (!folderInfo.opened) {
1662 // Close any open folder
1663 closeFolder();
1664 // Open the requested folder
1665 openFolder(folderInfo);
1666 } else {
1667 // Find the open folder...
1668 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1669 int folderScreen;
1670 if (openFolder != null) {
1671 folderScreen = mWorkspace.getScreenForView(openFolder);
1672 // .. and close it
1673 closeFolder(openFolder);
1674 if (folderScreen != mWorkspace.getCurrentScreen()) {
1675 // Close any folder open on the current screen
1676 closeFolder();
1677 // Pull the folder onto this screen
1678 openFolder(folderInfo);
1679 }
1680 }
1681 }
1682 }
1683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 /**
1685 * Opens the user fodler described by the specified tag. The opening of the folder
1686 * is animated relative to the specified View. If the View is null, no animation
1687 * is played.
1688 *
1689 * @param folderInfo The FolderInfo describing the folder to open.
1690 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001691 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 Folder openFolder;
1693
1694 if (folderInfo instanceof UserFolderInfo) {
1695 openFolder = UserFolder.fromXml(this);
1696 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001697 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 } else {
1699 return;
1700 }
1701
Joe Onorato00acb122009-08-04 16:04:30 -04001702 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 openFolder.setLauncher(this);
1704
1705 openFolder.bind(folderInfo);
1706 folderInfo.opened = true;
1707
Winson Chungaafa03c2010-06-11 17:34:16 -07001708 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 openFolder.onOpen();
1711 }
1712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001714 switch (v.getId()) {
1715 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001716 if (!isAllAppsVisible()) {
1717 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1718 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001719 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001720 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001721 return true;
1722 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001723 if (!isAllAppsVisible()) {
1724 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1725 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001726 showPreviews(v);
1727 }
1728 return true;
1729 case R.id.all_apps_button:
1730 if (!isAllAppsVisible()) {
1731 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1732 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1733 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001734 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001735 return true;
1736 }
1737
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 return false;
1740 }
1741
1742 if (!(v instanceof CellLayout)) {
1743 v = (View) v.getParent();
1744 }
1745
1746 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1747
1748 // This happens when long clicking an item with the dpad/trackball
1749 if (cellInfo == null) {
1750 return true;
1751 }
1752
1753 if (mWorkspace.allowLongPress()) {
1754 if (cellInfo.cell == null) {
1755 if (cellInfo.valid) {
1756 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001757 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001758 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1759 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 showAddDialog(cellInfo);
1761 }
1762 } else {
1763 if (!(cellInfo.cell instanceof Folder)) {
1764 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001765 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1766 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 mWorkspace.startDrag(cellInfo);
1768 }
1769 }
1770 }
1771 return true;
1772 }
1773
Romain Guye6b8e2f2009-11-10 11:56:55 -08001774 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001775 private void dismissPreview(final View v) {
1776 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001777 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001778 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1779 public void onDismiss() {
1780 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1781 int count = group.getChildCount();
1782 for (int i = 0; i < count; i++) {
1783 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1784 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001785 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1786 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1787
1788 v.setTag(R.id.workspace, null);
1789 v.setTag(R.id.icon, null);
1790 window.setOnDismissListener(null);
1791 }
1792 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001793 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001794 }
1795 v.setTag(null);
1796 }
1797
Romain Guyf8e6a802009-12-07 17:48:02 -08001798 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001799 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001800 }
1801
Romain Guya6abce82009-11-10 02:54:41 -08001802 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001803 final Resources resources = getResources();
1804 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001805
Romain Guya6abce82009-11-10 02:54:41 -08001806 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001807
Romain Guy9d31e9f2009-11-11 18:54:28 -08001808 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001809
Romain Guyf8e6a802009-12-07 17:48:02 -08001810 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001811 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001812 int extraW = (int) ((r.left + r.right) * max);
1813 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001814
1815 int aW = cell.getWidth() - extraW;
1816 float w = aW / max;
1817
1818 int width = cell.getWidth();
1819 int height = cell.getHeight();
1820 int x = cell.getLeftPadding();
1821 int y = cell.getTopPadding();
1822 width -= (x + cell.getRightPadding());
1823 height -= (y + cell.getBottomPadding());
1824
1825 float scale = w / width;
1826
1827 int count = end - start;
1828
1829 final float sWidth = width * scale;
1830 float sHeight = height * scale;
1831
Romain Guye6b8e2f2009-11-10 11:56:55 -08001832 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001833
Romain Guye6b8e2f2009-11-10 11:56:55 -08001834 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1835 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001836
1837 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001838 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001839 cell = (CellLayout) workspace.getChildAt(i);
1840
Romain Guyf8e6a802009-12-07 17:48:02 -08001841 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001842 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001843
1844 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001845 c.scale(scale, scale);
1846 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1847 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001848
Romain Guy9d31e9f2009-11-11 18:54:28 -08001849 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001850 image.setImageBitmap(bitmap);
1851 image.setTag(i);
1852 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001853 image.setOnFocusChangeListener(handler);
1854 image.setFocusable(true);
1855 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001856
1857 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001858 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1859
Winson Chungaafa03c2010-06-11 17:34:16 -07001860 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001861 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001862
1863 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001864 p.setContentView(preview);
1865 p.setWidth((int) (sWidth * count + extraW));
1866 p.setHeight((int) (sHeight + extraH));
1867 p.setAnimationStyle(R.style.AnimationPreview);
1868 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001869 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001870 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001871 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001872
Romain Guye6b8e2f2009-11-10 11:56:55 -08001873 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1874 public void onDismiss() {
1875 dismissPreview(anchor);
1876 }
1877 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001878
1879 anchor.setTag(p);
1880 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001881 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001882 }
1883
Romain Guy9d31e9f2009-11-11 18:54:28 -08001884 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001885 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001886
Romain Guye6b8e2f2009-11-10 11:56:55 -08001887 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001888 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001889 }
1890
1891 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001892 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001893 v.post(this);
1894 }
1895
1896 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001897 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001898 }
1899
1900 public void onFocusChange(View v, boolean hasFocus) {
1901 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001902 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001903 }
Romain Guya6abce82009-11-10 02:54:41 -08001904 }
1905 }
1906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 Workspace getWorkspace() {
1908 return mWorkspace;
1909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 @Override
1912 protected Dialog onCreateDialog(int id) {
1913 switch (id) {
1914 case DIALOG_CREATE_SHORTCUT:
1915 return new CreateShortcut().createDialog();
1916 case DIALOG_RENAME_FOLDER:
1917 return new RenameFolder().createDialog();
1918 }
1919
1920 return super.onCreateDialog(id);
1921 }
1922
1923 @Override
1924 protected void onPrepareDialog(int id, Dialog dialog) {
1925 switch (id) {
1926 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 break;
1928 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001929 if (mFolderInfo != null) {
1930 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1931 final CharSequence text = mFolderInfo.title;
1932 input.setText(text);
1933 input.setSelection(0, text.length());
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 break;
1936 }
1937 }
1938
1939 void showRenameDialog(FolderInfo info) {
1940 mFolderInfo = info;
1941 mWaitingForResult = true;
1942 showDialog(DIALOG_RENAME_FOLDER);
1943 }
1944
1945 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1946 mAddItemCellInfo = cellInfo;
1947 mWaitingForResult = true;
1948 showDialog(DIALOG_CREATE_SHORTCUT);
1949 }
1950
Joe Onoratodeb98af2010-02-19 14:59:39 -08001951 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001952 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001953 Bundle bundle = new Bundle();
1954
1955 ArrayList<String> shortcutNames = new ArrayList<String>();
1956 shortcutNames.add(getString(R.string.group_applications));
1957 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1958
1959 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1960 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1961 R.drawable.ic_launcher_application));
1962 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1963
1964 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1965 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001966 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001967 pickIntent.putExtras(bundle);
1968
Joe Onoratodeb98af2010-02-19 14:59:39 -08001969 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 private class RenameFolder {
1973 private EditText mInput;
1974
1975 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1977 mInput = (EditText) layout.findViewById(R.id.folder_name);
1978
1979 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1980 builder.setIcon(0);
1981 builder.setTitle(getString(R.string.rename_folder_title));
1982 builder.setCancelable(true);
1983 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1984 public void onCancel(DialogInterface dialog) {
1985 cleanup();
1986 }
1987 });
1988 builder.setNegativeButton(getString(R.string.cancel_action),
1989 new Dialog.OnClickListener() {
1990 public void onClick(DialogInterface dialog, int which) {
1991 cleanup();
1992 }
1993 }
1994 );
1995 builder.setPositiveButton(getString(R.string.rename_action),
1996 new Dialog.OnClickListener() {
1997 public void onClick(DialogInterface dialog, int which) {
1998 changeFolderName();
1999 }
2000 }
2001 );
2002 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002003
2004 final AlertDialog dialog = builder.create();
2005 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2006 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002007 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002008 mInput.requestFocus();
2009 InputMethodManager inputManager = (InputMethodManager)
2010 getSystemService(Context.INPUT_METHOD_SERVICE);
2011 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002012 }
2013 });
2014
2015 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
2017
2018 private void changeFolderName() {
2019 final String name = mInput.getText().toString();
2020 if (!TextUtils.isEmpty(name)) {
2021 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002022 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 mFolderInfo.title = name;
2024 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2025
Joe Onorato9c1289c2009-08-17 11:03:03 -04002026 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002027 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 } else {
2030 final FolderIcon folderIcon = (FolderIcon)
2031 mWorkspace.getViewForTag(mFolderInfo);
2032 if (folderIcon != null) {
2033 folderIcon.setText(name);
2034 getWorkspace().requestLayout();
2035 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002036 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 mWorkspaceLoading = true;
2038 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040 }
2041 }
2042 cleanup();
2043 }
2044
2045 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 dismissDialog(DIALOG_RENAME_FOLDER);
2047 mWaitingForResult = false;
2048 mFolderInfo = null;
2049 }
2050 }
2051
Daniel Sandler843e8602010-06-07 14:59:01 -04002052 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2053 public boolean isAllAppsVisible() {
2054 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002055 }
2056
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002057 // AllAppsView.Watcher
2058 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002059 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2060 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002061 mWorkspace.setVisibility(View.GONE);
2062 }
2063 }
2064
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002065 /**
2066 * Zoom the camera out from the workspace to reveal 'toView'.
2067 * Assumes that the view to show is anchored at either the very top or very bottom
2068 * of the screen.
2069 * @param toView The view to show when the animation is complete
2070 * @param above If true, toView will appear from the top of the screen
2071 */
2072 private void cameraZoomOut(final View toView, boolean above) {
2073 final Resources res = getResources();
2074 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2075 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2076 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002077
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002078 // toView should appear right at the end of the workspace shrink animation
2079 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2080
2081 Interpolator interp = new DecelerateInterpolator();
2082
2083 toView.setPivotX(toView.getWidth() / 2.0f);
2084 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2085 // Assumes that the view is normally anchored to either the top or bottom of the screen
2086 final int margin = 200;
2087 if (above) {
2088 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2089 } else {
2090 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2091 }
2092
2093 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2094 scaleXAnim.setInterpolator(interp);
2095 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2096 public void onAnimationStart(Animatable animation) {
2097 // Prepare the position
2098 toView.setTranslationX(0.0f);
2099 toView.setTranslationY(0.0f);
2100 toView.setVisibility(View.VISIBLE);
2101 }
2102 });
2103
2104 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2105 scaleYAnim.setInterpolator(interp);
2106
2107 Sequencer s = new Sequencer();
2108 s.playTogether(scaleXAnim, scaleYAnim);
2109 s.play(scaleXAnim).after(startDelay);
2110 s.start();
2111 }
2112
2113 /**
2114 * Zoom the camera back into the workspace, hiding 'fromView'.
2115 * This is the opposite of cameraZoomOut.
2116 * @param fromView The currently-focused view, which will be hidden.
2117 */
2118 private void cameraZoomIn(final View fromView) {
2119 Resources res = getResources();
2120 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2121 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2122
2123 Interpolator interp = new AccelerateInterpolator();
2124
2125 Sequencer s = new Sequencer();
2126 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2127 scaleXAnim.setInterpolator(interp);
2128 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2129 scaleYAnim.setInterpolator(interp);
2130 s.playTogether(scaleXAnim, scaleYAnim);
2131 s.addListener(new AnimatableListenerAdapter() {
2132 public void onAnimationEnd(Animatable animation) {
2133 fromView.setVisibility(View.GONE);
2134 fromView.setScaleX(1.0f);
2135 fromView.setScaleY(1.0f);
2136 }
2137 });
2138 s.start();
2139 }
2140
2141 /**
2142 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2143 * This is the transition used on xlarge screens to go between all apps and
2144 * the home customization drawer.
2145 * @param fromView The view to pan away from.
2146 * @param toView The view to pan into the frame.
2147 */
2148 private void cameraPan(final View fromView, final View toView) {
2149 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2150 final int workspaceHeight = mWorkspace.getHeight();
2151
2152 final boolean panDown = fromView.getY() < toView.getY();
2153
2154 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2155 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2156 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2157 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2158
2159 Sequencer s = new Sequencer();
2160 s.playTogether(
2161 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2162 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2163 s.addListener(new AnimatableListenerAdapter() {
2164 public void onAnimationStart(Animatable animation) {
2165 toView.setVisibility(View.VISIBLE);
2166 toView.setY(toViewStartY);
2167 }
2168 public void onAnimationEnd(Animatable animation) {
2169 fromView.setVisibility(View.GONE);
2170 }
2171 });
2172 s.start();
2173 }
2174
2175 void showAllApps(boolean animated) {
Michael Jurka79212d82010-07-30 16:36:20 -07002176 if (LauncherApplication.isScreenXLarge()) {
2177 mWorkspace.shrinkToBottom(animated);
2178 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002179
Patrick Dubroy558654c2010-07-23 16:48:11 -07002180 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002181 if (isCustomizationDrawerVisible()) {
2182 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
2183 } else {
2184 cameraZoomOut((View) mAllAppsGrid, true);
2185 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002186 } else {
2187 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002188 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002189 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002190
Romain Guyc16fea72010-03-12 17:17:56 -08002191 ((View) mAllAppsGrid).setFocusable(true);
2192 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002193
Joe Onorato7c312c12009-08-13 21:36:53 -07002194 // TODO: fade these two too
2195 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002196 }
2197
Joe Onoratob2061212009-11-24 16:13:54 -05002198 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002199 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002200 * - Home from workspace
2201 * - from center screen
2202 * - from other screens
2203 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002204 * - from center screen
2205 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002206 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002207 * - from center screen
2208 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002209 * - Launch app from workspace and quit
2210 * - with back
2211 * - with home
2212 * - Launch app from all apps and quit
2213 * - with back
2214 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002215 * - Go to a screen that's not the default, then all
2216 * apps, and launch and app, and go back
2217 * - with back
2218 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002219 * - On workspace, long press power and go back
2220 * - with back
2221 * - with home
2222 * - On all apps, long press power and go back
2223 * - with back
2224 * - with home
2225 * - On workspace, power off
2226 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002227 * - Launch an app and turn off the screen while in that app
2228 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002229 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002230 * - From all apps
2231 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002232 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2233 * - From all apps
2234 * - From the center workspace
2235 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002236 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002237 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002238 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002239 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002240 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002241 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002242 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002243 } else {
2244 mAllAppsGrid.zoom(0.0f, animated);
2245 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002246 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002247 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002248 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002249 }
2250
Joe Onorato7c312c12009-08-13 21:36:53 -07002251 void lockAllApps() {
2252 // TODO
2253 }
2254
2255 void unlockAllApps() {
2256 // TODO
2257 }
2258
Patrick Dubroy558654c2010-07-23 16:48:11 -07002259 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002260 return mHomeCustomizationDrawer != null &&
2261 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002262 }
2263
2264 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002265 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002266 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002267 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2268 } else {
2269 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002270 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002271 }
2272
Michael Jurka4bb4f732010-08-04 18:46:01 -07002273 void hideCustomizationDrawer() {
2274 hideCustomizationDrawer(true);
2275 }
2276
2277 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002278 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002279 if (unshrinkWorkspace) {
2280 mWorkspace.unshrink();
2281 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002282 cameraZoomIn(mHomeCustomizationDrawer);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002283 }
2284 }
2285
Michael Jurka213d9632010-07-28 11:29:25 -07002286 void onWorkspaceUnshrink() {
2287 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002288 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002289 }
2290 if (isCustomizationDrawerVisible()) {
2291 hideCustomizationDrawer();
2292 }
2293 }
2294
Joe Onorato7404ee42009-07-31 11:54:44 -07002295 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 * Displays the shortcut creation dialog and launches, if necessary, the
2297 * appropriate activity.
2298 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002299 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002300 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2301 DialogInterface.OnShowListener {
2302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2309 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002310 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 builder.setInverseBackgroundForced(true);
2313
2314 AlertDialog dialog = builder.create();
2315 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002316 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002317 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 return dialog;
2320 }
2321
2322 public void onCancel(DialogInterface dialog) {
2323 mWaitingForResult = false;
2324 cleanup();
2325 }
2326
Romain Guycbb89e42009-06-08 15:52:54 -07002327 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002328 }
2329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002331 try {
2332 dismissDialog(DIALOG_CREATE_SHORTCUT);
2333 } catch (Exception e) {
2334 // An exception is thrown if the dialog is not visible, which is fine
2335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 }
2337
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002338 /**
2339 * Handle the action clicked in the "Add to home" dialog.
2340 */
2341 public void onClick(DialogInterface dialog, int which) {
2342 Resources res = getResources();
2343 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002344
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002345 switch (which) {
2346 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002347 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002348 break;
2349 }
Romain Guycbb89e42009-06-08 15:52:54 -07002350
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002351 case AddAdapter.ITEM_APPWIDGET: {
2352 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002353
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002354 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2355 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002356 // start the pick activity
2357 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2358 break;
2359 }
Romain Guycbb89e42009-06-08 15:52:54 -07002360
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002361 case AddAdapter.ITEM_LIVE_FOLDER: {
2362 // Insert extra item to handle inserting folder
2363 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002364
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002365 ArrayList<String> shortcutNames = new ArrayList<String>();
2366 shortcutNames.add(res.getString(R.string.group_folder));
2367 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002368
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002369 ArrayList<ShortcutIconResource> shortcutIcons =
2370 new ArrayList<ShortcutIconResource>();
2371 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2372 R.drawable.ic_launcher_folder));
2373 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2374
2375 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2376 pickIntent.putExtra(Intent.EXTRA_INTENT,
2377 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2378 pickIntent.putExtra(Intent.EXTRA_TITLE,
2379 getText(R.string.title_select_live_folder));
2380 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002381
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002382 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2383 break;
2384 }
2385
2386 case AddAdapter.ITEM_WALLPAPER: {
2387 startWallpaper();
2388 break;
2389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 }
2391 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002392
2393 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002394 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 }
2397
2398 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002399 * Receives notifications when applications are added/removed.
2400 */
2401 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2402 @Override
2403 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002404 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002405 String reason = intent.getStringExtra("reason");
2406 if (!"homekey".equals(reason)) {
2407 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002408 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002409 animate = false;
2410 }
Joe Onoratob2061212009-11-24 16:13:54 -05002411 closeAllApps(animate);
2412 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002413 }
2414 }
2415
2416 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002417 * Receives notifications whenever the appwidgets are reset.
2418 */
2419 private class AppWidgetResetObserver extends ContentObserver {
2420 public AppWidgetResetObserver() {
2421 super(new Handler());
2422 }
2423
2424 @Override
2425 public void onChange(boolean selfChange) {
2426 onAppWidgetReset();
2427 }
2428 }
2429
2430 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002433 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002434 if (mWorkspace != null) {
2435 return mWorkspace.getCurrentScreen();
2436 } else {
2437 return SCREEN_COUNT / 2;
2438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002440
Joe Onorato9c1289c2009-08-17 11:03:03 -04002441 /**
2442 * Refreshes the shortcuts shown on the workspace.
2443 *
2444 * Implementation of the method from LauncherModel.Callbacks.
2445 */
2446 public void startBinding() {
2447 final Workspace workspace = mWorkspace;
2448 int count = workspace.getChildCount();
2449 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002450 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2452 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002453
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 if (DEBUG_USER_INTERFACE) {
2455 android.widget.Button finishButton = new android.widget.Button(this);
2456 finishButton.setText("Finish");
2457 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2458
2459 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2460 public void onClick(View v) {
2461 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002463 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 }
2465 }
2466
2467 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002468 * Bind the items start-end from the list.
2469 *
2470 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002472 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2473
2474 final Workspace workspace = mWorkspace;
2475
2476 for (int i=start; i<end; i++) {
2477 final ItemInfo item = shortcuts.get(i);
2478 mDesktopItems.add(item);
2479 switch (item.itemType) {
2480 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2481 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002482 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2484 false);
2485 break;
2486 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2487 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2488 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2489 (UserFolderInfo) item);
2490 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2491 false);
2492 break;
2493 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2494 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2495 R.layout.live_folder_icon, this,
2496 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2497 (LiveFolderInfo) item);
2498 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2499 false);
2500 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 }
2503
Joe Onorato9c1289c2009-08-17 11:03:03 -04002504 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 }
2506
Joe Onorato9c1289c2009-08-17 11:03:03 -04002507 /**
2508 * Implementation of the method from LauncherModel.Callbacks.
2509 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002510 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2511 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002512 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002514
2515 /**
2516 * Add the views for a widget to the workspace.
2517 *
2518 * Implementation of the method from LauncherModel.Callbacks.
2519 */
2520 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002521 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2522 if (DEBUG_WIDGETS) {
2523 Log.d(TAG, "bindAppWidget: " + item);
2524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002525 final Workspace workspace = mWorkspace;
2526
2527 final int appWidgetId = item.appWidgetId;
2528 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002529 if (DEBUG_WIDGETS) {
2530 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2531 }
2532
Joe Onorato9c1289c2009-08-17 11:03:03 -04002533 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2534
Joe Onorato9c1289c2009-08-17 11:03:03 -04002535 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2536 item.hostView.setTag(item);
2537
2538 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2539 item.cellY, item.spanX, item.spanY, false);
2540
2541 workspace.requestLayout();
2542
2543 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002544
2545 if (DEBUG_WIDGETS) {
2546 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2547 + (SystemClock.uptimeMillis()-start) + "ms");
2548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 }
2550
2551 /**
2552 * Callback saying that there aren't any more items to bind.
2553 *
2554 * Implementation of the method from LauncherModel.Callbacks.
2555 */
2556 public void finishBindingItems() {
2557 if (mSavedState != null) {
2558 if (!mWorkspace.hasFocus()) {
2559 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2560 }
2561
2562 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2563 if (userFolders != null) {
2564 for (long folderId : userFolders) {
2565 final FolderInfo info = mFolders.get(folderId);
2566 if (info != null) {
2567 openFolder(info);
2568 }
2569 }
2570 final Folder openFolder = mWorkspace.getOpenFolder();
2571 if (openFolder != null) {
2572 openFolder.requestFocus();
2573 }
2574 }
2575
Joe Onorato9c1289c2009-08-17 11:03:03 -04002576 mSavedState = null;
2577 }
2578
2579 if (mSavedInstanceState != null) {
2580 super.onRestoreInstanceState(mSavedInstanceState);
2581 mSavedInstanceState = null;
2582 }
2583
Joe Onorato9c1289c2009-08-17 11:03:03 -04002584 mWorkspaceLoading = false;
2585 }
2586
2587 /**
2588 * Add the icons for all apps.
2589 *
2590 * Implementation of the method from LauncherModel.Callbacks.
2591 */
2592 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002593 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002594 }
2595
2596 /**
2597 * A package was installed.
2598 *
2599 * Implementation of the method from LauncherModel.Callbacks.
2600 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002601 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002602 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002603 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002604 }
2605
2606 /**
2607 * A package was updated.
2608 *
2609 * Implementation of the method from LauncherModel.Callbacks.
2610 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002611 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002612 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002613 mWorkspace.updateShortcuts(apps);
2614 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002615 }
2616
2617 /**
2618 * A package was uninstalled.
2619 *
2620 * Implementation of the method from LauncherModel.Callbacks.
2621 */
Joe Onorato36115782010-06-17 13:28:48 -04002622 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002623 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002624 if (permanent) {
2625 mWorkspace.removeItems(apps);
2626 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002627 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002628 }
Joe Onoratobe386092009-11-17 17:32:16 -08002629
2630 /**
2631 * Prints out out state for debugging.
2632 */
2633 public void dumpState() {
2634 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002635 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002636 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2637 Log.d(TAG, "mRestoring=" + mRestoring);
2638 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2639 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2640 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2641 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002642 mModel.dumpState();
2643 mAllAppsGrid.dumpState();
2644 Log.d(TAG, "END launcher2 dump state");
2645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646}