blob: 03066c1694fae24b287ca8325cb2dc74fd2c595a [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;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.IntentFilter;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070043import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040044import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080047import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080049import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080056import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
60import android.text.TextUtils;
61import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080064import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.Menu;
68import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070069import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.View;
71import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070072import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.View.OnLongClickListener;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.animation.Animation;
Michael Jurkaaf442092010-06-10 17:01:57 -070075import android.view.animation.AnimationUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070078import android.widget.ImageView;
79import android.widget.LinearLayout;
80import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070081import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.widget.TextView;
83import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Michael Jurka0e260592010-06-30 17:07:39 -070085import java.io.DataInputStream;
86import java.io.DataOutputStream;
87import java.io.FileNotFoundException;
88import java.io.IOException;
89import java.util.ArrayList;
90import java.util.HashMap;
91import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080092
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093/**
94 * Default launcher application.
95 */
Michael Jurka946ad472010-07-09 18:05:18 -070096public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -070097 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
98 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080099 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700100 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Joe Onorato9c1289c2009-08-17 11:03:03 -0400102 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400103 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400104 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int WALLPAPER_SCREENS_SPAN = 2;
107
108 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800109 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 private static final int MENU_ADD = Menu.FIRST + 1;
112 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
113 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
114 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700115 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
117 private static final int REQUEST_CREATE_SHORTCUT = 1;
118 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int REQUEST_PICK_APPLICATION = 6;
121 private static final int REQUEST_PICK_SHORTCUT = 7;
122 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700124 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
127
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800128 static final int SCREEN_COUNT = 5;
129 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700131 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato7c312c12009-08-13 21:36:53 -0700133 static final int DIALOG_CREATE_SHORTCUT = 1;
134 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Romain Guy98d01652009-06-30 16:21:04 -0700136 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 // Type: int
139 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
140 // Type: boolean
141 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
142 // Type: long
143 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
152 // Type: int
153 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
154 // Type: int
155 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
158 // Type: int[]
159 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
160 // Type: boolean
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
162 // Type: long
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
164
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800168 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800170 private final BroadcastReceiver mCloseSystemDialogsReceiver
171 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800172 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private LayoutInflater mInflater;
175
Joe Onorato00acb122009-08-04 16:04:30 -0400176 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700178
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private AppWidgetManager mAppWidgetManager;
180 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private CellLayout.CellInfo mAddItemCellInfo;
183 private CellLayout.CellInfo mMenuAddInfo;
184 private final int[] mCellCoordinates = new int[2];
185 private FolderInfo mFolderInfo;
186
Joe Onorato7c312c12009-08-13 21:36:53 -0700187 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700188 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700189 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700190 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private Bundle mSavedState;
193
194 private SpannableStringBuilder mDefaultKeySsb = null;
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 private boolean mWorkspaceLoading = true;
197
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800198 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private boolean mRestoring;
200 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
202 private Bundle mSavedInstanceState;
203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800205 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206
Romain Guy84f296c2009-11-04 15:00:44 -0800207 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
208 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
Romain Guy1fbc1c82009-11-09 20:43:08 -0800210 private ImageView mPreviousView;
211 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500212
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400213 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400214 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400215 private String[] mHotseatConfig = null;
216 private Intent[] mHotseats = null;
217 private Drawable[] mHotseatIcons = null;
218 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400219
Winson Chungaafa03c2010-06-11 17:34:16 -0700220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 @Override
222 protected void onCreate(Bundle savedInstanceState) {
223 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700224
Winson Chungaafa03c2010-06-11 17:34:16 -0700225 if (LauncherApplication.isInPlaceRotationEnabled()) {
226 // hide the status bar (temporary until we get the status bar design figured out)
227 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
228 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
229 }
230
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 LauncherApplication app = ((LauncherApplication)getApplication());
232 mModel = app.setLauncher(this);
233 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400234 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700236
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
239 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 if (PROFILE_STARTUP) {
242 android.os.Debug.startMethodTracing("/sdcard/launcher");
243 }
244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 checkForLocaleChange();
247 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700249 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
250 if (mHomeCustomizationDrawer != null) {
251 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700252
Michael Jurka946ad472010-07-09 18:05:18 -0700253 String widgetsLabel = getString(R.string.widgets_tab_label);
254 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
255 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
256 String foldersLabel = getString(R.string.folders_tab_label);
257 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
258 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
259 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
260 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
261 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
262 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
263 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
264 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
265
266 mHomeCustomizationDrawer.setCurrentTab(0);
267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 setupViews();
269
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800270 registerContentObservers();
271
Joe Onorato7c312c12009-08-13 21:36:53 -0700272 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700273
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 mSavedState = savedInstanceState;
275 restoreState(mSavedState);
276
277 if (PROFILE_STARTUP) {
278 android.os.Debug.stopMethodTracing();
279 }
280
281 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
285 // For handling default keys
286 mDefaultKeySsb = new SpannableStringBuilder();
287 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800288
289 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
290 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 }
Romain Guycbb89e42009-06-08 15:52:54 -0700292
Winson Chungaafa03c2010-06-11 17:34:16 -0700293 @Override
294 public void onConfigurationChanged(Configuration newConfig) {
295 // TODO Auto-generated method stub
296 super.onConfigurationChanged(newConfig);
297
298 if (LauncherApplication.isInPlaceRotationEnabled()) {
299 mModel.updateOrientation();
300 mWorkspace.refreshWorkspaceChildren();
301 mWorkspace.rotateCurrentScreensChildren();
302 }
303 }
304
305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700307 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
308 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 final Configuration configuration = getResources().getConfiguration();
311
Romain Guy98d01652009-06-30 16:21:04 -0700312 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 final String locale = configuration.locale.toString();
314
Romain Guy98d01652009-06-30 16:21:04 -0700315 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 final int mcc = configuration.mcc;
317
Romain Guy98d01652009-06-30 16:21:04 -0700318 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 final int mnc = configuration.mnc;
320
Romain Guy84f296c2009-11-04 15:00:44 -0800321 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322
Romain Guy84f296c2009-11-04 15:00:44 -0800323 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700324 localeConfiguration.locale = locale;
325 localeConfiguration.mcc = mcc;
326 localeConfiguration.mnc = mnc;
327
328 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800329 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400330
331 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700332 }
333 }
334
335 private static class LocaleConfiguration {
336 public String locale;
337 public int mcc = -1;
338 public int mnc = -1;
339 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700340
Romain Guy98d01652009-06-30 16:21:04 -0700341 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
342 DataInputStream in = null;
343 try {
344 in = new DataInputStream(context.openFileInput(PREFERENCES));
345 configuration.locale = in.readUTF();
346 configuration.mcc = in.readInt();
347 configuration.mnc = in.readInt();
348 } catch (FileNotFoundException e) {
349 // Ignore
350 } catch (IOException e) {
351 // Ignore
352 } finally {
353 if (in != null) {
354 try {
355 in.close();
356 } catch (IOException e) {
357 // Ignore
358 }
359 }
360 }
361 }
362
363 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
364 DataOutputStream out = null;
365 try {
366 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
367 out.writeUTF(configuration.locale);
368 out.writeInt(configuration.mcc);
369 out.writeInt(configuration.mnc);
370 out.flush();
371 } catch (FileNotFoundException e) {
372 // Ignore
373 } catch (IOException e) {
374 //noinspection ResultOfMethodCallIgnored
375 context.getFileStreamPath(PREFERENCES).delete();
376 } finally {
377 if (out != null) {
378 try {
379 out.close();
380 } catch (IOException e) {
381 // Ignore
382 }
383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385 }
386
387 static int getScreen() {
388 synchronized (sLock) {
389 return sScreen;
390 }
391 }
392
393 static void setScreen(int screen) {
394 synchronized (sLock) {
395 sScreen = screen;
396 }
397 }
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700400 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
402 Display display = getWindowManager().getDefaultDisplay();
403 boolean isPortrait = display.getWidth() < display.getHeight();
404
405 final int width = isPortrait ? display.getWidth() : display.getHeight();
406 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700407 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400410 // Note: This doesn't do all the client-id magic that BrowserProvider does
411 // in Browser. (http://b/2425179)
412 private Uri getDefaultBrowserUri() {
413 String url = getString(R.string.default_browser_url);
414 if (url.indexOf("{CID}") != -1) {
415 url = url.replace("{CID}", "android-google");
416 }
417 return Uri.parse(url);
418 }
419
420 // Load the Intent templates from arrays.xml to populate the hotseats. For
421 // each Intent, if it resolves to a single app, use that as the launch
422 // intent & use that app's label as the contentDescription. Otherwise,
423 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400424 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400425 if (mHotseatConfig == null) {
426 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
427 if (mHotseatConfig.length > 0) {
428 mHotseats = new Intent[mHotseatConfig.length];
429 mHotseatLabels = new CharSequence[mHotseatConfig.length];
430 mHotseatIcons = new Drawable[mHotseatConfig.length];
431 } else {
432 mHotseats = null;
433 mHotseatIcons = null;
434 mHotseatLabels = null;
435 }
436
437 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
438 for (int i=0; i<mHotseatConfig.length; i++) {
439 // load icon for this slot; currently unrelated to the actual activity
440 try {
441 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
442 } catch (ArrayIndexOutOfBoundsException ex) {
443 Log.w(TAG, "Missing hotseat_icons array item #" + i);
444 mHotseatIcons[i] = null;
445 }
446 }
447 hotseatIconDrawables.recycle();
448 }
449
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400450 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400451 for (int i=0; i<mHotseatConfig.length; i++) {
452 Intent intent = null;
453 if (mHotseatConfig[i].equals("*BROWSER*")) {
454 // magic value meaning "launch user's default web browser"
455 // replace it with a generic web request so we can see if there is indeed a default
456 String defaultUri = getString(R.string.default_browser_url);
457 intent = new Intent(
458 Intent.ACTION_VIEW,
459 ((defaultUri != null)
460 ? Uri.parse(defaultUri)
461 : getDefaultBrowserUri())
462 ).addCategory(Intent.CATEGORY_BROWSABLE);
463 // note: if the user launches this without a default set, she
464 // will always be taken to the default URL above; this is
465 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700466 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400467 // URL is unavoidably sent to the chosen app.
468 } else {
469 try {
470 intent = Intent.parseUri(mHotseatConfig[i], 0);
471 } catch (java.net.URISyntaxException ex) {
472 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
473 // bogus; leave intent=null
474 }
475 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700476
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400477 if (intent == null) {
478 mHotseats[i] = null;
479 mHotseatLabels[i] = getText(R.string.activity_not_found);
480 continue;
481 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400482
483 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700484 Log.d(TAG, "loadHotseats: hotseat " + i
485 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400486 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400487 + "]");
488 }
489
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400490 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
491 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700492 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400493 Log.d(TAG, "Best match for intent: " + bestMatch);
494 Log.d(TAG, "All matches: ");
495 for (ResolveInfo ri : allMatches) {
496 Log.d(TAG, " --> " + ri);
497 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400498 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400499 // did this resolve to a single app, or the resolver?
500 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700501 // can't find any activity to handle this. let's leave the
502 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400503 // to launch.
504 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400505
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400506 // set accessibility text to "Not installed"
507 mHotseatLabels[i] = getText(R.string.activity_not_found);
508 } else {
509 boolean found = false;
510 for (ResolveInfo ri : allMatches) {
511 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
512 && bestMatch.activityInfo.applicationInfo.packageName
513 .equals(ri.activityInfo.applicationInfo.packageName)) {
514 found = true;
515 break;
516 }
517 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700518
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400519 if (!found) {
520 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
521 // the bestMatch is probably the ResolveActivity, meaning the
522 // user has not yet selected a default
523 // so: we'll keep the original intent for now
524 mHotseats[i] = intent;
525
526 // set the accessibility text to "Select shortcut"
527 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
528 } else {
529 // we have an app!
530 // now reconstruct the intent to launch it through the front
531 // door
532 ComponentName com = new ComponentName(
533 bestMatch.activityInfo.applicationInfo.packageName,
534 bestMatch.activityInfo.name);
535 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
536
537 // load the app label for accessibility
538 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
539 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400540 }
541
542 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700543 Log.d(TAG, "loadHotseats: hotseat " + i
544 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 + ((mHotseats[i] == null)
546 ? "null"
547 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400548 + "] label=[" + mHotseatLabels[i]
549 + "]"
550 );
551 }
552 }
553 }
554
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 @Override
556 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700557 mWaitingForResult = false;
558
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 // The pattern used here is that a user PICKs a specific application,
560 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
563 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700564
Romain Guy94dabf12009-07-21 10:55:43 -0700565 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 switch (requestCode) {
567 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 break;
570 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800571 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 break;
573 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 break;
576 case REQUEST_PICK_LIVE_FOLDER:
577 addLiveFolder(data);
578 break;
579 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400580 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700582 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700583 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700585 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700586 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700587 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700589 case REQUEST_PICK_WALLPAPER:
590 // We just wanted the activity result here so we can clear mWaitingForResult
591 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
Romain Guy18042c82009-11-06 11:44:55 -0800593 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
594 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
595 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700596 // Clean up the appWidgetId if we canceled
597 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
598 if (appWidgetId != -1) {
599 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
603
604 @Override
605 protected void onResume() {
606 super.onResume();
607
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800608 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400611 mWorkspaceLoading = true;
612 mModel.startLoader(this, true);
613 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615 }
616
617 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700618 protected void onPause() {
619 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700620 // Some launcher layouts don't have a previous and next view
621 if (mPreviousView != null) {
622 dismissPreview(mPreviousView);
623 dismissPreview(mNextView);
624 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800625 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700626 }
Romain Guycbb89e42009-06-08 15:52:54 -0700627
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700628 @Override
629 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 // Flag the loader to stop early before switching
631 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800632 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800633 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700634 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700635
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800636 // We can't hide the IME if it was forced open. So don't bother
637 /*
638 @Override
639 public void onWindowFocusChanged(boolean hasFocus) {
640 super.onWindowFocusChanged(hasFocus);
641
642 if (hasFocus) {
643 final InputMethodManager inputManager = (InputMethodManager)
644 getSystemService(Context.INPUT_METHOD_SERVICE);
645 WindowManager.LayoutParams lp = getWindow().getAttributes();
646 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
647 android.os.Handler()) {
648 protected void onReceiveResult(int resultCode, Bundle resultData) {
649 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
650 }
651 });
652 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
653 }
654 }
655 */
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 private boolean acceptFilter() {
658 final InputMethodManager inputManager = (InputMethodManager)
659 getSystemService(Context.INPUT_METHOD_SERVICE);
660 return !inputManager.isFullscreenMode();
661 }
662
663 @Override
664 public boolean onKeyDown(int keyCode, KeyEvent event) {
665 boolean handled = super.onKeyDown(keyCode, event);
666 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
667 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
668 keyCode, event);
669 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700670 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700671 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700672 // showSearchDialog()
673 // If there are multiple keystrokes before the search dialog takes focus,
674 // onSearchRequested() will be called for every keystroke,
675 // but it is idempotent, so it's fine.
676 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678 }
679
Joe Onorato8a9625e2010-01-28 15:55:35 -0800680 // Eat the long press event so the keyboard doesn't come up.
681 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
682 return true;
683 }
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 return handled;
686 }
687
Karl Rosaen138a0412009-04-23 19:00:21 -0700688 private String getTypedText() {
689 return mDefaultKeySsb.toString();
690 }
691
692 private void clearTypedText() {
693 mDefaultKeySsb.clear();
694 mDefaultKeySsb.clearSpans();
695 Selection.setSelection(mDefaultKeySsb, 0);
696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 /**
699 * Restores the previous state, if it exists.
700 *
701 * @param savedState The previous state.
702 */
703 private void restoreState(Bundle savedState) {
704 if (savedState == null) {
705 return;
706 }
707
Joe Onorato3a8820b2009-11-10 15:06:42 -0800708 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
709 if (allApps) {
710 showAllApps(false);
711 }
712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
714 if (currentScreen > -1) {
715 mWorkspace.setCurrentScreen(currentScreen);
716 }
717
718 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
719 if (addScreen > -1) {
720 mAddItemCellInfo = new CellLayout.CellInfo();
721 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
722 addItemCellInfo.valid = true;
723 addItemCellInfo.screen = addScreen;
724 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
725 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
726 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
727 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
728 addItemCellInfo.findVacantCellsFromOccupied(
729 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
730 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
731 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
732 mRestoring = true;
733 }
734
735 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
736 if (renameFolder) {
737 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 mRestoring = true;
740 }
741 }
742
743 /**
744 * Finds all the views we need and configure them properly.
745 */
746 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400747 DragController dragController = mDragController;
748
Romain Guyb1b69f52009-08-10 15:10:15 -0700749 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400750 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751
Joe Onorato7c312c12009-08-13 21:36:53 -0700752 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700753 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700754 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800755 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700756 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700757 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
760 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500761 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700762
763 // We only intercept touch events to dismiss the home customization drawer; if it doesn't
764 // exist, then no need to do this
765 if (mHomeCustomizationDrawer != null)
766 workspace.setOnInterceptTouchListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767
Joe Onorato7c312c12009-08-13 21:36:53 -0700768 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
769 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770
Joe Onorato7c312c12009-08-13 21:36:53 -0700771 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700772 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700773 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800774 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800775
Michael Jurka0e260592010-06-30 17:07:39 -0700776 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
777 if (widgetChooser != null) {
778 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
779 widgetChooser.setAdapter(widgetGalleryAdapter);
780 widgetChooser.setDragController(dragController);
781 widgetChooser.setLauncher(this);
782
783 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
784 IntentListAdapter folderTypes = new FolderListAdapter(this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
785 folderChooser.setAdapter(folderTypes);
786 folderChooser.setLauncher(this);
787
788 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
789 IntentListAdapter shortcutTypes = new ShortcutListAdapter(this, Intent.ACTION_CREATE_SHORTCUT);
790 shortcutChooser.setAdapter(shortcutTypes);
791 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700792 } else {
793 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
794 hotseatLeft.setContentDescription(mHotseatLabels[0]);
795 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
796 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
797 hotseatRight.setContentDescription(mHotseatLabels[1]);
798 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400799
Michael Jurkaaf442092010-06-10 17:01:57 -0700800 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
801 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800802
Michael Jurkaaf442092010-06-10 17:01:57 -0700803 Drawable previous = mPreviousView.getDrawable();
804 Drawable next = mNextView.getDrawable();
805 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806
Michael Jurkaaf442092010-06-10 17:01:57 -0700807 mPreviousView.setHapticFeedbackEnabled(false);
808 mPreviousView.setOnLongClickListener(this);
809 mNextView.setHapticFeedbackEnabled(false);
810 mNextView.setOnLongClickListener(this);
811 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400814 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816
817 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400818 deleteZone.setDragController(dragController);
Winson Chungaafa03c2010-06-11 17:34:16 -0700819 int deleteZoneHandleId = LauncherApplication.isScreenXLarge() ? R.id.add_button : R.id.all_apps_button_cluster;
Michael Jurkaaf442092010-06-10 17:01:57 -0700820 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
Joe Onorato00acb122009-08-04 16:04:30 -0400822 dragController.setDragScoller(workspace);
823 dragController.setDragListener(deleteZone);
824 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800825 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700826
Joe Onorato00acb122009-08-04 16:04:30 -0400827 // The order here is bottom to top.
828 dragController.addDropTarget(workspace);
829 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
Romain Guy8a73c512009-11-09 19:19:59 -0800832 @SuppressWarnings({"UnusedDeclaration"})
833 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800834 if (!isAllAppsVisible()) {
835 mWorkspace.scrollLeft();
836 }
Romain Guy8a73c512009-11-09 19:19:59 -0800837 }
838
839 @SuppressWarnings({"UnusedDeclaration"})
840 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800841 if (!isAllAppsVisible()) {
842 mWorkspace.scrollRight();
843 }
Romain Guy8a73c512009-11-09 19:19:59 -0800844 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400845
846 @SuppressWarnings({"UnusedDeclaration"})
847 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400848 if (isAllAppsVisible()) return;
849
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400850 int index = -1;
851 if (v.getId() == R.id.hotseat_left) {
852 index = 0;
853 } else if (v.getId() == R.id.hotseat_right) {
854 index = 1;
855 }
856
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400857 // reload these every tap; you never know when they might change
858 loadHotseats();
859 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
860 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400861 startActivitySafely(
862 mHotseats[index],
863 "hotseat"
864 );
865 }
866 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 /**
869 * Creates a view representing a shortcut.
870 *
871 * @param info The data structure describing the shortcut.
872 *
873 * @return A View inflated from R.layout.application.
874 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800875 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 return createShortcut(R.layout.application,
877 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
878 }
879
880 /**
881 * Creates a view representing a shortcut inflated from the specified resource.
882 *
883 * @param layoutResId The id of the XML layout used to create the shortcut.
884 * @param parent The group the shortcut belongs to.
885 * @param info The data structure describing the shortcut.
886 *
887 * @return A View inflated from layoutResId.
888 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800889 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
891
Joe Onorato0589f0f2010-02-08 13:44:00 -0800892 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
893 new FastBitmapDrawable(info.getIcon(mIconCache)),
894 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 favorite.setText(info.title);
896 favorite.setTag(info);
897 favorite.setOnClickListener(this);
898
899 return favorite;
900 }
901
902 /**
903 * Add an application shortcut to the workspace.
904 *
905 * @param data The intent describing the application.
906 * @param cellInfo The position on screen where to create the shortcut.
907 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 cellInfo.screen = mWorkspace.getCurrentScreen();
910 if (!findSingleSlot(cellInfo)) return;
911
Joe Onorato0589f0f2010-02-08 13:44:00 -0800912 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
913 data, context);
914
Romain Guy73b979d2009-06-09 12:57:21 -0700915 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800916 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800918 info.container = ItemInfo.NO_ID;
919 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
920 } else {
921 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923 }
Romain Guycbb89e42009-06-08 15:52:54 -0700924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 /**
926 * Add a shortcut to the workspace.
927 *
928 * @param data The intent describing the shortcut.
929 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 cellInfo.screen = mWorkspace.getCurrentScreen();
933 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700934
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
937 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
940 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942 }
943
Romain Guycbb89e42009-06-08 15:52:54 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700946 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700948 * @param data The intent describing the appWidgetId.
949 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700951 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700952 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700953
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700954 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700957
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 // Try finding open space on Launcher screen
959 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800960 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
961 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
962 return;
963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700965 // Build Launcher-specific widget info and save to database
966 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 launcherInfo.spanX = spans[0];
968 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 LauncherModel.addItemToDatabase(this, launcherInfo,
971 LauncherSettings.Favorites.CONTAINER_DESKTOP,
972 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
973
974 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700978 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700979
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700980 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986 }
Romain Guycbb89e42009-06-08 15:52:54 -0700987
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
989 mDesktopItems.remove(launcherInfo);
990 launcherInfo.hostView = null;
991 }
992
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 public LauncherAppWidgetHost getAppWidgetHost() {
994 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
Romain Guycbb89e42009-06-08 15:52:54 -0700996
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800997 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800998 getWindow().closeAllPanels();
999
1000 try {
1001 dismissDialog(DIALOG_CREATE_SHORTCUT);
1002 // Unlock the workspace if the dialog was showing
1003 } catch (Exception e) {
1004 // An exception is thrown if the dialog is not visible, which is fine
1005 }
1006
1007 try {
1008 dismissDialog(DIALOG_RENAME_FOLDER);
1009 // Unlock the workspace if the dialog was showing
1010 } catch (Exception e) {
1011 // An exception is thrown if the dialog is not visible, which is fine
1012 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001013
1014 // Whatever we were doing is hereby canceled.
1015 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 @Override
1019 protected void onNewIntent(Intent intent) {
1020 super.onNewIntent(intent);
1021
1022 // Close the menu
1023 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001024 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001025 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001026
Joe Onorato14f122b2009-11-19 14:06:36 -08001027 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1028 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1029 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001030 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001031 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001032 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001033 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -07001034
Joe Onorato3a8820b2009-11-10 15:06:42 -08001035 final View v = getWindow().peekDecorView();
1036 if (v != null && v.getWindowToken() != null) {
1037 InputMethodManager imm = (InputMethodManager)getSystemService(
1038 INPUT_METHOD_SERVICE);
1039 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
1041 }
1042 }
1043
1044 @Override
1045 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1046 // Do not call super here
1047 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001048
1049 if (mHomeCustomizationDrawer != null) {
1050 String cur = savedInstanceState.getString("currentTab");
1051 if (cur != null) {
1052 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1053 }
1054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056
1057 @Override
1058 protected void onSaveInstanceState(Bundle outState) {
1059 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1060
1061 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1062 if (folders.size() > 0) {
1063 final int count = folders.size();
1064 long[] ids = new long[count];
1065 for (int i = 0; i < count; i++) {
1066 final FolderInfo info = folders.get(i).getInfo();
1067 ids[i] = info.id;
1068 }
1069 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1070 } else {
1071 super.onSaveInstanceState(outState);
1072 }
1073
Joe Onoratofb0ca672009-09-14 17:55:46 -04001074 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001075 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078
1079 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1080 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1081 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1082
1083 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1084 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1085 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1086 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1087 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1088 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1089 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1090 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1091 layout.getOccupiedCells());
1092 }
1093
1094 if (mFolderInfo != null && mWaitingForResult) {
1095 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1096 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1097 }
Michael Jurka946ad472010-07-09 18:05:18 -07001098
1099 if (mHomeCustomizationDrawer != null) {
1100 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1101 if (currentTabTag != null) {
1102 outState.putString("currentTab", currentTabTag);
1103 }
1104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 }
1106
1107 @Override
1108 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001114 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116
1117 TextKeyListener.getInstance().release();
1118
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001122
1123 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001124
Romain Guy1fbc1c82009-11-09 20:43:08 -08001125 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001126 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001127
1128 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
1131 @Override
1132 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001133 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 super.startActivityForResult(intent, requestCode);
1135 }
1136
1137 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001138 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001140
Joe Onorato7bb17492009-09-24 17:51:01 -07001141 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001142
Karl Rosaen138a0412009-04-23 19:00:21 -07001143 if (initialQuery == null) {
1144 // Use any text typed in the launcher as the initial query
1145 initialQuery = getTypedText();
1146 clearTypedText();
1147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 if (appSearchData == null) {
1149 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001150 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Karl Rosaen138a0412009-04-23 19:00:21 -07001153 final SearchManager searchManager =
1154 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001155 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001156 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
1158
1159 @Override
1160 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 if (isWorkspaceLocked()) {
1162 return false;
1163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164
1165 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1168 .setIcon(android.R.drawable.ic_menu_add)
1169 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001170 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 .setIcon(android.R.drawable.ic_menu_gallery)
1172 .setAlphabeticShortcut('W');
1173 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1174 .setIcon(android.R.drawable.ic_search_category_default)
1175 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1176 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1177 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1178 .setAlphabeticShortcut('N');
1179
1180 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001181 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1182 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
1184 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1185 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1186 .setIntent(settings);
1187
1188 return true;
1189 }
1190
1191 @Override
1192 public boolean onPrepareOptionsMenu(Menu menu) {
1193 super.onPrepareOptionsMenu(menu);
1194
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 // If all apps is animating, don't show the menu, because we don't know
1196 // which one to show.
1197 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1198 return false;
1199 }
1200
1201 // Only show the add and wallpaper options when we're not in all apps.
1202 boolean visible = !mAllAppsGrid.isOpaque();
1203 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1204 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1205
1206 // Disable add if the workspace is full.
1207 if (visible) {
1208 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1209 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211
1212 return true;
1213 }
1214
Michael Jurka0e260592010-06-30 17:07:39 -07001215 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1216 // using the settings menu to add an item, something similar happens in showAddDialog
1217 public void prepareAddItemFromHomeCustomizationDrawer() {
1218 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1219 mAddItemCellInfo = mMenuAddInfo;
1220 }
1221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 @Override
1223 public boolean onOptionsItemSelected(MenuItem item) {
1224 switch (item.getItemId()) {
1225 case MENU_ADD:
1226 addItems();
1227 return true;
1228 case MENU_WALLPAPER_SETTINGS:
1229 startWallpaper();
1230 return true;
1231 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001232 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 return true;
1234 case MENU_NOTIFICATIONS:
1235 showNotifications();
1236 return true;
1237 }
1238
1239 return super.onOptionsItemSelected(item);
1240 }
Romain Guycbb89e42009-06-08 15:52:54 -07001241
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 /**
1243 * Indicates that we want global search for this activity by setting the globalSearch
1244 * argument for {@link #startSearch} to true.
1245 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001247 @Override
1248 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001249 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001250 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001251 }
1252
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 public boolean isWorkspaceLocked() {
1254 return mWorkspaceLoading || mWaitingForResult;
1255 }
1256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001258 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 showAddDialog(mMenuAddInfo);
1260 }
1261
Michael Jurkaaf442092010-06-10 17:01:57 -07001262 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1263 mAddItemCellInfo = cellInfo;
1264 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1265 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1266 addAppWidgetImpl(appWidgetId);
1267 }
1268
1269 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001270 // TODO: catch bad widget exception when sent
1271 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001272 // TODO: Is this log message meaningful?
1273 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1274 addAppWidgetImpl(appWidgetId);
1275 }
1276
1277 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001278 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279
Bjorn Bringert7984c942009-12-09 15:38:25 +00001280 if (appWidget.configure != null) {
1281 // Launch over to configure widget, if needed
1282 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1283 intent.setComponent(appWidget.configure);
1284 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1285
Romain Guy8e633c52010-03-04 12:51:36 -08001286 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001288 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001289 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
1291 }
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Joe Onoratodeb98af2010-02-19 14:59:39 -08001293 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001294 // Handle case where user selected "Applications"
1295 String applicationName = getResources().getString(R.string.group_applications);
1296 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001297
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001298 if (applicationName != null && applicationName.equals(shortcutName)) {
1299 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1300 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001301
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001302 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1303 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001304 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001305 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001306 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001311 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001312 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001313 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001315 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001317 } else {
1318 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 UserFolderInfo folderInfo = new UserFolderInfo();
1324 folderInfo.title = getText(R.string.folder_name);
1325
1326 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1327 cellInfo.screen = mWorkspace.getCurrentScreen();
1328 if (!findSingleSlot(cellInfo)) return;
1329
1330 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 LauncherModel.addItemToDatabase(this, folderInfo,
1332 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001334 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
1336 // Create the view
1337 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1338 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1339 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001340 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
Romain Guycbb89e42009-06-08 15:52:54 -07001342
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343 void removeFolder(FolderInfo folder) {
1344 mFolders.remove(folder.id);
1345 }
1346
1347 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 cellInfo.screen = mWorkspace.getCurrentScreen();
1349 if (!findSingleSlot(cellInfo)) return;
1350
1351 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1352
1353 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1355 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1357 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359 }
1360
1361 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1362 CellLayout.CellInfo cellInfo, boolean notify) {
1363
1364 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1365 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1366
1367 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 Intent.ShortcutIconResource iconResource = null;
1369
1370 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1371 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1372 try {
1373 iconResource = (Intent.ShortcutIconResource) extra;
1374 final PackageManager packageManager = context.getPackageManager();
1375 Resources resources = packageManager.getResourcesForApplication(
1376 iconResource.packageName);
1377 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1378 icon = resources.getDrawable(id);
1379 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001380 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382 }
1383
1384 if (icon == null) {
1385 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1386 }
1387
1388 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001389 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 info.title = name;
1391 info.iconResource = iconResource;
1392 info.uri = data.getData();
1393 info.baseIntent = baseIntent;
1394 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1395 LiveFolders.DISPLAY_MODE_GRID);
1396
1397 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1398 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001399 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400
1401 return info;
1402 }
1403
1404 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1405 final int[] xy = new int[2];
1406 if (findSlot(cellInfo, xy, 1, 1)) {
1407 cellInfo.cellX = xy[0];
1408 cellInfo.cellY = xy[1];
1409 return true;
1410 }
1411 return false;
1412 }
1413
1414 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1415 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1416 boolean[] occupied = mSavedState != null ?
1417 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1418 cellInfo = mWorkspace.findAllVacantCells(occupied);
1419 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1420 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1421 return false;
1422 }
1423 }
1424 return true;
1425 }
1426
1427 private void showNotifications() {
1428 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1429 if (statusBar != null) {
1430 statusBar.expand();
1431 }
1432 }
1433
1434 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001435 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001437 Intent chooser = Intent.createChooser(pickWallpaper,
1438 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001439 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1440 // Removed in Eclair MR1
1441// WallpaperManager wm = (WallpaperManager)
1442// getSystemService(Context.WALLPAPER_SERVICE);
1443// WallpaperInfo wi = wm.getWallpaperInfo();
1444// if (wi != null && wi.getSettingsActivity() != null) {
1445// LabeledIntent li = new LabeledIntent(getPackageName(),
1446// R.string.configure_wallpaper, 0);
1447// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1448// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1449// }
Mike Clerona0618e42009-10-22 13:55:21 -07001450 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001453 /**
1454 * Registers various content observers. The current implementation registers
1455 * only a favorites observer to keep track of the favorites applications.
1456 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001457 private void registerContentObservers() {
1458 ContentResolver resolver = getContentResolver();
1459 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1460 true, mWidgetObserver);
1461 }
1462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 @Override
1464 public boolean dispatchKeyEvent(KeyEvent event) {
1465 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1466 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001468 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001469 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001470 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001471 dumpState();
1472 return true;
1473 }
1474 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001475 }
1476 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1477 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001478 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 return true;
1480 }
1481 }
1482
1483 return super.dispatchKeyEvent(event);
1484 }
1485
Joe Onorato88ec0992009-11-19 13:16:06 -08001486 @Override
1487 public void onBackPressed() {
1488 if (isAllAppsVisible()) {
1489 closeAllApps(true);
1490 } else {
1491 closeFolder();
1492 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001493 // Some launcher layouts don't have a previous and next view
1494 if (mPreviousView != null) {
1495 dismissPreview(mPreviousView);
1496 dismissPreview(mNextView);
1497 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001498 }
1499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 private void closeFolder() {
1501 Folder folder = mWorkspace.getOpenFolder();
1502 if (folder != null) {
1503 closeFolder(folder);
1504 }
1505 }
1506
1507 void closeFolder(Folder folder) {
1508 folder.getInfo().opened = false;
1509 ViewGroup parent = (ViewGroup) folder.getParent();
1510 if (parent != null) {
1511 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001512 if (folder instanceof DropTarget) {
1513 // Live folders aren't DropTargets.
1514 mDragController.removeDropTarget((DropTarget)folder);
1515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517 folder.onClose();
1518 }
1519
1520 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001521 * Re-listen when widgets are reset.
1522 */
1523 private void onAppWidgetReset() {
1524 mAppWidgetHost.startListening();
1525 }
1526
1527 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1529 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001531 private void unbindDesktopItems() {
1532 for (ItemInfo item: mDesktopItems) {
1533 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 /**
1538 * Launches the intent referred by the clicked shortcut.
1539 *
1540 * @param v The view representing the clicked shortcut.
1541 */
1542 public void onClick(View v) {
1543 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001544 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001546 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001547 int[] pos = new int[2];
1548 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001549 intent.setSourceBounds(new Rect(pos[0], pos[1],
1550 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001551 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 } else if (tag instanceof FolderInfo) {
1553 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001554 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001555 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001556 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001557 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001558 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561 }
1562
Michael Jurka0e260592010-06-30 17:07:39 -07001563 private final class SlideDownFinishedListener implements Animation.AnimationListener {
1564 TabHost mHomeCustomizationDrawer;
1565 SlideDownFinishedListener(TabHost homeCustomizationDrawer) {
1566 mHomeCustomizationDrawer = homeCustomizationDrawer;
1567 }
1568 public void onAnimationEnd(Animation animation) {
1569 mHomeCustomizationDrawer.setVisibility(View.GONE);
1570 }
1571 public void onAnimationRepeat(Animation animation) {}
1572 public void onAnimationStart(Animation animation) {}
1573 }
1574
1575 public boolean onTouch(View v, MotionEvent event) {
1576 // this is being forwarded from mWorkspace;
1577 // clicking anywhere on the workspace causes the drawer to slide down
Michael Jurka946ad472010-07-09 18:05:18 -07001578 if (mHomeCustomizationDrawer != null && mHomeCustomizationDrawer.getVisibility() == View.VISIBLE) {
Michael Jurka0e260592010-06-30 17:07:39 -07001579 Animation slideDownAnimation = AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_down);
1580 slideDownAnimation.setAnimationListener(new SlideDownFinishedListener(mHomeCustomizationDrawer));
1581 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
1582 }
1583 return false;
1584 }
1585
Michael Jurkaaf442092010-06-10 17:01:57 -07001586 /**
1587 * Event handler for the "plus" button that appears on the home screen, which
1588 * enters home screen customization mode.
1589 *
1590 * @param v The view that was clicked.
1591 */
1592 public void onClickAddButton(View v) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001593
1594 // Animate the widget chooser up from the bottom of the screen
Michael Jurka946ad472010-07-09 18:05:18 -07001595 if (mHomeCustomizationDrawer != null && mHomeCustomizationDrawer.getVisibility() == View.GONE) {
Michael Jurka0e260592010-06-30 17:07:39 -07001596 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
1597 mHomeCustomizationDrawer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
1598 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001599 }
1600
1601 public void onClickAllAppsButton(View w) {
1602 showAllApps(true);
1603 }
1604
Joe Onoratof984e852010-03-25 09:47:45 -07001605 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1607 try {
1608 startActivity(intent);
1609 } catch (ActivityNotFoundException e) {
1610 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001611 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 } catch (SecurityException e) {
1613 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001614 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001616 "or use the exported attribute for this activity. "
1617 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 }
1619 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001620
Romain Guy8e633c52010-03-04 12:51:36 -08001621 void startActivityForResultSafely(Intent intent, int requestCode) {
1622 try {
1623 startActivityForResult(intent, requestCode);
1624 } catch (ActivityNotFoundException e) {
1625 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1626 } catch (SecurityException e) {
1627 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1628 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1629 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1630 "or use the exported attribute for this activity.", e);
1631 }
1632 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633
1634 private void handleFolderClick(FolderInfo folderInfo) {
1635 if (!folderInfo.opened) {
1636 // Close any open folder
1637 closeFolder();
1638 // Open the requested folder
1639 openFolder(folderInfo);
1640 } else {
1641 // Find the open folder...
1642 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1643 int folderScreen;
1644 if (openFolder != null) {
1645 folderScreen = mWorkspace.getScreenForView(openFolder);
1646 // .. and close it
1647 closeFolder(openFolder);
1648 if (folderScreen != mWorkspace.getCurrentScreen()) {
1649 // Close any folder open on the current screen
1650 closeFolder();
1651 // Pull the folder onto this screen
1652 openFolder(folderInfo);
1653 }
1654 }
1655 }
1656 }
1657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 /**
1659 * Opens the user fodler described by the specified tag. The opening of the folder
1660 * is animated relative to the specified View. If the View is null, no animation
1661 * is played.
1662 *
1663 * @param folderInfo The FolderInfo describing the folder to open.
1664 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001665 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 Folder openFolder;
1667
1668 if (folderInfo instanceof UserFolderInfo) {
1669 openFolder = UserFolder.fromXml(this);
1670 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001671 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 } else {
1673 return;
1674 }
1675
Joe Onorato00acb122009-08-04 16:04:30 -04001676 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 openFolder.setLauncher(this);
1678
1679 openFolder.bind(folderInfo);
1680 folderInfo.opened = true;
1681
Winson Chungaafa03c2010-06-11 17:34:16 -07001682 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 openFolder.onOpen();
1685 }
1686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001688 switch (v.getId()) {
1689 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001690 if (!isAllAppsVisible()) {
1691 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1692 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001693 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001694 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001695 return true;
1696 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001697 if (!isAllAppsVisible()) {
1698 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1699 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001700 showPreviews(v);
1701 }
1702 return true;
1703 case R.id.all_apps_button:
1704 if (!isAllAppsVisible()) {
1705 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1706 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1707 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001708 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001709 return true;
1710 }
1711
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 return false;
1714 }
1715
1716 if (!(v instanceof CellLayout)) {
1717 v = (View) v.getParent();
1718 }
1719
1720 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1721
1722 // This happens when long clicking an item with the dpad/trackball
1723 if (cellInfo == null) {
1724 return true;
1725 }
1726
1727 if (mWorkspace.allowLongPress()) {
1728 if (cellInfo.cell == null) {
1729 if (cellInfo.valid) {
1730 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001731 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001732 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1733 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 showAddDialog(cellInfo);
1735 }
1736 } else {
1737 if (!(cellInfo.cell instanceof Folder)) {
1738 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001739 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1740 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 mWorkspace.startDrag(cellInfo);
1742 }
1743 }
1744 }
1745 return true;
1746 }
1747
Romain Guye6b8e2f2009-11-10 11:56:55 -08001748 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001749 private void dismissPreview(final View v) {
1750 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001751 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001752 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1753 public void onDismiss() {
1754 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1755 int count = group.getChildCount();
1756 for (int i = 0; i < count; i++) {
1757 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1758 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001759 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1760 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1761
1762 v.setTag(R.id.workspace, null);
1763 v.setTag(R.id.icon, null);
1764 window.setOnDismissListener(null);
1765 }
1766 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001767 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001768 }
1769 v.setTag(null);
1770 }
1771
Romain Guyf8e6a802009-12-07 17:48:02 -08001772 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001773 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001774 }
1775
Romain Guya6abce82009-11-10 02:54:41 -08001776 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001777 final Resources resources = getResources();
1778 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001779
Romain Guya6abce82009-11-10 02:54:41 -08001780 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001781
Romain Guy9d31e9f2009-11-11 18:54:28 -08001782 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001783
Romain Guyf8e6a802009-12-07 17:48:02 -08001784 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001785 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001786 int extraW = (int) ((r.left + r.right) * max);
1787 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001788
1789 int aW = cell.getWidth() - extraW;
1790 float w = aW / max;
1791
1792 int width = cell.getWidth();
1793 int height = cell.getHeight();
1794 int x = cell.getLeftPadding();
1795 int y = cell.getTopPadding();
1796 width -= (x + cell.getRightPadding());
1797 height -= (y + cell.getBottomPadding());
1798
1799 float scale = w / width;
1800
1801 int count = end - start;
1802
1803 final float sWidth = width * scale;
1804 float sHeight = height * scale;
1805
Romain Guye6b8e2f2009-11-10 11:56:55 -08001806 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001807
Romain Guye6b8e2f2009-11-10 11:56:55 -08001808 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1809 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001810
1811 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001812 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001813 cell = (CellLayout) workspace.getChildAt(i);
1814
Romain Guyf8e6a802009-12-07 17:48:02 -08001815 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001816 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001817
1818 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001819 c.scale(scale, scale);
1820 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1821 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001822
Romain Guy9d31e9f2009-11-11 18:54:28 -08001823 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001824 image.setImageBitmap(bitmap);
1825 image.setTag(i);
1826 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001827 image.setOnFocusChangeListener(handler);
1828 image.setFocusable(true);
1829 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001830
1831 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001832 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1833
Winson Chungaafa03c2010-06-11 17:34:16 -07001834 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001835 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001836
1837 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001838 p.setContentView(preview);
1839 p.setWidth((int) (sWidth * count + extraW));
1840 p.setHeight((int) (sHeight + extraH));
1841 p.setAnimationStyle(R.style.AnimationPreview);
1842 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001843 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001844 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001845 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001846
Romain Guye6b8e2f2009-11-10 11:56:55 -08001847 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1848 public void onDismiss() {
1849 dismissPreview(anchor);
1850 }
1851 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001852
1853 anchor.setTag(p);
1854 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001855 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001856 }
1857
Romain Guy9d31e9f2009-11-11 18:54:28 -08001858 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001859 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001860
Romain Guye6b8e2f2009-11-10 11:56:55 -08001861 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001862 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001863 }
1864
1865 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001866 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001867 v.post(this);
1868 }
1869
1870 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001871 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001872 }
1873
1874 public void onFocusChange(View v, boolean hasFocus) {
1875 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001876 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001877 }
Romain Guya6abce82009-11-10 02:54:41 -08001878 }
1879 }
1880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 Workspace getWorkspace() {
1882 return mWorkspace;
1883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 @Override
1886 protected Dialog onCreateDialog(int id) {
1887 switch (id) {
1888 case DIALOG_CREATE_SHORTCUT:
1889 return new CreateShortcut().createDialog();
1890 case DIALOG_RENAME_FOLDER:
1891 return new RenameFolder().createDialog();
1892 }
1893
1894 return super.onCreateDialog(id);
1895 }
1896
1897 @Override
1898 protected void onPrepareDialog(int id, Dialog dialog) {
1899 switch (id) {
1900 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 break;
1902 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001903 if (mFolderInfo != null) {
1904 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1905 final CharSequence text = mFolderInfo.title;
1906 input.setText(text);
1907 input.setSelection(0, text.length());
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 break;
1910 }
1911 }
1912
1913 void showRenameDialog(FolderInfo info) {
1914 mFolderInfo = info;
1915 mWaitingForResult = true;
1916 showDialog(DIALOG_RENAME_FOLDER);
1917 }
1918
1919 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1920 mAddItemCellInfo = cellInfo;
1921 mWaitingForResult = true;
1922 showDialog(DIALOG_CREATE_SHORTCUT);
1923 }
1924
Joe Onoratodeb98af2010-02-19 14:59:39 -08001925 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001926 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001927 Bundle bundle = new Bundle();
1928
1929 ArrayList<String> shortcutNames = new ArrayList<String>();
1930 shortcutNames.add(getString(R.string.group_applications));
1931 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1932
1933 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1934 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1935 R.drawable.ic_launcher_application));
1936 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1937
1938 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1939 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001940 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001941 pickIntent.putExtras(bundle);
1942
Joe Onoratodeb98af2010-02-19 14:59:39 -08001943 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001944 }
1945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 private class RenameFolder {
1947 private EditText mInput;
1948
1949 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1951 mInput = (EditText) layout.findViewById(R.id.folder_name);
1952
1953 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1954 builder.setIcon(0);
1955 builder.setTitle(getString(R.string.rename_folder_title));
1956 builder.setCancelable(true);
1957 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1958 public void onCancel(DialogInterface dialog) {
1959 cleanup();
1960 }
1961 });
1962 builder.setNegativeButton(getString(R.string.cancel_action),
1963 new Dialog.OnClickListener() {
1964 public void onClick(DialogInterface dialog, int which) {
1965 cleanup();
1966 }
1967 }
1968 );
1969 builder.setPositiveButton(getString(R.string.rename_action),
1970 new Dialog.OnClickListener() {
1971 public void onClick(DialogInterface dialog, int which) {
1972 changeFolderName();
1973 }
1974 }
1975 );
1976 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001977
1978 final AlertDialog dialog = builder.create();
1979 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1980 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001981 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001982 mInput.requestFocus();
1983 InputMethodManager inputManager = (InputMethodManager)
1984 getSystemService(Context.INPUT_METHOD_SERVICE);
1985 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001986 }
1987 });
1988
1989 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991
1992 private void changeFolderName() {
1993 final String name = mInput.getText().toString();
1994 if (!TextUtils.isEmpty(name)) {
1995 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001996 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 mFolderInfo.title = name;
1998 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1999
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002001 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 } else {
2004 final FolderIcon folderIcon = (FolderIcon)
2005 mWorkspace.getViewForTag(mFolderInfo);
2006 if (folderIcon != null) {
2007 folderIcon.setText(name);
2008 getWorkspace().requestLayout();
2009 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002010 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002011 mWorkspaceLoading = true;
2012 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014 }
2015 }
2016 cleanup();
2017 }
2018
2019 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 dismissDialog(DIALOG_RENAME_FOLDER);
2021 mWaitingForResult = false;
2022 mFolderInfo = null;
2023 }
2024 }
2025
Daniel Sandler843e8602010-06-07 14:59:01 -04002026 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2027 public boolean isAllAppsVisible() {
2028 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002029 }
2030
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002031 // AllAppsView.Watcher
2032 public void zoomed(float zoom) {
2033 if (zoom == 1.0f) {
2034 mWorkspace.setVisibility(View.GONE);
2035 }
2036 }
2037
Joe Onorato3a8820b2009-11-10 15:06:42 -08002038 void showAllApps(boolean animated) {
2039 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08002040
Romain Guyc16fea72010-03-12 17:17:56 -08002041 ((View) mAllAppsGrid).setFocusable(true);
2042 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002043
Joe Onorato7c312c12009-08-13 21:36:53 -07002044 // TODO: fade these two too
2045 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002046 }
2047
Joe Onoratob2061212009-11-24 16:13:54 -05002048 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002049 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002050 * - Home from workspace
2051 * - from center screen
2052 * - from other screens
2053 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002054 * - from center screen
2055 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002056 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002057 * - from center screen
2058 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002059 * - Launch app from workspace and quit
2060 * - with back
2061 * - with home
2062 * - Launch app from all apps and quit
2063 * - with back
2064 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002065 * - Go to a screen that's not the default, then all
2066 * apps, and launch and app, and go back
2067 * - with back
2068 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002069 * - On workspace, long press power and go back
2070 * - with back
2071 * - with home
2072 * - On all apps, long press power and go back
2073 * - with back
2074 * - with home
2075 * - On workspace, power off
2076 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002077 * - Launch an app and turn off the screen while in that app
2078 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002079 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002080 * - From all apps
2081 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002082 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2083 * - From all apps
2084 * - From the center workspace
2085 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002086 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002087 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002088 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002089 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08002090 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05002091 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002092 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002093 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002094 }
2095
Joe Onorato7c312c12009-08-13 21:36:53 -07002096 void lockAllApps() {
2097 // TODO
2098 }
2099
2100 void unlockAllApps() {
2101 // TODO
2102 }
2103
Joe Onorato7404ee42009-07-31 11:54:44 -07002104 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 * Displays the shortcut creation dialog and launches, if necessary, the
2106 * appropriate activity.
2107 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002108 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002109 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2110 DialogInterface.OnShowListener {
2111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2118 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002119 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 builder.setInverseBackgroundForced(true);
2122
2123 AlertDialog dialog = builder.create();
2124 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002125 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002126 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 return dialog;
2129 }
2130
2131 public void onCancel(DialogInterface dialog) {
2132 mWaitingForResult = false;
2133 cleanup();
2134 }
2135
Romain Guycbb89e42009-06-08 15:52:54 -07002136 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002140 try {
2141 dismissDialog(DIALOG_CREATE_SHORTCUT);
2142 } catch (Exception e) {
2143 // An exception is thrown if the dialog is not visible, which is fine
2144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 }
2146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002147 /**
2148 * Handle the action clicked in the "Add to home" dialog.
2149 */
2150 public void onClick(DialogInterface dialog, int which) {
2151 Resources res = getResources();
2152 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002153
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002154 switch (which) {
2155 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002156 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002157 break;
2158 }
Romain Guycbb89e42009-06-08 15:52:54 -07002159
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002160 case AddAdapter.ITEM_APPWIDGET: {
2161 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002162
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002163 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2164 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002165 // start the pick activity
2166 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2167 break;
2168 }
Romain Guycbb89e42009-06-08 15:52:54 -07002169
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002170 case AddAdapter.ITEM_LIVE_FOLDER: {
2171 // Insert extra item to handle inserting folder
2172 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002173
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002174 ArrayList<String> shortcutNames = new ArrayList<String>();
2175 shortcutNames.add(res.getString(R.string.group_folder));
2176 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002177
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002178 ArrayList<ShortcutIconResource> shortcutIcons =
2179 new ArrayList<ShortcutIconResource>();
2180 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2181 R.drawable.ic_launcher_folder));
2182 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2183
2184 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2185 pickIntent.putExtra(Intent.EXTRA_INTENT,
2186 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2187 pickIntent.putExtra(Intent.EXTRA_TITLE,
2188 getText(R.string.title_select_live_folder));
2189 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002190
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002191 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2192 break;
2193 }
2194
2195 case AddAdapter.ITEM_WALLPAPER: {
2196 startWallpaper();
2197 break;
2198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002201
2202 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002203 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 }
2206
2207 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002208 * Receives notifications when applications are added/removed.
2209 */
2210 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2211 @Override
2212 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002213 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002214 String reason = intent.getStringExtra("reason");
2215 if (!"homekey".equals(reason)) {
2216 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002217 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002218 animate = false;
2219 }
Joe Onoratob2061212009-11-24 16:13:54 -05002220 closeAllApps(animate);
2221 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002222 }
2223 }
2224
2225 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002226 * Receives notifications whenever the appwidgets are reset.
2227 */
2228 private class AppWidgetResetObserver extends ContentObserver {
2229 public AppWidgetResetObserver() {
2230 super(new Handler());
2231 }
2232
2233 @Override
2234 public void onChange(boolean selfChange) {
2235 onAppWidgetReset();
2236 }
2237 }
2238
2239 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002242 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002243 if (mWorkspace != null) {
2244 return mWorkspace.getCurrentScreen();
2245 } else {
2246 return SCREEN_COUNT / 2;
2247 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002248 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002249
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 /**
2251 * Refreshes the shortcuts shown on the workspace.
2252 *
2253 * Implementation of the method from LauncherModel.Callbacks.
2254 */
2255 public void startBinding() {
2256 final Workspace workspace = mWorkspace;
2257 int count = workspace.getChildCount();
2258 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002259 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2261 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002262
Joe Onorato9c1289c2009-08-17 11:03:03 -04002263 if (DEBUG_USER_INTERFACE) {
2264 android.widget.Button finishButton = new android.widget.Button(this);
2265 finishButton.setText("Finish");
2266 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2267
2268 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2269 public void onClick(View v) {
2270 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002271 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002272 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
2274 }
2275
2276 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 * Bind the items start-end from the list.
2278 *
2279 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2282
2283 final Workspace workspace = mWorkspace;
2284
2285 for (int i=start; i<end; i++) {
2286 final ItemInfo item = shortcuts.get(i);
2287 mDesktopItems.add(item);
2288 switch (item.itemType) {
2289 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2290 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002291 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002292 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2293 false);
2294 break;
2295 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2296 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2297 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2298 (UserFolderInfo) item);
2299 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2300 false);
2301 break;
2302 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2303 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2304 R.layout.live_folder_icon, this,
2305 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2306 (LiveFolderInfo) item);
2307 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2308 false);
2309 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
2312
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 }
2315
Joe Onorato9c1289c2009-08-17 11:03:03 -04002316 /**
2317 * Implementation of the method from LauncherModel.Callbacks.
2318 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002319 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2320 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002321 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002323
2324 /**
2325 * Add the views for a widget to the workspace.
2326 *
2327 * Implementation of the method from LauncherModel.Callbacks.
2328 */
2329 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002330 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2331 if (DEBUG_WIDGETS) {
2332 Log.d(TAG, "bindAppWidget: " + item);
2333 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 final Workspace workspace = mWorkspace;
2335
2336 final int appWidgetId = item.appWidgetId;
2337 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002338 if (DEBUG_WIDGETS) {
2339 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2340 }
2341
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2343
Joe Onorato9c1289c2009-08-17 11:03:03 -04002344 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2345 item.hostView.setTag(item);
2346
2347 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2348 item.cellY, item.spanX, item.spanY, false);
2349
2350 workspace.requestLayout();
2351
2352 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002353
2354 if (DEBUG_WIDGETS) {
2355 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2356 + (SystemClock.uptimeMillis()-start) + "ms");
2357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002358 }
2359
2360 /**
2361 * Callback saying that there aren't any more items to bind.
2362 *
2363 * Implementation of the method from LauncherModel.Callbacks.
2364 */
2365 public void finishBindingItems() {
2366 if (mSavedState != null) {
2367 if (!mWorkspace.hasFocus()) {
2368 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2369 }
2370
2371 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2372 if (userFolders != null) {
2373 for (long folderId : userFolders) {
2374 final FolderInfo info = mFolders.get(folderId);
2375 if (info != null) {
2376 openFolder(info);
2377 }
2378 }
2379 final Folder openFolder = mWorkspace.getOpenFolder();
2380 if (openFolder != null) {
2381 openFolder.requestFocus();
2382 }
2383 }
2384
Joe Onorato9c1289c2009-08-17 11:03:03 -04002385 mSavedState = null;
2386 }
2387
2388 if (mSavedInstanceState != null) {
2389 super.onRestoreInstanceState(mSavedInstanceState);
2390 mSavedInstanceState = null;
2391 }
2392
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 mWorkspaceLoading = false;
2394 }
2395
2396 /**
2397 * Add the icons for all apps.
2398 *
2399 * Implementation of the method from LauncherModel.Callbacks.
2400 */
2401 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002402 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002403 }
2404
2405 /**
2406 * A package was installed.
2407 *
2408 * Implementation of the method from LauncherModel.Callbacks.
2409 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002410 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002411 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002412 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 }
2414
2415 /**
2416 * A package was updated.
2417 *
2418 * Implementation of the method from LauncherModel.Callbacks.
2419 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002420 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002421 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002422 mWorkspace.updateShortcuts(apps);
2423 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002424 }
2425
2426 /**
2427 * A package was uninstalled.
2428 *
2429 * Implementation of the method from LauncherModel.Callbacks.
2430 */
Joe Onorato36115782010-06-17 13:28:48 -04002431 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002432 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002433 if (permanent) {
2434 mWorkspace.removeItems(apps);
2435 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002436 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 }
Joe Onoratobe386092009-11-17 17:32:16 -08002438
2439 /**
2440 * Prints out out state for debugging.
2441 */
2442 public void dumpState() {
2443 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002444 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002445 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2446 Log.d(TAG, "mRestoring=" + mRestoring);
2447 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2448 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2449 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2450 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002451 mModel.dumpState();
2452 mAllAppsGrid.dumpState();
2453 Log.d(TAG, "END launcher2 dump state");
2454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455}