blob: 5a508abfcff885aa69cb55bd2715758b7270d43b [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka0e260592010-06-30 17:07:39 -070019import com.android.common.Search;
20import com.android.launcher.R;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000021
Patrick Dubroy7247f632010-08-04 16:02:59 -070022import android.animation.Animatable;
23import android.animation.AnimatableListenerAdapter;
24import android.animation.Animator;
25import android.animation.PropertyAnimator;
26import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070032import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070042import android.content.Intent.ShortcutIconResource;
Patrick Dubroy7247f632010-08-04 16:02:59 -070043import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080052import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080054import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070062import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.TextUtils;
66import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.View;
Patrick Dubroy7247f632010-08-04 16:02:59 -070076import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070078import android.view.WindowManager;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.animation.Animation;
Michael Jurkaaf442092010-06-10 17:01:57 -070080import android.view.animation.AnimationUtils;
Patrick Dubroy7247f632010-08-04 16:02:59 -070081import android.view.animation.DecelerateInterpolator;
82import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070085import android.widget.ImageView;
86import android.widget.LinearLayout;
87import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070088import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.widget.TextView;
90import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Michael Jurka0e260592010-06-30 17:07:39 -070092import java.io.DataInputStream;
93import java.io.DataOutputStream;
94import java.io.FileNotFoundException;
95import java.io.IOException;
96import java.util.ArrayList;
97import java.util.HashMap;
98import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Default launcher application.
102 */
Michael Jurka946ad472010-07-09 18:05:18 -0700103public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700104 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
105 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400110 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 private static final int WALLPAPER_SCREENS_SPAN = 2;
114
115 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800116 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 private static final int MENU_ADD = Menu.FIRST + 1;
119 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
120 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
121 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700122 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 private static final int REQUEST_CREATE_SHORTCUT = 1;
125 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int REQUEST_PICK_APPLICATION = 6;
128 private static final int REQUEST_PICK_SHORTCUT = 7;
129 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700131 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
134
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800135 static final int SCREEN_COUNT = 5;
136 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato7c312c12009-08-13 21:36:53 -0700138 static final int DIALOG_CREATE_SHORTCUT = 1;
139 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Romain Guy98d01652009-06-30 16:21:04 -0700141 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 // Type: int
144 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
145 // Type: boolean
146 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
147 // Type: long
148 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
163 // Type: int[]
164 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800173 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800175 private final BroadcastReceiver mCloseSystemDialogsReceiver
176 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800177 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private LayoutInflater mInflater;
180
Joe Onorato00acb122009-08-04 16:04:30 -0400181 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700183
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700184 private AppWidgetManager mAppWidgetManager;
185 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private CellLayout.CellInfo mAddItemCellInfo;
188 private CellLayout.CellInfo mMenuAddInfo;
189 private final int[] mCellCoordinates = new int[2];
190 private FolderInfo mFolderInfo;
191
Joe Onorato7c312c12009-08-13 21:36:53 -0700192 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700194 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700195 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private Bundle mSavedState;
198
199 private SpannableStringBuilder mDefaultKeySsb = null;
200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 private boolean mWorkspaceLoading = true;
202
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800203 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private boolean mRestoring;
205 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
207 private Bundle mSavedInstanceState;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Romain Guy84f296c2009-11-04 15:00:44 -0800212 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
213 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Romain Guy1fbc1c82009-11-09 20:43:08 -0800215 private ImageView mPreviousView;
216 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500217
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400218 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400219 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400220 private String[] mHotseatConfig = null;
221 private Intent[] mHotseats = null;
222 private Drawable[] mHotseatIcons = null;
223 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 @Override
226 protected void onCreate(Bundle savedInstanceState) {
227 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700228
Winson Chungaafa03c2010-06-11 17:34:16 -0700229 if (LauncherApplication.isInPlaceRotationEnabled()) {
230 // hide the status bar (temporary until we get the status bar design figured out)
231 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
232 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
233 }
234
Joe Onorato0589f0f2010-02-08 13:44:00 -0800235 LauncherApplication app = ((LauncherApplication)getApplication());
236 mModel = app.setLauncher(this);
237 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400238 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
243 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 if (PROFILE_STARTUP) {
246 android.os.Debug.startMethodTracing("/sdcard/launcher");
247 }
248
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400249 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 checkForLocaleChange();
251 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700253 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
254 if (mHomeCustomizationDrawer != null) {
255 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700256
Michael Jurka946ad472010-07-09 18:05:18 -0700257 String widgetsLabel = getString(R.string.widgets_tab_label);
258 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
259 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
260 String foldersLabel = getString(R.string.folders_tab_label);
261 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
262 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
263 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
264 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
265 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
266 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
267 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
268 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
269
270 mHomeCustomizationDrawer.setCurrentTab(0);
271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 setupViews();
273
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800274 registerContentObservers();
275
Joe Onorato7c312c12009-08-13 21:36:53 -0700276 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 mSavedState = savedInstanceState;
279 restoreState(mSavedState);
280
281 if (PROFILE_STARTUP) {
282 android.os.Debug.stopMethodTracing();
283 }
284
285 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 }
288
289 // For handling default keys
290 mDefaultKeySsb = new SpannableStringBuilder();
291 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292
293 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
294 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 }
Romain Guycbb89e42009-06-08 15:52:54 -0700296
Winson Chungaafa03c2010-06-11 17:34:16 -0700297 @Override
298 public void onConfigurationChanged(Configuration newConfig) {
299 // TODO Auto-generated method stub
300 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700301 }
302
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700305 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
306 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 final Configuration configuration = getResources().getConfiguration();
309
Romain Guy98d01652009-06-30 16:21:04 -0700310 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 final String locale = configuration.locale.toString();
312
Romain Guy98d01652009-06-30 16:21:04 -0700313 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 final int mcc = configuration.mcc;
315
Romain Guy98d01652009-06-30 16:21:04 -0700316 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 final int mnc = configuration.mnc;
318
Romain Guy84f296c2009-11-04 15:00:44 -0800319 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320
Romain Guy84f296c2009-11-04 15:00:44 -0800321 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700322 localeConfiguration.locale = locale;
323 localeConfiguration.mcc = mcc;
324 localeConfiguration.mnc = mnc;
325
326 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800327 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400328
329 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700330 }
331 }
332
333 private static class LocaleConfiguration {
334 public String locale;
335 public int mcc = -1;
336 public int mnc = -1;
337 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700338
Romain Guy98d01652009-06-30 16:21:04 -0700339 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
340 DataInputStream in = null;
341 try {
342 in = new DataInputStream(context.openFileInput(PREFERENCES));
343 configuration.locale = in.readUTF();
344 configuration.mcc = in.readInt();
345 configuration.mnc = in.readInt();
346 } catch (FileNotFoundException e) {
347 // Ignore
348 } catch (IOException e) {
349 // Ignore
350 } finally {
351 if (in != null) {
352 try {
353 in.close();
354 } catch (IOException e) {
355 // Ignore
356 }
357 }
358 }
359 }
360
361 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
362 DataOutputStream out = null;
363 try {
364 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
365 out.writeUTF(configuration.locale);
366 out.writeInt(configuration.mcc);
367 out.writeInt(configuration.mnc);
368 out.flush();
369 } catch (FileNotFoundException e) {
370 // Ignore
371 } catch (IOException e) {
372 //noinspection ResultOfMethodCallIgnored
373 context.getFileStreamPath(PREFERENCES).delete();
374 } finally {
375 if (out != null) {
376 try {
377 out.close();
378 } catch (IOException e) {
379 // Ignore
380 }
381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 }
383 }
384
385 static int getScreen() {
386 synchronized (sLock) {
387 return sScreen;
388 }
389 }
390
391 static void setScreen(int screen) {
392 synchronized (sLock) {
393 sScreen = screen;
394 }
395 }
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700398 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
400 Display display = getWindowManager().getDefaultDisplay();
401 boolean isPortrait = display.getWidth() < display.getHeight();
402
403 final int width = isPortrait ? display.getWidth() : display.getHeight();
404 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700405 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400408 // Note: This doesn't do all the client-id magic that BrowserProvider does
409 // in Browser. (http://b/2425179)
410 private Uri getDefaultBrowserUri() {
411 String url = getString(R.string.default_browser_url);
412 if (url.indexOf("{CID}") != -1) {
413 url = url.replace("{CID}", "android-google");
414 }
415 return Uri.parse(url);
416 }
417
418 // Load the Intent templates from arrays.xml to populate the hotseats. For
419 // each Intent, if it resolves to a single app, use that as the launch
420 // intent & use that app's label as the contentDescription. Otherwise,
421 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400422 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400423 if (mHotseatConfig == null) {
424 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
425 if (mHotseatConfig.length > 0) {
426 mHotseats = new Intent[mHotseatConfig.length];
427 mHotseatLabels = new CharSequence[mHotseatConfig.length];
428 mHotseatIcons = new Drawable[mHotseatConfig.length];
429 } else {
430 mHotseats = null;
431 mHotseatIcons = null;
432 mHotseatLabels = null;
433 }
434
435 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
436 for (int i=0; i<mHotseatConfig.length; i++) {
437 // load icon for this slot; currently unrelated to the actual activity
438 try {
439 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
440 } catch (ArrayIndexOutOfBoundsException ex) {
441 Log.w(TAG, "Missing hotseat_icons array item #" + i);
442 mHotseatIcons[i] = null;
443 }
444 }
445 hotseatIconDrawables.recycle();
446 }
447
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400448 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400449 for (int i=0; i<mHotseatConfig.length; i++) {
450 Intent intent = null;
451 if (mHotseatConfig[i].equals("*BROWSER*")) {
452 // magic value meaning "launch user's default web browser"
453 // replace it with a generic web request so we can see if there is indeed a default
454 String defaultUri = getString(R.string.default_browser_url);
455 intent = new Intent(
456 Intent.ACTION_VIEW,
457 ((defaultUri != null)
458 ? Uri.parse(defaultUri)
459 : getDefaultBrowserUri())
460 ).addCategory(Intent.CATEGORY_BROWSABLE);
461 // note: if the user launches this without a default set, she
462 // will always be taken to the default URL above; this is
463 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400465 // URL is unavoidably sent to the chosen app.
466 } else {
467 try {
468 intent = Intent.parseUri(mHotseatConfig[i], 0);
469 } catch (java.net.URISyntaxException ex) {
470 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
471 // bogus; leave intent=null
472 }
473 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700474
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400475 if (intent == null) {
476 mHotseats[i] = null;
477 mHotseatLabels[i] = getText(R.string.activity_not_found);
478 continue;
479 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400480
481 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700482 Log.d(TAG, "loadHotseats: hotseat " + i
483 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400484 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400485 + "]");
486 }
487
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400488 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
489 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700490 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 Log.d(TAG, "Best match for intent: " + bestMatch);
492 Log.d(TAG, "All matches: ");
493 for (ResolveInfo ri : allMatches) {
494 Log.d(TAG, " --> " + ri);
495 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400496 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400497 // did this resolve to a single app, or the resolver?
498 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700499 // can't find any activity to handle this. let's leave the
500 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400501 // to launch.
502 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400503
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400504 // set accessibility text to "Not installed"
505 mHotseatLabels[i] = getText(R.string.activity_not_found);
506 } else {
507 boolean found = false;
508 for (ResolveInfo ri : allMatches) {
509 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
510 && bestMatch.activityInfo.applicationInfo.packageName
511 .equals(ri.activityInfo.applicationInfo.packageName)) {
512 found = true;
513 break;
514 }
515 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700516
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400517 if (!found) {
518 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
519 // the bestMatch is probably the ResolveActivity, meaning the
520 // user has not yet selected a default
521 // so: we'll keep the original intent for now
522 mHotseats[i] = intent;
523
524 // set the accessibility text to "Select shortcut"
525 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
526 } else {
527 // we have an app!
528 // now reconstruct the intent to launch it through the front
529 // door
530 ComponentName com = new ComponentName(
531 bestMatch.activityInfo.applicationInfo.packageName,
532 bestMatch.activityInfo.name);
533 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
534
535 // load the app label for accessibility
536 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
537 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400538 }
539
540 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 Log.d(TAG, "loadHotseats: hotseat " + i
542 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 + ((mHotseats[i] == null)
544 ? "null"
545 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546 + "] label=[" + mHotseatLabels[i]
547 + "]"
548 );
549 }
550 }
551 }
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 @Override
554 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700555 mWaitingForResult = false;
556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 // The pattern used here is that a user PICKs a specific application,
558 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
561 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700562
Romain Guy94dabf12009-07-21 10:55:43 -0700563 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 switch (requestCode) {
565 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 break;
568 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800569 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 break;
571 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 break;
574 case REQUEST_PICK_LIVE_FOLDER:
575 addLiveFolder(data);
576 break;
577 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400578 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700580 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700581 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700584 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700585 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700587 case REQUEST_PICK_WALLPAPER:
588 // We just wanted the activity result here so we can clear mWaitingForResult
589 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 }
Romain Guy18042c82009-11-06 11:44:55 -0800591 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
592 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
593 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700594 // Clean up the appWidgetId if we canceled
595 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
596 if (appWidgetId != -1) {
597 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
602 @Override
603 protected void onResume() {
604 super.onResume();
605
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800606 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800607
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609 mWorkspaceLoading = true;
610 mModel.startLoader(this, true);
611 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
613 }
614
615 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700616 protected void onPause() {
617 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700618 // Some launcher layouts don't have a previous and next view
619 if (mPreviousView != null) {
620 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700621 }
622 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700623 dismissPreview(mNextView);
624 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800625 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700626 }
Romain Guycbb89e42009-06-08 15:52:54 -0700627
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700628 @Override
629 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 // Flag the loader to stop early before switching
631 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800632 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800633 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700634 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700635
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800636 // We can't hide the IME if it was forced open. So don't bother
637 /*
638 @Override
639 public void onWindowFocusChanged(boolean hasFocus) {
640 super.onWindowFocusChanged(hasFocus);
641
642 if (hasFocus) {
643 final InputMethodManager inputManager = (InputMethodManager)
644 getSystemService(Context.INPUT_METHOD_SERVICE);
645 WindowManager.LayoutParams lp = getWindow().getAttributes();
646 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
647 android.os.Handler()) {
648 protected void onReceiveResult(int resultCode, Bundle resultData) {
649 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
650 }
651 });
652 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
653 }
654 }
655 */
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 private boolean acceptFilter() {
658 final InputMethodManager inputManager = (InputMethodManager)
659 getSystemService(Context.INPUT_METHOD_SERVICE);
660 return !inputManager.isFullscreenMode();
661 }
662
663 @Override
664 public boolean onKeyDown(int keyCode, KeyEvent event) {
665 boolean handled = super.onKeyDown(keyCode, event);
666 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
667 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
668 keyCode, event);
669 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700670 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700671 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700672 // showSearchDialog()
673 // If there are multiple keystrokes before the search dialog takes focus,
674 // onSearchRequested() will be called for every keystroke,
675 // but it is idempotent, so it's fine.
676 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678 }
679
Joe Onorato8a9625e2010-01-28 15:55:35 -0800680 // Eat the long press event so the keyboard doesn't come up.
681 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
682 return true;
683 }
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 return handled;
686 }
687
Karl Rosaen138a0412009-04-23 19:00:21 -0700688 private String getTypedText() {
689 return mDefaultKeySsb.toString();
690 }
691
692 private void clearTypedText() {
693 mDefaultKeySsb.clear();
694 mDefaultKeySsb.clearSpans();
695 Selection.setSelection(mDefaultKeySsb, 0);
696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 /**
699 * Restores the previous state, if it exists.
700 *
701 * @param savedState The previous state.
702 */
703 private void restoreState(Bundle savedState) {
704 if (savedState == null) {
705 return;
706 }
707
Joe Onorato3a8820b2009-11-10 15:06:42 -0800708 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
709 if (allApps) {
710 showAllApps(false);
711 }
712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
714 if (currentScreen > -1) {
715 mWorkspace.setCurrentScreen(currentScreen);
716 }
717
718 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
719 if (addScreen > -1) {
720 mAddItemCellInfo = new CellLayout.CellInfo();
721 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
722 addItemCellInfo.valid = true;
723 addItemCellInfo.screen = addScreen;
724 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
725 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
726 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
727 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
728 addItemCellInfo.findVacantCellsFromOccupied(
729 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
730 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
731 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
732 mRestoring = true;
733 }
734
735 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
736 if (renameFolder) {
737 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 mRestoring = true;
740 }
741 }
742
743 /**
744 * Finds all the views we need and configure them properly.
745 */
746 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400747 DragController dragController = mDragController;
748
Romain Guyb1b69f52009-08-10 15:10:15 -0700749 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400750 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751
Joe Onorato7c312c12009-08-13 21:36:53 -0700752 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700753 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700754 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800755 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700756 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700757 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
760 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500761 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700762
Joe Onorato7c312c12009-08-13 21:36:53 -0700763 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
764 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700766 View handleView = findViewById(R.id.all_apps_button);
767 if (handleView != null && handleView instanceof HandleView) {
768 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700769 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700770 mHandleView.setLauncher(this);
771 mHandleView.setOnClickListener(this);
772 mHandleView.setOnLongClickListener(this);
773 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800774
Michael Jurka0e260592010-06-30 17:07:39 -0700775 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
776 if (widgetChooser != null) {
777 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
778 widgetChooser.setAdapter(widgetGalleryAdapter);
779 widgetChooser.setDragController(dragController);
780 widgetChooser.setLauncher(this);
781
782 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700783 IntentListAdapter folderTypes = new FolderListAdapter(
784 this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
Michael Jurka0e260592010-06-30 17:07:39 -0700785 folderChooser.setAdapter(folderTypes);
786 folderChooser.setLauncher(this);
787
788 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700789 IntentListAdapter shortcutTypes = new IntentListAdapter(
790 this, Intent.ACTION_CREATE_SHORTCUT);
Michael Jurka0e260592010-06-30 17:07:39 -0700791 shortcutChooser.setAdapter(shortcutTypes);
792 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700793 } else {
794 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
795 hotseatLeft.setContentDescription(mHotseatLabels[0]);
796 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
797 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
798 hotseatRight.setContentDescription(mHotseatLabels[1]);
799 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400800
Michael Jurkaaf442092010-06-10 17:01:57 -0700801 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
802 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800803
Michael Jurkaaf442092010-06-10 17:01:57 -0700804 Drawable previous = mPreviousView.getDrawable();
805 Drawable next = mNextView.getDrawable();
806 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
Michael Jurkaaf442092010-06-10 17:01:57 -0700808 mPreviousView.setHapticFeedbackEnabled(false);
809 mPreviousView.setOnLongClickListener(this);
810 mNextView.setHapticFeedbackEnabled(false);
811 mNextView.setOnLongClickListener(this);
812 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400815 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
818 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400819 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700820 int deleteZoneHandleId;
821 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700822 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700823 } else {
824 deleteZoneHandleId = R.id.all_apps_button_cluster;
825 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700826 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827
Joe Onorato00acb122009-08-04 16:04:30 -0400828 dragController.setDragScoller(workspace);
829 dragController.setDragListener(deleteZone);
830 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800831 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700832
Joe Onorato00acb122009-08-04 16:04:30 -0400833 // The order here is bottom to top.
834 dragController.addDropTarget(workspace);
835 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
837
Romain Guy8a73c512009-11-09 19:19:59 -0800838 @SuppressWarnings({"UnusedDeclaration"})
839 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800840 if (!isAllAppsVisible()) {
841 mWorkspace.scrollLeft();
842 }
Romain Guy8a73c512009-11-09 19:19:59 -0800843 }
844
845 @SuppressWarnings({"UnusedDeclaration"})
846 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800847 if (!isAllAppsVisible()) {
848 mWorkspace.scrollRight();
849 }
Romain Guy8a73c512009-11-09 19:19:59 -0800850 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400851
852 @SuppressWarnings({"UnusedDeclaration"})
853 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400854 if (isAllAppsVisible()) return;
855
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400856 int index = -1;
857 if (v.getId() == R.id.hotseat_left) {
858 index = 0;
859 } else if (v.getId() == R.id.hotseat_right) {
860 index = 1;
861 }
862
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400863 // reload these every tap; you never know when they might change
864 loadHotseats();
865 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
866 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400867 startActivitySafely(
868 mHotseats[index],
869 "hotseat"
870 );
871 }
872 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 /**
875 * Creates a view representing a shortcut.
876 *
877 * @param info The data structure describing the shortcut.
878 *
879 * @return A View inflated from R.layout.application.
880 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800881 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 return createShortcut(R.layout.application,
883 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
884 }
885
886 /**
887 * Creates a view representing a shortcut inflated from the specified resource.
888 *
889 * @param layoutResId The id of the XML layout used to create the shortcut.
890 * @param parent The group the shortcut belongs to.
891 * @param info The data structure describing the shortcut.
892 *
893 * @return A View inflated from layoutResId.
894 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800895 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
897
Joe Onorato0589f0f2010-02-08 13:44:00 -0800898 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
899 new FastBitmapDrawable(info.getIcon(mIconCache)),
900 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 favorite.setText(info.title);
902 favorite.setTag(info);
903 favorite.setOnClickListener(this);
904
905 return favorite;
906 }
907
908 /**
909 * Add an application shortcut to the workspace.
910 *
911 * @param data The intent describing the application.
912 * @param cellInfo The position on screen where to create the shortcut.
913 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 cellInfo.screen = mWorkspace.getCurrentScreen();
916 if (!findSingleSlot(cellInfo)) return;
917
Joe Onorato0589f0f2010-02-08 13:44:00 -0800918 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
919 data, context);
920
Romain Guy73b979d2009-06-09 12:57:21 -0700921 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800922 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800924 info.container = ItemInfo.NO_ID;
925 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
926 } else {
927 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
929 }
Romain Guycbb89e42009-06-08 15:52:54 -0700930
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 /**
932 * Add a shortcut to the workspace.
933 *
934 * @param data The intent describing the shortcut.
935 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 cellInfo.screen = mWorkspace.getCurrentScreen();
939 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700940
Joe Onorato0589f0f2010-02-08 13:44:00 -0800941 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
943 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
946 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948 }
949
Romain Guycbb89e42009-06-08 15:52:54 -0700950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700952 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700954 * @param data The intent describing the appWidgetId.
955 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700957 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700958 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700959
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700962 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 // Try finding open space on Launcher screen
965 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800966 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
967 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
968 return;
969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700971 // Build Launcher-specific widget info and save to database
972 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 launcherInfo.spanX = spans[0];
974 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 LauncherModel.addItemToDatabase(this, launcherInfo,
977 LauncherSettings.Favorites.CONTAINER_DESKTOP,
978 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
979
980 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700984 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700985
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700986 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
992 }
Romain Guycbb89e42009-06-08 15:52:54 -0700993
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
995 mDesktopItems.remove(launcherInfo);
996 launcherInfo.hostView = null;
997 }
998
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700999 public LauncherAppWidgetHost getAppWidgetHost() {
1000 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
Romain Guycbb89e42009-06-08 15:52:54 -07001002
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001003 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001004 getWindow().closeAllPanels();
1005
1006 try {
1007 dismissDialog(DIALOG_CREATE_SHORTCUT);
1008 // Unlock the workspace if the dialog was showing
1009 } catch (Exception e) {
1010 // An exception is thrown if the dialog is not visible, which is fine
1011 }
1012
1013 try {
1014 dismissDialog(DIALOG_RENAME_FOLDER);
1015 // Unlock the workspace if the dialog was showing
1016 } catch (Exception e) {
1017 // An exception is thrown if the dialog is not visible, which is fine
1018 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001019
1020 // Whatever we were doing is hereby canceled.
1021 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 @Override
1025 protected void onNewIntent(Intent intent) {
1026 super.onNewIntent(intent);
1027
1028 // Close the menu
1029 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001030 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001031 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001032
Joe Onorato14f122b2009-11-19 14:06:36 -08001033 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1034 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1035 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001036
1037 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001038 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001039 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001040 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001041 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001042 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001043
Joe Onorato3a8820b2009-11-10 15:06:42 -08001044 final View v = getWindow().peekDecorView();
1045 if (v != null && v.getWindowToken() != null) {
1046 InputMethodManager imm = (InputMethodManager)getSystemService(
1047 INPUT_METHOD_SERVICE);
1048 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050 }
1051 }
1052
1053 @Override
1054 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1055 // Do not call super here
1056 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001057
1058 if (mHomeCustomizationDrawer != null) {
1059 String cur = savedInstanceState.getString("currentTab");
1060 if (cur != null) {
1061 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1062 }
1063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065
1066 @Override
1067 protected void onSaveInstanceState(Bundle outState) {
1068 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1069
1070 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1071 if (folders.size() > 0) {
1072 final int count = folders.size();
1073 long[] ids = new long[count];
1074 for (int i = 0; i < count; i++) {
1075 final FolderInfo info = folders.get(i).getInfo();
1076 ids[i] = info.id;
1077 }
1078 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1079 } else {
1080 super.onSaveInstanceState(outState);
1081 }
1082
Joe Onoratofb0ca672009-09-14 17:55:46 -04001083 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001084 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087
1088 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1089 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1090 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1091
1092 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1093 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1094 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1095 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1096 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1097 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1098 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1099 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001100 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
1102
1103 if (mFolderInfo != null && mWaitingForResult) {
1104 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1105 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1106 }
Michael Jurka946ad472010-07-09 18:05:18 -07001107
1108 if (mHomeCustomizationDrawer != null) {
1109 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1110 if (currentTabTag != null) {
1111 outState.putString("currentTab", currentTabTag);
1112 }
1113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 }
1115
1116 @Override
1117 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001121 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001123 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125
1126 TextKeyListener.getInstance().release();
1127
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001131
1132 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001133
Patrick Dubroyab962b72010-07-26 12:10:10 -07001134 // Some launcher layouts don't have a previous and next view
1135 if (mPreviousView != null) {
1136 dismissPreview(mPreviousView);
1137 }
1138 if (mNextView != null) {
1139 dismissPreview(mNextView);
1140 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001141
1142 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144
1145 @Override
1146 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001147 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 super.startActivityForResult(intent, requestCode);
1149 }
1150
1151 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001152 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001154
Joe Onorato7bb17492009-09-24 17:51:01 -07001155 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Karl Rosaen138a0412009-04-23 19:00:21 -07001157 if (initialQuery == null) {
1158 // Use any text typed in the launcher as the initial query
1159 initialQuery = getTypedText();
1160 clearTypedText();
1161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 if (appSearchData == null) {
1163 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001164 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 }
Romain Guycbb89e42009-06-08 15:52:54 -07001166
Karl Rosaen138a0412009-04-23 19:00:21 -07001167 final SearchManager searchManager =
1168 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001169 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001170 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172
1173 @Override
1174 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 if (isWorkspaceLocked()) {
1176 return false;
1177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
1179 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1182 .setIcon(android.R.drawable.ic_menu_add)
1183 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 .setIcon(android.R.drawable.ic_menu_gallery)
1186 .setAlphabeticShortcut('W');
1187 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1188 .setIcon(android.R.drawable.ic_search_category_default)
1189 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1190 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1191 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1192 .setAlphabeticShortcut('N');
1193
1194 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001195 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1196 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197
1198 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1199 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1200 .setIntent(settings);
1201
1202 return true;
1203 }
1204
1205 @Override
1206 public boolean onPrepareOptionsMenu(Menu menu) {
1207 super.onPrepareOptionsMenu(menu);
1208
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001209 // If all apps is animating, don't show the menu, because we don't know
1210 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001211 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 return false;
1213 }
1214
1215 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001216 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1218 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1219
1220 // Disable add if the workspace is full.
1221 if (visible) {
1222 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1223 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
1226 return true;
1227 }
1228
Michael Jurka0e260592010-06-30 17:07:39 -07001229 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1230 // using the settings menu to add an item, something similar happens in showAddDialog
1231 public void prepareAddItemFromHomeCustomizationDrawer() {
1232 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1233 mAddItemCellInfo = mMenuAddInfo;
1234 }
1235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 @Override
1237 public boolean onOptionsItemSelected(MenuItem item) {
1238 switch (item.getItemId()) {
1239 case MENU_ADD:
1240 addItems();
1241 return true;
1242 case MENU_WALLPAPER_SETTINGS:
1243 startWallpaper();
1244 return true;
1245 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001246 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 return true;
1248 case MENU_NOTIFICATIONS:
1249 showNotifications();
1250 return true;
1251 }
1252
1253 return super.onOptionsItemSelected(item);
1254 }
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Karl Rosaen138a0412009-04-23 19:00:21 -07001256 /**
1257 * Indicates that we want global search for this activity by setting the globalSearch
1258 * argument for {@link #startSearch} to true.
1259 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001261 @Override
1262 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001263 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001264 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001265 }
1266
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 public boolean isWorkspaceLocked() {
1268 return mWorkspaceLoading || mWaitingForResult;
1269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001272 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001273 if (LauncherApplication.isScreenXLarge()) {
1274 // Animate the widget chooser up from the bottom of the screen
1275 if (!isCustomizationDrawerVisible()) {
1276 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001277 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001278 }
1279 } else {
1280 showAddDialog(mMenuAddInfo);
1281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283
Michael Jurkaaf442092010-06-10 17:01:57 -07001284 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1285 mAddItemCellInfo = cellInfo;
1286 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1287 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1288 addAppWidgetImpl(appWidgetId);
1289 }
1290
1291 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001292 // TODO: catch bad widget exception when sent
1293 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001294 // TODO: Is this log message meaningful?
1295 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1296 addAppWidgetImpl(appWidgetId);
1297 }
1298
1299 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001300 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301
Bjorn Bringert7984c942009-12-09 15:38:25 +00001302 if (appWidget.configure != null) {
1303 // Launch over to configure widget, if needed
1304 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1305 intent.setComponent(appWidget.configure);
1306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1307
Romain Guy8e633c52010-03-04 12:51:36 -08001308 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001310 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001311 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313 }
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Joe Onoratodeb98af2010-02-19 14:59:39 -08001315 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001316 // Handle case where user selected "Applications"
1317 String applicationName = getResources().getString(R.string.group_applications);
1318 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001319
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001320 if (applicationName != null && applicationName.equals(shortcutName)) {
1321 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1322 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001323
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001324 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1325 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001326 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001327 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001328 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
1332 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001333 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001334 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001335 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001336
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001337 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001339 } else {
1340 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
Joe Onorato9c1289c2009-08-17 11:03:03 -04001344 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 UserFolderInfo folderInfo = new UserFolderInfo();
1346 folderInfo.title = getText(R.string.folder_name);
1347
1348 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1349 cellInfo.screen = mWorkspace.getCurrentScreen();
1350 if (!findSingleSlot(cellInfo)) return;
1351
1352 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 LauncherModel.addItemToDatabase(this, folderInfo,
1354 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357
1358 // Create the view
1359 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1360 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1361 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Romain Guycbb89e42009-06-08 15:52:54 -07001364
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 void removeFolder(FolderInfo folder) {
1366 mFolders.remove(folder.id);
1367 }
1368
1369 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 cellInfo.screen = mWorkspace.getCurrentScreen();
1371 if (!findSingleSlot(cellInfo)) return;
1372
1373 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1374
1375 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1377 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1379 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381 }
1382
1383 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1384 CellLayout.CellInfo cellInfo, boolean notify) {
1385
1386 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1387 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1388
1389 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 Intent.ShortcutIconResource iconResource = null;
1391
1392 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1393 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1394 try {
1395 iconResource = (Intent.ShortcutIconResource) extra;
1396 final PackageManager packageManager = context.getPackageManager();
1397 Resources resources = packageManager.getResourcesForApplication(
1398 iconResource.packageName);
1399 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1400 icon = resources.getDrawable(id);
1401 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001402 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404 }
1405
1406 if (icon == null) {
1407 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1408 }
1409
1410 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001411 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 info.title = name;
1413 info.iconResource = iconResource;
1414 info.uri = data.getData();
1415 info.baseIntent = baseIntent;
1416 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1417 LiveFolders.DISPLAY_MODE_GRID);
1418
1419 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1420 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422
1423 return info;
1424 }
1425
1426 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1427 final int[] xy = new int[2];
1428 if (findSlot(cellInfo, xy, 1, 1)) {
1429 cellInfo.cellX = xy[0];
1430 cellInfo.cellY = xy[1];
1431 return true;
1432 }
1433 return false;
1434 }
1435
1436 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1437 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1438 boolean[] occupied = mSavedState != null ?
1439 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1440 cellInfo = mWorkspace.findAllVacantCells(occupied);
1441 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1442 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1443 return false;
1444 }
1445 }
1446 return true;
1447 }
1448
1449 private void showNotifications() {
1450 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1451 if (statusBar != null) {
1452 statusBar.expand();
1453 }
1454 }
1455
1456 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001457 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001459 Intent chooser = Intent.createChooser(pickWallpaper,
1460 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001461 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1462 // Removed in Eclair MR1
1463// WallpaperManager wm = (WallpaperManager)
1464// getSystemService(Context.WALLPAPER_SERVICE);
1465// WallpaperInfo wi = wm.getWallpaperInfo();
1466// if (wi != null && wi.getSettingsActivity() != null) {
1467// LabeledIntent li = new LabeledIntent(getPackageName(),
1468// R.string.configure_wallpaper, 0);
1469// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1470// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1471// }
Mike Clerona0618e42009-10-22 13:55:21 -07001472 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001475 /**
1476 * Registers various content observers. The current implementation registers
1477 * only a favorites observer to keep track of the favorites applications.
1478 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001479 private void registerContentObservers() {
1480 ContentResolver resolver = getContentResolver();
1481 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1482 true, mWidgetObserver);
1483 }
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 @Override
1486 public boolean dispatchKeyEvent(KeyEvent event) {
1487 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1488 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001490 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001491 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001492 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001493 dumpState();
1494 return true;
1495 }
1496 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001497 }
1498 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1499 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001500 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 return true;
1502 }
1503 }
1504
1505 return super.dispatchKeyEvent(event);
1506 }
1507
Joe Onorato88ec0992009-11-19 13:16:06 -08001508 @Override
1509 public void onBackPressed() {
1510 if (isAllAppsVisible()) {
1511 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001512 } else if (isCustomizationDrawerVisible()) {
1513 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001514 } else {
1515 closeFolder();
1516 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001517 // Some launcher layouts don't have a previous and next view
1518 if (mPreviousView != null) {
1519 dismissPreview(mPreviousView);
1520 dismissPreview(mNextView);
1521 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001522 }
1523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 private void closeFolder() {
1525 Folder folder = mWorkspace.getOpenFolder();
1526 if (folder != null) {
1527 closeFolder(folder);
1528 }
1529 }
1530
1531 void closeFolder(Folder folder) {
1532 folder.getInfo().opened = false;
1533 ViewGroup parent = (ViewGroup) folder.getParent();
1534 if (parent != null) {
1535 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001536 if (folder instanceof DropTarget) {
1537 // Live folders aren't DropTargets.
1538 mDragController.removeDropTarget((DropTarget)folder);
1539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541 folder.onClose();
1542 }
1543
1544 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001545 * Re-listen when widgets are reset.
1546 */
1547 private void onAppWidgetReset() {
1548 mAppWidgetHost.startListening();
1549 }
1550
1551 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1553 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 private void unbindDesktopItems() {
1556 for (ItemInfo item: mDesktopItems) {
1557 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 /**
1562 * Launches the intent referred by the clicked shortcut.
1563 *
1564 * @param v The view representing the clicked shortcut.
1565 */
1566 public void onClick(View v) {
1567 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001568 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001570 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001571 int[] pos = new int[2];
1572 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001573 intent.setSourceBounds(new Rect(pos[0], pos[1],
1574 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001575 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 } else if (tag instanceof FolderInfo) {
1577 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001578 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001579 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001580 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001581 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001582 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
1586
Michael Jurka0e260592010-06-30 17:07:39 -07001587 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001588 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001589 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001590 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001591 return false;
1592 }
1593
Michael Jurkaaf442092010-06-10 17:01:57 -07001594 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001595 * Event handler for the search button
1596 *
1597 * @param v The view that was clicked.
1598 */
1599 public void onClickSearchButton(View v) {
1600 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1601 View button = findViewById(R.id.search_button);
1602 i.setSourceBounds(
1603 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1604 startActivity(i);
1605 }
1606
1607 /**
1608 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001609 * enters home screen customization mode.
1610 *
1611 * @param v The view that was clicked.
1612 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001613 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001614 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001615 }
1616
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001617 /**
1618 * Event handler for the "grid" button that appears on the home screen, which
1619 * enters all apps mode.
1620 *
1621 * @param v The view that was clicked.
1622 */
1623 public void onClickAllAppsButton(View v) {
1624 showAllApps(true);
1625 }
1626
Joe Onoratof984e852010-03-25 09:47:45 -07001627 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1629 try {
1630 startActivity(intent);
1631 } catch (ActivityNotFoundException e) {
1632 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001633 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 } catch (SecurityException e) {
1635 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001636 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001638 "or use the exported attribute for this activity. "
1639 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
1641 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001642
Romain Guy8e633c52010-03-04 12:51:36 -08001643 void startActivityForResultSafely(Intent intent, int requestCode) {
1644 try {
1645 startActivityForResult(intent, requestCode);
1646 } catch (ActivityNotFoundException e) {
1647 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1648 } catch (SecurityException e) {
1649 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1650 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1651 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1652 "or use the exported attribute for this activity.", e);
1653 }
1654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655
1656 private void handleFolderClick(FolderInfo folderInfo) {
1657 if (!folderInfo.opened) {
1658 // Close any open folder
1659 closeFolder();
1660 // Open the requested folder
1661 openFolder(folderInfo);
1662 } else {
1663 // Find the open folder...
1664 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1665 int folderScreen;
1666 if (openFolder != null) {
1667 folderScreen = mWorkspace.getScreenForView(openFolder);
1668 // .. and close it
1669 closeFolder(openFolder);
1670 if (folderScreen != mWorkspace.getCurrentScreen()) {
1671 // Close any folder open on the current screen
1672 closeFolder();
1673 // Pull the folder onto this screen
1674 openFolder(folderInfo);
1675 }
1676 }
1677 }
1678 }
1679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 /**
1681 * Opens the user fodler described by the specified tag. The opening of the folder
1682 * is animated relative to the specified View. If the View is null, no animation
1683 * is played.
1684 *
1685 * @param folderInfo The FolderInfo describing the folder to open.
1686 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001687 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 Folder openFolder;
1689
1690 if (folderInfo instanceof UserFolderInfo) {
1691 openFolder = UserFolder.fromXml(this);
1692 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001693 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 } else {
1695 return;
1696 }
1697
Joe Onorato00acb122009-08-04 16:04:30 -04001698 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 openFolder.setLauncher(this);
1700
1701 openFolder.bind(folderInfo);
1702 folderInfo.opened = true;
1703
Winson Chungaafa03c2010-06-11 17:34:16 -07001704 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 openFolder.onOpen();
1707 }
1708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001710 switch (v.getId()) {
1711 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001712 if (!isAllAppsVisible()) {
1713 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1714 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001715 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001716 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001717 return true;
1718 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001719 if (!isAllAppsVisible()) {
1720 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1721 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001722 showPreviews(v);
1723 }
1724 return true;
1725 case R.id.all_apps_button:
1726 if (!isAllAppsVisible()) {
1727 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1728 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1729 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001730 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001731 return true;
1732 }
1733
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 return false;
1736 }
1737
1738 if (!(v instanceof CellLayout)) {
1739 v = (View) v.getParent();
1740 }
1741
1742 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1743
1744 // This happens when long clicking an item with the dpad/trackball
1745 if (cellInfo == null) {
1746 return true;
1747 }
1748
1749 if (mWorkspace.allowLongPress()) {
1750 if (cellInfo.cell == null) {
1751 if (cellInfo.valid) {
1752 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001753 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001754 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1755 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 showAddDialog(cellInfo);
1757 }
1758 } else {
1759 if (!(cellInfo.cell instanceof Folder)) {
1760 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001761 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1762 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 mWorkspace.startDrag(cellInfo);
1764 }
1765 }
1766 }
1767 return true;
1768 }
1769
Romain Guye6b8e2f2009-11-10 11:56:55 -08001770 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001771 private void dismissPreview(final View v) {
1772 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001773 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001774 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1775 public void onDismiss() {
1776 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1777 int count = group.getChildCount();
1778 for (int i = 0; i < count; i++) {
1779 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1780 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001781 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1782 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1783
1784 v.setTag(R.id.workspace, null);
1785 v.setTag(R.id.icon, null);
1786 window.setOnDismissListener(null);
1787 }
1788 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001789 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001790 }
1791 v.setTag(null);
1792 }
1793
Romain Guyf8e6a802009-12-07 17:48:02 -08001794 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001795 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001796 }
1797
Romain Guya6abce82009-11-10 02:54:41 -08001798 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001799 final Resources resources = getResources();
1800 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001801
Romain Guya6abce82009-11-10 02:54:41 -08001802 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001803
Romain Guy9d31e9f2009-11-11 18:54:28 -08001804 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001805
Romain Guyf8e6a802009-12-07 17:48:02 -08001806 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001807 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001808 int extraW = (int) ((r.left + r.right) * max);
1809 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001810
1811 int aW = cell.getWidth() - extraW;
1812 float w = aW / max;
1813
1814 int width = cell.getWidth();
1815 int height = cell.getHeight();
1816 int x = cell.getLeftPadding();
1817 int y = cell.getTopPadding();
1818 width -= (x + cell.getRightPadding());
1819 height -= (y + cell.getBottomPadding());
1820
1821 float scale = w / width;
1822
1823 int count = end - start;
1824
1825 final float sWidth = width * scale;
1826 float sHeight = height * scale;
1827
Romain Guye6b8e2f2009-11-10 11:56:55 -08001828 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001829
Romain Guye6b8e2f2009-11-10 11:56:55 -08001830 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1831 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001832
1833 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001834 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001835 cell = (CellLayout) workspace.getChildAt(i);
1836
Romain Guyf8e6a802009-12-07 17:48:02 -08001837 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001838 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001839
1840 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001841 c.scale(scale, scale);
1842 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1843 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001844
Romain Guy9d31e9f2009-11-11 18:54:28 -08001845 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001846 image.setImageBitmap(bitmap);
1847 image.setTag(i);
1848 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001849 image.setOnFocusChangeListener(handler);
1850 image.setFocusable(true);
1851 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001852
1853 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001854 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1855
Winson Chungaafa03c2010-06-11 17:34:16 -07001856 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001857 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001858
1859 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001860 p.setContentView(preview);
1861 p.setWidth((int) (sWidth * count + extraW));
1862 p.setHeight((int) (sHeight + extraH));
1863 p.setAnimationStyle(R.style.AnimationPreview);
1864 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001865 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001866 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001867 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001868
Romain Guye6b8e2f2009-11-10 11:56:55 -08001869 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1870 public void onDismiss() {
1871 dismissPreview(anchor);
1872 }
1873 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001874
1875 anchor.setTag(p);
1876 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001877 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001878 }
1879
Romain Guy9d31e9f2009-11-11 18:54:28 -08001880 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001881 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001882
Romain Guye6b8e2f2009-11-10 11:56:55 -08001883 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001884 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001885 }
1886
1887 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001888 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001889 v.post(this);
1890 }
1891
1892 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001893 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001894 }
1895
1896 public void onFocusChange(View v, boolean hasFocus) {
1897 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001898 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001899 }
Romain Guya6abce82009-11-10 02:54:41 -08001900 }
1901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 Workspace getWorkspace() {
1904 return mWorkspace;
1905 }
1906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 @Override
1908 protected Dialog onCreateDialog(int id) {
1909 switch (id) {
1910 case DIALOG_CREATE_SHORTCUT:
1911 return new CreateShortcut().createDialog();
1912 case DIALOG_RENAME_FOLDER:
1913 return new RenameFolder().createDialog();
1914 }
1915
1916 return super.onCreateDialog(id);
1917 }
1918
1919 @Override
1920 protected void onPrepareDialog(int id, Dialog dialog) {
1921 switch (id) {
1922 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 break;
1924 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001925 if (mFolderInfo != null) {
1926 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1927 final CharSequence text = mFolderInfo.title;
1928 input.setText(text);
1929 input.setSelection(0, text.length());
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 break;
1932 }
1933 }
1934
1935 void showRenameDialog(FolderInfo info) {
1936 mFolderInfo = info;
1937 mWaitingForResult = true;
1938 showDialog(DIALOG_RENAME_FOLDER);
1939 }
1940
1941 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1942 mAddItemCellInfo = cellInfo;
1943 mWaitingForResult = true;
1944 showDialog(DIALOG_CREATE_SHORTCUT);
1945 }
1946
Joe Onoratodeb98af2010-02-19 14:59:39 -08001947 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001948 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001949 Bundle bundle = new Bundle();
1950
1951 ArrayList<String> shortcutNames = new ArrayList<String>();
1952 shortcutNames.add(getString(R.string.group_applications));
1953 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1954
1955 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1956 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1957 R.drawable.ic_launcher_application));
1958 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1959
1960 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1961 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001962 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001963 pickIntent.putExtras(bundle);
1964
Joe Onoratodeb98af2010-02-19 14:59:39 -08001965 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001966 }
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 private class RenameFolder {
1969 private EditText mInput;
1970
1971 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1973 mInput = (EditText) layout.findViewById(R.id.folder_name);
1974
1975 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1976 builder.setIcon(0);
1977 builder.setTitle(getString(R.string.rename_folder_title));
1978 builder.setCancelable(true);
1979 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1980 public void onCancel(DialogInterface dialog) {
1981 cleanup();
1982 }
1983 });
1984 builder.setNegativeButton(getString(R.string.cancel_action),
1985 new Dialog.OnClickListener() {
1986 public void onClick(DialogInterface dialog, int which) {
1987 cleanup();
1988 }
1989 }
1990 );
1991 builder.setPositiveButton(getString(R.string.rename_action),
1992 new Dialog.OnClickListener() {
1993 public void onClick(DialogInterface dialog, int which) {
1994 changeFolderName();
1995 }
1996 }
1997 );
1998 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001999
2000 final AlertDialog dialog = builder.create();
2001 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2002 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002003 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002004 mInput.requestFocus();
2005 InputMethodManager inputManager = (InputMethodManager)
2006 getSystemService(Context.INPUT_METHOD_SERVICE);
2007 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002008 }
2009 });
2010
2011 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
2013
2014 private void changeFolderName() {
2015 final String name = mInput.getText().toString();
2016 if (!TextUtils.isEmpty(name)) {
2017 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002018 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 mFolderInfo.title = name;
2020 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2021
Joe Onorato9c1289c2009-08-17 11:03:03 -04002022 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002023 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 } else {
2026 final FolderIcon folderIcon = (FolderIcon)
2027 mWorkspace.getViewForTag(mFolderInfo);
2028 if (folderIcon != null) {
2029 folderIcon.setText(name);
2030 getWorkspace().requestLayout();
2031 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002032 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033 mWorkspaceLoading = true;
2034 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036 }
2037 }
2038 cleanup();
2039 }
2040
2041 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 dismissDialog(DIALOG_RENAME_FOLDER);
2043 mWaitingForResult = false;
2044 mFolderInfo = null;
2045 }
2046 }
2047
Daniel Sandler843e8602010-06-07 14:59:01 -04002048 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2049 public boolean isAllAppsVisible() {
2050 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002051 }
2052
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002053 // AllAppsView.Watcher
2054 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002055 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2056 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002057 mWorkspace.setVisibility(View.GONE);
2058 }
2059 }
2060
Joe Onorato3a8820b2009-11-10 15:06:42 -08002061 void showAllApps(boolean animated) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002062 boolean unshrinkWorkspace = false;
2063 hideCustomizationDrawer(unshrinkWorkspace);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002064
Michael Jurka79212d82010-07-30 16:36:20 -07002065 if (LauncherApplication.isScreenXLarge()) {
2066 mWorkspace.shrinkToBottom(animated);
2067 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002068 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy7247f632010-08-04 16:02:59 -07002069 final View allApps = (View)mAllAppsGrid;
2070
2071 final Resources res = getResources();
2072 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2073 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2074 final int height = allApps.getHeight();
2075
2076 // All apps should appear right at the end of the workspace shrink animation
2077 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2078
2079 Interpolator interp = new DecelerateInterpolator(2.0f);
2080
2081 allApps.setPivotX(allApps.getWidth() / 2.0f);
2082 allApps.setPivotY(height);
2083
2084 Animator scaleXAnim = new PropertyAnimator(duration, allApps, "scaleX", scale, 1.0f);
2085 scaleXAnim.setInterpolator(interp);
2086 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2087 public void onAnimationStart(Animatable animation) {
2088 // Not really a zoom -- this just makes the view visible
2089 mAllAppsGrid.zoom(1.0f, false);
2090 }
2091 });
2092
2093 Animator scaleYAnim = new PropertyAnimator(duration, allApps, "scaleY", scale, 1.0f);
2094 scaleYAnim.setInterpolator(interp);
2095
2096 // Translate down by 20% of the total height
2097 float oldY = (-allApps.getHeight() * 0.2f);
2098 Animator yAnim = new PropertyAnimator(duration, allApps, "y", oldY, 0.0f);
2099 yAnim.setInterpolator(interp);
2100
2101 Sequencer s = new Sequencer();
2102 s.playTogether(scaleXAnim, scaleYAnim, yAnim);
2103 s.play(scaleXAnim).after(startDelay);
2104 s.start();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002105 } else {
2106 mAllAppsGrid.zoom(1.0f, animated);
2107 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002108
Romain Guyc16fea72010-03-12 17:17:56 -08002109 ((View) mAllAppsGrid).setFocusable(true);
2110 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002111
Joe Onorato7c312c12009-08-13 21:36:53 -07002112 // TODO: fade these two too
2113 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002114 }
2115
Joe Onoratob2061212009-11-24 16:13:54 -05002116 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002117 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002118 * - Home from workspace
2119 * - from center screen
2120 * - from other screens
2121 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002122 * - from center screen
2123 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002124 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002125 * - from center screen
2126 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002127 * - Launch app from workspace and quit
2128 * - with back
2129 * - with home
2130 * - Launch app from all apps and quit
2131 * - with back
2132 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002133 * - Go to a screen that's not the default, then all
2134 * apps, and launch and app, and go back
2135 * - with back
2136 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002137 * - On workspace, long press power and go back
2138 * - with back
2139 * - with home
2140 * - On all apps, long press power and go back
2141 * - with back
2142 * - with home
2143 * - On workspace, power off
2144 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002145 * - Launch an app and turn off the screen while in that app
2146 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002147 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002148 * - From all apps
2149 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002150 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2151 * - From all apps
2152 * - From the center workspace
2153 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002154 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002155 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002156 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002157 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002158 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy7247f632010-08-04 16:02:59 -07002159 Resources res = getResources();
2160 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2161 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2162 View allApps = (View) mAllAppsGrid;
2163
2164 allApps.setPivotX(allApps.getWidth() / 2.0f);
2165 allApps.setPivotY(allApps.getHeight());
2166
2167 // Translate up by 20% of the total height
2168 float newY = allApps.getY() - allApps.getHeight() * 0.2f;
2169
2170 Sequencer seq = new Sequencer();
2171 seq.playTogether(
2172 new PropertyAnimator(duration, allApps, "scaleX", scaleFactor),
2173 new PropertyAnimator(duration, allApps, "scaleY", scaleFactor),
2174 new PropertyAnimator(duration, allApps, "y", newY));
2175 seq.addListener(new AnimatableListenerAdapter() {
2176 public void onAnimationEnd(Animatable animation) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002177 mAllAppsGrid.zoom(0.0f, false);
2178 }
2179 });
Patrick Dubroy7247f632010-08-04 16:02:59 -07002180 // Start the AllApps animation at the same time as the workspace unshrink
2181 seq.start();
Michael Jurka213d9632010-07-28 11:29:25 -07002182 mWorkspace.unshrink();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002183 } else {
2184 mAllAppsGrid.zoom(0.0f, animated);
2185 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002186 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002187 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002188 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002189 }
2190
Joe Onorato7c312c12009-08-13 21:36:53 -07002191 void lockAllApps() {
2192 // TODO
2193 }
2194
2195 void unlockAllApps() {
2196 // TODO
2197 }
2198
Patrick Dubroy558654c2010-07-23 16:48:11 -07002199 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002200 return mHomeCustomizationDrawer != null &&
2201 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002202 }
2203
2204 private void showCustomizationDrawer() {
2205 if (isAllAppsVisible()) {
2206 // TODO: Make a smoother transition here
2207 closeAllApps(false);
2208 }
2209 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
Michael Jurka54f7ac32010-08-02 13:56:46 -07002210 mHomeCustomizationDrawer.startAnimation(
2211 AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
Patrick Dubroy558654c2010-07-23 16:48:11 -07002212 }
2213
Michael Jurka4bb4f732010-08-04 18:46:01 -07002214 void hideCustomizationDrawer() {
2215 hideCustomizationDrawer(true);
2216 }
2217
2218 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002219 if (isCustomizationDrawerVisible()) {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002220 Animation slideDownAnimation = AnimationUtils.loadAnimation(
2221 this, R.anim.home_customization_drawer_slide_down);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002222 slideDownAnimation.setAnimationListener(new Animation.AnimationListener() {
2223 public void onAnimationEnd(Animation animation) {
2224 mHomeCustomizationDrawer.setVisibility(View.GONE);
2225 }
2226 public void onAnimationRepeat(Animation animation) {}
2227 public void onAnimationStart(Animation animation) {}
2228 });
2229 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
Michael Jurka4bb4f732010-08-04 18:46:01 -07002230 if (unshrinkWorkspace) {
2231 mWorkspace.unshrink();
2232 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002233 }
2234 }
2235
Michael Jurka213d9632010-07-28 11:29:25 -07002236 void onWorkspaceUnshrink() {
2237 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002238 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002239 }
2240 if (isCustomizationDrawerVisible()) {
2241 hideCustomizationDrawer();
2242 }
2243 }
2244
Joe Onorato7404ee42009-07-31 11:54:44 -07002245 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 * Displays the shortcut creation dialog and launches, if necessary, the
2247 * appropriate activity.
2248 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002249 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002250 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2251 DialogInterface.OnShowListener {
2252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2259 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002260 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 builder.setInverseBackgroundForced(true);
2263
2264 AlertDialog dialog = builder.create();
2265 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002266 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002267 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 return dialog;
2270 }
2271
2272 public void onCancel(DialogInterface dialog) {
2273 mWaitingForResult = false;
2274 cleanup();
2275 }
2276
Romain Guycbb89e42009-06-08 15:52:54 -07002277 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002278 }
2279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002281 try {
2282 dismissDialog(DIALOG_CREATE_SHORTCUT);
2283 } catch (Exception e) {
2284 // An exception is thrown if the dialog is not visible, which is fine
2285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 }
2287
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002288 /**
2289 * Handle the action clicked in the "Add to home" dialog.
2290 */
2291 public void onClick(DialogInterface dialog, int which) {
2292 Resources res = getResources();
2293 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002294
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002295 switch (which) {
2296 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002297 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002298 break;
2299 }
Romain Guycbb89e42009-06-08 15:52:54 -07002300
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002301 case AddAdapter.ITEM_APPWIDGET: {
2302 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002303
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002304 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2305 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002306 // start the pick activity
2307 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2308 break;
2309 }
Romain Guycbb89e42009-06-08 15:52:54 -07002310
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002311 case AddAdapter.ITEM_LIVE_FOLDER: {
2312 // Insert extra item to handle inserting folder
2313 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002314
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002315 ArrayList<String> shortcutNames = new ArrayList<String>();
2316 shortcutNames.add(res.getString(R.string.group_folder));
2317 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002318
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002319 ArrayList<ShortcutIconResource> shortcutIcons =
2320 new ArrayList<ShortcutIconResource>();
2321 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2322 R.drawable.ic_launcher_folder));
2323 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2324
2325 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2326 pickIntent.putExtra(Intent.EXTRA_INTENT,
2327 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2328 pickIntent.putExtra(Intent.EXTRA_TITLE,
2329 getText(R.string.title_select_live_folder));
2330 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002331
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002332 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2333 break;
2334 }
2335
2336 case AddAdapter.ITEM_WALLPAPER: {
2337 startWallpaper();
2338 break;
2339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 }
2341 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002342
2343 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002344 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 }
2347
2348 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002349 * Receives notifications when applications are added/removed.
2350 */
2351 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2352 @Override
2353 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002354 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002355 String reason = intent.getStringExtra("reason");
2356 if (!"homekey".equals(reason)) {
2357 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002358 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002359 animate = false;
2360 }
Joe Onoratob2061212009-11-24 16:13:54 -05002361 closeAllApps(animate);
2362 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002363 }
2364 }
2365
2366 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002367 * Receives notifications whenever the appwidgets are reset.
2368 */
2369 private class AppWidgetResetObserver extends ContentObserver {
2370 public AppWidgetResetObserver() {
2371 super(new Handler());
2372 }
2373
2374 @Override
2375 public void onChange(boolean selfChange) {
2376 onAppWidgetReset();
2377 }
2378 }
2379
2380 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002381 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002383 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002384 if (mWorkspace != null) {
2385 return mWorkspace.getCurrentScreen();
2386 } else {
2387 return SCREEN_COUNT / 2;
2388 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002389 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002390
Joe Onorato9c1289c2009-08-17 11:03:03 -04002391 /**
2392 * Refreshes the shortcuts shown on the workspace.
2393 *
2394 * Implementation of the method from LauncherModel.Callbacks.
2395 */
2396 public void startBinding() {
2397 final Workspace workspace = mWorkspace;
2398 int count = workspace.getChildCount();
2399 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002400 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2402 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002403
Joe Onorato9c1289c2009-08-17 11:03:03 -04002404 if (DEBUG_USER_INTERFACE) {
2405 android.widget.Button finishButton = new android.widget.Button(this);
2406 finishButton.setText("Finish");
2407 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2408
2409 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2410 public void onClick(View v) {
2411 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002412 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 }
2415 }
2416
2417 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 * Bind the items start-end from the list.
2419 *
2420 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2423
2424 final Workspace workspace = mWorkspace;
2425
2426 for (int i=start; i<end; i++) {
2427 final ItemInfo item = shortcuts.get(i);
2428 mDesktopItems.add(item);
2429 switch (item.itemType) {
2430 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2431 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002432 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002433 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2434 false);
2435 break;
2436 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2437 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2438 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2439 (UserFolderInfo) item);
2440 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2441 false);
2442 break;
2443 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2444 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2445 R.layout.live_folder_icon, this,
2446 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2447 (LiveFolderInfo) item);
2448 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2449 false);
2450 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 }
2453
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 }
2456
Joe Onorato9c1289c2009-08-17 11:03:03 -04002457 /**
2458 * Implementation of the method from LauncherModel.Callbacks.
2459 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002460 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2461 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002462 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002464
2465 /**
2466 * Add the views for a widget to the workspace.
2467 *
2468 * Implementation of the method from LauncherModel.Callbacks.
2469 */
2470 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002471 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2472 if (DEBUG_WIDGETS) {
2473 Log.d(TAG, "bindAppWidget: " + item);
2474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002475 final Workspace workspace = mWorkspace;
2476
2477 final int appWidgetId = item.appWidgetId;
2478 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002479 if (DEBUG_WIDGETS) {
2480 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2481 }
2482
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2484
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2486 item.hostView.setTag(item);
2487
2488 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2489 item.cellY, item.spanX, item.spanY, false);
2490
2491 workspace.requestLayout();
2492
2493 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002494
2495 if (DEBUG_WIDGETS) {
2496 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2497 + (SystemClock.uptimeMillis()-start) + "ms");
2498 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002499 }
2500
2501 /**
2502 * Callback saying that there aren't any more items to bind.
2503 *
2504 * Implementation of the method from LauncherModel.Callbacks.
2505 */
2506 public void finishBindingItems() {
2507 if (mSavedState != null) {
2508 if (!mWorkspace.hasFocus()) {
2509 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2510 }
2511
2512 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2513 if (userFolders != null) {
2514 for (long folderId : userFolders) {
2515 final FolderInfo info = mFolders.get(folderId);
2516 if (info != null) {
2517 openFolder(info);
2518 }
2519 }
2520 final Folder openFolder = mWorkspace.getOpenFolder();
2521 if (openFolder != null) {
2522 openFolder.requestFocus();
2523 }
2524 }
2525
Joe Onorato9c1289c2009-08-17 11:03:03 -04002526 mSavedState = null;
2527 }
2528
2529 if (mSavedInstanceState != null) {
2530 super.onRestoreInstanceState(mSavedInstanceState);
2531 mSavedInstanceState = null;
2532 }
2533
Joe Onorato9c1289c2009-08-17 11:03:03 -04002534 mWorkspaceLoading = false;
2535 }
2536
2537 /**
2538 * Add the icons for all apps.
2539 *
2540 * Implementation of the method from LauncherModel.Callbacks.
2541 */
2542 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002543 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002544 }
2545
2546 /**
2547 * A package was installed.
2548 *
2549 * Implementation of the method from LauncherModel.Callbacks.
2550 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002551 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002553 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002554 }
2555
2556 /**
2557 * A package was updated.
2558 *
2559 * Implementation of the method from LauncherModel.Callbacks.
2560 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002561 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002562 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002563 mWorkspace.updateShortcuts(apps);
2564 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002565 }
2566
2567 /**
2568 * A package was uninstalled.
2569 *
2570 * Implementation of the method from LauncherModel.Callbacks.
2571 */
Joe Onorato36115782010-06-17 13:28:48 -04002572 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002573 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002574 if (permanent) {
2575 mWorkspace.removeItems(apps);
2576 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002577 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 }
Joe Onoratobe386092009-11-17 17:32:16 -08002579
2580 /**
2581 * Prints out out state for debugging.
2582 */
2583 public void dumpState() {
2584 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002585 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002586 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2587 Log.d(TAG, "mRestoring=" + mRestoring);
2588 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2589 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2590 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2591 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002592 mModel.dumpState();
2593 mAllAppsGrid.dumpState();
2594 Log.d(TAG, "END launcher2 dump state");
2595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596}