blob: 43b12ed4e943c09100788ed656839d87f684b699 [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;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.content.Intent.ShortcutIconResource;
Patrick Dubroy558654c2010-07-23 16:48:11 -070038import android.content.IntentFilter;
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;
Patrick Dubroy558654c2010-07-23 16:48:11 -070071import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070073import android.view.WindowManager;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.animation.Animation;
Patrick Dubroy558654c2010-07-23 16:48:11 -070075import android.view.animation.Animation.AnimationListener;
Michael Jurkaaf442092010-06-10 17:01:57 -070076import android.view.animation.AnimationUtils;
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 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700132 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato7c312c12009-08-13 21:36:53 -0700134 static final int DIALOG_CREATE_SHORTCUT = 1;
135 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Romain Guy98d01652009-06-30 16:21:04 -0700137 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 // Type: int
140 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
141 // Type: boolean
142 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
143 // Type: long
144 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
159 // Type: int[]
160 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
165
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800169 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800171 private final BroadcastReceiver mCloseSystemDialogsReceiver
172 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800173 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private LayoutInflater mInflater;
176
Joe Onorato00acb122009-08-04 16:04:30 -0400177 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private AppWidgetManager mAppWidgetManager;
181 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private CellLayout.CellInfo mAddItemCellInfo;
184 private CellLayout.CellInfo mMenuAddInfo;
185 private final int[] mCellCoordinates = new int[2];
186 private FolderInfo mFolderInfo;
187
Joe Onorato7c312c12009-08-13 21:36:53 -0700188 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700190 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700191 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
203 private Bundle mSavedInstanceState;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207
Romain Guy84f296c2009-11-04 15:00:44 -0800208 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
209 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Romain Guy1fbc1c82009-11-09 20:43:08 -0800211 private ImageView mPreviousView;
212 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500213
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400214 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400215 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400216 private String[] mHotseatConfig = null;
217 private Intent[] mHotseats = null;
218 private Drawable[] mHotseatIcons = null;
219 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400220
Winson Chungaafa03c2010-06-11 17:34:16 -0700221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 @Override
223 protected void onCreate(Bundle savedInstanceState) {
224 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700225
Winson Chungaafa03c2010-06-11 17:34:16 -0700226 if (LauncherApplication.isInPlaceRotationEnabled()) {
227 // hide the status bar (temporary until we get the status bar design figured out)
228 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
229 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
230 }
231
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 LauncherApplication app = ((LauncherApplication)getApplication());
233 mModel = app.setLauncher(this);
234 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400235 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700237
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
240 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 if (PROFILE_STARTUP) {
243 android.os.Debug.startMethodTracing("/sdcard/launcher");
244 }
245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 checkForLocaleChange();
248 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700250 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
251 if (mHomeCustomizationDrawer != null) {
252 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700253
Michael Jurka946ad472010-07-09 18:05:18 -0700254 String widgetsLabel = getString(R.string.widgets_tab_label);
255 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
256 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
257 String foldersLabel = getString(R.string.folders_tab_label);
258 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
259 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
260 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
261 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
262 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
263 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
264 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
265 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
266
267 mHomeCustomizationDrawer.setCurrentTab(0);
268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 setupViews();
270
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800271 registerContentObservers();
272
Joe Onorato7c312c12009-08-13 21:36:53 -0700273 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 mSavedState = savedInstanceState;
276 restoreState(mSavedState);
277
278 if (PROFILE_STARTUP) {
279 android.os.Debug.stopMethodTracing();
280 }
281
282 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 }
285
286 // For handling default keys
287 mDefaultKeySsb = new SpannableStringBuilder();
288 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289
290 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
291 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
Romain Guycbb89e42009-06-08 15:52:54 -0700293
Winson Chungaafa03c2010-06-11 17:34:16 -0700294 @Override
295 public void onConfigurationChanged(Configuration newConfig) {
296 // TODO Auto-generated method stub
297 super.onConfigurationChanged(newConfig);
298
299 if (LauncherApplication.isInPlaceRotationEnabled()) {
300 mModel.updateOrientation();
301 mWorkspace.refreshWorkspaceChildren();
302 mWorkspace.rotateCurrentScreensChildren();
303 }
304 }
305
306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700308 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
309 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700310
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 final Configuration configuration = getResources().getConfiguration();
312
Romain Guy98d01652009-06-30 16:21:04 -0700313 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 final String locale = configuration.locale.toString();
315
Romain Guy98d01652009-06-30 16:21:04 -0700316 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 final int mcc = configuration.mcc;
318
Romain Guy98d01652009-06-30 16:21:04 -0700319 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 final int mnc = configuration.mnc;
321
Romain Guy84f296c2009-11-04 15:00:44 -0800322 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323
Romain Guy84f296c2009-11-04 15:00:44 -0800324 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700325 localeConfiguration.locale = locale;
326 localeConfiguration.mcc = mcc;
327 localeConfiguration.mnc = mnc;
328
329 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800330 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400331
332 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700333 }
334 }
335
336 private static class LocaleConfiguration {
337 public String locale;
338 public int mcc = -1;
339 public int mnc = -1;
340 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700341
Romain Guy98d01652009-06-30 16:21:04 -0700342 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
343 DataInputStream in = null;
344 try {
345 in = new DataInputStream(context.openFileInput(PREFERENCES));
346 configuration.locale = in.readUTF();
347 configuration.mcc = in.readInt();
348 configuration.mnc = in.readInt();
349 } catch (FileNotFoundException e) {
350 // Ignore
351 } catch (IOException e) {
352 // Ignore
353 } finally {
354 if (in != null) {
355 try {
356 in.close();
357 } catch (IOException e) {
358 // Ignore
359 }
360 }
361 }
362 }
363
364 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
365 DataOutputStream out = null;
366 try {
367 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
368 out.writeUTF(configuration.locale);
369 out.writeInt(configuration.mcc);
370 out.writeInt(configuration.mnc);
371 out.flush();
372 } catch (FileNotFoundException e) {
373 // Ignore
374 } catch (IOException e) {
375 //noinspection ResultOfMethodCallIgnored
376 context.getFileStreamPath(PREFERENCES).delete();
377 } finally {
378 if (out != null) {
379 try {
380 out.close();
381 } catch (IOException e) {
382 // Ignore
383 }
384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386 }
387
388 static int getScreen() {
389 synchronized (sLock) {
390 return sScreen;
391 }
392 }
393
394 static void setScreen(int screen) {
395 synchronized (sLock) {
396 sScreen = screen;
397 }
398 }
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700401 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402
403 Display display = getWindowManager().getDefaultDisplay();
404 boolean isPortrait = display.getWidth() < display.getHeight();
405
406 final int width = isPortrait ? display.getWidth() : display.getHeight();
407 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700408 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400411 // Note: This doesn't do all the client-id magic that BrowserProvider does
412 // in Browser. (http://b/2425179)
413 private Uri getDefaultBrowserUri() {
414 String url = getString(R.string.default_browser_url);
415 if (url.indexOf("{CID}") != -1) {
416 url = url.replace("{CID}", "android-google");
417 }
418 return Uri.parse(url);
419 }
420
421 // Load the Intent templates from arrays.xml to populate the hotseats. For
422 // each Intent, if it resolves to a single app, use that as the launch
423 // intent & use that app's label as the contentDescription. Otherwise,
424 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400425 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400426 if (mHotseatConfig == null) {
427 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
428 if (mHotseatConfig.length > 0) {
429 mHotseats = new Intent[mHotseatConfig.length];
430 mHotseatLabels = new CharSequence[mHotseatConfig.length];
431 mHotseatIcons = new Drawable[mHotseatConfig.length];
432 } else {
433 mHotseats = null;
434 mHotseatIcons = null;
435 mHotseatLabels = null;
436 }
437
438 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
439 for (int i=0; i<mHotseatConfig.length; i++) {
440 // load icon for this slot; currently unrelated to the actual activity
441 try {
442 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
443 } catch (ArrayIndexOutOfBoundsException ex) {
444 Log.w(TAG, "Missing hotseat_icons array item #" + i);
445 mHotseatIcons[i] = null;
446 }
447 }
448 hotseatIconDrawables.recycle();
449 }
450
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400451 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400452 for (int i=0; i<mHotseatConfig.length; i++) {
453 Intent intent = null;
454 if (mHotseatConfig[i].equals("*BROWSER*")) {
455 // magic value meaning "launch user's default web browser"
456 // replace it with a generic web request so we can see if there is indeed a default
457 String defaultUri = getString(R.string.default_browser_url);
458 intent = new Intent(
459 Intent.ACTION_VIEW,
460 ((defaultUri != null)
461 ? Uri.parse(defaultUri)
462 : getDefaultBrowserUri())
463 ).addCategory(Intent.CATEGORY_BROWSABLE);
464 // note: if the user launches this without a default set, she
465 // will always be taken to the default URL above; this is
466 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700467 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400468 // URL is unavoidably sent to the chosen app.
469 } else {
470 try {
471 intent = Intent.parseUri(mHotseatConfig[i], 0);
472 } catch (java.net.URISyntaxException ex) {
473 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
474 // bogus; leave intent=null
475 }
476 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700477
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400478 if (intent == null) {
479 mHotseats[i] = null;
480 mHotseatLabels[i] = getText(R.string.activity_not_found);
481 continue;
482 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400483
484 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700485 Log.d(TAG, "loadHotseats: hotseat " + i
486 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400487 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400488 + "]");
489 }
490
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
492 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700493 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400494 Log.d(TAG, "Best match for intent: " + bestMatch);
495 Log.d(TAG, "All matches: ");
496 for (ResolveInfo ri : allMatches) {
497 Log.d(TAG, " --> " + ri);
498 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400499 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400500 // did this resolve to a single app, or the resolver?
501 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700502 // can't find any activity to handle this. let's leave the
503 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400504 // to launch.
505 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400506
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400507 // set accessibility text to "Not installed"
508 mHotseatLabels[i] = getText(R.string.activity_not_found);
509 } else {
510 boolean found = false;
511 for (ResolveInfo ri : allMatches) {
512 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
513 && bestMatch.activityInfo.applicationInfo.packageName
514 .equals(ri.activityInfo.applicationInfo.packageName)) {
515 found = true;
516 break;
517 }
518 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700519
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400520 if (!found) {
521 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
522 // the bestMatch is probably the ResolveActivity, meaning the
523 // user has not yet selected a default
524 // so: we'll keep the original intent for now
525 mHotseats[i] = intent;
526
527 // set the accessibility text to "Select shortcut"
528 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
529 } else {
530 // we have an app!
531 // now reconstruct the intent to launch it through the front
532 // door
533 ComponentName com = new ComponentName(
534 bestMatch.activityInfo.applicationInfo.packageName,
535 bestMatch.activityInfo.name);
536 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
537
538 // load the app label for accessibility
539 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
540 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400541 }
542
543 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700544 Log.d(TAG, "loadHotseats: hotseat " + i
545 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 + ((mHotseats[i] == null)
547 ? "null"
548 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400549 + "] label=[" + mHotseatLabels[i]
550 + "]"
551 );
552 }
553 }
554 }
555
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 @Override
557 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700558 mWaitingForResult = false;
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 // The pattern used here is that a user PICKs a specific application,
561 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
564 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700565
Romain Guy94dabf12009-07-21 10:55:43 -0700566 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 switch (requestCode) {
568 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 break;
571 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800572 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 break;
574 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400575 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 break;
577 case REQUEST_PICK_LIVE_FOLDER:
578 addLiveFolder(data);
579 break;
580 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400581 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700584 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700586 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700587 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700588 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700590 case REQUEST_PICK_WALLPAPER:
591 // We just wanted the activity result here so we can clear mWaitingForResult
592 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
Romain Guy18042c82009-11-06 11:44:55 -0800594 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
595 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
596 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700597 // Clean up the appWidgetId if we canceled
598 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
599 if (appWidgetId != -1) {
600 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 }
602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
605 @Override
606 protected void onResume() {
607 super.onResume();
608
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800609 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 mWorkspaceLoading = true;
613 mModel.startLoader(this, true);
614 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616 }
617
618 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700619 protected void onPause() {
620 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700621 // Some launcher layouts don't have a previous and next view
622 if (mPreviousView != null) {
623 dismissPreview(mPreviousView);
624 dismissPreview(mNextView);
625 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800626 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700627 }
Romain Guycbb89e42009-06-08 15:52:54 -0700628
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700629 @Override
630 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 // Flag the loader to stop early before switching
632 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800633 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800634 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700635 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700636
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800637 // We can't hide the IME if it was forced open. So don't bother
638 /*
639 @Override
640 public void onWindowFocusChanged(boolean hasFocus) {
641 super.onWindowFocusChanged(hasFocus);
642
643 if (hasFocus) {
644 final InputMethodManager inputManager = (InputMethodManager)
645 getSystemService(Context.INPUT_METHOD_SERVICE);
646 WindowManager.LayoutParams lp = getWindow().getAttributes();
647 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
648 android.os.Handler()) {
649 protected void onReceiveResult(int resultCode, Bundle resultData) {
650 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
651 }
652 });
653 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
654 }
655 }
656 */
657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 private boolean acceptFilter() {
659 final InputMethodManager inputManager = (InputMethodManager)
660 getSystemService(Context.INPUT_METHOD_SERVICE);
661 return !inputManager.isFullscreenMode();
662 }
663
664 @Override
665 public boolean onKeyDown(int keyCode, KeyEvent event) {
666 boolean handled = super.onKeyDown(keyCode, event);
667 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
668 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
669 keyCode, event);
670 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700671 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700672 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700673 // showSearchDialog()
674 // If there are multiple keystrokes before the search dialog takes focus,
675 // onSearchRequested() will be called for every keystroke,
676 // but it is idempotent, so it's fine.
677 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
679 }
680
Joe Onorato8a9625e2010-01-28 15:55:35 -0800681 // Eat the long press event so the keyboard doesn't come up.
682 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
683 return true;
684 }
685
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 return handled;
687 }
688
Karl Rosaen138a0412009-04-23 19:00:21 -0700689 private String getTypedText() {
690 return mDefaultKeySsb.toString();
691 }
692
693 private void clearTypedText() {
694 mDefaultKeySsb.clear();
695 mDefaultKeySsb.clearSpans();
696 Selection.setSelection(mDefaultKeySsb, 0);
697 }
698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 /**
700 * Restores the previous state, if it exists.
701 *
702 * @param savedState The previous state.
703 */
704 private void restoreState(Bundle savedState) {
705 if (savedState == null) {
706 return;
707 }
708
Joe Onorato3a8820b2009-11-10 15:06:42 -0800709 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
710 if (allApps) {
711 showAllApps(false);
712 }
713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
715 if (currentScreen > -1) {
716 mWorkspace.setCurrentScreen(currentScreen);
717 }
718
719 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
720 if (addScreen > -1) {
721 mAddItemCellInfo = new CellLayout.CellInfo();
722 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
723 addItemCellInfo.valid = true;
724 addItemCellInfo.screen = addScreen;
725 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
726 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
727 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
728 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
729 addItemCellInfo.findVacantCellsFromOccupied(
730 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
731 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
732 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
733 mRestoring = true;
734 }
735
736 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
737 if (renameFolder) {
738 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 mRestoring = true;
741 }
742 }
743
744 /**
745 * Finds all the views we need and configure them properly.
746 */
747 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400748 DragController dragController = mDragController;
749
Romain Guyb1b69f52009-08-10 15:10:15 -0700750 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400751 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752
Joe Onorato7c312c12009-08-13 21:36:53 -0700753 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700754 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700755 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800756 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700757 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700758 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
761 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500762 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700763
764 // We only intercept touch events to dismiss the home customization drawer; if it doesn't
765 // exist, then no need to do this
766 if (mHomeCustomizationDrawer != null)
767 workspace.setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768
Joe Onorato7c312c12009-08-13 21:36:53 -0700769 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
770 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771
Joe Onorato7c312c12009-08-13 21:36:53 -0700772 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700774 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800775 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800776
Michael Jurka0e260592010-06-30 17:07:39 -0700777 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
778 if (widgetChooser != null) {
779 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
780 widgetChooser.setAdapter(widgetGalleryAdapter);
781 widgetChooser.setDragController(dragController);
782 widgetChooser.setLauncher(this);
783
784 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
785 IntentListAdapter folderTypes = new FolderListAdapter(this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
786 folderChooser.setAdapter(folderTypes);
787 folderChooser.setLauncher(this);
788
789 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
790 IntentListAdapter shortcutTypes = new ShortcutListAdapter(this, Intent.ACTION_CREATE_SHORTCUT);
791 shortcutChooser.setAdapter(shortcutTypes);
792 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700793 } else {
794 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
795 hotseatLeft.setContentDescription(mHotseatLabels[0]);
796 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
797 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
798 hotseatRight.setContentDescription(mHotseatLabels[1]);
799 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400800
Michael Jurkaaf442092010-06-10 17:01:57 -0700801 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
802 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800803
Michael Jurkaaf442092010-06-10 17:01:57 -0700804 Drawable previous = mPreviousView.getDrawable();
805 Drawable next = mNextView.getDrawable();
806 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
Michael Jurkaaf442092010-06-10 17:01:57 -0700808 mPreviousView.setHapticFeedbackEnabled(false);
809 mPreviousView.setOnLongClickListener(this);
810 mNextView.setHapticFeedbackEnabled(false);
811 mNextView.setOnLongClickListener(this);
812 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400815 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
818 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400819 deleteZone.setDragController(dragController);
Winson Chungaafa03c2010-06-11 17:34:16 -0700820 int deleteZoneHandleId = LauncherApplication.isScreenXLarge() ? R.id.add_button : R.id.all_apps_button_cluster;
Michael Jurkaaf442092010-06-10 17:01:57 -0700821 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822
Joe Onorato00acb122009-08-04 16:04:30 -0400823 dragController.setDragScoller(workspace);
824 dragController.setDragListener(deleteZone);
825 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800826 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700827
Joe Onorato00acb122009-08-04 16:04:30 -0400828 // The order here is bottom to top.
829 dragController.addDropTarget(workspace);
830 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832
Romain Guy8a73c512009-11-09 19:19:59 -0800833 @SuppressWarnings({"UnusedDeclaration"})
834 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800835 if (!isAllAppsVisible()) {
836 mWorkspace.scrollLeft();
837 }
Romain Guy8a73c512009-11-09 19:19:59 -0800838 }
839
840 @SuppressWarnings({"UnusedDeclaration"})
841 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800842 if (!isAllAppsVisible()) {
843 mWorkspace.scrollRight();
844 }
Romain Guy8a73c512009-11-09 19:19:59 -0800845 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400846
847 @SuppressWarnings({"UnusedDeclaration"})
848 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400849 if (isAllAppsVisible()) return;
850
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400851 int index = -1;
852 if (v.getId() == R.id.hotseat_left) {
853 index = 0;
854 } else if (v.getId() == R.id.hotseat_right) {
855 index = 1;
856 }
857
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400858 // reload these every tap; you never know when they might change
859 loadHotseats();
860 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
861 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400862 startActivitySafely(
863 mHotseats[index],
864 "hotseat"
865 );
866 }
867 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 /**
870 * Creates a view representing a shortcut.
871 *
872 * @param info The data structure describing the shortcut.
873 *
874 * @return A View inflated from R.layout.application.
875 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800876 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 return createShortcut(R.layout.application,
878 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
879 }
880
881 /**
882 * Creates a view representing a shortcut inflated from the specified resource.
883 *
884 * @param layoutResId The id of the XML layout used to create the shortcut.
885 * @param parent The group the shortcut belongs to.
886 * @param info The data structure describing the shortcut.
887 *
888 * @return A View inflated from layoutResId.
889 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800890 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
892
Joe Onorato0589f0f2010-02-08 13:44:00 -0800893 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
894 new FastBitmapDrawable(info.getIcon(mIconCache)),
895 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 favorite.setText(info.title);
897 favorite.setTag(info);
898 favorite.setOnClickListener(this);
899
900 return favorite;
901 }
902
903 /**
904 * Add an application shortcut to the workspace.
905 *
906 * @param data The intent describing the application.
907 * @param cellInfo The position on screen where to create the shortcut.
908 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 cellInfo.screen = mWorkspace.getCurrentScreen();
911 if (!findSingleSlot(cellInfo)) return;
912
Joe Onorato0589f0f2010-02-08 13:44:00 -0800913 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
914 data, context);
915
Romain Guy73b979d2009-06-09 12:57:21 -0700916 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800917 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800919 info.container = ItemInfo.NO_ID;
920 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
921 } else {
922 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924 }
Romain Guycbb89e42009-06-08 15:52:54 -0700925
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 /**
927 * Add a shortcut to the workspace.
928 *
929 * @param data The intent describing the shortcut.
930 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 cellInfo.screen = mWorkspace.getCurrentScreen();
934 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700935
Joe Onorato0589f0f2010-02-08 13:44:00 -0800936 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937
938 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
941 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
944
Romain Guycbb89e42009-06-08 15:52:54 -0700945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700947 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700949 * @param data The intent describing the appWidgetId.
950 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700952 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700953 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700954
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700955 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700957 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 // Try finding open space on Launcher screen
960 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800961 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
962 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
963 return;
964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700966 // Build Launcher-specific widget info and save to database
967 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 launcherInfo.spanX = spans[0];
969 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 LauncherModel.addItemToDatabase(this, launcherInfo,
972 LauncherSettings.Favorites.CONTAINER_DESKTOP,
973 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
974
975 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700979 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700980
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700981 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
987 }
Romain Guycbb89e42009-06-08 15:52:54 -0700988
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
990 mDesktopItems.remove(launcherInfo);
991 launcherInfo.hostView = null;
992 }
993
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700994 public LauncherAppWidgetHost getAppWidgetHost() {
995 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
Romain Guycbb89e42009-06-08 15:52:54 -0700997
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800998 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800999 getWindow().closeAllPanels();
1000
1001 try {
1002 dismissDialog(DIALOG_CREATE_SHORTCUT);
1003 // Unlock the workspace if the dialog was showing
1004 } catch (Exception e) {
1005 // An exception is thrown if the dialog is not visible, which is fine
1006 }
1007
1008 try {
1009 dismissDialog(DIALOG_RENAME_FOLDER);
1010 // Unlock the workspace if the dialog was showing
1011 } catch (Exception e) {
1012 // An exception is thrown if the dialog is not visible, which is fine
1013 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001014
1015 // Whatever we were doing is hereby canceled.
1016 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001017 }
1018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 @Override
1020 protected void onNewIntent(Intent intent) {
1021 super.onNewIntent(intent);
1022
1023 // Close the menu
1024 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001025 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001026 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001027
Joe Onorato14f122b2009-11-19 14:06:36 -08001028 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1029 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1030 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001031
1032 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001033 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001034 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001035 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001036 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001037 hideCustomizationDrawer();
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
Romain Guy1fbc1c82009-11-09 20:43:08 -08001129 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001130 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001131
1132 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134
1135 @Override
1136 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001137 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 super.startActivityForResult(intent, requestCode);
1139 }
1140
1141 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001142 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001144
Joe Onorato7bb17492009-09-24 17:51:01 -07001145 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Karl Rosaen138a0412009-04-23 19:00:21 -07001147 if (initialQuery == null) {
1148 // Use any text typed in the launcher as the initial query
1149 initialQuery = getTypedText();
1150 clearTypedText();
1151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 if (appSearchData == null) {
1153 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001154 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Karl Rosaen138a0412009-04-23 19:00:21 -07001157 final SearchManager searchManager =
1158 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001159 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001160 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 }
1162
1163 @Override
1164 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 if (isWorkspaceLocked()) {
1166 return false;
1167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168
1169 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1172 .setIcon(android.R.drawable.ic_menu_add)
1173 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001174 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 .setIcon(android.R.drawable.ic_menu_gallery)
1176 .setAlphabeticShortcut('W');
1177 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1178 .setIcon(android.R.drawable.ic_search_category_default)
1179 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1180 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1181 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1182 .setAlphabeticShortcut('N');
1183
1184 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001185 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1186 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
1188 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1189 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1190 .setIntent(settings);
1191
1192 return true;
1193 }
1194
1195 @Override
1196 public boolean onPrepareOptionsMenu(Menu menu) {
1197 super.onPrepareOptionsMenu(menu);
1198
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001199 // If all apps is animating, don't show the menu, because we don't know
1200 // which one to show.
1201 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1202 return false;
1203 }
1204
1205 // Only show the add and wallpaper options when we're not in all apps.
1206 boolean visible = !mAllAppsGrid.isOpaque();
1207 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1208 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1209
1210 // Disable add if the workspace is full.
1211 if (visible) {
1212 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1213 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215
1216 return true;
1217 }
1218
Michael Jurka0e260592010-06-30 17:07:39 -07001219 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1220 // using the settings menu to add an item, something similar happens in showAddDialog
1221 public void prepareAddItemFromHomeCustomizationDrawer() {
1222 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1223 mAddItemCellInfo = mMenuAddInfo;
1224 }
1225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 @Override
1227 public boolean onOptionsItemSelected(MenuItem item) {
1228 switch (item.getItemId()) {
1229 case MENU_ADD:
1230 addItems();
1231 return true;
1232 case MENU_WALLPAPER_SETTINGS:
1233 startWallpaper();
1234 return true;
1235 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001236 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 return true;
1238 case MENU_NOTIFICATIONS:
1239 showNotifications();
1240 return true;
1241 }
1242
1243 return super.onOptionsItemSelected(item);
1244 }
Romain Guycbb89e42009-06-08 15:52:54 -07001245
Karl Rosaen138a0412009-04-23 19:00:21 -07001246 /**
1247 * Indicates that we want global search for this activity by setting the globalSearch
1248 * argument for {@link #startSearch} to true.
1249 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001251 @Override
1252 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001253 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001255 }
1256
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 public boolean isWorkspaceLocked() {
1258 return mWorkspaceLoading || mWaitingForResult;
1259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001262 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001263 if (LauncherApplication.isScreenXLarge()) {
1264 // Animate the widget chooser up from the bottom of the screen
1265 if (!isCustomizationDrawerVisible()) {
1266 showCustomizationDrawer();
1267 }
1268 } else {
1269 showAddDialog(mMenuAddInfo);
1270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
Michael Jurkaaf442092010-06-10 17:01:57 -07001273 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1274 mAddItemCellInfo = cellInfo;
1275 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1276 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1277 addAppWidgetImpl(appWidgetId);
1278 }
1279
1280 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001281 // TODO: catch bad widget exception when sent
1282 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001283 // TODO: Is this log message meaningful?
1284 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1285 addAppWidgetImpl(appWidgetId);
1286 }
1287
1288 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001289 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
Bjorn Bringert7984c942009-12-09 15:38:25 +00001291 if (appWidget.configure != null) {
1292 // Launch over to configure widget, if needed
1293 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1294 intent.setComponent(appWidget.configure);
1295 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1296
Romain Guy8e633c52010-03-04 12:51:36 -08001297 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001299 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001300 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302 }
Romain Guycbb89e42009-06-08 15:52:54 -07001303
Joe Onoratodeb98af2010-02-19 14:59:39 -08001304 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001305 // Handle case where user selected "Applications"
1306 String applicationName = getResources().getString(R.string.group_applications);
1307 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001308
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001309 if (applicationName != null && applicationName.equals(shortcutName)) {
1310 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1311 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001312
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001313 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1314 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001315 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001316 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001317 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001322 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001323 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001324 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001326 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001328 } else {
1329 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 UserFolderInfo folderInfo = new UserFolderInfo();
1335 folderInfo.title = getText(R.string.folder_name);
1336
1337 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1338 cellInfo.screen = mWorkspace.getCurrentScreen();
1339 if (!findSingleSlot(cellInfo)) return;
1340
1341 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001342 LauncherModel.addItemToDatabase(this, folderInfo,
1343 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001345 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346
1347 // Create the view
1348 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1349 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1350 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
Romain Guycbb89e42009-06-08 15:52:54 -07001353
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 void removeFolder(FolderInfo folder) {
1355 mFolders.remove(folder.id);
1356 }
1357
1358 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 cellInfo.screen = mWorkspace.getCurrentScreen();
1360 if (!findSingleSlot(cellInfo)) return;
1361
1362 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1363
1364 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1366 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1368 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
1370 }
1371
1372 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1373 CellLayout.CellInfo cellInfo, boolean notify) {
1374
1375 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1376 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1377
1378 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 Intent.ShortcutIconResource iconResource = null;
1380
1381 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1382 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1383 try {
1384 iconResource = (Intent.ShortcutIconResource) extra;
1385 final PackageManager packageManager = context.getPackageManager();
1386 Resources resources = packageManager.getResourcesForApplication(
1387 iconResource.packageName);
1388 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1389 icon = resources.getDrawable(id);
1390 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001391 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393 }
1394
1395 if (icon == null) {
1396 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1397 }
1398
1399 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001400 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 info.title = name;
1402 info.iconResource = iconResource;
1403 info.uri = data.getData();
1404 info.baseIntent = baseIntent;
1405 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1406 LiveFolders.DISPLAY_MODE_GRID);
1407
1408 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1409 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411
1412 return info;
1413 }
1414
1415 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1416 final int[] xy = new int[2];
1417 if (findSlot(cellInfo, xy, 1, 1)) {
1418 cellInfo.cellX = xy[0];
1419 cellInfo.cellY = xy[1];
1420 return true;
1421 }
1422 return false;
1423 }
1424
1425 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1426 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1427 boolean[] occupied = mSavedState != null ?
1428 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1429 cellInfo = mWorkspace.findAllVacantCells(occupied);
1430 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1431 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1432 return false;
1433 }
1434 }
1435 return true;
1436 }
1437
1438 private void showNotifications() {
1439 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1440 if (statusBar != null) {
1441 statusBar.expand();
1442 }
1443 }
1444
1445 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001446 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001448 Intent chooser = Intent.createChooser(pickWallpaper,
1449 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001450 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1451 // Removed in Eclair MR1
1452// WallpaperManager wm = (WallpaperManager)
1453// getSystemService(Context.WALLPAPER_SERVICE);
1454// WallpaperInfo wi = wm.getWallpaperInfo();
1455// if (wi != null && wi.getSettingsActivity() != null) {
1456// LabeledIntent li = new LabeledIntent(getPackageName(),
1457// R.string.configure_wallpaper, 0);
1458// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1459// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1460// }
Mike Clerona0618e42009-10-22 13:55:21 -07001461 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001464 /**
1465 * Registers various content observers. The current implementation registers
1466 * only a favorites observer to keep track of the favorites applications.
1467 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001468 private void registerContentObservers() {
1469 ContentResolver resolver = getContentResolver();
1470 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1471 true, mWidgetObserver);
1472 }
1473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 @Override
1475 public boolean dispatchKeyEvent(KeyEvent event) {
1476 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1477 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001479 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001480 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001481 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001482 dumpState();
1483 return true;
1484 }
1485 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001486 }
1487 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1488 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001489 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 return true;
1491 }
1492 }
1493
1494 return super.dispatchKeyEvent(event);
1495 }
1496
Joe Onorato88ec0992009-11-19 13:16:06 -08001497 @Override
1498 public void onBackPressed() {
1499 if (isAllAppsVisible()) {
1500 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001501 } else if (isCustomizationDrawerVisible()) {
1502 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001503 } else {
1504 closeFolder();
1505 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001506 // Some launcher layouts don't have a previous and next view
1507 if (mPreviousView != null) {
1508 dismissPreview(mPreviousView);
1509 dismissPreview(mNextView);
1510 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001511 }
1512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 private void closeFolder() {
1514 Folder folder = mWorkspace.getOpenFolder();
1515 if (folder != null) {
1516 closeFolder(folder);
1517 }
1518 }
1519
1520 void closeFolder(Folder folder) {
1521 folder.getInfo().opened = false;
1522 ViewGroup parent = (ViewGroup) folder.getParent();
1523 if (parent != null) {
1524 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001525 if (folder instanceof DropTarget) {
1526 // Live folders aren't DropTargets.
1527 mDragController.removeDropTarget((DropTarget)folder);
1528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530 folder.onClose();
1531 }
1532
1533 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001534 * Re-listen when widgets are reset.
1535 */
1536 private void onAppWidgetReset() {
1537 mAppWidgetHost.startListening();
1538 }
1539
1540 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1542 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001544 private void unbindDesktopItems() {
1545 for (ItemInfo item: mDesktopItems) {
1546 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 /**
1551 * Launches the intent referred by the clicked shortcut.
1552 *
1553 * @param v The view representing the clicked shortcut.
1554 */
1555 public void onClick(View v) {
1556 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001557 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001559 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001560 int[] pos = new int[2];
1561 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001562 intent.setSourceBounds(new Rect(pos[0], pos[1],
1563 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001564 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 } else if (tag instanceof FolderInfo) {
1566 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001567 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001568 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001569 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001570 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001571 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
1575
Michael Jurka0e260592010-06-30 17:07:39 -07001576 public boolean onTouch(View v, MotionEvent event) {
1577 // this is being forwarded from mWorkspace;
1578 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001579 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001580 return false;
1581 }
1582
Michael Jurkaaf442092010-06-10 17:01:57 -07001583 /**
1584 * Event handler for the "plus" button that appears on the home screen, which
1585 * enters home screen customization mode.
1586 *
1587 * @param v The view that was clicked.
1588 */
1589 public void onClickAddButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001590 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001591 }
1592
Joe Onoratof984e852010-03-25 09:47:45 -07001593 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1595 try {
1596 startActivity(intent);
1597 } catch (ActivityNotFoundException e) {
1598 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001599 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 } catch (SecurityException e) {
1601 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001602 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001604 "or use the exported attribute for this activity. "
1605 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
1607 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001608
Romain Guy8e633c52010-03-04 12:51:36 -08001609 void startActivityForResultSafely(Intent intent, int requestCode) {
1610 try {
1611 startActivityForResult(intent, requestCode);
1612 } catch (ActivityNotFoundException e) {
1613 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1614 } catch (SecurityException e) {
1615 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1616 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1617 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1618 "or use the exported attribute for this activity.", e);
1619 }
1620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621
1622 private void handleFolderClick(FolderInfo folderInfo) {
1623 if (!folderInfo.opened) {
1624 // Close any open folder
1625 closeFolder();
1626 // Open the requested folder
1627 openFolder(folderInfo);
1628 } else {
1629 // Find the open folder...
1630 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1631 int folderScreen;
1632 if (openFolder != null) {
1633 folderScreen = mWorkspace.getScreenForView(openFolder);
1634 // .. and close it
1635 closeFolder(openFolder);
1636 if (folderScreen != mWorkspace.getCurrentScreen()) {
1637 // Close any folder open on the current screen
1638 closeFolder();
1639 // Pull the folder onto this screen
1640 openFolder(folderInfo);
1641 }
1642 }
1643 }
1644 }
1645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 /**
1647 * Opens the user fodler described by the specified tag. The opening of the folder
1648 * is animated relative to the specified View. If the View is null, no animation
1649 * is played.
1650 *
1651 * @param folderInfo The FolderInfo describing the folder to open.
1652 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001653 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 Folder openFolder;
1655
1656 if (folderInfo instanceof UserFolderInfo) {
1657 openFolder = UserFolder.fromXml(this);
1658 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001659 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 } else {
1661 return;
1662 }
1663
Joe Onorato00acb122009-08-04 16:04:30 -04001664 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 openFolder.setLauncher(this);
1666
1667 openFolder.bind(folderInfo);
1668 folderInfo.opened = true;
1669
Winson Chungaafa03c2010-06-11 17:34:16 -07001670 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 openFolder.onOpen();
1673 }
1674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001676 switch (v.getId()) {
1677 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001678 if (!isAllAppsVisible()) {
1679 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1680 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001681 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001682 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001683 return true;
1684 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001685 if (!isAllAppsVisible()) {
1686 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1687 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001688 showPreviews(v);
1689 }
1690 return true;
1691 case R.id.all_apps_button:
1692 if (!isAllAppsVisible()) {
1693 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1694 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1695 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001696 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001697 return true;
1698 }
1699
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 return false;
1702 }
1703
1704 if (!(v instanceof CellLayout)) {
1705 v = (View) v.getParent();
1706 }
1707
1708 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1709
1710 // This happens when long clicking an item with the dpad/trackball
1711 if (cellInfo == null) {
1712 return true;
1713 }
1714
1715 if (mWorkspace.allowLongPress()) {
1716 if (cellInfo.cell == null) {
1717 if (cellInfo.valid) {
1718 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001719 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001720 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1721 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 showAddDialog(cellInfo);
1723 }
1724 } else {
1725 if (!(cellInfo.cell instanceof Folder)) {
1726 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001727 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1728 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 mWorkspace.startDrag(cellInfo);
1730 }
1731 }
1732 }
1733 return true;
1734 }
1735
Romain Guye6b8e2f2009-11-10 11:56:55 -08001736 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001737 private void dismissPreview(final View v) {
1738 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001739 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001740 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1741 public void onDismiss() {
1742 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1743 int count = group.getChildCount();
1744 for (int i = 0; i < count; i++) {
1745 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1746 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001747 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1748 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1749
1750 v.setTag(R.id.workspace, null);
1751 v.setTag(R.id.icon, null);
1752 window.setOnDismissListener(null);
1753 }
1754 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001755 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001756 }
1757 v.setTag(null);
1758 }
1759
Romain Guyf8e6a802009-12-07 17:48:02 -08001760 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001761 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001762 }
1763
Romain Guya6abce82009-11-10 02:54:41 -08001764 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001765 final Resources resources = getResources();
1766 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001767
Romain Guya6abce82009-11-10 02:54:41 -08001768 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001769
Romain Guy9d31e9f2009-11-11 18:54:28 -08001770 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001771
Romain Guyf8e6a802009-12-07 17:48:02 -08001772 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001773 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001774 int extraW = (int) ((r.left + r.right) * max);
1775 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001776
1777 int aW = cell.getWidth() - extraW;
1778 float w = aW / max;
1779
1780 int width = cell.getWidth();
1781 int height = cell.getHeight();
1782 int x = cell.getLeftPadding();
1783 int y = cell.getTopPadding();
1784 width -= (x + cell.getRightPadding());
1785 height -= (y + cell.getBottomPadding());
1786
1787 float scale = w / width;
1788
1789 int count = end - start;
1790
1791 final float sWidth = width * scale;
1792 float sHeight = height * scale;
1793
Romain Guye6b8e2f2009-11-10 11:56:55 -08001794 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001795
Romain Guye6b8e2f2009-11-10 11:56:55 -08001796 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1797 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001798
1799 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001800 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001801 cell = (CellLayout) workspace.getChildAt(i);
1802
Romain Guyf8e6a802009-12-07 17:48:02 -08001803 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001804 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001805
1806 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001807 c.scale(scale, scale);
1808 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1809 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001810
Romain Guy9d31e9f2009-11-11 18:54:28 -08001811 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001812 image.setImageBitmap(bitmap);
1813 image.setTag(i);
1814 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001815 image.setOnFocusChangeListener(handler);
1816 image.setFocusable(true);
1817 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001818
1819 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001820 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1821
Winson Chungaafa03c2010-06-11 17:34:16 -07001822 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001823 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001824
1825 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001826 p.setContentView(preview);
1827 p.setWidth((int) (sWidth * count + extraW));
1828 p.setHeight((int) (sHeight + extraH));
1829 p.setAnimationStyle(R.style.AnimationPreview);
1830 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001831 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001832 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001833 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001834
Romain Guye6b8e2f2009-11-10 11:56:55 -08001835 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1836 public void onDismiss() {
1837 dismissPreview(anchor);
1838 }
1839 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001840
1841 anchor.setTag(p);
1842 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001843 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001844 }
1845
Romain Guy9d31e9f2009-11-11 18:54:28 -08001846 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001847 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001848
Romain Guye6b8e2f2009-11-10 11:56:55 -08001849 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001850 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001851 }
1852
1853 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001854 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001855 v.post(this);
1856 }
1857
1858 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001859 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001860 }
1861
1862 public void onFocusChange(View v, boolean hasFocus) {
1863 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001864 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001865 }
Romain Guya6abce82009-11-10 02:54:41 -08001866 }
1867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 Workspace getWorkspace() {
1870 return mWorkspace;
1871 }
1872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 @Override
1874 protected Dialog onCreateDialog(int id) {
1875 switch (id) {
1876 case DIALOG_CREATE_SHORTCUT:
1877 return new CreateShortcut().createDialog();
1878 case DIALOG_RENAME_FOLDER:
1879 return new RenameFolder().createDialog();
1880 }
1881
1882 return super.onCreateDialog(id);
1883 }
1884
1885 @Override
1886 protected void onPrepareDialog(int id, Dialog dialog) {
1887 switch (id) {
1888 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 break;
1890 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001891 if (mFolderInfo != null) {
1892 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1893 final CharSequence text = mFolderInfo.title;
1894 input.setText(text);
1895 input.setSelection(0, text.length());
1896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 break;
1898 }
1899 }
1900
1901 void showRenameDialog(FolderInfo info) {
1902 mFolderInfo = info;
1903 mWaitingForResult = true;
1904 showDialog(DIALOG_RENAME_FOLDER);
1905 }
1906
1907 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1908 mAddItemCellInfo = cellInfo;
1909 mWaitingForResult = true;
1910 showDialog(DIALOG_CREATE_SHORTCUT);
1911 }
1912
Joe Onoratodeb98af2010-02-19 14:59:39 -08001913 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001914 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001915 Bundle bundle = new Bundle();
1916
1917 ArrayList<String> shortcutNames = new ArrayList<String>();
1918 shortcutNames.add(getString(R.string.group_applications));
1919 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1920
1921 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1922 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1923 R.drawable.ic_launcher_application));
1924 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1925
1926 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1927 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001928 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001929 pickIntent.putExtras(bundle);
1930
Joe Onoratodeb98af2010-02-19 14:59:39 -08001931 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001932 }
1933
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 private class RenameFolder {
1935 private EditText mInput;
1936
1937 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1939 mInput = (EditText) layout.findViewById(R.id.folder_name);
1940
1941 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1942 builder.setIcon(0);
1943 builder.setTitle(getString(R.string.rename_folder_title));
1944 builder.setCancelable(true);
1945 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1946 public void onCancel(DialogInterface dialog) {
1947 cleanup();
1948 }
1949 });
1950 builder.setNegativeButton(getString(R.string.cancel_action),
1951 new Dialog.OnClickListener() {
1952 public void onClick(DialogInterface dialog, int which) {
1953 cleanup();
1954 }
1955 }
1956 );
1957 builder.setPositiveButton(getString(R.string.rename_action),
1958 new Dialog.OnClickListener() {
1959 public void onClick(DialogInterface dialog, int which) {
1960 changeFolderName();
1961 }
1962 }
1963 );
1964 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001965
1966 final AlertDialog dialog = builder.create();
1967 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1968 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001969 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001970 mInput.requestFocus();
1971 InputMethodManager inputManager = (InputMethodManager)
1972 getSystemService(Context.INPUT_METHOD_SERVICE);
1973 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001974 }
1975 });
1976
1977 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979
1980 private void changeFolderName() {
1981 final String name = mInput.getText().toString();
1982 if (!TextUtils.isEmpty(name)) {
1983 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 mFolderInfo.title = name;
1986 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1987
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001989 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 } else {
1992 final FolderIcon folderIcon = (FolderIcon)
1993 mWorkspace.getViewForTag(mFolderInfo);
1994 if (folderIcon != null) {
1995 folderIcon.setText(name);
1996 getWorkspace().requestLayout();
1997 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001998 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999 mWorkspaceLoading = true;
2000 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002 }
2003 }
2004 cleanup();
2005 }
2006
2007 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 dismissDialog(DIALOG_RENAME_FOLDER);
2009 mWaitingForResult = false;
2010 mFolderInfo = null;
2011 }
2012 }
2013
Daniel Sandler843e8602010-06-07 14:59:01 -04002014 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2015 public boolean isAllAppsVisible() {
2016 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002017 }
2018
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002019 // AllAppsView.Watcher
2020 public void zoomed(float zoom) {
2021 if (zoom == 1.0f) {
2022 mWorkspace.setVisibility(View.GONE);
2023 }
2024 }
2025
Joe Onorato3a8820b2009-11-10 15:06:42 -08002026 void showAllApps(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002027 hideCustomizationDrawer();
2028
2029 if (LauncherApplication.isScreenXLarge() && animated) {
2030 // Not really a zoom -- this just makes the view visible
2031 mAllAppsGrid.zoom(1.0f, false);
2032 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_in);
2033 ((View) mAllAppsGrid).startAnimation(anim);
2034 } else {
2035 mAllAppsGrid.zoom(1.0f, animated);
2036 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002037
Romain Guyc16fea72010-03-12 17:17:56 -08002038 ((View) mAllAppsGrid).setFocusable(true);
2039 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002040
Joe Onorato7c312c12009-08-13 21:36:53 -07002041 // TODO: fade these two too
2042 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002043 }
2044
Joe Onoratob2061212009-11-24 16:13:54 -05002045 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002046 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002047 * - Home from workspace
2048 * - from center screen
2049 * - from other screens
2050 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002051 * - from center screen
2052 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002053 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002054 * - from center screen
2055 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002056 * - Launch app from workspace and quit
2057 * - with back
2058 * - with home
2059 * - Launch app from all apps and quit
2060 * - with back
2061 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002062 * - Go to a screen that's not the default, then all
2063 * apps, and launch and app, and go back
2064 * - with back
2065 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002066 * - On workspace, long press power and go back
2067 * - with back
2068 * - with home
2069 * - On all apps, long press power and go back
2070 * - with back
2071 * - with home
2072 * - On workspace, power off
2073 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002074 * - Launch an app and turn off the screen while in that app
2075 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002076 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002077 * - From all apps
2078 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002079 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2080 * - From all apps
2081 * - From the center workspace
2082 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002083 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002084 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002085 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002086 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002087 if (LauncherApplication.isScreenXLarge() && animated) {
2088 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_out);
2089 anim.setAnimationListener(new AnimationListener() {
2090 public void onAnimationStart(Animation animation) {}
2091 public void onAnimationRepeat(Animation animation) {}
2092 public void onAnimationEnd(Animation animation) {
2093 mAllAppsGrid.zoom(0.0f, false);
2094 }
2095 });
2096 ((View)mAllAppsGrid).startAnimation(anim);
2097 } else {
2098 mAllAppsGrid.zoom(0.0f, animated);
2099 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002100 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002101 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002102 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002103 }
2104
Joe Onorato7c312c12009-08-13 21:36:53 -07002105 void lockAllApps() {
2106 // TODO
2107 }
2108
2109 void unlockAllApps() {
2110 // TODO
2111 }
2112
Patrick Dubroy558654c2010-07-23 16:48:11 -07002113 private boolean isCustomizationDrawerVisible() {
2114 return mHomeCustomizationDrawer != null && mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
2115 }
2116
2117 private void showCustomizationDrawer() {
2118 if (isAllAppsVisible()) {
2119 // TODO: Make a smoother transition here
2120 closeAllApps(false);
2121 }
2122 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
2123 mHomeCustomizationDrawer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
2124 }
2125
2126 private void hideCustomizationDrawer() {
2127 if (isCustomizationDrawerVisible()) {
2128 Animation slideDownAnimation = AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_down);
2129 slideDownAnimation.setAnimationListener(new Animation.AnimationListener() {
2130 public void onAnimationEnd(Animation animation) {
2131 mHomeCustomizationDrawer.setVisibility(View.GONE);
2132 }
2133 public void onAnimationRepeat(Animation animation) {}
2134 public void onAnimationStart(Animation animation) {}
2135 });
2136 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
2137 }
2138 }
2139
Joe Onorato7404ee42009-07-31 11:54:44 -07002140 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 * Displays the shortcut creation dialog and launches, if necessary, the
2142 * appropriate activity.
2143 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002144 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002145 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2146 DialogInterface.OnShowListener {
2147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2154 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002155 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 builder.setInverseBackgroundForced(true);
2158
2159 AlertDialog dialog = builder.create();
2160 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002161 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002162 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 return dialog;
2165 }
2166
2167 public void onCancel(DialogInterface dialog) {
2168 mWaitingForResult = false;
2169 cleanup();
2170 }
2171
Romain Guycbb89e42009-06-08 15:52:54 -07002172 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002173 }
2174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002176 try {
2177 dismissDialog(DIALOG_CREATE_SHORTCUT);
2178 } catch (Exception e) {
2179 // An exception is thrown if the dialog is not visible, which is fine
2180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 }
2182
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002183 /**
2184 * Handle the action clicked in the "Add to home" dialog.
2185 */
2186 public void onClick(DialogInterface dialog, int which) {
2187 Resources res = getResources();
2188 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002189
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002190 switch (which) {
2191 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002192 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002193 break;
2194 }
Romain Guycbb89e42009-06-08 15:52:54 -07002195
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002196 case AddAdapter.ITEM_APPWIDGET: {
2197 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002199 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2200 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002201 // start the pick activity
2202 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2203 break;
2204 }
Romain Guycbb89e42009-06-08 15:52:54 -07002205
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002206 case AddAdapter.ITEM_LIVE_FOLDER: {
2207 // Insert extra item to handle inserting folder
2208 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002209
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002210 ArrayList<String> shortcutNames = new ArrayList<String>();
2211 shortcutNames.add(res.getString(R.string.group_folder));
2212 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002213
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002214 ArrayList<ShortcutIconResource> shortcutIcons =
2215 new ArrayList<ShortcutIconResource>();
2216 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2217 R.drawable.ic_launcher_folder));
2218 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2219
2220 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2221 pickIntent.putExtra(Intent.EXTRA_INTENT,
2222 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2223 pickIntent.putExtra(Intent.EXTRA_TITLE,
2224 getText(R.string.title_select_live_folder));
2225 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002226
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002227 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2228 break;
2229 }
2230
2231 case AddAdapter.ITEM_WALLPAPER: {
2232 startWallpaper();
2233 break;
2234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002237
2238 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002239 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 }
2242
2243 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002244 * Receives notifications when applications are added/removed.
2245 */
2246 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2247 @Override
2248 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002249 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002250 String reason = intent.getStringExtra("reason");
2251 if (!"homekey".equals(reason)) {
2252 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002253 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002254 animate = false;
2255 }
Joe Onoratob2061212009-11-24 16:13:54 -05002256 closeAllApps(animate);
2257 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002258 }
2259 }
2260
2261 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002262 * Receives notifications whenever the appwidgets are reset.
2263 */
2264 private class AppWidgetResetObserver extends ContentObserver {
2265 public AppWidgetResetObserver() {
2266 super(new Handler());
2267 }
2268
2269 @Override
2270 public void onChange(boolean selfChange) {
2271 onAppWidgetReset();
2272 }
2273 }
2274
2275 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002278 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002279 if (mWorkspace != null) {
2280 return mWorkspace.getCurrentScreen();
2281 } else {
2282 return SCREEN_COUNT / 2;
2283 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002284 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002285
Joe Onorato9c1289c2009-08-17 11:03:03 -04002286 /**
2287 * Refreshes the shortcuts shown on the workspace.
2288 *
2289 * Implementation of the method from LauncherModel.Callbacks.
2290 */
2291 public void startBinding() {
2292 final Workspace workspace = mWorkspace;
2293 int count = workspace.getChildCount();
2294 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002295 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002296 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2297 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002298
Joe Onorato9c1289c2009-08-17 11:03:03 -04002299 if (DEBUG_USER_INTERFACE) {
2300 android.widget.Button finishButton = new android.widget.Button(this);
2301 finishButton.setText("Finish");
2302 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2303
2304 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2305 public void onClick(View v) {
2306 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002307 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
2310 }
2311
2312 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 * Bind the items start-end from the list.
2314 *
2315 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002317 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2318
2319 final Workspace workspace = mWorkspace;
2320
2321 for (int i=start; i<end; i++) {
2322 final ItemInfo item = shortcuts.get(i);
2323 mDesktopItems.add(item);
2324 switch (item.itemType) {
2325 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2326 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002327 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2329 false);
2330 break;
2331 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2332 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2333 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2334 (UserFolderInfo) item);
2335 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2336 false);
2337 break;
2338 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2339 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2340 R.layout.live_folder_icon, this,
2341 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2342 (LiveFolderInfo) item);
2343 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2344 false);
2345 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 }
2348
Joe Onorato9c1289c2009-08-17 11:03:03 -04002349 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 }
2351
Joe Onorato9c1289c2009-08-17 11:03:03 -04002352 /**
2353 * Implementation of the method from LauncherModel.Callbacks.
2354 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002355 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2356 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002357 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002359
2360 /**
2361 * Add the views for a widget to the workspace.
2362 *
2363 * Implementation of the method from LauncherModel.Callbacks.
2364 */
2365 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002366 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2367 if (DEBUG_WIDGETS) {
2368 Log.d(TAG, "bindAppWidget: " + item);
2369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002370 final Workspace workspace = mWorkspace;
2371
2372 final int appWidgetId = item.appWidgetId;
2373 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002374 if (DEBUG_WIDGETS) {
2375 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2376 }
2377
Joe Onorato9c1289c2009-08-17 11:03:03 -04002378 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2379
Joe Onorato9c1289c2009-08-17 11:03:03 -04002380 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2381 item.hostView.setTag(item);
2382
2383 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2384 item.cellY, item.spanX, item.spanY, false);
2385
2386 workspace.requestLayout();
2387
2388 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002389
2390 if (DEBUG_WIDGETS) {
2391 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2392 + (SystemClock.uptimeMillis()-start) + "ms");
2393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 }
2395
2396 /**
2397 * Callback saying that there aren't any more items to bind.
2398 *
2399 * Implementation of the method from LauncherModel.Callbacks.
2400 */
2401 public void finishBindingItems() {
2402 if (mSavedState != null) {
2403 if (!mWorkspace.hasFocus()) {
2404 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2405 }
2406
2407 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2408 if (userFolders != null) {
2409 for (long folderId : userFolders) {
2410 final FolderInfo info = mFolders.get(folderId);
2411 if (info != null) {
2412 openFolder(info);
2413 }
2414 }
2415 final Folder openFolder = mWorkspace.getOpenFolder();
2416 if (openFolder != null) {
2417 openFolder.requestFocus();
2418 }
2419 }
2420
Joe Onorato9c1289c2009-08-17 11:03:03 -04002421 mSavedState = null;
2422 }
2423
2424 if (mSavedInstanceState != null) {
2425 super.onRestoreInstanceState(mSavedInstanceState);
2426 mSavedInstanceState = null;
2427 }
2428
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 mWorkspaceLoading = false;
2430 }
2431
2432 /**
2433 * Add the icons for all apps.
2434 *
2435 * Implementation of the method from LauncherModel.Callbacks.
2436 */
2437 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002438 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439 }
2440
2441 /**
2442 * A package was installed.
2443 *
2444 * Implementation of the method from LauncherModel.Callbacks.
2445 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002446 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002447 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002448 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002449 }
2450
2451 /**
2452 * A package was updated.
2453 *
2454 * Implementation of the method from LauncherModel.Callbacks.
2455 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002456 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002457 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002458 mWorkspace.updateShortcuts(apps);
2459 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002460 }
2461
2462 /**
2463 * A package was uninstalled.
2464 *
2465 * Implementation of the method from LauncherModel.Callbacks.
2466 */
Joe Onorato36115782010-06-17 13:28:48 -04002467 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002468 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002469 if (permanent) {
2470 mWorkspace.removeItems(apps);
2471 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002472 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002473 }
Joe Onoratobe386092009-11-17 17:32:16 -08002474
2475 /**
2476 * Prints out out state for debugging.
2477 */
2478 public void dumpState() {
2479 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002480 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002481 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2482 Log.d(TAG, "mRestoring=" + mRestoring);
2483 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2484 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2485 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2486 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002487 mModel.dumpState();
2488 mAllAppsGrid.dumpState();
2489 Log.d(TAG, "END launcher2 dump state");
2490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491}