blob: ee854366805cb15a739a44871459995c79535d8f [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
Michael Jurka946ad472010-07-09 18:05:18 -070022import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.app.Dialog;
25import android.app.SearchManager;
26import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070027import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080031import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040032import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080033import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
35import android.content.DialogInterface;
36import android.content.Intent;
Patrick Dubroy558654c2010-07-23 16:48:11 -070037import android.content.IntentFilter;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070038import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070043import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040044import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080047import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080049import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080056import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
60import android.text.TextUtils;
61import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080064import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.Menu;
68import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070069import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.View;
71import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070072import android.view.WindowManager;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070073import android.view.View.OnLongClickListener;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.animation.Animation;
Michael Jurkaaf442092010-06-10 17:01:57 -070075import android.view.animation.AnimationUtils;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070076import android.view.animation.Animation.AnimationListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070079import android.widget.ImageView;
80import android.widget.LinearLayout;
81import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070082import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.TextView;
84import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Michael Jurka0e260592010-06-30 17:07:39 -070086import java.io.DataInputStream;
87import java.io.DataOutputStream;
88import java.io.FileNotFoundException;
89import java.io.IOException;
90import java.util.ArrayList;
91import java.util.HashMap;
92import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094/**
95 * Default launcher application.
96 */
Michael Jurka946ad472010-07-09 18:05:18 -070097public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -070098 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
99 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800100 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700101 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400104 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int WALLPAPER_SCREENS_SPAN = 2;
108
109 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800110 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 private static final int MENU_ADD = Menu.FIRST + 1;
113 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
114 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
115 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700116 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
118 private static final int REQUEST_CREATE_SHORTCUT = 1;
119 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
123 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato7c312c12009-08-13 21:36:53 -0700132 static final int DIALOG_CREATE_SHORTCUT = 1;
133 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Romain Guy98d01652009-06-30 16:21:04 -0700135 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 // Type: int
138 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
139 // Type: boolean
140 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
141 // Type: long
142 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
157 // Type: int[]
158 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800167 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800169 private final BroadcastReceiver mCloseSystemDialogsReceiver
170 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800171 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private LayoutInflater mInflater;
174
Joe Onorato00acb122009-08-04 16:04:30 -0400175 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private AppWidgetManager mAppWidgetManager;
179 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private CellLayout.CellInfo mAddItemCellInfo;
182 private CellLayout.CellInfo mMenuAddInfo;
183 private final int[] mCellCoordinates = new int[2];
184 private FolderInfo mFolderInfo;
185
Joe Onorato7c312c12009-08-13 21:36:53 -0700186 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700187 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700188 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700189 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Bundle mSavedState;
192
193 private SpannableStringBuilder mDefaultKeySsb = null;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private boolean mWorkspaceLoading = true;
196
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800197 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private boolean mRestoring;
199 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
201 private Bundle mSavedInstanceState;
202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800204 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205
Romain Guy84f296c2009-11-04 15:00:44 -0800206 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
207 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700208
Romain Guy1fbc1c82009-11-09 20:43:08 -0800209 private ImageView mPreviousView;
210 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500211
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400212 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400213 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400214 private String[] mHotseatConfig = null;
215 private Intent[] mHotseats = null;
216 private Drawable[] mHotseatIcons = null;
217 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 @Override
220 protected void onCreate(Bundle savedInstanceState) {
221 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700222
Winson Chungaafa03c2010-06-11 17:34:16 -0700223 if (LauncherApplication.isInPlaceRotationEnabled()) {
224 // hide the status bar (temporary until we get the status bar design figured out)
225 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
226 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
227 }
228
Joe Onorato0589f0f2010-02-08 13:44:00 -0800229 LauncherApplication app = ((LauncherApplication)getApplication());
230 mModel = app.setLauncher(this);
231 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400232 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700234
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700235 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
237 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 if (PROFILE_STARTUP) {
240 android.os.Debug.startMethodTracing("/sdcard/launcher");
241 }
242
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400243 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 checkForLocaleChange();
245 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700247 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
248 if (mHomeCustomizationDrawer != null) {
249 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700250
Michael Jurka946ad472010-07-09 18:05:18 -0700251 String widgetsLabel = getString(R.string.widgets_tab_label);
252 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
253 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
254 String foldersLabel = getString(R.string.folders_tab_label);
255 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
256 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
257 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
258 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
259 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
260 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
261 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
262 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
263
264 mHomeCustomizationDrawer.setCurrentTab(0);
265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 setupViews();
267
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800268 registerContentObservers();
269
Joe Onorato7c312c12009-08-13 21:36:53 -0700270 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 mSavedState = savedInstanceState;
273 restoreState(mSavedState);
274
275 if (PROFILE_STARTUP) {
276 android.os.Debug.stopMethodTracing();
277 }
278
279 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 }
282
283 // For handling default keys
284 mDefaultKeySsb = new SpannableStringBuilder();
285 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800286
287 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
288 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 }
Romain Guycbb89e42009-06-08 15:52:54 -0700290
Winson Chungaafa03c2010-06-11 17:34:16 -0700291 @Override
292 public void onConfigurationChanged(Configuration newConfig) {
293 // TODO Auto-generated method stub
294 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700295 }
296
297
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700299 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
300 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 final Configuration configuration = getResources().getConfiguration();
303
Romain Guy98d01652009-06-30 16:21:04 -0700304 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 final String locale = configuration.locale.toString();
306
Romain Guy98d01652009-06-30 16:21:04 -0700307 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 final int mcc = configuration.mcc;
309
Romain Guy98d01652009-06-30 16:21:04 -0700310 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 final int mnc = configuration.mnc;
312
Romain Guy84f296c2009-11-04 15:00:44 -0800313 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314
Romain Guy84f296c2009-11-04 15:00:44 -0800315 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700316 localeConfiguration.locale = locale;
317 localeConfiguration.mcc = mcc;
318 localeConfiguration.mnc = mnc;
319
320 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800321 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400322
323 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700324 }
325 }
326
327 private static class LocaleConfiguration {
328 public String locale;
329 public int mcc = -1;
330 public int mnc = -1;
331 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700332
Romain Guy98d01652009-06-30 16:21:04 -0700333 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
334 DataInputStream in = null;
335 try {
336 in = new DataInputStream(context.openFileInput(PREFERENCES));
337 configuration.locale = in.readUTF();
338 configuration.mcc = in.readInt();
339 configuration.mnc = in.readInt();
340 } catch (FileNotFoundException e) {
341 // Ignore
342 } catch (IOException e) {
343 // Ignore
344 } finally {
345 if (in != null) {
346 try {
347 in.close();
348 } catch (IOException e) {
349 // Ignore
350 }
351 }
352 }
353 }
354
355 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
356 DataOutputStream out = null;
357 try {
358 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
359 out.writeUTF(configuration.locale);
360 out.writeInt(configuration.mcc);
361 out.writeInt(configuration.mnc);
362 out.flush();
363 } catch (FileNotFoundException e) {
364 // Ignore
365 } catch (IOException e) {
366 //noinspection ResultOfMethodCallIgnored
367 context.getFileStreamPath(PREFERENCES).delete();
368 } finally {
369 if (out != null) {
370 try {
371 out.close();
372 } catch (IOException e) {
373 // Ignore
374 }
375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
377 }
378
379 static int getScreen() {
380 synchronized (sLock) {
381 return sScreen;
382 }
383 }
384
385 static void setScreen(int screen) {
386 synchronized (sLock) {
387 sScreen = screen;
388 }
389 }
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700392 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393
394 Display display = getWindowManager().getDefaultDisplay();
395 boolean isPortrait = display.getWidth() < display.getHeight();
396
397 final int width = isPortrait ? display.getWidth() : display.getHeight();
398 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700399 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400402 // Note: This doesn't do all the client-id magic that BrowserProvider does
403 // in Browser. (http://b/2425179)
404 private Uri getDefaultBrowserUri() {
405 String url = getString(R.string.default_browser_url);
406 if (url.indexOf("{CID}") != -1) {
407 url = url.replace("{CID}", "android-google");
408 }
409 return Uri.parse(url);
410 }
411
412 // Load the Intent templates from arrays.xml to populate the hotseats. For
413 // each Intent, if it resolves to a single app, use that as the launch
414 // intent & use that app's label as the contentDescription. Otherwise,
415 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400416 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400417 if (mHotseatConfig == null) {
418 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
419 if (mHotseatConfig.length > 0) {
420 mHotseats = new Intent[mHotseatConfig.length];
421 mHotseatLabels = new CharSequence[mHotseatConfig.length];
422 mHotseatIcons = new Drawable[mHotseatConfig.length];
423 } else {
424 mHotseats = null;
425 mHotseatIcons = null;
426 mHotseatLabels = null;
427 }
428
429 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
430 for (int i=0; i<mHotseatConfig.length; i++) {
431 // load icon for this slot; currently unrelated to the actual activity
432 try {
433 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
434 } catch (ArrayIndexOutOfBoundsException ex) {
435 Log.w(TAG, "Missing hotseat_icons array item #" + i);
436 mHotseatIcons[i] = null;
437 }
438 }
439 hotseatIconDrawables.recycle();
440 }
441
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400442 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400443 for (int i=0; i<mHotseatConfig.length; i++) {
444 Intent intent = null;
445 if (mHotseatConfig[i].equals("*BROWSER*")) {
446 // magic value meaning "launch user's default web browser"
447 // replace it with a generic web request so we can see if there is indeed a default
448 String defaultUri = getString(R.string.default_browser_url);
449 intent = new Intent(
450 Intent.ACTION_VIEW,
451 ((defaultUri != null)
452 ? Uri.parse(defaultUri)
453 : getDefaultBrowserUri())
454 ).addCategory(Intent.CATEGORY_BROWSABLE);
455 // note: if the user launches this without a default set, she
456 // will always be taken to the default URL above; this is
457 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700458 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400459 // URL is unavoidably sent to the chosen app.
460 } else {
461 try {
462 intent = Intent.parseUri(mHotseatConfig[i], 0);
463 } catch (java.net.URISyntaxException ex) {
464 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
465 // bogus; leave intent=null
466 }
467 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700468
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400469 if (intent == null) {
470 mHotseats[i] = null;
471 mHotseatLabels[i] = getText(R.string.activity_not_found);
472 continue;
473 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400474
475 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700476 Log.d(TAG, "loadHotseats: hotseat " + i
477 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400478 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400479 + "]");
480 }
481
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400482 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
483 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400485 Log.d(TAG, "Best match for intent: " + bestMatch);
486 Log.d(TAG, "All matches: ");
487 for (ResolveInfo ri : allMatches) {
488 Log.d(TAG, " --> " + ri);
489 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400490 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 // did this resolve to a single app, or the resolver?
492 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700493 // can't find any activity to handle this. let's leave the
494 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400495 // to launch.
496 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400497
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400498 // set accessibility text to "Not installed"
499 mHotseatLabels[i] = getText(R.string.activity_not_found);
500 } else {
501 boolean found = false;
502 for (ResolveInfo ri : allMatches) {
503 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
504 && bestMatch.activityInfo.applicationInfo.packageName
505 .equals(ri.activityInfo.applicationInfo.packageName)) {
506 found = true;
507 break;
508 }
509 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700510
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400511 if (!found) {
512 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
513 // the bestMatch is probably the ResolveActivity, meaning the
514 // user has not yet selected a default
515 // so: we'll keep the original intent for now
516 mHotseats[i] = intent;
517
518 // set the accessibility text to "Select shortcut"
519 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
520 } else {
521 // we have an app!
522 // now reconstruct the intent to launch it through the front
523 // door
524 ComponentName com = new ComponentName(
525 bestMatch.activityInfo.applicationInfo.packageName,
526 bestMatch.activityInfo.name);
527 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
528
529 // load the app label for accessibility
530 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
531 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400532 }
533
534 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700535 Log.d(TAG, "loadHotseats: hotseat " + i
536 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400537 + ((mHotseats[i] == null)
538 ? "null"
539 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400540 + "] label=[" + mHotseatLabels[i]
541 + "]"
542 );
543 }
544 }
545 }
546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 @Override
548 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700549 mWaitingForResult = false;
550
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 // The pattern used here is that a user PICKs a specific application,
552 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
555 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700556
Romain Guy94dabf12009-07-21 10:55:43 -0700557 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 switch (requestCode) {
559 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 break;
562 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800563 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 break;
565 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 break;
568 case REQUEST_PICK_LIVE_FOLDER:
569 addLiveFolder(data);
570 break;
571 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700574 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700575 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700577 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700578 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700579 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700581 case REQUEST_PICK_WALLPAPER:
582 // We just wanted the activity result here so we can clear mWaitingForResult
583 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 }
Romain Guy18042c82009-11-06 11:44:55 -0800585 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
586 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
587 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700588 // Clean up the appWidgetId if we canceled
589 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
590 if (appWidgetId != -1) {
591 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595
596 @Override
597 protected void onResume() {
598 super.onResume();
599
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800600 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800601
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603 mWorkspaceLoading = true;
604 mModel.startLoader(this, true);
605 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 }
607 }
608
609 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700610 protected void onPause() {
611 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700612 // Some launcher layouts don't have a previous and next view
613 if (mPreviousView != null) {
614 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700615 }
616 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700617 dismissPreview(mNextView);
618 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800619 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700620 }
Romain Guycbb89e42009-06-08 15:52:54 -0700621
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700622 @Override
623 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 // Flag the loader to stop early before switching
625 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800626 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800627 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700628 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700629
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800630 // We can't hide the IME if it was forced open. So don't bother
631 /*
632 @Override
633 public void onWindowFocusChanged(boolean hasFocus) {
634 super.onWindowFocusChanged(hasFocus);
635
636 if (hasFocus) {
637 final InputMethodManager inputManager = (InputMethodManager)
638 getSystemService(Context.INPUT_METHOD_SERVICE);
639 WindowManager.LayoutParams lp = getWindow().getAttributes();
640 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
641 android.os.Handler()) {
642 protected void onReceiveResult(int resultCode, Bundle resultData) {
643 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
644 }
645 });
646 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
647 }
648 }
649 */
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 private boolean acceptFilter() {
652 final InputMethodManager inputManager = (InputMethodManager)
653 getSystemService(Context.INPUT_METHOD_SERVICE);
654 return !inputManager.isFullscreenMode();
655 }
656
657 @Override
658 public boolean onKeyDown(int keyCode, KeyEvent event) {
659 boolean handled = super.onKeyDown(keyCode, event);
660 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
661 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
662 keyCode, event);
663 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700664 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700665 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700666 // showSearchDialog()
667 // If there are multiple keystrokes before the search dialog takes focus,
668 // onSearchRequested() will be called for every keystroke,
669 // but it is idempotent, so it's fine.
670 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
672 }
673
Joe Onorato8a9625e2010-01-28 15:55:35 -0800674 // Eat the long press event so the keyboard doesn't come up.
675 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
676 return true;
677 }
678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 return handled;
680 }
681
Karl Rosaen138a0412009-04-23 19:00:21 -0700682 private String getTypedText() {
683 return mDefaultKeySsb.toString();
684 }
685
686 private void clearTypedText() {
687 mDefaultKeySsb.clear();
688 mDefaultKeySsb.clearSpans();
689 Selection.setSelection(mDefaultKeySsb, 0);
690 }
691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 /**
693 * Restores the previous state, if it exists.
694 *
695 * @param savedState The previous state.
696 */
697 private void restoreState(Bundle savedState) {
698 if (savedState == null) {
699 return;
700 }
701
Joe Onorato3a8820b2009-11-10 15:06:42 -0800702 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
703 if (allApps) {
704 showAllApps(false);
705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
708 if (currentScreen > -1) {
709 mWorkspace.setCurrentScreen(currentScreen);
710 }
711
712 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
713 if (addScreen > -1) {
714 mAddItemCellInfo = new CellLayout.CellInfo();
715 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
716 addItemCellInfo.valid = true;
717 addItemCellInfo.screen = addScreen;
718 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
719 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
720 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
721 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
722 addItemCellInfo.findVacantCellsFromOccupied(
723 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
724 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
725 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
726 mRestoring = true;
727 }
728
729 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
730 if (renameFolder) {
731 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 mRestoring = true;
734 }
735 }
736
737 /**
738 * Finds all the views we need and configure them properly.
739 */
740 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400741 DragController dragController = mDragController;
742
Romain Guyb1b69f52009-08-10 15:10:15 -0700743 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400744 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
Joe Onorato7c312c12009-08-13 21:36:53 -0700746 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700747 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700748 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800749 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700750 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700751 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
754 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500755 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700756
Joe Onorato7c312c12009-08-13 21:36:53 -0700757 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
758 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700760 View handleView = findViewById(R.id.all_apps_button);
761 if (handleView != null && handleView instanceof HandleView) {
762 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700763 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700764 mHandleView.setLauncher(this);
765 mHandleView.setOnClickListener(this);
766 mHandleView.setOnLongClickListener(this);
767 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800768
Michael Jurka0e260592010-06-30 17:07:39 -0700769 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
770 if (widgetChooser != null) {
771 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
772 widgetChooser.setAdapter(widgetGalleryAdapter);
773 widgetChooser.setDragController(dragController);
774 widgetChooser.setLauncher(this);
775
776 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700777 IntentListAdapter folderTypes = new FolderListAdapter(
778 this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
Michael Jurka0e260592010-06-30 17:07:39 -0700779 folderChooser.setAdapter(folderTypes);
780 folderChooser.setLauncher(this);
781
782 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700783 IntentListAdapter shortcutTypes = new IntentListAdapter(
784 this, Intent.ACTION_CREATE_SHORTCUT);
Michael Jurka0e260592010-06-30 17:07:39 -0700785 shortcutChooser.setAdapter(shortcutTypes);
786 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 } else {
788 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
789 hotseatLeft.setContentDescription(mHotseatLabels[0]);
790 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
791 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
792 hotseatRight.setContentDescription(mHotseatLabels[1]);
793 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400794
Michael Jurkaaf442092010-06-10 17:01:57 -0700795 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
796 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800797
Michael Jurkaaf442092010-06-10 17:01:57 -0700798 Drawable previous = mPreviousView.getDrawable();
799 Drawable next = mNextView.getDrawable();
800 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801
Michael Jurkaaf442092010-06-10 17:01:57 -0700802 mPreviousView.setHapticFeedbackEnabled(false);
803 mPreviousView.setOnLongClickListener(this);
804 mNextView.setHapticFeedbackEnabled(false);
805 mNextView.setOnLongClickListener(this);
806 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400809 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
812 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400813 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700814 int deleteZoneHandleId;
815 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700816 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700817 } else {
818 deleteZoneHandleId = R.id.all_apps_button_cluster;
819 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700820 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
Joe Onorato00acb122009-08-04 16:04:30 -0400822 dragController.setDragScoller(workspace);
823 dragController.setDragListener(deleteZone);
824 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800825 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700826
Joe Onorato00acb122009-08-04 16:04:30 -0400827 // The order here is bottom to top.
828 dragController.addDropTarget(workspace);
829 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
Romain Guy8a73c512009-11-09 19:19:59 -0800832 @SuppressWarnings({"UnusedDeclaration"})
833 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800834 if (!isAllAppsVisible()) {
835 mWorkspace.scrollLeft();
836 }
Romain Guy8a73c512009-11-09 19:19:59 -0800837 }
838
839 @SuppressWarnings({"UnusedDeclaration"})
840 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800841 if (!isAllAppsVisible()) {
842 mWorkspace.scrollRight();
843 }
Romain Guy8a73c512009-11-09 19:19:59 -0800844 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400845
846 @SuppressWarnings({"UnusedDeclaration"})
847 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400848 if (isAllAppsVisible()) return;
849
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400850 int index = -1;
851 if (v.getId() == R.id.hotseat_left) {
852 index = 0;
853 } else if (v.getId() == R.id.hotseat_right) {
854 index = 1;
855 }
856
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400857 // reload these every tap; you never know when they might change
858 loadHotseats();
859 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
860 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400861 startActivitySafely(
862 mHotseats[index],
863 "hotseat"
864 );
865 }
866 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 /**
869 * Creates a view representing a shortcut.
870 *
871 * @param info The data structure describing the shortcut.
872 *
873 * @return A View inflated from R.layout.application.
874 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800875 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 return createShortcut(R.layout.application,
877 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
878 }
879
880 /**
881 * Creates a view representing a shortcut inflated from the specified resource.
882 *
883 * @param layoutResId The id of the XML layout used to create the shortcut.
884 * @param parent The group the shortcut belongs to.
885 * @param info The data structure describing the shortcut.
886 *
887 * @return A View inflated from layoutResId.
888 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800889 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
891
Joe Onorato0589f0f2010-02-08 13:44:00 -0800892 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
893 new FastBitmapDrawable(info.getIcon(mIconCache)),
894 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 favorite.setText(info.title);
896 favorite.setTag(info);
897 favorite.setOnClickListener(this);
898
899 return favorite;
900 }
901
902 /**
903 * Add an application shortcut to the workspace.
904 *
905 * @param data The intent describing the application.
906 * @param cellInfo The position on screen where to create the shortcut.
907 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 cellInfo.screen = mWorkspace.getCurrentScreen();
910 if (!findSingleSlot(cellInfo)) return;
911
Joe Onorato0589f0f2010-02-08 13:44:00 -0800912 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
913 data, context);
914
Romain Guy73b979d2009-06-09 12:57:21 -0700915 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800916 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800918 info.container = ItemInfo.NO_ID;
919 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
920 } else {
921 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923 }
Romain Guycbb89e42009-06-08 15:52:54 -0700924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 /**
926 * Add a shortcut to the workspace.
927 *
928 * @param data The intent describing the shortcut.
929 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 cellInfo.screen = mWorkspace.getCurrentScreen();
933 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700934
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
937 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
940 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942 }
943
Romain Guycbb89e42009-06-08 15:52:54 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700946 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700948 * @param data The intent describing the appWidgetId.
949 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700951 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700952 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700953
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700954 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700956 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700957
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 // Try finding open space on Launcher screen
959 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800960 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
961 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
962 return;
963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700965 // Build Launcher-specific widget info and save to database
966 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 launcherInfo.spanX = spans[0];
968 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 LauncherModel.addItemToDatabase(this, launcherInfo,
971 LauncherSettings.Favorites.CONTAINER_DESKTOP,
972 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
973
974 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700978 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700979
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700980 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986 }
Romain Guycbb89e42009-06-08 15:52:54 -0700987
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
989 mDesktopItems.remove(launcherInfo);
990 launcherInfo.hostView = null;
991 }
992
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 public LauncherAppWidgetHost getAppWidgetHost() {
994 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
Romain Guycbb89e42009-06-08 15:52:54 -0700996
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800997 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800998 getWindow().closeAllPanels();
999
1000 try {
1001 dismissDialog(DIALOG_CREATE_SHORTCUT);
1002 // Unlock the workspace if the dialog was showing
1003 } catch (Exception e) {
1004 // An exception is thrown if the dialog is not visible, which is fine
1005 }
1006
1007 try {
1008 dismissDialog(DIALOG_RENAME_FOLDER);
1009 // Unlock the workspace if the dialog was showing
1010 } catch (Exception e) {
1011 // An exception is thrown if the dialog is not visible, which is fine
1012 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001013
1014 // Whatever we were doing is hereby canceled.
1015 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 @Override
1019 protected void onNewIntent(Intent intent) {
1020 super.onNewIntent(intent);
1021
1022 // Close the menu
1023 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001024 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001025 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001026
Joe Onorato14f122b2009-11-19 14:06:36 -08001027 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1028 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1029 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001030
1031 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001032 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001033 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001034 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001035 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001036 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001037 mWorkspace.unshrink();
Romain Guy1dd3a072009-07-16 13:21:01 -07001038
Joe Onorato3a8820b2009-11-10 15:06:42 -08001039 final View v = getWindow().peekDecorView();
1040 if (v != null && v.getWindowToken() != null) {
1041 InputMethodManager imm = (InputMethodManager)getSystemService(
1042 INPUT_METHOD_SERVICE);
1043 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 }
1045 }
1046 }
1047
1048 @Override
1049 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1050 // Do not call super here
1051 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001052
1053 if (mHomeCustomizationDrawer != null) {
1054 String cur = savedInstanceState.getString("currentTab");
1055 if (cur != null) {
1056 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1057 }
1058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060
1061 @Override
1062 protected void onSaveInstanceState(Bundle outState) {
1063 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1064
1065 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1066 if (folders.size() > 0) {
1067 final int count = folders.size();
1068 long[] ids = new long[count];
1069 for (int i = 0; i < count; i++) {
1070 final FolderInfo info = folders.get(i).getInfo();
1071 ids[i] = info.id;
1072 }
1073 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1074 } else {
1075 super.onSaveInstanceState(outState);
1076 }
1077
Joe Onoratofb0ca672009-09-14 17:55:46 -04001078 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001079 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082
1083 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1084 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1085 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1086
1087 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1088 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1089 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1090 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1091 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1092 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1093 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1094 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001095 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
1097
1098 if (mFolderInfo != null && mWaitingForResult) {
1099 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1100 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1101 }
Michael Jurka946ad472010-07-09 18:05:18 -07001102
1103 if (mHomeCustomizationDrawer != null) {
1104 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1105 if (currentTabTag != null) {
1106 outState.putString("currentTab", currentTabTag);
1107 }
1108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
1110
1111 @Override
1112 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001118 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
1120
1121 TextKeyListener.getInstance().release();
1122
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001126
1127 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001128
Patrick Dubroyab962b72010-07-26 12:10:10 -07001129 // Some launcher layouts don't have a previous and next view
1130 if (mPreviousView != null) {
1131 dismissPreview(mPreviousView);
1132 }
1133 if (mNextView != null) {
1134 dismissPreview(mNextView);
1135 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001136
1137 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139
1140 @Override
1141 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001142 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 super.startActivityForResult(intent, requestCode);
1144 }
1145
1146 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001147 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001149
Joe Onorato7bb17492009-09-24 17:51:01 -07001150 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Karl Rosaen138a0412009-04-23 19:00:21 -07001152 if (initialQuery == null) {
1153 // Use any text typed in the launcher as the initial query
1154 initialQuery = getTypedText();
1155 clearTypedText();
1156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 if (appSearchData == null) {
1158 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001159 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 }
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Karl Rosaen138a0412009-04-23 19:00:21 -07001162 final SearchManager searchManager =
1163 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001164 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001165 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167
1168 @Override
1169 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 if (isWorkspaceLocked()) {
1171 return false;
1172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173
1174 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1177 .setIcon(android.R.drawable.ic_menu_add)
1178 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001179 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 .setIcon(android.R.drawable.ic_menu_gallery)
1181 .setAlphabeticShortcut('W');
1182 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1183 .setIcon(android.R.drawable.ic_search_category_default)
1184 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1185 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1186 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1187 .setAlphabeticShortcut('N');
1188
1189 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001190 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1191 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
1193 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1194 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1195 .setIntent(settings);
1196
1197 return true;
1198 }
1199
1200 @Override
1201 public boolean onPrepareOptionsMenu(Menu menu) {
1202 super.onPrepareOptionsMenu(menu);
1203
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 // If all apps is animating, don't show the menu, because we don't know
1205 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001206 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001207 return false;
1208 }
1209
1210 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001211 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1213 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1214
1215 // Disable add if the workspace is full.
1216 if (visible) {
1217 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1218 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
1221 return true;
1222 }
1223
Michael Jurka0e260592010-06-30 17:07:39 -07001224 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1225 // using the settings menu to add an item, something similar happens in showAddDialog
1226 public void prepareAddItemFromHomeCustomizationDrawer() {
1227 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1228 mAddItemCellInfo = mMenuAddInfo;
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 @Override
1232 public boolean onOptionsItemSelected(MenuItem item) {
1233 switch (item.getItemId()) {
1234 case MENU_ADD:
1235 addItems();
1236 return true;
1237 case MENU_WALLPAPER_SETTINGS:
1238 startWallpaper();
1239 return true;
1240 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001241 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 return true;
1243 case MENU_NOTIFICATIONS:
1244 showNotifications();
1245 return true;
1246 }
1247
1248 return super.onOptionsItemSelected(item);
1249 }
Romain Guycbb89e42009-06-08 15:52:54 -07001250
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 /**
1252 * Indicates that we want global search for this activity by setting the globalSearch
1253 * argument for {@link #startSearch} to true.
1254 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001256 @Override
1257 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001258 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001260 }
1261
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 public boolean isWorkspaceLocked() {
1263 return mWorkspaceLoading || mWaitingForResult;
1264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001267 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001268 if (LauncherApplication.isScreenXLarge()) {
1269 // Animate the widget chooser up from the bottom of the screen
1270 if (!isCustomizationDrawerVisible()) {
1271 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001272 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001273 }
1274 } else {
1275 showAddDialog(mMenuAddInfo);
1276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
Michael Jurkaaf442092010-06-10 17:01:57 -07001279 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1280 mAddItemCellInfo = cellInfo;
1281 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1282 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1283 addAppWidgetImpl(appWidgetId);
1284 }
1285
1286 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001287 // TODO: catch bad widget exception when sent
1288 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001289 // TODO: Is this log message meaningful?
1290 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1291 addAppWidgetImpl(appWidgetId);
1292 }
1293
1294 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001295 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296
Bjorn Bringert7984c942009-12-09 15:38:25 +00001297 if (appWidget.configure != null) {
1298 // Launch over to configure widget, if needed
1299 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1300 intent.setComponent(appWidget.configure);
1301 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1302
Romain Guy8e633c52010-03-04 12:51:36 -08001303 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001305 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001306 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308 }
Romain Guycbb89e42009-06-08 15:52:54 -07001309
Joe Onoratodeb98af2010-02-19 14:59:39 -08001310 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001311 // Handle case where user selected "Applications"
1312 String applicationName = getResources().getString(R.string.group_applications);
1313 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001315 if (applicationName != null && applicationName.equals(shortcutName)) {
1316 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1317 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001318
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001319 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1320 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001321 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001322 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001323 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001328 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001329 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001330 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001331
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001332 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001334 } else {
1335 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 UserFolderInfo folderInfo = new UserFolderInfo();
1341 folderInfo.title = getText(R.string.folder_name);
1342
1343 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1344 cellInfo.screen = mWorkspace.getCurrentScreen();
1345 if (!findSingleSlot(cellInfo)) return;
1346
1347 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 LauncherModel.addItemToDatabase(this, folderInfo,
1349 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
1353 // Create the view
1354 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1355 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1356 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
Romain Guycbb89e42009-06-08 15:52:54 -07001359
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 void removeFolder(FolderInfo folder) {
1361 mFolders.remove(folder.id);
1362 }
1363
1364 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 cellInfo.screen = mWorkspace.getCurrentScreen();
1366 if (!findSingleSlot(cellInfo)) return;
1367
1368 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1369
1370 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1372 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1374 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
1376 }
1377
1378 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1379 CellLayout.CellInfo cellInfo, boolean notify) {
1380
1381 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1382 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1383
1384 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 Intent.ShortcutIconResource iconResource = null;
1386
1387 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1388 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1389 try {
1390 iconResource = (Intent.ShortcutIconResource) extra;
1391 final PackageManager packageManager = context.getPackageManager();
1392 Resources resources = packageManager.getResourcesForApplication(
1393 iconResource.packageName);
1394 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1395 icon = resources.getDrawable(id);
1396 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001397 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399 }
1400
1401 if (icon == null) {
1402 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1403 }
1404
1405 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001406 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 info.title = name;
1408 info.iconResource = iconResource;
1409 info.uri = data.getData();
1410 info.baseIntent = baseIntent;
1411 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1412 LiveFolders.DISPLAY_MODE_GRID);
1413
1414 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1415 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417
1418 return info;
1419 }
1420
1421 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1422 final int[] xy = new int[2];
1423 if (findSlot(cellInfo, xy, 1, 1)) {
1424 cellInfo.cellX = xy[0];
1425 cellInfo.cellY = xy[1];
1426 return true;
1427 }
1428 return false;
1429 }
1430
1431 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1432 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1433 boolean[] occupied = mSavedState != null ?
1434 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1435 cellInfo = mWorkspace.findAllVacantCells(occupied);
1436 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1437 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1438 return false;
1439 }
1440 }
1441 return true;
1442 }
1443
1444 private void showNotifications() {
1445 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1446 if (statusBar != null) {
1447 statusBar.expand();
1448 }
1449 }
1450
1451 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001452 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001454 Intent chooser = Intent.createChooser(pickWallpaper,
1455 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001456 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1457 // Removed in Eclair MR1
1458// WallpaperManager wm = (WallpaperManager)
1459// getSystemService(Context.WALLPAPER_SERVICE);
1460// WallpaperInfo wi = wm.getWallpaperInfo();
1461// if (wi != null && wi.getSettingsActivity() != null) {
1462// LabeledIntent li = new LabeledIntent(getPackageName(),
1463// R.string.configure_wallpaper, 0);
1464// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1465// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1466// }
Mike Clerona0618e42009-10-22 13:55:21 -07001467 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001470 /**
1471 * Registers various content observers. The current implementation registers
1472 * only a favorites observer to keep track of the favorites applications.
1473 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001474 private void registerContentObservers() {
1475 ContentResolver resolver = getContentResolver();
1476 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1477 true, mWidgetObserver);
1478 }
1479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 @Override
1481 public boolean dispatchKeyEvent(KeyEvent event) {
1482 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1483 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001485 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001486 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001487 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001488 dumpState();
1489 return true;
1490 }
1491 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001492 }
1493 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1494 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001495 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 return true;
1497 }
1498 }
1499
1500 return super.dispatchKeyEvent(event);
1501 }
1502
Joe Onorato88ec0992009-11-19 13:16:06 -08001503 @Override
1504 public void onBackPressed() {
1505 if (isAllAppsVisible()) {
1506 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001507 } else if (isCustomizationDrawerVisible()) {
1508 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001509 mWorkspace.unshrink();
Joe Onorato88ec0992009-11-19 13:16:06 -08001510 } else {
1511 closeFolder();
1512 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001513 // Some launcher layouts don't have a previous and next view
1514 if (mPreviousView != null) {
1515 dismissPreview(mPreviousView);
1516 dismissPreview(mNextView);
1517 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001518 }
1519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 private void closeFolder() {
1521 Folder folder = mWorkspace.getOpenFolder();
1522 if (folder != null) {
1523 closeFolder(folder);
1524 }
1525 }
1526
1527 void closeFolder(Folder folder) {
1528 folder.getInfo().opened = false;
1529 ViewGroup parent = (ViewGroup) folder.getParent();
1530 if (parent != null) {
1531 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001532 if (folder instanceof DropTarget) {
1533 // Live folders aren't DropTargets.
1534 mDragController.removeDropTarget((DropTarget)folder);
1535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537 folder.onClose();
1538 }
1539
1540 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001541 * Re-listen when widgets are reset.
1542 */
1543 private void onAppWidgetReset() {
1544 mAppWidgetHost.startListening();
1545 }
1546
1547 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1549 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551 private void unbindDesktopItems() {
1552 for (ItemInfo item: mDesktopItems) {
1553 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 /**
1558 * Launches the intent referred by the clicked shortcut.
1559 *
1560 * @param v The view representing the clicked shortcut.
1561 */
1562 public void onClick(View v) {
1563 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001564 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001566 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001567 int[] pos = new int[2];
1568 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001569 intent.setSourceBounds(new Rect(pos[0], pos[1],
1570 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001571 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 } else if (tag instanceof FolderInfo) {
1573 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001574 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001575 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001576 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001577 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001578 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581 }
1582
Michael Jurka0e260592010-06-30 17:07:39 -07001583 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001584 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001585 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001586 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001587 mWorkspace.unshrink();
Michael Jurka0e260592010-06-30 17:07:39 -07001588 return false;
1589 }
1590
Michael Jurkaaf442092010-06-10 17:01:57 -07001591 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001592 * Event handler for the search button
1593 *
1594 * @param v The view that was clicked.
1595 */
1596 public void onClickSearchButton(View v) {
1597 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1598 View button = findViewById(R.id.search_button);
1599 i.setSourceBounds(
1600 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1601 startActivity(i);
1602 }
1603
1604 /**
1605 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001606 * enters home screen customization mode.
1607 *
1608 * @param v The view that was clicked.
1609 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001610 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001611 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001612 }
1613
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001614 /**
1615 * Event handler for the "grid" button that appears on the home screen, which
1616 * enters all apps mode.
1617 *
1618 * @param v The view that was clicked.
1619 */
1620 public void onClickAllAppsButton(View v) {
1621 showAllApps(true);
1622 }
1623
Joe Onoratof984e852010-03-25 09:47:45 -07001624 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1626 try {
1627 startActivity(intent);
1628 } catch (ActivityNotFoundException e) {
1629 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001630 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 } catch (SecurityException e) {
1632 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001633 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001635 "or use the exported attribute for this activity. "
1636 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
1638 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001639
Romain Guy8e633c52010-03-04 12:51:36 -08001640 void startActivityForResultSafely(Intent intent, int requestCode) {
1641 try {
1642 startActivityForResult(intent, requestCode);
1643 } catch (ActivityNotFoundException e) {
1644 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1645 } catch (SecurityException e) {
1646 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1647 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1648 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1649 "or use the exported attribute for this activity.", e);
1650 }
1651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
1653 private void handleFolderClick(FolderInfo folderInfo) {
1654 if (!folderInfo.opened) {
1655 // Close any open folder
1656 closeFolder();
1657 // Open the requested folder
1658 openFolder(folderInfo);
1659 } else {
1660 // Find the open folder...
1661 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1662 int folderScreen;
1663 if (openFolder != null) {
1664 folderScreen = mWorkspace.getScreenForView(openFolder);
1665 // .. and close it
1666 closeFolder(openFolder);
1667 if (folderScreen != mWorkspace.getCurrentScreen()) {
1668 // Close any folder open on the current screen
1669 closeFolder();
1670 // Pull the folder onto this screen
1671 openFolder(folderInfo);
1672 }
1673 }
1674 }
1675 }
1676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 /**
1678 * Opens the user fodler described by the specified tag. The opening of the folder
1679 * is animated relative to the specified View. If the View is null, no animation
1680 * is played.
1681 *
1682 * @param folderInfo The FolderInfo describing the folder to open.
1683 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001684 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 Folder openFolder;
1686
1687 if (folderInfo instanceof UserFolderInfo) {
1688 openFolder = UserFolder.fromXml(this);
1689 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001690 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 } else {
1692 return;
1693 }
1694
Joe Onorato00acb122009-08-04 16:04:30 -04001695 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 openFolder.setLauncher(this);
1697
1698 openFolder.bind(folderInfo);
1699 folderInfo.opened = true;
1700
Winson Chungaafa03c2010-06-11 17:34:16 -07001701 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 openFolder.onOpen();
1704 }
1705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001707 switch (v.getId()) {
1708 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001709 if (!isAllAppsVisible()) {
1710 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1711 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001712 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001713 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001714 return true;
1715 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001716 if (!isAllAppsVisible()) {
1717 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1718 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001719 showPreviews(v);
1720 }
1721 return true;
1722 case R.id.all_apps_button:
1723 if (!isAllAppsVisible()) {
1724 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1725 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1726 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001727 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001728 return true;
1729 }
1730
Joe Onorato9c1289c2009-08-17 11:03:03 -04001731 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 return false;
1733 }
1734
1735 if (!(v instanceof CellLayout)) {
1736 v = (View) v.getParent();
1737 }
1738
1739 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1740
1741 // This happens when long clicking an item with the dpad/trackball
1742 if (cellInfo == null) {
1743 return true;
1744 }
1745
1746 if (mWorkspace.allowLongPress()) {
1747 if (cellInfo.cell == null) {
1748 if (cellInfo.valid) {
1749 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001750 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001751 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1752 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 showAddDialog(cellInfo);
1754 }
1755 } else {
1756 if (!(cellInfo.cell instanceof Folder)) {
1757 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001758 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1759 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 mWorkspace.startDrag(cellInfo);
1761 }
1762 }
1763 }
1764 return true;
1765 }
1766
Romain Guye6b8e2f2009-11-10 11:56:55 -08001767 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001768 private void dismissPreview(final View v) {
1769 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001770 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001771 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1772 public void onDismiss() {
1773 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1774 int count = group.getChildCount();
1775 for (int i = 0; i < count; i++) {
1776 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1777 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001778 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1779 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1780
1781 v.setTag(R.id.workspace, null);
1782 v.setTag(R.id.icon, null);
1783 window.setOnDismissListener(null);
1784 }
1785 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001786 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001787 }
1788 v.setTag(null);
1789 }
1790
Romain Guyf8e6a802009-12-07 17:48:02 -08001791 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001792 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001793 }
1794
Romain Guya6abce82009-11-10 02:54:41 -08001795 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001796 final Resources resources = getResources();
1797 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001798
Romain Guya6abce82009-11-10 02:54:41 -08001799 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001800
Romain Guy9d31e9f2009-11-11 18:54:28 -08001801 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001802
Romain Guyf8e6a802009-12-07 17:48:02 -08001803 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001804 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001805 int extraW = (int) ((r.left + r.right) * max);
1806 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001807
1808 int aW = cell.getWidth() - extraW;
1809 float w = aW / max;
1810
1811 int width = cell.getWidth();
1812 int height = cell.getHeight();
1813 int x = cell.getLeftPadding();
1814 int y = cell.getTopPadding();
1815 width -= (x + cell.getRightPadding());
1816 height -= (y + cell.getBottomPadding());
1817
1818 float scale = w / width;
1819
1820 int count = end - start;
1821
1822 final float sWidth = width * scale;
1823 float sHeight = height * scale;
1824
Romain Guye6b8e2f2009-11-10 11:56:55 -08001825 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001826
Romain Guye6b8e2f2009-11-10 11:56:55 -08001827 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1828 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001829
1830 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001831 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001832 cell = (CellLayout) workspace.getChildAt(i);
1833
Romain Guyf8e6a802009-12-07 17:48:02 -08001834 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001835 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001836
1837 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001838 c.scale(scale, scale);
1839 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1840 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001841
Romain Guy9d31e9f2009-11-11 18:54:28 -08001842 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001843 image.setImageBitmap(bitmap);
1844 image.setTag(i);
1845 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001846 image.setOnFocusChangeListener(handler);
1847 image.setFocusable(true);
1848 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001849
1850 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001851 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1852
Winson Chungaafa03c2010-06-11 17:34:16 -07001853 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001854 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001855
1856 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001857 p.setContentView(preview);
1858 p.setWidth((int) (sWidth * count + extraW));
1859 p.setHeight((int) (sHeight + extraH));
1860 p.setAnimationStyle(R.style.AnimationPreview);
1861 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001862 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001863 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001864 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001865
Romain Guye6b8e2f2009-11-10 11:56:55 -08001866 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1867 public void onDismiss() {
1868 dismissPreview(anchor);
1869 }
1870 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001871
1872 anchor.setTag(p);
1873 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001874 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001875 }
1876
Romain Guy9d31e9f2009-11-11 18:54:28 -08001877 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001878 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001879
Romain Guye6b8e2f2009-11-10 11:56:55 -08001880 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001881 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001882 }
1883
1884 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001885 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001886 v.post(this);
1887 }
1888
1889 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001890 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001891 }
1892
1893 public void onFocusChange(View v, boolean hasFocus) {
1894 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001895 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001896 }
Romain Guya6abce82009-11-10 02:54:41 -08001897 }
1898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 Workspace getWorkspace() {
1901 return mWorkspace;
1902 }
1903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 @Override
1905 protected Dialog onCreateDialog(int id) {
1906 switch (id) {
1907 case DIALOG_CREATE_SHORTCUT:
1908 return new CreateShortcut().createDialog();
1909 case DIALOG_RENAME_FOLDER:
1910 return new RenameFolder().createDialog();
1911 }
1912
1913 return super.onCreateDialog(id);
1914 }
1915
1916 @Override
1917 protected void onPrepareDialog(int id, Dialog dialog) {
1918 switch (id) {
1919 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 break;
1921 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001922 if (mFolderInfo != null) {
1923 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1924 final CharSequence text = mFolderInfo.title;
1925 input.setText(text);
1926 input.setSelection(0, text.length());
1927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 break;
1929 }
1930 }
1931
1932 void showRenameDialog(FolderInfo info) {
1933 mFolderInfo = info;
1934 mWaitingForResult = true;
1935 showDialog(DIALOG_RENAME_FOLDER);
1936 }
1937
1938 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1939 mAddItemCellInfo = cellInfo;
1940 mWaitingForResult = true;
1941 showDialog(DIALOG_CREATE_SHORTCUT);
1942 }
1943
Joe Onoratodeb98af2010-02-19 14:59:39 -08001944 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001945 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001946 Bundle bundle = new Bundle();
1947
1948 ArrayList<String> shortcutNames = new ArrayList<String>();
1949 shortcutNames.add(getString(R.string.group_applications));
1950 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1951
1952 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1953 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1954 R.drawable.ic_launcher_application));
1955 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1956
1957 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1958 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001959 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001960 pickIntent.putExtras(bundle);
1961
Joe Onoratodeb98af2010-02-19 14:59:39 -08001962 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001963 }
1964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 private class RenameFolder {
1966 private EditText mInput;
1967
1968 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1970 mInput = (EditText) layout.findViewById(R.id.folder_name);
1971
1972 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1973 builder.setIcon(0);
1974 builder.setTitle(getString(R.string.rename_folder_title));
1975 builder.setCancelable(true);
1976 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1977 public void onCancel(DialogInterface dialog) {
1978 cleanup();
1979 }
1980 });
1981 builder.setNegativeButton(getString(R.string.cancel_action),
1982 new Dialog.OnClickListener() {
1983 public void onClick(DialogInterface dialog, int which) {
1984 cleanup();
1985 }
1986 }
1987 );
1988 builder.setPositiveButton(getString(R.string.rename_action),
1989 new Dialog.OnClickListener() {
1990 public void onClick(DialogInterface dialog, int which) {
1991 changeFolderName();
1992 }
1993 }
1994 );
1995 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001996
1997 final AlertDialog dialog = builder.create();
1998 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1999 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002000 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002001 mInput.requestFocus();
2002 InputMethodManager inputManager = (InputMethodManager)
2003 getSystemService(Context.INPUT_METHOD_SERVICE);
2004 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002005 }
2006 });
2007
2008 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
2011 private void changeFolderName() {
2012 final String name = mInput.getText().toString();
2013 if (!TextUtils.isEmpty(name)) {
2014 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 mFolderInfo.title = name;
2017 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2018
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002020 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 } else {
2023 final FolderIcon folderIcon = (FolderIcon)
2024 mWorkspace.getViewForTag(mFolderInfo);
2025 if (folderIcon != null) {
2026 folderIcon.setText(name);
2027 getWorkspace().requestLayout();
2028 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002029 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030 mWorkspaceLoading = true;
2031 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033 }
2034 }
2035 cleanup();
2036 }
2037
2038 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 dismissDialog(DIALOG_RENAME_FOLDER);
2040 mWaitingForResult = false;
2041 mFolderInfo = null;
2042 }
2043 }
2044
Daniel Sandler843e8602010-06-07 14:59:01 -04002045 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2046 public boolean isAllAppsVisible() {
2047 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002048 }
2049
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002050 // AllAppsView.Watcher
2051 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002052 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2053 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002054 mWorkspace.setVisibility(View.GONE);
2055 }
2056 }
2057
Joe Onorato3a8820b2009-11-10 15:06:42 -08002058 void showAllApps(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002059 hideCustomizationDrawer();
2060
Michael Jurka79212d82010-07-30 16:36:20 -07002061 if (LauncherApplication.isScreenXLarge()) {
2062 mWorkspace.shrinkToBottom(animated);
2063 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002064 if (LauncherApplication.isScreenXLarge() && animated) {
2065 // Not really a zoom -- this just makes the view visible
2066 mAllAppsGrid.zoom(1.0f, false);
2067 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_in);
2068 ((View) mAllAppsGrid).startAnimation(anim);
2069 } else {
2070 mAllAppsGrid.zoom(1.0f, animated);
2071 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002072
Romain Guyc16fea72010-03-12 17:17:56 -08002073 ((View) mAllAppsGrid).setFocusable(true);
2074 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002075
Joe Onorato7c312c12009-08-13 21:36:53 -07002076 // TODO: fade these two too
2077 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002078 }
2079
Joe Onoratob2061212009-11-24 16:13:54 -05002080 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002081 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002082 * - Home from workspace
2083 * - from center screen
2084 * - from other screens
2085 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002086 * - from center screen
2087 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002088 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002089 * - from center screen
2090 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002091 * - Launch app from workspace and quit
2092 * - with back
2093 * - with home
2094 * - Launch app from all apps and quit
2095 * - with back
2096 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002097 * - Go to a screen that's not the default, then all
2098 * apps, and launch and app, and go back
2099 * - with back
2100 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002101 * - On workspace, long press power and go back
2102 * - with back
2103 * - with home
2104 * - On all apps, long press power and go back
2105 * - with back
2106 * - with home
2107 * - On workspace, power off
2108 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002109 * - Launch an app and turn off the screen while in that app
2110 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002111 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002112 * - From all apps
2113 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002114 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2115 * - From all apps
2116 * - From the center workspace
2117 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002118 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002119 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002120 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002121 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002122 if (LauncherApplication.isScreenXLarge() && animated) {
2123 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_out);
2124 anim.setAnimationListener(new AnimationListener() {
2125 public void onAnimationStart(Animation animation) {}
2126 public void onAnimationRepeat(Animation animation) {}
2127 public void onAnimationEnd(Animation animation) {
2128 mAllAppsGrid.zoom(0.0f, false);
2129 }
2130 });
2131 ((View)mAllAppsGrid).startAnimation(anim);
Michael Jurka213d9632010-07-28 11:29:25 -07002132 mWorkspace.unshrink();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002133 } else {
2134 mAllAppsGrid.zoom(0.0f, animated);
2135 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002136 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002137 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002138 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002139 }
2140
Joe Onorato7c312c12009-08-13 21:36:53 -07002141 void lockAllApps() {
2142 // TODO
2143 }
2144
2145 void unlockAllApps() {
2146 // TODO
2147 }
2148
Patrick Dubroy558654c2010-07-23 16:48:11 -07002149 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002150 return mHomeCustomizationDrawer != null &&
2151 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002152 }
2153
2154 private void showCustomizationDrawer() {
2155 if (isAllAppsVisible()) {
2156 // TODO: Make a smoother transition here
2157 closeAllApps(false);
2158 }
2159 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
Michael Jurka54f7ac32010-08-02 13:56:46 -07002160 mHomeCustomizationDrawer.startAnimation(
2161 AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
Patrick Dubroy558654c2010-07-23 16:48:11 -07002162 }
2163
Michael Jurka213d9632010-07-28 11:29:25 -07002164 private void hideCustomizationDrawer() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002165 if (isCustomizationDrawerVisible()) {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002166 Animation slideDownAnimation = AnimationUtils.loadAnimation(
2167 this, R.anim.home_customization_drawer_slide_down);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002168 slideDownAnimation.setAnimationListener(new Animation.AnimationListener() {
2169 public void onAnimationEnd(Animation animation) {
2170 mHomeCustomizationDrawer.setVisibility(View.GONE);
2171 }
2172 public void onAnimationRepeat(Animation animation) {}
2173 public void onAnimationStart(Animation animation) {}
2174 });
2175 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
2176 }
2177 }
2178
Michael Jurka213d9632010-07-28 11:29:25 -07002179 void onWorkspaceUnshrink() {
2180 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002181 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002182 }
2183 if (isCustomizationDrawerVisible()) {
2184 hideCustomizationDrawer();
2185 }
2186 }
2187
Joe Onorato7404ee42009-07-31 11:54:44 -07002188 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 * Displays the shortcut creation dialog and launches, if necessary, the
2190 * appropriate activity.
2191 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002192 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002193 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2194 DialogInterface.OnShowListener {
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2202 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002203 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 builder.setInverseBackgroundForced(true);
2206
2207 AlertDialog dialog = builder.create();
2208 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002209 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002210 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 return dialog;
2213 }
2214
2215 public void onCancel(DialogInterface dialog) {
2216 mWaitingForResult = false;
2217 cleanup();
2218 }
2219
Romain Guycbb89e42009-06-08 15:52:54 -07002220 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002221 }
2222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002224 try {
2225 dismissDialog(DIALOG_CREATE_SHORTCUT);
2226 } catch (Exception e) {
2227 // An exception is thrown if the dialog is not visible, which is fine
2228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 }
2230
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002231 /**
2232 * Handle the action clicked in the "Add to home" dialog.
2233 */
2234 public void onClick(DialogInterface dialog, int which) {
2235 Resources res = getResources();
2236 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002237
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002238 switch (which) {
2239 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002240 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002241 break;
2242 }
Romain Guycbb89e42009-06-08 15:52:54 -07002243
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002244 case AddAdapter.ITEM_APPWIDGET: {
2245 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002246
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002247 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2248 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002249 // start the pick activity
2250 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2251 break;
2252 }
Romain Guycbb89e42009-06-08 15:52:54 -07002253
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002254 case AddAdapter.ITEM_LIVE_FOLDER: {
2255 // Insert extra item to handle inserting folder
2256 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002257
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002258 ArrayList<String> shortcutNames = new ArrayList<String>();
2259 shortcutNames.add(res.getString(R.string.group_folder));
2260 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002261
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002262 ArrayList<ShortcutIconResource> shortcutIcons =
2263 new ArrayList<ShortcutIconResource>();
2264 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2265 R.drawable.ic_launcher_folder));
2266 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2267
2268 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2269 pickIntent.putExtra(Intent.EXTRA_INTENT,
2270 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2271 pickIntent.putExtra(Intent.EXTRA_TITLE,
2272 getText(R.string.title_select_live_folder));
2273 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002274
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002275 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2276 break;
2277 }
2278
2279 case AddAdapter.ITEM_WALLPAPER: {
2280 startWallpaper();
2281 break;
2282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 }
2284 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002285
2286 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002287 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002288 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 }
2290
2291 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002292 * Receives notifications when applications are added/removed.
2293 */
2294 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2295 @Override
2296 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002297 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002298 String reason = intent.getStringExtra("reason");
2299 if (!"homekey".equals(reason)) {
2300 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002301 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002302 animate = false;
2303 }
Joe Onoratob2061212009-11-24 16:13:54 -05002304 closeAllApps(animate);
2305 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002306 }
2307 }
2308
2309 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002310 * Receives notifications whenever the appwidgets are reset.
2311 */
2312 private class AppWidgetResetObserver extends ContentObserver {
2313 public AppWidgetResetObserver() {
2314 super(new Handler());
2315 }
2316
2317 @Override
2318 public void onChange(boolean selfChange) {
2319 onAppWidgetReset();
2320 }
2321 }
2322
2323 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002324 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002326 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002327 if (mWorkspace != null) {
2328 return mWorkspace.getCurrentScreen();
2329 } else {
2330 return SCREEN_COUNT / 2;
2331 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002333
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 /**
2335 * Refreshes the shortcuts shown on the workspace.
2336 *
2337 * Implementation of the method from LauncherModel.Callbacks.
2338 */
2339 public void startBinding() {
2340 final Workspace workspace = mWorkspace;
2341 int count = workspace.getChildCount();
2342 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002343 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002344 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2345 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002346
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 if (DEBUG_USER_INTERFACE) {
2348 android.widget.Button finishButton = new android.widget.Button(this);
2349 finishButton.setText("Finish");
2350 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2351
2352 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2353 public void onClick(View v) {
2354 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002356 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 }
2358 }
2359
2360 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002361 * Bind the items start-end from the list.
2362 *
2363 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002365 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2366
2367 final Workspace workspace = mWorkspace;
2368
2369 for (int i=start; i<end; i++) {
2370 final ItemInfo item = shortcuts.get(i);
2371 mDesktopItems.add(item);
2372 switch (item.itemType) {
2373 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2374 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002375 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002376 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2377 false);
2378 break;
2379 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2380 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2381 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2382 (UserFolderInfo) item);
2383 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2384 false);
2385 break;
2386 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2387 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2388 R.layout.live_folder_icon, this,
2389 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2390 (LiveFolderInfo) item);
2391 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2392 false);
2393 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 }
2396
Joe Onorato9c1289c2009-08-17 11:03:03 -04002397 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 }
2399
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 /**
2401 * Implementation of the method from LauncherModel.Callbacks.
2402 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002403 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2404 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002405 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002407
2408 /**
2409 * Add the views for a widget to the workspace.
2410 *
2411 * Implementation of the method from LauncherModel.Callbacks.
2412 */
2413 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002414 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2415 if (DEBUG_WIDGETS) {
2416 Log.d(TAG, "bindAppWidget: " + item);
2417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 final Workspace workspace = mWorkspace;
2419
2420 final int appWidgetId = item.appWidgetId;
2421 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002422 if (DEBUG_WIDGETS) {
2423 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2424 }
2425
Joe Onorato9c1289c2009-08-17 11:03:03 -04002426 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2427
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2429 item.hostView.setTag(item);
2430
2431 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2432 item.cellY, item.spanX, item.spanY, false);
2433
2434 workspace.requestLayout();
2435
2436 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002437
2438 if (DEBUG_WIDGETS) {
2439 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2440 + (SystemClock.uptimeMillis()-start) + "ms");
2441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002442 }
2443
2444 /**
2445 * Callback saying that there aren't any more items to bind.
2446 *
2447 * Implementation of the method from LauncherModel.Callbacks.
2448 */
2449 public void finishBindingItems() {
2450 if (mSavedState != null) {
2451 if (!mWorkspace.hasFocus()) {
2452 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2453 }
2454
2455 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2456 if (userFolders != null) {
2457 for (long folderId : userFolders) {
2458 final FolderInfo info = mFolders.get(folderId);
2459 if (info != null) {
2460 openFolder(info);
2461 }
2462 }
2463 final Folder openFolder = mWorkspace.getOpenFolder();
2464 if (openFolder != null) {
2465 openFolder.requestFocus();
2466 }
2467 }
2468
Joe Onorato9c1289c2009-08-17 11:03:03 -04002469 mSavedState = null;
2470 }
2471
2472 if (mSavedInstanceState != null) {
2473 super.onRestoreInstanceState(mSavedInstanceState);
2474 mSavedInstanceState = null;
2475 }
2476
Joe Onorato9c1289c2009-08-17 11:03:03 -04002477 mWorkspaceLoading = false;
2478 }
2479
2480 /**
2481 * Add the icons for all apps.
2482 *
2483 * Implementation of the method from LauncherModel.Callbacks.
2484 */
2485 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002486 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002487 }
2488
2489 /**
2490 * A package was installed.
2491 *
2492 * Implementation of the method from LauncherModel.Callbacks.
2493 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002494 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002496 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 }
2498
2499 /**
2500 * A package was updated.
2501 *
2502 * Implementation of the method from LauncherModel.Callbacks.
2503 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002504 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002506 mWorkspace.updateShortcuts(apps);
2507 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002508 }
2509
2510 /**
2511 * A package was uninstalled.
2512 *
2513 * Implementation of the method from LauncherModel.Callbacks.
2514 */
Joe Onorato36115782010-06-17 13:28:48 -04002515 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002517 if (permanent) {
2518 mWorkspace.removeItems(apps);
2519 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002520 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 }
Joe Onoratobe386092009-11-17 17:32:16 -08002522
2523 /**
2524 * Prints out out state for debugging.
2525 */
2526 public void dumpState() {
2527 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002528 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002529 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2530 Log.d(TAG, "mRestoring=" + mRestoring);
2531 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2532 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2533 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2534 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002535 mModel.dumpState();
2536 mAllAppsGrid.dumpState();
2537 Log.d(TAG, "END launcher2 dump state");
2538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539}