blob: 83fe114705682d6096366320a169bd11bc37f999 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka0e260592010-06-30 17:07:39 -070019import com.android.common.Search;
20import com.android.launcher.R;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000021
Michael Jurka946ad472010-07-09 18:05:18 -070022import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.app.Dialog;
25import android.app.SearchManager;
26import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070027import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080031import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040032import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080033import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
35import android.content.DialogInterface;
36import android.content.Intent;
Patrick Dubroy558654c2010-07-23 16:48:11 -070037import android.content.IntentFilter;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070038import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070043import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040044import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080045import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080047import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080049import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080056import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
60import android.text.TextUtils;
61import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080064import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.Menu;
68import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070069import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.View;
71import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070072import android.view.WindowManager;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070073import android.view.View.OnLongClickListener;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.animation.Animation;
Michael Jurkaaf442092010-06-10 17:01:57 -070075import android.view.animation.AnimationUtils;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070076import android.view.animation.Animation.AnimationListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070079import android.widget.ImageView;
80import android.widget.LinearLayout;
81import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070082import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.TextView;
84import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Michael Jurka0e260592010-06-30 17:07:39 -070086import java.io.DataInputStream;
87import java.io.DataOutputStream;
88import java.io.FileNotFoundException;
89import java.io.IOException;
90import java.util.ArrayList;
91import java.util.HashMap;
92import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094/**
95 * Default launcher application.
96 */
Michael Jurka946ad472010-07-09 18:05:18 -070097public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -070098 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
99 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800100 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700101 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400104 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int WALLPAPER_SCREENS_SPAN = 2;
108
109 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800110 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 private static final int MENU_ADD = Menu.FIRST + 1;
113 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
114 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
115 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700116 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
118 private static final int REQUEST_CREATE_SHORTCUT = 1;
119 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
123 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700132 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Joe Onorato7c312c12009-08-13 21:36:53 -0700134 static final int DIALOG_CREATE_SHORTCUT = 1;
135 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Romain Guy98d01652009-06-30 16:21:04 -0700137 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 // Type: int
140 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
141 // Type: boolean
142 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
143 // Type: long
144 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
159 // Type: int[]
160 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
165
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800169 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800171 private final BroadcastReceiver mCloseSystemDialogsReceiver
172 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800173 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private LayoutInflater mInflater;
176
Joe Onorato00acb122009-08-04 16:04:30 -0400177 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private AppWidgetManager mAppWidgetManager;
181 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private CellLayout.CellInfo mAddItemCellInfo;
184 private CellLayout.CellInfo mMenuAddInfo;
185 private final int[] mCellCoordinates = new int[2];
186 private FolderInfo mFolderInfo;
187
Joe Onorato7c312c12009-08-13 21:36:53 -0700188 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700190 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700191 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
203 private Bundle mSavedInstanceState;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207
Romain Guy84f296c2009-11-04 15:00:44 -0800208 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
209 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Romain Guy1fbc1c82009-11-09 20:43:08 -0800211 private ImageView mPreviousView;
212 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500213
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400214 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400215 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400216 private String[] mHotseatConfig = null;
217 private Intent[] mHotseats = null;
218 private Drawable[] mHotseatIcons = null;
219 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400220
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);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700623 }
624 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700625 dismissPreview(mNextView);
626 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800627 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700628 }
Romain Guycbb89e42009-06-08 15:52:54 -0700629
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700630 @Override
631 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 // Flag the loader to stop early before switching
633 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800634 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800635 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700636 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700637
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800638 // We can't hide the IME if it was forced open. So don't bother
639 /*
640 @Override
641 public void onWindowFocusChanged(boolean hasFocus) {
642 super.onWindowFocusChanged(hasFocus);
643
644 if (hasFocus) {
645 final InputMethodManager inputManager = (InputMethodManager)
646 getSystemService(Context.INPUT_METHOD_SERVICE);
647 WindowManager.LayoutParams lp = getWindow().getAttributes();
648 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
649 android.os.Handler()) {
650 protected void onReceiveResult(int resultCode, Bundle resultData) {
651 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
652 }
653 });
654 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
655 }
656 }
657 */
658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 private boolean acceptFilter() {
660 final InputMethodManager inputManager = (InputMethodManager)
661 getSystemService(Context.INPUT_METHOD_SERVICE);
662 return !inputManager.isFullscreenMode();
663 }
664
665 @Override
666 public boolean onKeyDown(int keyCode, KeyEvent event) {
667 boolean handled = super.onKeyDown(keyCode, event);
668 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
669 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
670 keyCode, event);
671 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700672 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700673 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700674 // showSearchDialog()
675 // If there are multiple keystrokes before the search dialog takes focus,
676 // onSearchRequested() will be called for every keystroke,
677 // but it is idempotent, so it's fine.
678 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680 }
681
Joe Onorato8a9625e2010-01-28 15:55:35 -0800682 // Eat the long press event so the keyboard doesn't come up.
683 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
684 return true;
685 }
686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 return handled;
688 }
689
Karl Rosaen138a0412009-04-23 19:00:21 -0700690 private String getTypedText() {
691 return mDefaultKeySsb.toString();
692 }
693
694 private void clearTypedText() {
695 mDefaultKeySsb.clear();
696 mDefaultKeySsb.clearSpans();
697 Selection.setSelection(mDefaultKeySsb, 0);
698 }
699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 /**
701 * Restores the previous state, if it exists.
702 *
703 * @param savedState The previous state.
704 */
705 private void restoreState(Bundle savedState) {
706 if (savedState == null) {
707 return;
708 }
709
Joe Onorato3a8820b2009-11-10 15:06:42 -0800710 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
711 if (allApps) {
712 showAllApps(false);
713 }
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
716 if (currentScreen > -1) {
717 mWorkspace.setCurrentScreen(currentScreen);
718 }
719
720 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
721 if (addScreen > -1) {
722 mAddItemCellInfo = new CellLayout.CellInfo();
723 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
724 addItemCellInfo.valid = true;
725 addItemCellInfo.screen = addScreen;
726 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
727 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
728 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
729 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
730 addItemCellInfo.findVacantCellsFromOccupied(
731 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
732 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
733 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
734 mRestoring = true;
735 }
736
737 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
738 if (renameFolder) {
739 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 mRestoring = true;
742 }
743 }
744
745 /**
746 * Finds all the views we need and configure them properly.
747 */
748 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400749 DragController dragController = mDragController;
750
Romain Guyb1b69f52009-08-10 15:10:15 -0700751 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400752 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753
Joe Onorato7c312c12009-08-13 21:36:53 -0700754 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700755 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700756 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800757 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700758 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700759 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
762 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500763 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700764
Joe Onorato7c312c12009-08-13 21:36:53 -0700765 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
766 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700768 View handleView = findViewById(R.id.all_apps_button);
769 if (handleView != null && handleView instanceof HandleView) {
770 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700771 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700772 mHandleView.setLauncher(this);
773 mHandleView.setOnClickListener(this);
774 mHandleView.setOnLongClickListener(this);
775 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800776
Michael Jurka0e260592010-06-30 17:07:39 -0700777 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
778 if (widgetChooser != null) {
779 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
780 widgetChooser.setAdapter(widgetGalleryAdapter);
781 widgetChooser.setDragController(dragController);
782 widgetChooser.setLauncher(this);
783
784 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700785 IntentListAdapter folderTypes = new FolderListAdapter(
786 this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
Michael Jurka0e260592010-06-30 17:07:39 -0700787 folderChooser.setAdapter(folderTypes);
788 folderChooser.setLauncher(this);
789
790 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700791 IntentListAdapter shortcutTypes = new IntentListAdapter(
792 this, Intent.ACTION_CREATE_SHORTCUT);
Michael Jurka0e260592010-06-30 17:07:39 -0700793 shortcutChooser.setAdapter(shortcutTypes);
794 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700795 } else {
796 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
797 hotseatLeft.setContentDescription(mHotseatLabels[0]);
798 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
799 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
800 hotseatRight.setContentDescription(mHotseatLabels[1]);
801 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400802
Michael Jurkaaf442092010-06-10 17:01:57 -0700803 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
804 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800805
Michael Jurkaaf442092010-06-10 17:01:57 -0700806 Drawable previous = mPreviousView.getDrawable();
807 Drawable next = mNextView.getDrawable();
808 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
Michael Jurkaaf442092010-06-10 17:01:57 -0700810 mPreviousView.setHapticFeedbackEnabled(false);
811 mPreviousView.setOnLongClickListener(this);
812 mNextView.setHapticFeedbackEnabled(false);
813 mNextView.setOnLongClickListener(this);
814 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400817 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819
820 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400821 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700822 int deleteZoneHandleId;
823 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700824 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700825 } else {
826 deleteZoneHandleId = R.id.all_apps_button_cluster;
827 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700828 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829
Joe Onorato00acb122009-08-04 16:04:30 -0400830 dragController.setDragScoller(workspace);
831 dragController.setDragListener(deleteZone);
832 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800833 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700834
Joe Onorato00acb122009-08-04 16:04:30 -0400835 // The order here is bottom to top.
836 dragController.addDropTarget(workspace);
837 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
839
Romain Guy8a73c512009-11-09 19:19:59 -0800840 @SuppressWarnings({"UnusedDeclaration"})
841 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800842 if (!isAllAppsVisible()) {
843 mWorkspace.scrollLeft();
844 }
Romain Guy8a73c512009-11-09 19:19:59 -0800845 }
846
847 @SuppressWarnings({"UnusedDeclaration"})
848 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800849 if (!isAllAppsVisible()) {
850 mWorkspace.scrollRight();
851 }
Romain Guy8a73c512009-11-09 19:19:59 -0800852 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400853
854 @SuppressWarnings({"UnusedDeclaration"})
855 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400856 if (isAllAppsVisible()) return;
857
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400858 int index = -1;
859 if (v.getId() == R.id.hotseat_left) {
860 index = 0;
861 } else if (v.getId() == R.id.hotseat_right) {
862 index = 1;
863 }
864
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400865 // reload these every tap; you never know when they might change
866 loadHotseats();
867 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
868 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400869 startActivitySafely(
870 mHotseats[index],
871 "hotseat"
872 );
873 }
874 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 /**
877 * Creates a view representing a shortcut.
878 *
879 * @param info The data structure describing the shortcut.
880 *
881 * @return A View inflated from R.layout.application.
882 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800883 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 return createShortcut(R.layout.application,
885 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
886 }
887
888 /**
889 * Creates a view representing a shortcut inflated from the specified resource.
890 *
891 * @param layoutResId The id of the XML layout used to create the shortcut.
892 * @param parent The group the shortcut belongs to.
893 * @param info The data structure describing the shortcut.
894 *
895 * @return A View inflated from layoutResId.
896 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800897 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
899
Joe Onorato0589f0f2010-02-08 13:44:00 -0800900 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
901 new FastBitmapDrawable(info.getIcon(mIconCache)),
902 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 favorite.setText(info.title);
904 favorite.setTag(info);
905 favorite.setOnClickListener(this);
906
907 return favorite;
908 }
909
910 /**
911 * Add an application shortcut to the workspace.
912 *
913 * @param data The intent describing the application.
914 * @param cellInfo The position on screen where to create the shortcut.
915 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 cellInfo.screen = mWorkspace.getCurrentScreen();
918 if (!findSingleSlot(cellInfo)) return;
919
Joe Onorato0589f0f2010-02-08 13:44:00 -0800920 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
921 data, context);
922
Romain Guy73b979d2009-06-09 12:57:21 -0700923 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800924 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800926 info.container = ItemInfo.NO_ID;
927 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
928 } else {
929 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 }
931 }
Romain Guycbb89e42009-06-08 15:52:54 -0700932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 /**
934 * Add a shortcut to the workspace.
935 *
936 * @param data The intent describing the shortcut.
937 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 cellInfo.screen = mWorkspace.getCurrentScreen();
941 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700942
Joe Onorato0589f0f2010-02-08 13:44:00 -0800943 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944
945 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
948 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 }
950 }
951
Romain Guycbb89e42009-06-08 15:52:54 -0700952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700954 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 * @param data The intent describing the appWidgetId.
957 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700959 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700961
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700962 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700964 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 // Try finding open space on Launcher screen
967 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800968 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
969 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
970 return;
971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700973 // Build Launcher-specific widget info and save to database
974 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 launcherInfo.spanX = spans[0];
976 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 LauncherModel.addItemToDatabase(this, launcherInfo,
979 LauncherSettings.Favorites.CONTAINER_DESKTOP,
980 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
981
982 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700986 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700987
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700988 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994 }
Romain Guycbb89e42009-06-08 15:52:54 -0700995
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
997 mDesktopItems.remove(launcherInfo);
998 launcherInfo.hostView = null;
999 }
1000
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001001 public LauncherAppWidgetHost getAppWidgetHost() {
1002 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
Romain Guycbb89e42009-06-08 15:52:54 -07001004
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001005 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001006 getWindow().closeAllPanels();
1007
1008 try {
1009 dismissDialog(DIALOG_CREATE_SHORTCUT);
1010 // Unlock the workspace if the dialog was showing
1011 } catch (Exception e) {
1012 // An exception is thrown if the dialog is not visible, which is fine
1013 }
1014
1015 try {
1016 dismissDialog(DIALOG_RENAME_FOLDER);
1017 // Unlock the workspace if the dialog was showing
1018 } catch (Exception e) {
1019 // An exception is thrown if the dialog is not visible, which is fine
1020 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001021
1022 // Whatever we were doing is hereby canceled.
1023 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001024 }
1025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 @Override
1027 protected void onNewIntent(Intent intent) {
1028 super.onNewIntent(intent);
1029
1030 // Close the menu
1031 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001032 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001033 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001034
Joe Onorato14f122b2009-11-19 14:06:36 -08001035 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1036 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1037 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001038
1039 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001040 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001041 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001042 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001043 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001044 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001045 mWorkspace.unshrink();
Romain Guy1dd3a072009-07-16 13:21:01 -07001046
Joe Onorato3a8820b2009-11-10 15:06:42 -08001047 final View v = getWindow().peekDecorView();
1048 if (v != null && v.getWindowToken() != null) {
1049 InputMethodManager imm = (InputMethodManager)getSystemService(
1050 INPUT_METHOD_SERVICE);
1051 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053 }
1054 }
1055
1056 @Override
1057 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1058 // Do not call super here
1059 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001060
1061 if (mHomeCustomizationDrawer != null) {
1062 String cur = savedInstanceState.getString("currentTab");
1063 if (cur != null) {
1064 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1065 }
1066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 }
1068
1069 @Override
1070 protected void onSaveInstanceState(Bundle outState) {
1071 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1072
1073 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1074 if (folders.size() > 0) {
1075 final int count = folders.size();
1076 long[] ids = new long[count];
1077 for (int i = 0; i < count; i++) {
1078 final FolderInfo info = folders.get(i).getInfo();
1079 ids[i] = info.id;
1080 }
1081 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1082 } else {
1083 super.onSaveInstanceState(outState);
1084 }
1085
Joe Onoratofb0ca672009-09-14 17:55:46 -04001086 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001087 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1092 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1093 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1094
1095 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1096 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1097 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1098 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1099 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1100 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1101 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1102 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001103 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
1105
1106 if (mFolderInfo != null && mWaitingForResult) {
1107 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1108 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1109 }
Michael Jurka946ad472010-07-09 18:05:18 -07001110
1111 if (mHomeCustomizationDrawer != null) {
1112 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1113 if (currentTabTag != null) {
1114 outState.putString("currentTab", currentTabTag);
1115 }
1116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118
1119 @Override
1120 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001124 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001126 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128
1129 TextKeyListener.getInstance().release();
1130
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001134
1135 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001136
Patrick Dubroyab962b72010-07-26 12:10:10 -07001137 // Some launcher layouts don't have a previous and next view
1138 if (mPreviousView != null) {
1139 dismissPreview(mPreviousView);
1140 }
1141 if (mNextView != null) {
1142 dismissPreview(mNextView);
1143 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001144
1145 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 }
1147
1148 @Override
1149 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001150 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 super.startActivityForResult(intent, requestCode);
1152 }
1153
1154 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001155 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001157
Joe Onorato7bb17492009-09-24 17:51:01 -07001158 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001159
Karl Rosaen138a0412009-04-23 19:00:21 -07001160 if (initialQuery == null) {
1161 // Use any text typed in the launcher as the initial query
1162 initialQuery = getTypedText();
1163 clearTypedText();
1164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 if (appSearchData == null) {
1166 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001167 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
Romain Guycbb89e42009-06-08 15:52:54 -07001169
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 final SearchManager searchManager =
1171 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001172 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001173 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175
1176 @Override
1177 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178 if (isWorkspaceLocked()) {
1179 return false;
1180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181
1182 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1185 .setIcon(android.R.drawable.ic_menu_add)
1186 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001187 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 .setIcon(android.R.drawable.ic_menu_gallery)
1189 .setAlphabeticShortcut('W');
1190 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1191 .setIcon(android.R.drawable.ic_search_category_default)
1192 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1193 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1194 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1195 .setAlphabeticShortcut('N');
1196
1197 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001198 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1199 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200
1201 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1202 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1203 .setIntent(settings);
1204
1205 return true;
1206 }
1207
1208 @Override
1209 public boolean onPrepareOptionsMenu(Menu menu) {
1210 super.onPrepareOptionsMenu(menu);
1211
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 // If all apps is animating, don't show the menu, because we don't know
1213 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001214 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 return false;
1216 }
1217
1218 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001219 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1221 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1222
1223 // Disable add if the workspace is full.
1224 if (visible) {
1225 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1226 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
1229 return true;
1230 }
1231
Michael Jurka0e260592010-06-30 17:07:39 -07001232 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1233 // using the settings menu to add an item, something similar happens in showAddDialog
1234 public void prepareAddItemFromHomeCustomizationDrawer() {
1235 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1236 mAddItemCellInfo = mMenuAddInfo;
1237 }
1238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 @Override
1240 public boolean onOptionsItemSelected(MenuItem item) {
1241 switch (item.getItemId()) {
1242 case MENU_ADD:
1243 addItems();
1244 return true;
1245 case MENU_WALLPAPER_SETTINGS:
1246 startWallpaper();
1247 return true;
1248 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001249 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 return true;
1251 case MENU_NOTIFICATIONS:
1252 showNotifications();
1253 return true;
1254 }
1255
1256 return super.onOptionsItemSelected(item);
1257 }
Romain Guycbb89e42009-06-08 15:52:54 -07001258
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 /**
1260 * Indicates that we want global search for this activity by setting the globalSearch
1261 * argument for {@link #startSearch} to true.
1262 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001264 @Override
1265 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001266 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001268 }
1269
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270 public boolean isWorkspaceLocked() {
1271 return mWorkspaceLoading || mWaitingForResult;
1272 }
1273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001275 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001276 if (LauncherApplication.isScreenXLarge()) {
1277 // Animate the widget chooser up from the bottom of the screen
1278 if (!isCustomizationDrawerVisible()) {
1279 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001280 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001281 }
1282 } else {
1283 showAddDialog(mMenuAddInfo);
1284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
Michael Jurkaaf442092010-06-10 17:01:57 -07001287 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1288 mAddItemCellInfo = cellInfo;
1289 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1290 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1291 addAppWidgetImpl(appWidgetId);
1292 }
1293
1294 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001295 // TODO: catch bad widget exception when sent
1296 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001297 // TODO: Is this log message meaningful?
1298 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1299 addAppWidgetImpl(appWidgetId);
1300 }
1301
1302 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001303 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
Bjorn Bringert7984c942009-12-09 15:38:25 +00001305 if (appWidget.configure != null) {
1306 // Launch over to configure widget, if needed
1307 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1308 intent.setComponent(appWidget.configure);
1309 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1310
Romain Guy8e633c52010-03-04 12:51:36 -08001311 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001313 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001314 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316 }
Romain Guycbb89e42009-06-08 15:52:54 -07001317
Joe Onoratodeb98af2010-02-19 14:59:39 -08001318 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001319 // Handle case where user selected "Applications"
1320 String applicationName = getResources().getString(R.string.group_applications);
1321 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001322
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001323 if (applicationName != null && applicationName.equals(shortcutName)) {
1324 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1325 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001326
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001327 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1328 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001329 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001330 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001331 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001336 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001337 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001338 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001339
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001340 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001342 } else {
1343 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 UserFolderInfo folderInfo = new UserFolderInfo();
1349 folderInfo.title = getText(R.string.folder_name);
1350
1351 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1352 cellInfo.screen = mWorkspace.getCurrentScreen();
1353 if (!findSingleSlot(cellInfo)) return;
1354
1355 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 LauncherModel.addItemToDatabase(this, folderInfo,
1357 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360
1361 // Create the view
1362 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1363 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1364 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
Romain Guycbb89e42009-06-08 15:52:54 -07001367
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 void removeFolder(FolderInfo folder) {
1369 mFolders.remove(folder.id);
1370 }
1371
1372 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 cellInfo.screen = mWorkspace.getCurrentScreen();
1374 if (!findSingleSlot(cellInfo)) return;
1375
1376 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1377
1378 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1380 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1382 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384 }
1385
1386 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1387 CellLayout.CellInfo cellInfo, boolean notify) {
1388
1389 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1390 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1391
1392 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 Intent.ShortcutIconResource iconResource = null;
1394
1395 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1396 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1397 try {
1398 iconResource = (Intent.ShortcutIconResource) extra;
1399 final PackageManager packageManager = context.getPackageManager();
1400 Resources resources = packageManager.getResourcesForApplication(
1401 iconResource.packageName);
1402 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1403 icon = resources.getDrawable(id);
1404 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001405 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407 }
1408
1409 if (icon == null) {
1410 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1411 }
1412
1413 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001414 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 info.title = name;
1416 info.iconResource = iconResource;
1417 info.uri = data.getData();
1418 info.baseIntent = baseIntent;
1419 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1420 LiveFolders.DISPLAY_MODE_GRID);
1421
1422 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1423 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425
1426 return info;
1427 }
1428
1429 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1430 final int[] xy = new int[2];
1431 if (findSlot(cellInfo, xy, 1, 1)) {
1432 cellInfo.cellX = xy[0];
1433 cellInfo.cellY = xy[1];
1434 return true;
1435 }
1436 return false;
1437 }
1438
1439 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1440 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1441 boolean[] occupied = mSavedState != null ?
1442 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1443 cellInfo = mWorkspace.findAllVacantCells(occupied);
1444 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1445 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1446 return false;
1447 }
1448 }
1449 return true;
1450 }
1451
1452 private void showNotifications() {
1453 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1454 if (statusBar != null) {
1455 statusBar.expand();
1456 }
1457 }
1458
1459 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001460 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001462 Intent chooser = Intent.createChooser(pickWallpaper,
1463 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001464 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1465 // Removed in Eclair MR1
1466// WallpaperManager wm = (WallpaperManager)
1467// getSystemService(Context.WALLPAPER_SERVICE);
1468// WallpaperInfo wi = wm.getWallpaperInfo();
1469// if (wi != null && wi.getSettingsActivity() != null) {
1470// LabeledIntent li = new LabeledIntent(getPackageName(),
1471// R.string.configure_wallpaper, 0);
1472// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1473// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1474// }
Mike Clerona0618e42009-10-22 13:55:21 -07001475 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001478 /**
1479 * Registers various content observers. The current implementation registers
1480 * only a favorites observer to keep track of the favorites applications.
1481 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001482 private void registerContentObservers() {
1483 ContentResolver resolver = getContentResolver();
1484 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1485 true, mWidgetObserver);
1486 }
1487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 @Override
1489 public boolean dispatchKeyEvent(KeyEvent event) {
1490 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1491 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001493 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001494 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001495 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001496 dumpState();
1497 return true;
1498 }
1499 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001500 }
1501 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1502 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001503 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return true;
1505 }
1506 }
1507
1508 return super.dispatchKeyEvent(event);
1509 }
1510
Joe Onorato88ec0992009-11-19 13:16:06 -08001511 @Override
1512 public void onBackPressed() {
1513 if (isAllAppsVisible()) {
1514 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001515 } else if (isCustomizationDrawerVisible()) {
1516 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001517 mWorkspace.unshrink();
Joe Onorato88ec0992009-11-19 13:16:06 -08001518 } else {
1519 closeFolder();
1520 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001521 // Some launcher layouts don't have a previous and next view
1522 if (mPreviousView != null) {
1523 dismissPreview(mPreviousView);
1524 dismissPreview(mNextView);
1525 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001526 }
1527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 private void closeFolder() {
1529 Folder folder = mWorkspace.getOpenFolder();
1530 if (folder != null) {
1531 closeFolder(folder);
1532 }
1533 }
1534
1535 void closeFolder(Folder folder) {
1536 folder.getInfo().opened = false;
1537 ViewGroup parent = (ViewGroup) folder.getParent();
1538 if (parent != null) {
1539 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001540 if (folder instanceof DropTarget) {
1541 // Live folders aren't DropTargets.
1542 mDragController.removeDropTarget((DropTarget)folder);
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545 folder.onClose();
1546 }
1547
1548 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001549 * Re-listen when widgets are reset.
1550 */
1551 private void onAppWidgetReset() {
1552 mAppWidgetHost.startListening();
1553 }
1554
1555 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1557 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 private void unbindDesktopItems() {
1560 for (ItemInfo item: mDesktopItems) {
1561 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 /**
1566 * Launches the intent referred by the clicked shortcut.
1567 *
1568 * @param v The view representing the clicked shortcut.
1569 */
1570 public void onClick(View v) {
1571 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001572 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001574 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001575 int[] pos = new int[2];
1576 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001577 intent.setSourceBounds(new Rect(pos[0], pos[1],
1578 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001579 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 } else if (tag instanceof FolderInfo) {
1581 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001582 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001583 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001584 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001585 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001586 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589 }
1590
Michael Jurka0e260592010-06-30 17:07:39 -07001591 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001592 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001593 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001594 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001595 mWorkspace.unshrink();
Michael Jurka0e260592010-06-30 17:07:39 -07001596 return false;
1597 }
1598
Michael Jurkaaf442092010-06-10 17:01:57 -07001599 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001600 * Event handler for the search button
1601 *
1602 * @param v The view that was clicked.
1603 */
1604 public void onClickSearchButton(View v) {
1605 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1606 View button = findViewById(R.id.search_button);
1607 i.setSourceBounds(
1608 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1609 startActivity(i);
1610 }
1611
1612 /**
1613 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001614 * enters home screen customization mode.
1615 *
1616 * @param v The view that was clicked.
1617 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001618 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001619 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001620 }
1621
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001622 /**
1623 * Event handler for the "grid" button that appears on the home screen, which
1624 * enters all apps mode.
1625 *
1626 * @param v The view that was clicked.
1627 */
1628 public void onClickAllAppsButton(View v) {
1629 showAllApps(true);
1630 }
1631
Joe Onoratof984e852010-03-25 09:47:45 -07001632 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1634 try {
1635 startActivity(intent);
1636 } catch (ActivityNotFoundException e) {
1637 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001638 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 } catch (SecurityException e) {
1640 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001641 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001643 "or use the exported attribute for this activity. "
1644 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001647
Romain Guy8e633c52010-03-04 12:51:36 -08001648 void startActivityForResultSafely(Intent intent, int requestCode) {
1649 try {
1650 startActivityForResult(intent, requestCode);
1651 } catch (ActivityNotFoundException e) {
1652 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1653 } catch (SecurityException e) {
1654 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1655 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1656 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1657 "or use the exported attribute for this activity.", e);
1658 }
1659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660
1661 private void handleFolderClick(FolderInfo folderInfo) {
1662 if (!folderInfo.opened) {
1663 // Close any open folder
1664 closeFolder();
1665 // Open the requested folder
1666 openFolder(folderInfo);
1667 } else {
1668 // Find the open folder...
1669 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1670 int folderScreen;
1671 if (openFolder != null) {
1672 folderScreen = mWorkspace.getScreenForView(openFolder);
1673 // .. and close it
1674 closeFolder(openFolder);
1675 if (folderScreen != mWorkspace.getCurrentScreen()) {
1676 // Close any folder open on the current screen
1677 closeFolder();
1678 // Pull the folder onto this screen
1679 openFolder(folderInfo);
1680 }
1681 }
1682 }
1683 }
1684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 /**
1686 * Opens the user fodler described by the specified tag. The opening of the folder
1687 * is animated relative to the specified View. If the View is null, no animation
1688 * is played.
1689 *
1690 * @param folderInfo The FolderInfo describing the folder to open.
1691 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001692 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 Folder openFolder;
1694
1695 if (folderInfo instanceof UserFolderInfo) {
1696 openFolder = UserFolder.fromXml(this);
1697 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001698 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 } else {
1700 return;
1701 }
1702
Joe Onorato00acb122009-08-04 16:04:30 -04001703 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 openFolder.setLauncher(this);
1705
1706 openFolder.bind(folderInfo);
1707 folderInfo.opened = true;
1708
Winson Chungaafa03c2010-06-11 17:34:16 -07001709 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 openFolder.onOpen();
1712 }
1713
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001715 switch (v.getId()) {
1716 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001717 if (!isAllAppsVisible()) {
1718 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1719 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001720 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001721 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001722 return true;
1723 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001724 if (!isAllAppsVisible()) {
1725 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1726 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001727 showPreviews(v);
1728 }
1729 return true;
1730 case R.id.all_apps_button:
1731 if (!isAllAppsVisible()) {
1732 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1733 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1734 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001735 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001736 return true;
1737 }
1738
Joe Onorato9c1289c2009-08-17 11:03:03 -04001739 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 return false;
1741 }
1742
1743 if (!(v instanceof CellLayout)) {
1744 v = (View) v.getParent();
1745 }
1746
1747 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1748
1749 // This happens when long clicking an item with the dpad/trackball
1750 if (cellInfo == null) {
1751 return true;
1752 }
1753
1754 if (mWorkspace.allowLongPress()) {
1755 if (cellInfo.cell == null) {
1756 if (cellInfo.valid) {
1757 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001758 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001759 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1760 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 showAddDialog(cellInfo);
1762 }
1763 } else {
1764 if (!(cellInfo.cell instanceof Folder)) {
1765 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001766 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1767 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 mWorkspace.startDrag(cellInfo);
1769 }
1770 }
1771 }
1772 return true;
1773 }
1774
Romain Guye6b8e2f2009-11-10 11:56:55 -08001775 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001776 private void dismissPreview(final View v) {
1777 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001778 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001779 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1780 public void onDismiss() {
1781 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1782 int count = group.getChildCount();
1783 for (int i = 0; i < count; i++) {
1784 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1785 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001786 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1787 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1788
1789 v.setTag(R.id.workspace, null);
1790 v.setTag(R.id.icon, null);
1791 window.setOnDismissListener(null);
1792 }
1793 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001794 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001795 }
1796 v.setTag(null);
1797 }
1798
Romain Guyf8e6a802009-12-07 17:48:02 -08001799 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001800 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001801 }
1802
Romain Guya6abce82009-11-10 02:54:41 -08001803 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001804 final Resources resources = getResources();
1805 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001806
Romain Guya6abce82009-11-10 02:54:41 -08001807 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001808
Romain Guy9d31e9f2009-11-11 18:54:28 -08001809 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001810
Romain Guyf8e6a802009-12-07 17:48:02 -08001811 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001812 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001813 int extraW = (int) ((r.left + r.right) * max);
1814 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001815
1816 int aW = cell.getWidth() - extraW;
1817 float w = aW / max;
1818
1819 int width = cell.getWidth();
1820 int height = cell.getHeight();
1821 int x = cell.getLeftPadding();
1822 int y = cell.getTopPadding();
1823 width -= (x + cell.getRightPadding());
1824 height -= (y + cell.getBottomPadding());
1825
1826 float scale = w / width;
1827
1828 int count = end - start;
1829
1830 final float sWidth = width * scale;
1831 float sHeight = height * scale;
1832
Romain Guye6b8e2f2009-11-10 11:56:55 -08001833 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001834
Romain Guye6b8e2f2009-11-10 11:56:55 -08001835 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1836 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001837
1838 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001839 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001840 cell = (CellLayout) workspace.getChildAt(i);
1841
Romain Guyf8e6a802009-12-07 17:48:02 -08001842 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001843 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001844
1845 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001846 c.scale(scale, scale);
1847 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1848 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001849
Romain Guy9d31e9f2009-11-11 18:54:28 -08001850 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001851 image.setImageBitmap(bitmap);
1852 image.setTag(i);
1853 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001854 image.setOnFocusChangeListener(handler);
1855 image.setFocusable(true);
1856 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001857
1858 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001859 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1860
Winson Chungaafa03c2010-06-11 17:34:16 -07001861 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001862 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001863
1864 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001865 p.setContentView(preview);
1866 p.setWidth((int) (sWidth * count + extraW));
1867 p.setHeight((int) (sHeight + extraH));
1868 p.setAnimationStyle(R.style.AnimationPreview);
1869 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001870 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001871 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001872 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001873
Romain Guye6b8e2f2009-11-10 11:56:55 -08001874 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1875 public void onDismiss() {
1876 dismissPreview(anchor);
1877 }
1878 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001879
1880 anchor.setTag(p);
1881 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001882 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001883 }
1884
Romain Guy9d31e9f2009-11-11 18:54:28 -08001885 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001886 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001887
Romain Guye6b8e2f2009-11-10 11:56:55 -08001888 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001889 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001890 }
1891
1892 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001893 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001894 v.post(this);
1895 }
1896
1897 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001898 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001899 }
1900
1901 public void onFocusChange(View v, boolean hasFocus) {
1902 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001903 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001904 }
Romain Guya6abce82009-11-10 02:54:41 -08001905 }
1906 }
1907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 Workspace getWorkspace() {
1909 return mWorkspace;
1910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 @Override
1913 protected Dialog onCreateDialog(int id) {
1914 switch (id) {
1915 case DIALOG_CREATE_SHORTCUT:
1916 return new CreateShortcut().createDialog();
1917 case DIALOG_RENAME_FOLDER:
1918 return new RenameFolder().createDialog();
1919 }
1920
1921 return super.onCreateDialog(id);
1922 }
1923
1924 @Override
1925 protected void onPrepareDialog(int id, Dialog dialog) {
1926 switch (id) {
1927 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 break;
1929 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001930 if (mFolderInfo != null) {
1931 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1932 final CharSequence text = mFolderInfo.title;
1933 input.setText(text);
1934 input.setSelection(0, text.length());
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 break;
1937 }
1938 }
1939
1940 void showRenameDialog(FolderInfo info) {
1941 mFolderInfo = info;
1942 mWaitingForResult = true;
1943 showDialog(DIALOG_RENAME_FOLDER);
1944 }
1945
1946 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1947 mAddItemCellInfo = cellInfo;
1948 mWaitingForResult = true;
1949 showDialog(DIALOG_CREATE_SHORTCUT);
1950 }
1951
Joe Onoratodeb98af2010-02-19 14:59:39 -08001952 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001953 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001954 Bundle bundle = new Bundle();
1955
1956 ArrayList<String> shortcutNames = new ArrayList<String>();
1957 shortcutNames.add(getString(R.string.group_applications));
1958 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1959
1960 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1961 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1962 R.drawable.ic_launcher_application));
1963 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1964
1965 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1966 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001967 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001968 pickIntent.putExtras(bundle);
1969
Joe Onoratodeb98af2010-02-19 14:59:39 -08001970 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 private class RenameFolder {
1974 private EditText mInput;
1975
1976 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1978 mInput = (EditText) layout.findViewById(R.id.folder_name);
1979
1980 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1981 builder.setIcon(0);
1982 builder.setTitle(getString(R.string.rename_folder_title));
1983 builder.setCancelable(true);
1984 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1985 public void onCancel(DialogInterface dialog) {
1986 cleanup();
1987 }
1988 });
1989 builder.setNegativeButton(getString(R.string.cancel_action),
1990 new Dialog.OnClickListener() {
1991 public void onClick(DialogInterface dialog, int which) {
1992 cleanup();
1993 }
1994 }
1995 );
1996 builder.setPositiveButton(getString(R.string.rename_action),
1997 new Dialog.OnClickListener() {
1998 public void onClick(DialogInterface dialog, int which) {
1999 changeFolderName();
2000 }
2001 }
2002 );
2003 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002004
2005 final AlertDialog dialog = builder.create();
2006 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2007 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002008 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002009 mInput.requestFocus();
2010 InputMethodManager inputManager = (InputMethodManager)
2011 getSystemService(Context.INPUT_METHOD_SERVICE);
2012 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002013 }
2014 });
2015
2016 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
2019 private void changeFolderName() {
2020 final String name = mInput.getText().toString();
2021 if (!TextUtils.isEmpty(name)) {
2022 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 mFolderInfo.title = name;
2025 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2026
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002028 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002029 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 } else {
2031 final FolderIcon folderIcon = (FolderIcon)
2032 mWorkspace.getViewForTag(mFolderInfo);
2033 if (folderIcon != null) {
2034 folderIcon.setText(name);
2035 getWorkspace().requestLayout();
2036 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002037 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 mWorkspaceLoading = true;
2039 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041 }
2042 }
2043 cleanup();
2044 }
2045
2046 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 dismissDialog(DIALOG_RENAME_FOLDER);
2048 mWaitingForResult = false;
2049 mFolderInfo = null;
2050 }
2051 }
2052
Daniel Sandler843e8602010-06-07 14:59:01 -04002053 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2054 public boolean isAllAppsVisible() {
2055 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002056 }
2057
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002058 // AllAppsView.Watcher
2059 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002060 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2061 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002062 mWorkspace.setVisibility(View.GONE);
2063 }
2064 }
2065
Joe Onorato3a8820b2009-11-10 15:06:42 -08002066 void showAllApps(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002067 hideCustomizationDrawer();
2068
Michael Jurka79212d82010-07-30 16:36:20 -07002069 if (LauncherApplication.isScreenXLarge()) {
2070 mWorkspace.shrinkToBottom(animated);
2071 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002072 if (LauncherApplication.isScreenXLarge() && animated) {
2073 // Not really a zoom -- this just makes the view visible
2074 mAllAppsGrid.zoom(1.0f, false);
2075 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_in);
2076 ((View) mAllAppsGrid).startAnimation(anim);
2077 } else {
2078 mAllAppsGrid.zoom(1.0f, animated);
2079 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002080
Romain Guyc16fea72010-03-12 17:17:56 -08002081 ((View) mAllAppsGrid).setFocusable(true);
2082 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002083
Joe Onorato7c312c12009-08-13 21:36:53 -07002084 // TODO: fade these two too
2085 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002086 }
2087
Joe Onoratob2061212009-11-24 16:13:54 -05002088 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002089 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002090 * - Home from workspace
2091 * - from center screen
2092 * - from other screens
2093 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002094 * - from center screen
2095 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002096 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002097 * - from center screen
2098 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002099 * - Launch app from workspace and quit
2100 * - with back
2101 * - with home
2102 * - Launch app from all apps and quit
2103 * - with back
2104 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002105 * - Go to a screen that's not the default, then all
2106 * apps, and launch and app, and go back
2107 * - with back
2108 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002109 * - On workspace, long press power and go back
2110 * - with back
2111 * - with home
2112 * - On all apps, long press power and go back
2113 * - with back
2114 * - with home
2115 * - On workspace, power off
2116 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002117 * - Launch an app and turn off the screen while in that app
2118 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002119 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002120 * - From all apps
2121 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002122 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2123 * - From all apps
2124 * - From the center workspace
2125 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002126 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002127 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002128 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002129 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002130 if (LauncherApplication.isScreenXLarge() && animated) {
2131 Animation anim = AnimationUtils.loadAnimation(this, R.anim.all_apps_zoom_out);
2132 anim.setAnimationListener(new AnimationListener() {
2133 public void onAnimationStart(Animation animation) {}
2134 public void onAnimationRepeat(Animation animation) {}
2135 public void onAnimationEnd(Animation animation) {
2136 mAllAppsGrid.zoom(0.0f, false);
2137 }
2138 });
2139 ((View)mAllAppsGrid).startAnimation(anim);
Michael Jurka213d9632010-07-28 11:29:25 -07002140 mWorkspace.unshrink();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002141 } else {
2142 mAllAppsGrid.zoom(0.0f, animated);
2143 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002144 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002145 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002146 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002147 }
2148
Joe Onorato7c312c12009-08-13 21:36:53 -07002149 void lockAllApps() {
2150 // TODO
2151 }
2152
2153 void unlockAllApps() {
2154 // TODO
2155 }
2156
Patrick Dubroy558654c2010-07-23 16:48:11 -07002157 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002158 return mHomeCustomizationDrawer != null &&
2159 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002160 }
2161
2162 private void showCustomizationDrawer() {
2163 if (isAllAppsVisible()) {
2164 // TODO: Make a smoother transition here
2165 closeAllApps(false);
2166 }
2167 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
Michael Jurka54f7ac32010-08-02 13:56:46 -07002168 mHomeCustomizationDrawer.startAnimation(
2169 AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
Patrick Dubroy558654c2010-07-23 16:48:11 -07002170 }
2171
Michael Jurka213d9632010-07-28 11:29:25 -07002172 private void hideCustomizationDrawer() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002173 if (isCustomizationDrawerVisible()) {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002174 Animation slideDownAnimation = AnimationUtils.loadAnimation(
2175 this, R.anim.home_customization_drawer_slide_down);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002176 slideDownAnimation.setAnimationListener(new Animation.AnimationListener() {
2177 public void onAnimationEnd(Animation animation) {
2178 mHomeCustomizationDrawer.setVisibility(View.GONE);
2179 }
2180 public void onAnimationRepeat(Animation animation) {}
2181 public void onAnimationStart(Animation animation) {}
2182 });
2183 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
2184 }
2185 }
2186
Michael Jurka213d9632010-07-28 11:29:25 -07002187 void onWorkspaceUnshrink() {
2188 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002189 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002190 }
2191 if (isCustomizationDrawerVisible()) {
2192 hideCustomizationDrawer();
2193 }
2194 }
2195
Joe Onorato7404ee42009-07-31 11:54:44 -07002196 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 * Displays the shortcut creation dialog and launches, if necessary, the
2198 * appropriate activity.
2199 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002200 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002201 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2202 DialogInterface.OnShowListener {
2203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2210 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002211 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 builder.setInverseBackgroundForced(true);
2214
2215 AlertDialog dialog = builder.create();
2216 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002217 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002218 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 return dialog;
2221 }
2222
2223 public void onCancel(DialogInterface dialog) {
2224 mWaitingForResult = false;
2225 cleanup();
2226 }
2227
Romain Guycbb89e42009-06-08 15:52:54 -07002228 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002229 }
2230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002232 try {
2233 dismissDialog(DIALOG_CREATE_SHORTCUT);
2234 } catch (Exception e) {
2235 // An exception is thrown if the dialog is not visible, which is fine
2236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 }
2238
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002239 /**
2240 * Handle the action clicked in the "Add to home" dialog.
2241 */
2242 public void onClick(DialogInterface dialog, int which) {
2243 Resources res = getResources();
2244 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002245
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002246 switch (which) {
2247 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002248 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002249 break;
2250 }
Romain Guycbb89e42009-06-08 15:52:54 -07002251
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002252 case AddAdapter.ITEM_APPWIDGET: {
2253 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002254
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002255 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2256 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002257 // start the pick activity
2258 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2259 break;
2260 }
Romain Guycbb89e42009-06-08 15:52:54 -07002261
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002262 case AddAdapter.ITEM_LIVE_FOLDER: {
2263 // Insert extra item to handle inserting folder
2264 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002265
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002266 ArrayList<String> shortcutNames = new ArrayList<String>();
2267 shortcutNames.add(res.getString(R.string.group_folder));
2268 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002269
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002270 ArrayList<ShortcutIconResource> shortcutIcons =
2271 new ArrayList<ShortcutIconResource>();
2272 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2273 R.drawable.ic_launcher_folder));
2274 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2275
2276 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2277 pickIntent.putExtra(Intent.EXTRA_INTENT,
2278 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2279 pickIntent.putExtra(Intent.EXTRA_TITLE,
2280 getText(R.string.title_select_live_folder));
2281 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002282
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002283 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2284 break;
2285 }
2286
2287 case AddAdapter.ITEM_WALLPAPER: {
2288 startWallpaper();
2289 break;
2290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 }
2292 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002293
2294 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002295 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 }
2298
2299 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002300 * Receives notifications when applications are added/removed.
2301 */
2302 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2303 @Override
2304 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002305 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002306 String reason = intent.getStringExtra("reason");
2307 if (!"homekey".equals(reason)) {
2308 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002309 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002310 animate = false;
2311 }
Joe Onoratob2061212009-11-24 16:13:54 -05002312 closeAllApps(animate);
2313 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002314 }
2315 }
2316
2317 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002318 * Receives notifications whenever the appwidgets are reset.
2319 */
2320 private class AppWidgetResetObserver extends ContentObserver {
2321 public AppWidgetResetObserver() {
2322 super(new Handler());
2323 }
2324
2325 @Override
2326 public void onChange(boolean selfChange) {
2327 onAppWidgetReset();
2328 }
2329 }
2330
2331 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002335 if (mWorkspace != null) {
2336 return mWorkspace.getCurrentScreen();
2337 } else {
2338 return SCREEN_COUNT / 2;
2339 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002340 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002341
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 /**
2343 * Refreshes the shortcuts shown on the workspace.
2344 *
2345 * Implementation of the method from LauncherModel.Callbacks.
2346 */
2347 public void startBinding() {
2348 final Workspace workspace = mWorkspace;
2349 int count = workspace.getChildCount();
2350 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002351 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002352 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2353 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002354
Joe Onorato9c1289c2009-08-17 11:03:03 -04002355 if (DEBUG_USER_INTERFACE) {
2356 android.widget.Button finishButton = new android.widget.Button(this);
2357 finishButton.setText("Finish");
2358 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2359
2360 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2361 public void onClick(View v) {
2362 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002364 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 }
2366 }
2367
2368 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002369 * Bind the items start-end from the list.
2370 *
2371 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002373 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2374
2375 final Workspace workspace = mWorkspace;
2376
2377 for (int i=start; i<end; i++) {
2378 final ItemInfo item = shortcuts.get(i);
2379 mDesktopItems.add(item);
2380 switch (item.itemType) {
2381 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2382 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002383 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002384 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2385 false);
2386 break;
2387 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2388 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2389 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2390 (UserFolderInfo) item);
2391 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2392 false);
2393 break;
2394 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2395 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2396 R.layout.live_folder_icon, this,
2397 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2398 (LiveFolderInfo) item);
2399 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2400 false);
2401 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 }
2404
Joe Onorato9c1289c2009-08-17 11:03:03 -04002405 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
2407
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 /**
2409 * Implementation of the method from LauncherModel.Callbacks.
2410 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002411 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2412 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415
2416 /**
2417 * Add the views for a widget to the workspace.
2418 *
2419 * Implementation of the method from LauncherModel.Callbacks.
2420 */
2421 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002422 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2423 if (DEBUG_WIDGETS) {
2424 Log.d(TAG, "bindAppWidget: " + item);
2425 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002426 final Workspace workspace = mWorkspace;
2427
2428 final int appWidgetId = item.appWidgetId;
2429 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002430 if (DEBUG_WIDGETS) {
2431 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2432 }
2433
Joe Onorato9c1289c2009-08-17 11:03:03 -04002434 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2435
Joe Onorato9c1289c2009-08-17 11:03:03 -04002436 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2437 item.hostView.setTag(item);
2438
2439 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2440 item.cellY, item.spanX, item.spanY, false);
2441
2442 workspace.requestLayout();
2443
2444 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002445
2446 if (DEBUG_WIDGETS) {
2447 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2448 + (SystemClock.uptimeMillis()-start) + "ms");
2449 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002450 }
2451
2452 /**
2453 * Callback saying that there aren't any more items to bind.
2454 *
2455 * Implementation of the method from LauncherModel.Callbacks.
2456 */
2457 public void finishBindingItems() {
2458 if (mSavedState != null) {
2459 if (!mWorkspace.hasFocus()) {
2460 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2461 }
2462
2463 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2464 if (userFolders != null) {
2465 for (long folderId : userFolders) {
2466 final FolderInfo info = mFolders.get(folderId);
2467 if (info != null) {
2468 openFolder(info);
2469 }
2470 }
2471 final Folder openFolder = mWorkspace.getOpenFolder();
2472 if (openFolder != null) {
2473 openFolder.requestFocus();
2474 }
2475 }
2476
Joe Onorato9c1289c2009-08-17 11:03:03 -04002477 mSavedState = null;
2478 }
2479
2480 if (mSavedInstanceState != null) {
2481 super.onRestoreInstanceState(mSavedInstanceState);
2482 mSavedInstanceState = null;
2483 }
2484
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 mWorkspaceLoading = false;
2486 }
2487
2488 /**
2489 * Add the icons for all apps.
2490 *
2491 * Implementation of the method from LauncherModel.Callbacks.
2492 */
2493 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002494 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 }
2496
2497 /**
2498 * A package was installed.
2499 *
2500 * Implementation of the method from LauncherModel.Callbacks.
2501 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002502 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002504 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 }
2506
2507 /**
2508 * A package was updated.
2509 *
2510 * Implementation of the method from LauncherModel.Callbacks.
2511 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002512 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002513 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002514 mWorkspace.updateShortcuts(apps);
2515 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002516 }
2517
2518 /**
2519 * A package was uninstalled.
2520 *
2521 * Implementation of the method from LauncherModel.Callbacks.
2522 */
Joe Onorato36115782010-06-17 13:28:48 -04002523 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002524 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002525 if (permanent) {
2526 mWorkspace.removeItems(apps);
2527 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002528 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002529 }
Joe Onoratobe386092009-11-17 17:32:16 -08002530
2531 /**
2532 * Prints out out state for debugging.
2533 */
2534 public void dumpState() {
2535 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002536 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002537 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2538 Log.d(TAG, "mRestoring=" + mRestoring);
2539 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2540 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2541 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2542 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002543 mModel.dumpState();
2544 mAllAppsGrid.dumpState();
2545 Log.d(TAG, "END launcher2 dump state");
2546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547}