blob: f846b516c3834ea80581d8258f870e939f7c2044 [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);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700624 }
625 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700626 dismissPreview(mNextView);
627 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800628 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700629 }
Romain Guycbb89e42009-06-08 15:52:54 -0700630
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700631 @Override
632 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 // Flag the loader to stop early before switching
634 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800635 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800636 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700637 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700638
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800639 // We can't hide the IME if it was forced open. So don't bother
640 /*
641 @Override
642 public void onWindowFocusChanged(boolean hasFocus) {
643 super.onWindowFocusChanged(hasFocus);
644
645 if (hasFocus) {
646 final InputMethodManager inputManager = (InputMethodManager)
647 getSystemService(Context.INPUT_METHOD_SERVICE);
648 WindowManager.LayoutParams lp = getWindow().getAttributes();
649 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
650 android.os.Handler()) {
651 protected void onReceiveResult(int resultCode, Bundle resultData) {
652 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
653 }
654 });
655 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
656 }
657 }
658 */
659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 private boolean acceptFilter() {
661 final InputMethodManager inputManager = (InputMethodManager)
662 getSystemService(Context.INPUT_METHOD_SERVICE);
663 return !inputManager.isFullscreenMode();
664 }
665
666 @Override
667 public boolean onKeyDown(int keyCode, KeyEvent event) {
668 boolean handled = super.onKeyDown(keyCode, event);
669 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
670 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
671 keyCode, event);
672 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700673 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700674 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700675 // showSearchDialog()
676 // If there are multiple keystrokes before the search dialog takes focus,
677 // onSearchRequested() will be called for every keystroke,
678 // but it is idempotent, so it's fine.
679 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681 }
682
Joe Onorato8a9625e2010-01-28 15:55:35 -0800683 // Eat the long press event so the keyboard doesn't come up.
684 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
685 return true;
686 }
687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 return handled;
689 }
690
Karl Rosaen138a0412009-04-23 19:00:21 -0700691 private String getTypedText() {
692 return mDefaultKeySsb.toString();
693 }
694
695 private void clearTypedText() {
696 mDefaultKeySsb.clear();
697 mDefaultKeySsb.clearSpans();
698 Selection.setSelection(mDefaultKeySsb, 0);
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 /**
702 * Restores the previous state, if it exists.
703 *
704 * @param savedState The previous state.
705 */
706 private void restoreState(Bundle savedState) {
707 if (savedState == null) {
708 return;
709 }
710
Joe Onorato3a8820b2009-11-10 15:06:42 -0800711 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
712 if (allApps) {
713 showAllApps(false);
714 }
715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
717 if (currentScreen > -1) {
718 mWorkspace.setCurrentScreen(currentScreen);
719 }
720
721 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
722 if (addScreen > -1) {
723 mAddItemCellInfo = new CellLayout.CellInfo();
724 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
725 addItemCellInfo.valid = true;
726 addItemCellInfo.screen = addScreen;
727 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
728 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
729 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
730 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
731 addItemCellInfo.findVacantCellsFromOccupied(
732 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
733 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
734 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
735 mRestoring = true;
736 }
737
738 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
739 if (renameFolder) {
740 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 mRestoring = true;
743 }
744 }
745
746 /**
747 * Finds all the views we need and configure them properly.
748 */
749 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400750 DragController dragController = mDragController;
751
Romain Guyb1b69f52009-08-10 15:10:15 -0700752 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400753 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
Joe Onorato7c312c12009-08-13 21:36:53 -0700755 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700756 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700757 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800758 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700759 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
763 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500764 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700765
766 // We only intercept touch events to dismiss the home customization drawer; if it doesn't
767 // exist, then no need to do this
768 if (mHomeCustomizationDrawer != null)
769 workspace.setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770
Joe Onorato7c312c12009-08-13 21:36:53 -0700771 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
772 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773
Joe Onorato7c312c12009-08-13 21:36:53 -0700774 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700776 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800777 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800778
Michael Jurka0e260592010-06-30 17:07:39 -0700779 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
780 if (widgetChooser != null) {
781 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
782 widgetChooser.setAdapter(widgetGalleryAdapter);
783 widgetChooser.setDragController(dragController);
784 widgetChooser.setLauncher(this);
785
786 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
787 IntentListAdapter folderTypes = new FolderListAdapter(this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
788 folderChooser.setAdapter(folderTypes);
789 folderChooser.setLauncher(this);
790
791 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
792 IntentListAdapter shortcutTypes = new ShortcutListAdapter(this, Intent.ACTION_CREATE_SHORTCUT);
793 shortcutChooser.setAdapter(shortcutTypes);
794 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700795 } else {
796 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
797 hotseatLeft.setContentDescription(mHotseatLabels[0]);
798 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
799 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
800 hotseatRight.setContentDescription(mHotseatLabels[1]);
801 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400802
Michael Jurkaaf442092010-06-10 17:01:57 -0700803 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
804 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800805
Michael Jurkaaf442092010-06-10 17:01:57 -0700806 Drawable previous = mPreviousView.getDrawable();
807 Drawable next = mNextView.getDrawable();
808 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
Michael Jurkaaf442092010-06-10 17:01:57 -0700810 mPreviousView.setHapticFeedbackEnabled(false);
811 mPreviousView.setOnLongClickListener(this);
812 mNextView.setHapticFeedbackEnabled(false);
813 mNextView.setOnLongClickListener(this);
814 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400817 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819
820 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400821 deleteZone.setDragController(dragController);
Winson Chungaafa03c2010-06-11 17:34:16 -0700822 int deleteZoneHandleId = LauncherApplication.isScreenXLarge() ? R.id.add_button : R.id.all_apps_button_cluster;
Michael Jurkaaf442092010-06-10 17:01:57 -0700823 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824
Joe Onorato00acb122009-08-04 16:04:30 -0400825 dragController.setDragScoller(workspace);
826 dragController.setDragListener(deleteZone);
827 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800828 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700829
Joe Onorato00acb122009-08-04 16:04:30 -0400830 // The order here is bottom to top.
831 dragController.addDropTarget(workspace);
832 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
834
Romain Guy8a73c512009-11-09 19:19:59 -0800835 @SuppressWarnings({"UnusedDeclaration"})
836 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800837 if (!isAllAppsVisible()) {
838 mWorkspace.scrollLeft();
839 }
Romain Guy8a73c512009-11-09 19:19:59 -0800840 }
841
842 @SuppressWarnings({"UnusedDeclaration"})
843 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800844 if (!isAllAppsVisible()) {
845 mWorkspace.scrollRight();
846 }
Romain Guy8a73c512009-11-09 19:19:59 -0800847 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400848
849 @SuppressWarnings({"UnusedDeclaration"})
850 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400851 if (isAllAppsVisible()) return;
852
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400853 int index = -1;
854 if (v.getId() == R.id.hotseat_left) {
855 index = 0;
856 } else if (v.getId() == R.id.hotseat_right) {
857 index = 1;
858 }
859
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400860 // reload these every tap; you never know when they might change
861 loadHotseats();
862 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
863 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400864 startActivitySafely(
865 mHotseats[index],
866 "hotseat"
867 );
868 }
869 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700870
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 /**
872 * Creates a view representing a shortcut.
873 *
874 * @param info The data structure describing the shortcut.
875 *
876 * @return A View inflated from R.layout.application.
877 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800878 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 return createShortcut(R.layout.application,
880 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
881 }
882
883 /**
884 * Creates a view representing a shortcut inflated from the specified resource.
885 *
886 * @param layoutResId The id of the XML layout used to create the shortcut.
887 * @param parent The group the shortcut belongs to.
888 * @param info The data structure describing the shortcut.
889 *
890 * @return A View inflated from layoutResId.
891 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800892 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
894
Joe Onorato0589f0f2010-02-08 13:44:00 -0800895 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
896 new FastBitmapDrawable(info.getIcon(mIconCache)),
897 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 favorite.setText(info.title);
899 favorite.setTag(info);
900 favorite.setOnClickListener(this);
901
902 return favorite;
903 }
904
905 /**
906 * Add an application shortcut to the workspace.
907 *
908 * @param data The intent describing the application.
909 * @param cellInfo The position on screen where to create the shortcut.
910 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 cellInfo.screen = mWorkspace.getCurrentScreen();
913 if (!findSingleSlot(cellInfo)) return;
914
Joe Onorato0589f0f2010-02-08 13:44:00 -0800915 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
916 data, context);
917
Romain Guy73b979d2009-06-09 12:57:21 -0700918 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800919 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800921 info.container = ItemInfo.NO_ID;
922 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
923 } else {
924 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926 }
Romain Guycbb89e42009-06-08 15:52:54 -0700927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 /**
929 * Add a shortcut to the workspace.
930 *
931 * @param data The intent describing the shortcut.
932 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 cellInfo.screen = mWorkspace.getCurrentScreen();
936 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Joe Onorato0589f0f2010-02-08 13:44:00 -0800938 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939
940 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
943 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945 }
946
Romain Guycbb89e42009-06-08 15:52:54 -0700947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700949 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700951 * @param data The intent describing the appWidgetId.
952 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700954 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700955 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700956
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700957 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700959 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 // Try finding open space on Launcher screen
962 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800963 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
964 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
965 return;
966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700968 // Build Launcher-specific widget info and save to database
969 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 launcherInfo.spanX = spans[0];
971 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 LauncherModel.addItemToDatabase(this, launcherInfo,
974 LauncherSettings.Favorites.CONTAINER_DESKTOP,
975 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
976
977 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700981 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700983 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
989 }
Romain Guycbb89e42009-06-08 15:52:54 -0700990
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
992 mDesktopItems.remove(launcherInfo);
993 launcherInfo.hostView = null;
994 }
995
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700996 public LauncherAppWidgetHost getAppWidgetHost() {
997 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 }
Romain Guycbb89e42009-06-08 15:52:54 -0700999
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001000 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001001 getWindow().closeAllPanels();
1002
1003 try {
1004 dismissDialog(DIALOG_CREATE_SHORTCUT);
1005 // Unlock the workspace if the dialog was showing
1006 } catch (Exception e) {
1007 // An exception is thrown if the dialog is not visible, which is fine
1008 }
1009
1010 try {
1011 dismissDialog(DIALOG_RENAME_FOLDER);
1012 // Unlock the workspace if the dialog was showing
1013 } catch (Exception e) {
1014 // An exception is thrown if the dialog is not visible, which is fine
1015 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001016
1017 // Whatever we were doing is hereby canceled.
1018 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 @Override
1022 protected void onNewIntent(Intent intent) {
1023 super.onNewIntent(intent);
1024
1025 // Close the menu
1026 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001027 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001028 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001029
Joe Onorato14f122b2009-11-19 14:06:36 -08001030 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1031 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1032 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001033
1034 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001035 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001036 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001037 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001038 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001039 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001040
Joe Onorato3a8820b2009-11-10 15:06:42 -08001041 final View v = getWindow().peekDecorView();
1042 if (v != null && v.getWindowToken() != null) {
1043 InputMethodManager imm = (InputMethodManager)getSystemService(
1044 INPUT_METHOD_SERVICE);
1045 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047 }
1048 }
1049
1050 @Override
1051 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1052 // Do not call super here
1053 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001054
1055 if (mHomeCustomizationDrawer != null) {
1056 String cur = savedInstanceState.getString("currentTab");
1057 if (cur != null) {
1058 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1059 }
1060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 }
1062
1063 @Override
1064 protected void onSaveInstanceState(Bundle outState) {
1065 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1066
1067 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1068 if (folders.size() > 0) {
1069 final int count = folders.size();
1070 long[] ids = new long[count];
1071 for (int i = 0; i < count; i++) {
1072 final FolderInfo info = folders.get(i).getInfo();
1073 ids[i] = info.id;
1074 }
1075 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1076 } else {
1077 super.onSaveInstanceState(outState);
1078 }
1079
Joe Onoratofb0ca672009-09-14 17:55:46 -04001080 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001081 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084
1085 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1086 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1087 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1088
1089 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1090 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1091 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1092 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1093 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1094 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1095 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1096 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001097 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099
1100 if (mFolderInfo != null && mWaitingForResult) {
1101 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1102 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1103 }
Michael Jurka946ad472010-07-09 18:05:18 -07001104
1105 if (mHomeCustomizationDrawer != null) {
1106 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1107 if (currentTabTag != null) {
1108 outState.putString("currentTab", currentTabTag);
1109 }
1110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112
1113 @Override
1114 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001120 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122
1123 TextKeyListener.getInstance().release();
1124
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001128
1129 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001130
Patrick Dubroyab962b72010-07-26 12:10:10 -07001131 // Some launcher layouts don't have a previous and next view
1132 if (mPreviousView != null) {
1133 dismissPreview(mPreviousView);
1134 }
1135 if (mNextView != null) {
1136 dismissPreview(mNextView);
1137 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001138
1139 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141
1142 @Override
1143 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001144 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 super.startActivityForResult(intent, requestCode);
1146 }
1147
1148 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001149 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001151
Joe Onorato7bb17492009-09-24 17:51:01 -07001152 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 if (initialQuery == null) {
1155 // Use any text typed in the launcher as the initial query
1156 initialQuery = getTypedText();
1157 clearTypedText();
1158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 if (appSearchData == null) {
1160 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001161 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
Romain Guycbb89e42009-06-08 15:52:54 -07001163
Karl Rosaen138a0412009-04-23 19:00:21 -07001164 final SearchManager searchManager =
1165 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001166 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001167 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
1170 @Override
1171 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 if (isWorkspaceLocked()) {
1173 return false;
1174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175
1176 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1179 .setIcon(android.R.drawable.ic_menu_add)
1180 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001181 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 .setIcon(android.R.drawable.ic_menu_gallery)
1183 .setAlphabeticShortcut('W');
1184 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1185 .setIcon(android.R.drawable.ic_search_category_default)
1186 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1187 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1188 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1189 .setAlphabeticShortcut('N');
1190
1191 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001192 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1193 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194
1195 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1196 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1197 .setIntent(settings);
1198
1199 return true;
1200 }
1201
1202 @Override
1203 public boolean onPrepareOptionsMenu(Menu menu) {
1204 super.onPrepareOptionsMenu(menu);
1205
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001206 // If all apps is animating, don't show the menu, because we don't know
1207 // which one to show.
1208 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1209 return false;
1210 }
1211
1212 // Only show the add and wallpaper options when we're not in all apps.
1213 boolean visible = !mAllAppsGrid.isOpaque();
1214 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1215 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1216
1217 // Disable add if the workspace is full.
1218 if (visible) {
1219 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1220 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222
1223 return true;
1224 }
1225
Michael Jurka0e260592010-06-30 17:07:39 -07001226 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1227 // using the settings menu to add an item, something similar happens in showAddDialog
1228 public void prepareAddItemFromHomeCustomizationDrawer() {
1229 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1230 mAddItemCellInfo = mMenuAddInfo;
1231 }
1232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 @Override
1234 public boolean onOptionsItemSelected(MenuItem item) {
1235 switch (item.getItemId()) {
1236 case MENU_ADD:
1237 addItems();
1238 return true;
1239 case MENU_WALLPAPER_SETTINGS:
1240 startWallpaper();
1241 return true;
1242 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001243 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 return true;
1245 case MENU_NOTIFICATIONS:
1246 showNotifications();
1247 return true;
1248 }
1249
1250 return super.onOptionsItemSelected(item);
1251 }
Romain Guycbb89e42009-06-08 15:52:54 -07001252
Karl Rosaen138a0412009-04-23 19:00:21 -07001253 /**
1254 * Indicates that we want global search for this activity by setting the globalSearch
1255 * argument for {@link #startSearch} to true.
1256 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001258 @Override
1259 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001260 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001262 }
1263
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 public boolean isWorkspaceLocked() {
1265 return mWorkspaceLoading || mWaitingForResult;
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001269 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001270 if (LauncherApplication.isScreenXLarge()) {
1271 // Animate the widget chooser up from the bottom of the screen
1272 if (!isCustomizationDrawerVisible()) {
1273 showCustomizationDrawer();
1274 }
1275 } else {
1276 showAddDialog(mMenuAddInfo);
1277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279
Michael Jurkaaf442092010-06-10 17:01:57 -07001280 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1281 mAddItemCellInfo = cellInfo;
1282 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1283 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1284 addAppWidgetImpl(appWidgetId);
1285 }
1286
1287 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001288 // TODO: catch bad widget exception when sent
1289 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001290 // TODO: Is this log message meaningful?
1291 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1292 addAppWidgetImpl(appWidgetId);
1293 }
1294
1295 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001296 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297
Bjorn Bringert7984c942009-12-09 15:38:25 +00001298 if (appWidget.configure != null) {
1299 // Launch over to configure widget, if needed
1300 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1301 intent.setComponent(appWidget.configure);
1302 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1303
Romain Guy8e633c52010-03-04 12:51:36 -08001304 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001306 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001307 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309 }
Romain Guycbb89e42009-06-08 15:52:54 -07001310
Joe Onoratodeb98af2010-02-19 14:59:39 -08001311 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001312 // Handle case where user selected "Applications"
1313 String applicationName = getResources().getString(R.string.group_applications);
1314 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001316 if (applicationName != null && applicationName.equals(shortcutName)) {
1317 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1318 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001319
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001320 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1321 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001322 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001323 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001324 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
1328 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001329 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001330 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001331 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001332
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001333 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001334 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001335 } else {
1336 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
Joe Onorato9c1289c2009-08-17 11:03:03 -04001340 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 UserFolderInfo folderInfo = new UserFolderInfo();
1342 folderInfo.title = getText(R.string.folder_name);
1343
1344 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1345 cellInfo.screen = mWorkspace.getCurrentScreen();
1346 if (!findSingleSlot(cellInfo)) return;
1347
1348 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 LauncherModel.addItemToDatabase(this, folderInfo,
1350 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001352 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353
1354 // Create the view
1355 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1356 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1357 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 void removeFolder(FolderInfo folder) {
1362 mFolders.remove(folder.id);
1363 }
1364
1365 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 cellInfo.screen = mWorkspace.getCurrentScreen();
1367 if (!findSingleSlot(cellInfo)) return;
1368
1369 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1370
1371 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1373 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1375 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377 }
1378
1379 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1380 CellLayout.CellInfo cellInfo, boolean notify) {
1381
1382 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1383 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1384
1385 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 Intent.ShortcutIconResource iconResource = null;
1387
1388 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1389 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1390 try {
1391 iconResource = (Intent.ShortcutIconResource) extra;
1392 final PackageManager packageManager = context.getPackageManager();
1393 Resources resources = packageManager.getResourcesForApplication(
1394 iconResource.packageName);
1395 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1396 icon = resources.getDrawable(id);
1397 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001398 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400 }
1401
1402 if (icon == null) {
1403 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1404 }
1405
1406 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001407 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 info.title = name;
1409 info.iconResource = iconResource;
1410 info.uri = data.getData();
1411 info.baseIntent = baseIntent;
1412 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1413 LiveFolders.DISPLAY_MODE_GRID);
1414
1415 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1416 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418
1419 return info;
1420 }
1421
1422 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1423 final int[] xy = new int[2];
1424 if (findSlot(cellInfo, xy, 1, 1)) {
1425 cellInfo.cellX = xy[0];
1426 cellInfo.cellY = xy[1];
1427 return true;
1428 }
1429 return false;
1430 }
1431
1432 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1433 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1434 boolean[] occupied = mSavedState != null ?
1435 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1436 cellInfo = mWorkspace.findAllVacantCells(occupied);
1437 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1438 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1439 return false;
1440 }
1441 }
1442 return true;
1443 }
1444
1445 private void showNotifications() {
1446 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1447 if (statusBar != null) {
1448 statusBar.expand();
1449 }
1450 }
1451
1452 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001453 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001455 Intent chooser = Intent.createChooser(pickWallpaper,
1456 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001457 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1458 // Removed in Eclair MR1
1459// WallpaperManager wm = (WallpaperManager)
1460// getSystemService(Context.WALLPAPER_SERVICE);
1461// WallpaperInfo wi = wm.getWallpaperInfo();
1462// if (wi != null && wi.getSettingsActivity() != null) {
1463// LabeledIntent li = new LabeledIntent(getPackageName(),
1464// R.string.configure_wallpaper, 0);
1465// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1466// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1467// }
Mike Clerona0618e42009-10-22 13:55:21 -07001468 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001471 /**
1472 * Registers various content observers. The current implementation registers
1473 * only a favorites observer to keep track of the favorites applications.
1474 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001475 private void registerContentObservers() {
1476 ContentResolver resolver = getContentResolver();
1477 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1478 true, mWidgetObserver);
1479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 @Override
1482 public boolean dispatchKeyEvent(KeyEvent event) {
1483 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1484 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001486 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001487 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001488 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001489 dumpState();
1490 return true;
1491 }
1492 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001493 }
1494 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1495 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001496 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return true;
1498 }
1499 }
1500
1501 return super.dispatchKeyEvent(event);
1502 }
1503
Joe Onorato88ec0992009-11-19 13:16:06 -08001504 @Override
1505 public void onBackPressed() {
1506 if (isAllAppsVisible()) {
1507 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001508 } else if (isCustomizationDrawerVisible()) {
1509 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001510 } else {
1511 closeFolder();
1512 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001513 // Some launcher layouts don't have a previous and next view
1514 if (mPreviousView != null) {
1515 dismissPreview(mPreviousView);
1516 dismissPreview(mNextView);
1517 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001518 }
1519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 private void closeFolder() {
1521 Folder folder = mWorkspace.getOpenFolder();
1522 if (folder != null) {
1523 closeFolder(folder);
1524 }
1525 }
1526
1527 void closeFolder(Folder folder) {
1528 folder.getInfo().opened = false;
1529 ViewGroup parent = (ViewGroup) folder.getParent();
1530 if (parent != null) {
1531 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001532 if (folder instanceof DropTarget) {
1533 // Live folders aren't DropTargets.
1534 mDragController.removeDropTarget((DropTarget)folder);
1535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537 folder.onClose();
1538 }
1539
1540 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001541 * Re-listen when widgets are reset.
1542 */
1543 private void onAppWidgetReset() {
1544 mAppWidgetHost.startListening();
1545 }
1546
1547 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1549 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551 private void unbindDesktopItems() {
1552 for (ItemInfo item: mDesktopItems) {
1553 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 /**
1558 * Launches the intent referred by the clicked shortcut.
1559 *
1560 * @param v The view representing the clicked shortcut.
1561 */
1562 public void onClick(View v) {
1563 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001564 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001566 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001567 int[] pos = new int[2];
1568 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001569 intent.setSourceBounds(new Rect(pos[0], pos[1],
1570 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001571 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 } else if (tag instanceof FolderInfo) {
1573 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001574 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001575 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001576 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001577 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001578 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581 }
1582
Michael Jurka0e260592010-06-30 17:07:39 -07001583 public boolean onTouch(View v, MotionEvent event) {
1584 // this is being forwarded from mWorkspace;
1585 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001586 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001587 return false;
1588 }
1589
Michael Jurkaaf442092010-06-10 17:01:57 -07001590 /**
1591 * Event handler for the "plus" button that appears on the home screen, which
1592 * enters home screen customization mode.
1593 *
1594 * @param v The view that was clicked.
1595 */
1596 public void onClickAddButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001597 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001598 }
1599
Joe Onoratof984e852010-03-25 09:47:45 -07001600 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1602 try {
1603 startActivity(intent);
1604 } catch (ActivityNotFoundException e) {
1605 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001606 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 } catch (SecurityException e) {
1608 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001609 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001611 "or use the exported attribute for this activity. "
1612 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
1614 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001615
Romain Guy8e633c52010-03-04 12:51:36 -08001616 void startActivityForResultSafely(Intent intent, int requestCode) {
1617 try {
1618 startActivityForResult(intent, requestCode);
1619 } catch (ActivityNotFoundException e) {
1620 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1621 } catch (SecurityException e) {
1622 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1623 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1624 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1625 "or use the exported attribute for this activity.", e);
1626 }
1627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628
1629 private void handleFolderClick(FolderInfo folderInfo) {
1630 if (!folderInfo.opened) {
1631 // Close any open folder
1632 closeFolder();
1633 // Open the requested folder
1634 openFolder(folderInfo);
1635 } else {
1636 // Find the open folder...
1637 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1638 int folderScreen;
1639 if (openFolder != null) {
1640 folderScreen = mWorkspace.getScreenForView(openFolder);
1641 // .. and close it
1642 closeFolder(openFolder);
1643 if (folderScreen != mWorkspace.getCurrentScreen()) {
1644 // Close any folder open on the current screen
1645 closeFolder();
1646 // Pull the folder onto this screen
1647 openFolder(folderInfo);
1648 }
1649 }
1650 }
1651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 /**
1654 * Opens the user fodler described by the specified tag. The opening of the folder
1655 * is animated relative to the specified View. If the View is null, no animation
1656 * is played.
1657 *
1658 * @param folderInfo The FolderInfo describing the folder to open.
1659 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001660 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 Folder openFolder;
1662
1663 if (folderInfo instanceof UserFolderInfo) {
1664 openFolder = UserFolder.fromXml(this);
1665 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001666 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 } else {
1668 return;
1669 }
1670
Joe Onorato00acb122009-08-04 16:04:30 -04001671 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 openFolder.setLauncher(this);
1673
1674 openFolder.bind(folderInfo);
1675 folderInfo.opened = true;
1676
Winson Chungaafa03c2010-06-11 17:34:16 -07001677 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 openFolder.onOpen();
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001683 switch (v.getId()) {
1684 case R.id.previous_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);
Joe Onorato0d44e942009-11-16 18:20:51 -08001689 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001690 return true;
1691 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001692 if (!isAllAppsVisible()) {
1693 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1694 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001695 showPreviews(v);
1696 }
1697 return true;
1698 case R.id.all_apps_button:
1699 if (!isAllAppsVisible()) {
1700 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1701 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1702 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001703 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001704 return true;
1705 }
1706
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 return false;
1709 }
1710
1711 if (!(v instanceof CellLayout)) {
1712 v = (View) v.getParent();
1713 }
1714
1715 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1716
1717 // This happens when long clicking an item with the dpad/trackball
1718 if (cellInfo == null) {
1719 return true;
1720 }
1721
1722 if (mWorkspace.allowLongPress()) {
1723 if (cellInfo.cell == null) {
1724 if (cellInfo.valid) {
1725 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001726 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001727 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1728 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 showAddDialog(cellInfo);
1730 }
1731 } else {
1732 if (!(cellInfo.cell instanceof Folder)) {
1733 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001734 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1735 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 mWorkspace.startDrag(cellInfo);
1737 }
1738 }
1739 }
1740 return true;
1741 }
1742
Romain Guye6b8e2f2009-11-10 11:56:55 -08001743 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001744 private void dismissPreview(final View v) {
1745 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001746 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001747 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1748 public void onDismiss() {
1749 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1750 int count = group.getChildCount();
1751 for (int i = 0; i < count; i++) {
1752 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1753 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001754 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1755 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1756
1757 v.setTag(R.id.workspace, null);
1758 v.setTag(R.id.icon, null);
1759 window.setOnDismissListener(null);
1760 }
1761 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001762 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001763 }
1764 v.setTag(null);
1765 }
1766
Romain Guyf8e6a802009-12-07 17:48:02 -08001767 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001768 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001769 }
1770
Romain Guya6abce82009-11-10 02:54:41 -08001771 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001772 final Resources resources = getResources();
1773 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001774
Romain Guya6abce82009-11-10 02:54:41 -08001775 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001776
Romain Guy9d31e9f2009-11-11 18:54:28 -08001777 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001778
Romain Guyf8e6a802009-12-07 17:48:02 -08001779 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001780 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001781 int extraW = (int) ((r.left + r.right) * max);
1782 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001783
1784 int aW = cell.getWidth() - extraW;
1785 float w = aW / max;
1786
1787 int width = cell.getWidth();
1788 int height = cell.getHeight();
1789 int x = cell.getLeftPadding();
1790 int y = cell.getTopPadding();
1791 width -= (x + cell.getRightPadding());
1792 height -= (y + cell.getBottomPadding());
1793
1794 float scale = w / width;
1795
1796 int count = end - start;
1797
1798 final float sWidth = width * scale;
1799 float sHeight = height * scale;
1800
Romain Guye6b8e2f2009-11-10 11:56:55 -08001801 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001802
Romain Guye6b8e2f2009-11-10 11:56:55 -08001803 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1804 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001805
1806 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001807 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001808 cell = (CellLayout) workspace.getChildAt(i);
1809
Romain Guyf8e6a802009-12-07 17:48:02 -08001810 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001811 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001812
1813 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001814 c.scale(scale, scale);
1815 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1816 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001817
Romain Guy9d31e9f2009-11-11 18:54:28 -08001818 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001819 image.setImageBitmap(bitmap);
1820 image.setTag(i);
1821 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001822 image.setOnFocusChangeListener(handler);
1823 image.setFocusable(true);
1824 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001825
1826 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001827 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1828
Winson Chungaafa03c2010-06-11 17:34:16 -07001829 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001830 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001831
1832 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001833 p.setContentView(preview);
1834 p.setWidth((int) (sWidth * count + extraW));
1835 p.setHeight((int) (sHeight + extraH));
1836 p.setAnimationStyle(R.style.AnimationPreview);
1837 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001838 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001839 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001840 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001841
Romain Guye6b8e2f2009-11-10 11:56:55 -08001842 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1843 public void onDismiss() {
1844 dismissPreview(anchor);
1845 }
1846 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001847
1848 anchor.setTag(p);
1849 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001850 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001851 }
1852
Romain Guy9d31e9f2009-11-11 18:54:28 -08001853 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001854 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001855
Romain Guye6b8e2f2009-11-10 11:56:55 -08001856 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001857 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001858 }
1859
1860 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001861 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001862 v.post(this);
1863 }
1864
1865 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001866 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001867 }
1868
1869 public void onFocusChange(View v, boolean hasFocus) {
1870 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001871 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001872 }
Romain Guya6abce82009-11-10 02:54:41 -08001873 }
1874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 Workspace getWorkspace() {
1877 return mWorkspace;
1878 }
1879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 @Override
1881 protected Dialog onCreateDialog(int id) {
1882 switch (id) {
1883 case DIALOG_CREATE_SHORTCUT:
1884 return new CreateShortcut().createDialog();
1885 case DIALOG_RENAME_FOLDER:
1886 return new RenameFolder().createDialog();
1887 }
1888
1889 return super.onCreateDialog(id);
1890 }
1891
1892 @Override
1893 protected void onPrepareDialog(int id, Dialog dialog) {
1894 switch (id) {
1895 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 break;
1897 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001898 if (mFolderInfo != null) {
1899 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1900 final CharSequence text = mFolderInfo.title;
1901 input.setText(text);
1902 input.setSelection(0, text.length());
1903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 break;
1905 }
1906 }
1907
1908 void showRenameDialog(FolderInfo info) {
1909 mFolderInfo = info;
1910 mWaitingForResult = true;
1911 showDialog(DIALOG_RENAME_FOLDER);
1912 }
1913
1914 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1915 mAddItemCellInfo = cellInfo;
1916 mWaitingForResult = true;
1917 showDialog(DIALOG_CREATE_SHORTCUT);
1918 }
1919
Joe Onoratodeb98af2010-02-19 14:59:39 -08001920 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001921 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001922 Bundle bundle = new Bundle();
1923
1924 ArrayList<String> shortcutNames = new ArrayList<String>();
1925 shortcutNames.add(getString(R.string.group_applications));
1926 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1927
1928 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1929 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1930 R.drawable.ic_launcher_application));
1931 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1932
1933 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1934 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001935 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001936 pickIntent.putExtras(bundle);
1937
Joe Onoratodeb98af2010-02-19 14:59:39 -08001938 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 private class RenameFolder {
1942 private EditText mInput;
1943
1944 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1946 mInput = (EditText) layout.findViewById(R.id.folder_name);
1947
1948 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1949 builder.setIcon(0);
1950 builder.setTitle(getString(R.string.rename_folder_title));
1951 builder.setCancelable(true);
1952 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1953 public void onCancel(DialogInterface dialog) {
1954 cleanup();
1955 }
1956 });
1957 builder.setNegativeButton(getString(R.string.cancel_action),
1958 new Dialog.OnClickListener() {
1959 public void onClick(DialogInterface dialog, int which) {
1960 cleanup();
1961 }
1962 }
1963 );
1964 builder.setPositiveButton(getString(R.string.rename_action),
1965 new Dialog.OnClickListener() {
1966 public void onClick(DialogInterface dialog, int which) {
1967 changeFolderName();
1968 }
1969 }
1970 );
1971 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001972
1973 final AlertDialog dialog = builder.create();
1974 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1975 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001976 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001977 mInput.requestFocus();
1978 InputMethodManager inputManager = (InputMethodManager)
1979 getSystemService(Context.INPUT_METHOD_SERVICE);
1980 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001981 }
1982 });
1983
1984 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986
1987 private void changeFolderName() {
1988 final String name = mInput.getText().toString();
1989 if (!TextUtils.isEmpty(name)) {
1990 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 mFolderInfo.title = name;
1993 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1994
Joe Onorato9c1289c2009-08-17 11:03:03 -04001995 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001996 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 } else {
1999 final FolderIcon folderIcon = (FolderIcon)
2000 mWorkspace.getViewForTag(mFolderInfo);
2001 if (folderIcon != null) {
2002 folderIcon.setText(name);
2003 getWorkspace().requestLayout();
2004 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002005 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 mWorkspaceLoading = true;
2007 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009 }
2010 }
2011 cleanup();
2012 }
2013
2014 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 dismissDialog(DIALOG_RENAME_FOLDER);
2016 mWaitingForResult = false;
2017 mFolderInfo = null;
2018 }
2019 }
2020
Daniel Sandler843e8602010-06-07 14:59:01 -04002021 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2022 public boolean isAllAppsVisible() {
2023 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002024 }
2025
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002026 // AllAppsView.Watcher
2027 public void zoomed(float zoom) {
2028 if (zoom == 1.0f) {
2029 mWorkspace.setVisibility(View.GONE);
2030 }
2031 }
2032
Joe Onorato3a8820b2009-11-10 15:06:42 -08002033 void showAllApps(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002034 hideCustomizationDrawer();
2035
2036 if (LauncherApplication.isScreenXLarge() && animated) {
2037 // Not really a zoom -- this just makes the view visible
2038 mAllAppsGrid.zoom(1.0f, false);
2039 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_in);
2040 ((View) mAllAppsGrid).startAnimation(anim);
2041 } else {
2042 mAllAppsGrid.zoom(1.0f, animated);
2043 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002044
Romain Guyc16fea72010-03-12 17:17:56 -08002045 ((View) mAllAppsGrid).setFocusable(true);
2046 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002047
Joe Onorato7c312c12009-08-13 21:36:53 -07002048 // TODO: fade these two too
2049 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002050 }
2051
Joe Onoratob2061212009-11-24 16:13:54 -05002052 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002053 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002054 * - Home from workspace
2055 * - from center screen
2056 * - from other screens
2057 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002058 * - from center screen
2059 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002060 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 * - from center screen
2062 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002063 * - Launch app from workspace and quit
2064 * - with back
2065 * - with home
2066 * - Launch app from all apps and quit
2067 * - with back
2068 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002069 * - Go to a screen that's not the default, then all
2070 * apps, and launch and app, and go back
2071 * - with back
2072 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002073 * - On workspace, long press power and go back
2074 * - with back
2075 * - with home
2076 * - On all apps, long press power and go back
2077 * - with back
2078 * - with home
2079 * - On workspace, power off
2080 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002081 * - Launch an app and turn off the screen while in that app
2082 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002083 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002084 * - From all apps
2085 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002086 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2087 * - From all apps
2088 * - From the center workspace
2089 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002090 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002091 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002092 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002093 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002094 if (LauncherApplication.isScreenXLarge() && animated) {
2095 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_out);
2096 anim.setAnimationListener(new AnimationListener() {
2097 public void onAnimationStart(Animation animation) {}
2098 public void onAnimationRepeat(Animation animation) {}
2099 public void onAnimationEnd(Animation animation) {
2100 mAllAppsGrid.zoom(0.0f, false);
2101 }
2102 });
2103 ((View)mAllAppsGrid).startAnimation(anim);
2104 } else {
2105 mAllAppsGrid.zoom(0.0f, animated);
2106 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002107 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002108 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002109 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002110 }
2111
Joe Onorato7c312c12009-08-13 21:36:53 -07002112 void lockAllApps() {
2113 // TODO
2114 }
2115
2116 void unlockAllApps() {
2117 // TODO
2118 }
2119
Patrick Dubroy558654c2010-07-23 16:48:11 -07002120 private boolean isCustomizationDrawerVisible() {
2121 return mHomeCustomizationDrawer != null && mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
2122 }
2123
2124 private void showCustomizationDrawer() {
2125 if (isAllAppsVisible()) {
2126 // TODO: Make a smoother transition here
2127 closeAllApps(false);
2128 }
2129 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
2130 mHomeCustomizationDrawer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
2131 }
2132
2133 private void hideCustomizationDrawer() {
2134 if (isCustomizationDrawerVisible()) {
2135 Animation slideDownAnimation = AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_down);
2136 slideDownAnimation.setAnimationListener(new Animation.AnimationListener() {
2137 public void onAnimationEnd(Animation animation) {
2138 mHomeCustomizationDrawer.setVisibility(View.GONE);
2139 }
2140 public void onAnimationRepeat(Animation animation) {}
2141 public void onAnimationStart(Animation animation) {}
2142 });
2143 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
2144 }
2145 }
2146
Joe Onorato7404ee42009-07-31 11:54:44 -07002147 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 * Displays the shortcut creation dialog and launches, if necessary, the
2149 * appropriate activity.
2150 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002151 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002152 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2153 DialogInterface.OnShowListener {
2154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2161 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002162 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 builder.setInverseBackgroundForced(true);
2165
2166 AlertDialog dialog = builder.create();
2167 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002168 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002169 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 return dialog;
2172 }
2173
2174 public void onCancel(DialogInterface dialog) {
2175 mWaitingForResult = false;
2176 cleanup();
2177 }
2178
Romain Guycbb89e42009-06-08 15:52:54 -07002179 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002180 }
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002183 try {
2184 dismissDialog(DIALOG_CREATE_SHORTCUT);
2185 } catch (Exception e) {
2186 // An exception is thrown if the dialog is not visible, which is fine
2187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
2189
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002190 /**
2191 * Handle the action clicked in the "Add to home" dialog.
2192 */
2193 public void onClick(DialogInterface dialog, int which) {
2194 Resources res = getResources();
2195 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002196
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002197 switch (which) {
2198 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002199 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002200 break;
2201 }
Romain Guycbb89e42009-06-08 15:52:54 -07002202
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002203 case AddAdapter.ITEM_APPWIDGET: {
2204 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002205
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002206 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2207 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002208 // start the pick activity
2209 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2210 break;
2211 }
Romain Guycbb89e42009-06-08 15:52:54 -07002212
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002213 case AddAdapter.ITEM_LIVE_FOLDER: {
2214 // Insert extra item to handle inserting folder
2215 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002217 ArrayList<String> shortcutNames = new ArrayList<String>();
2218 shortcutNames.add(res.getString(R.string.group_folder));
2219 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002221 ArrayList<ShortcutIconResource> shortcutIcons =
2222 new ArrayList<ShortcutIconResource>();
2223 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2224 R.drawable.ic_launcher_folder));
2225 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2226
2227 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2228 pickIntent.putExtra(Intent.EXTRA_INTENT,
2229 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2230 pickIntent.putExtra(Intent.EXTRA_TITLE,
2231 getText(R.string.title_select_live_folder));
2232 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002233
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002234 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2235 break;
2236 }
2237
2238 case AddAdapter.ITEM_WALLPAPER: {
2239 startWallpaper();
2240 break;
2241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002244
2245 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002246 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249
2250 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002251 * Receives notifications when applications are added/removed.
2252 */
2253 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2254 @Override
2255 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002256 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002257 String reason = intent.getStringExtra("reason");
2258 if (!"homekey".equals(reason)) {
2259 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002260 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002261 animate = false;
2262 }
Joe Onoratob2061212009-11-24 16:13:54 -05002263 closeAllApps(animate);
2264 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002265 }
2266 }
2267
2268 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002269 * Receives notifications whenever the appwidgets are reset.
2270 */
2271 private class AppWidgetResetObserver extends ContentObserver {
2272 public AppWidgetResetObserver() {
2273 super(new Handler());
2274 }
2275
2276 @Override
2277 public void onChange(boolean selfChange) {
2278 onAppWidgetReset();
2279 }
2280 }
2281
2282 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002285 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002286 if (mWorkspace != null) {
2287 return mWorkspace.getCurrentScreen();
2288 } else {
2289 return SCREEN_COUNT / 2;
2290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002292
Joe Onorato9c1289c2009-08-17 11:03:03 -04002293 /**
2294 * Refreshes the shortcuts shown on the workspace.
2295 *
2296 * Implementation of the method from LauncherModel.Callbacks.
2297 */
2298 public void startBinding() {
2299 final Workspace workspace = mWorkspace;
2300 int count = workspace.getChildCount();
2301 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002302 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002303 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2304 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002305
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 if (DEBUG_USER_INTERFACE) {
2307 android.widget.Button finishButton = new android.widget.Button(this);
2308 finishButton.setText("Finish");
2309 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2310
2311 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2312 public void onClick(View v) {
2313 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002315 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 }
2317 }
2318
2319 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002320 * Bind the items start-end from the list.
2321 *
2322 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002324 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2325
2326 final Workspace workspace = mWorkspace;
2327
2328 for (int i=start; i<end; i++) {
2329 final ItemInfo item = shortcuts.get(i);
2330 mDesktopItems.add(item);
2331 switch (item.itemType) {
2332 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2333 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002334 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002335 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2336 false);
2337 break;
2338 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2339 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2340 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2341 (UserFolderInfo) item);
2342 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2343 false);
2344 break;
2345 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2346 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2347 R.layout.live_folder_icon, this,
2348 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2349 (LiveFolderInfo) item);
2350 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2351 false);
2352 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 }
2355
Joe Onorato9c1289c2009-08-17 11:03:03 -04002356 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 }
2358
Joe Onorato9c1289c2009-08-17 11:03:03 -04002359 /**
2360 * Implementation of the method from LauncherModel.Callbacks.
2361 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002362 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2363 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002364 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002366
2367 /**
2368 * Add the views for a widget to the workspace.
2369 *
2370 * Implementation of the method from LauncherModel.Callbacks.
2371 */
2372 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002373 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2374 if (DEBUG_WIDGETS) {
2375 Log.d(TAG, "bindAppWidget: " + item);
2376 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002377 final Workspace workspace = mWorkspace;
2378
2379 final int appWidgetId = item.appWidgetId;
2380 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002381 if (DEBUG_WIDGETS) {
2382 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2383 }
2384
Joe Onorato9c1289c2009-08-17 11:03:03 -04002385 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2386
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2388 item.hostView.setTag(item);
2389
2390 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2391 item.cellY, item.spanX, item.spanY, false);
2392
2393 workspace.requestLayout();
2394
2395 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002396
2397 if (DEBUG_WIDGETS) {
2398 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2399 + (SystemClock.uptimeMillis()-start) + "ms");
2400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 }
2402
2403 /**
2404 * Callback saying that there aren't any more items to bind.
2405 *
2406 * Implementation of the method from LauncherModel.Callbacks.
2407 */
2408 public void finishBindingItems() {
2409 if (mSavedState != null) {
2410 if (!mWorkspace.hasFocus()) {
2411 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2412 }
2413
2414 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2415 if (userFolders != null) {
2416 for (long folderId : userFolders) {
2417 final FolderInfo info = mFolders.get(folderId);
2418 if (info != null) {
2419 openFolder(info);
2420 }
2421 }
2422 final Folder openFolder = mWorkspace.getOpenFolder();
2423 if (openFolder != null) {
2424 openFolder.requestFocus();
2425 }
2426 }
2427
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 mSavedState = null;
2429 }
2430
2431 if (mSavedInstanceState != null) {
2432 super.onRestoreInstanceState(mSavedInstanceState);
2433 mSavedInstanceState = null;
2434 }
2435
Joe Onorato9c1289c2009-08-17 11:03:03 -04002436 mWorkspaceLoading = false;
2437 }
2438
2439 /**
2440 * Add the icons for all apps.
2441 *
2442 * Implementation of the method from LauncherModel.Callbacks.
2443 */
2444 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002445 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 }
2447
2448 /**
2449 * A package was installed.
2450 *
2451 * Implementation of the method from LauncherModel.Callbacks.
2452 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002453 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002455 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002456 }
2457
2458 /**
2459 * A package was updated.
2460 *
2461 * Implementation of the method from LauncherModel.Callbacks.
2462 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002463 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002464 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002465 mWorkspace.updateShortcuts(apps);
2466 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002467 }
2468
2469 /**
2470 * A package was uninstalled.
2471 *
2472 * Implementation of the method from LauncherModel.Callbacks.
2473 */
Joe Onorato36115782010-06-17 13:28:48 -04002474 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002475 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002476 if (permanent) {
2477 mWorkspace.removeItems(apps);
2478 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002479 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002480 }
Joe Onoratobe386092009-11-17 17:32:16 -08002481
2482 /**
2483 * Prints out out state for debugging.
2484 */
2485 public void dumpState() {
2486 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002487 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002488 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2489 Log.d(TAG, "mRestoring=" + mRestoring);
2490 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2491 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2492 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2493 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002494 mModel.dumpState();
2495 mAllAppsGrid.dumpState();
2496 Log.d(TAG, "END launcher2 dump state");
2497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498}