blob: 52f122479bb0449d22e157590488b4d2a22aad05 [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
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040036import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040038import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080044import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080045import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080047import android.graphics.drawable.ColorDrawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040048import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080052import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.text.Selection;
55import android.text.SpannableStringBuilder;
56import android.text.TextUtils;
57import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070058import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080060import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.view.KeyEvent;
62import android.view.LayoutInflater;
63import android.view.Menu;
64import android.view.MenuItem;
65import android.view.View;
66import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.view.View.OnLongClickListener;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.widget.TextView;
71import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080072import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080073import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080074import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070075import android.appwidget.AppWidgetManager;
76import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import java.util.ArrayList;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040079import java.util.List;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070081import java.io.DataOutputStream;
82import java.io.FileNotFoundException;
83import java.io.IOException;
84import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Romain Guyedcce092010-03-04 13:03:17 -080086import com.android.launcher.R;
87
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088/**
89 * Default launcher application.
90 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040091public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050092 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080093 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 static final boolean LOGD = false;
95
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final boolean PROFILE_STARTUP = false;
97 static final boolean PROFILE_ROTATE = false;
98 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 private static final int WALLPAPER_SCREENS_SPAN = 2;
101
102 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800103 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int MENU_ADD = Menu.FIRST + 1;
106 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
107 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
108 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700109 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 private static final int REQUEST_CREATE_SHORTCUT = 1;
112 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 private static final int REQUEST_PICK_APPLICATION = 6;
115 private static final int REQUEST_PICK_SHORTCUT = 7;
116 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700117 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700118 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
121
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800122 static final int SCREEN_COUNT = 5;
123 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700125 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato7c312c12009-08-13 21:36:53 -0700127 static final int DIALOG_CREATE_SHORTCUT = 1;
128 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Romain Guy98d01652009-06-30 16:21:04 -0700130 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 // Type: int
133 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
134 // Type: boolean
135 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
136 // Type: long
137 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
152 // Type: int[]
153 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
154 // Type: boolean
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
156 // Type: long
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800162 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800164 private final BroadcastReceiver mCloseSystemDialogsReceiver
165 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800166 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private LayoutInflater mInflater;
169
Joe Onorato00acb122009-08-04 16:04:30 -0400170 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private AppWidgetManager mAppWidgetManager;
174 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private CellLayout.CellInfo mAddItemCellInfo;
177 private CellLayout.CellInfo mMenuAddInfo;
178 private final int[] mCellCoordinates = new int[2];
179 private FolderInfo mFolderInfo;
180
Joe Onorato7c312c12009-08-13 21:36:53 -0700181 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700182 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private Bundle mSavedState;
186
187 private SpannableStringBuilder mDefaultKeySsb = null;
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 private boolean mWorkspaceLoading = true;
190
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800191 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private boolean mRestoring;
193 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
195 private Bundle mSavedInstanceState;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199
Romain Guy84f296c2009-11-04 15:00:44 -0800200 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
201 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Romain Guy1fbc1c82009-11-09 20:43:08 -0800203 private ImageView mPreviousView;
204 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500205
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400206 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400207 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400208 private String[] mHotseatConfig = null;
209 private Intent[] mHotseats = null;
210 private Drawable[] mHotseatIcons = null;
211 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 @Override
214 protected void onCreate(Bundle savedInstanceState) {
215 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700216
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 LauncherApplication app = ((LauncherApplication)getApplication());
218 mModel = app.setLauncher(this);
219 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400220 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700222
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700223 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700224 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
225 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 if (PROFILE_STARTUP) {
228 android.os.Debug.startMethodTracing("/sdcard/launcher");
229 }
230
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400231 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 checkForLocaleChange();
233 setWallpaperDimension();
234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 setContentView(R.layout.launcher);
236 setupViews();
237
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800238 registerContentObservers();
239
Joe Onorato7c312c12009-08-13 21:36:53 -0700240 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 mSavedState = savedInstanceState;
243 restoreState(mSavedState);
244
245 if (PROFILE_STARTUP) {
246 android.os.Debug.stopMethodTracing();
247 }
248
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700249 // We have a new AllAppsView, we need to re-bind everything, and it could have
250 // changed in our absence.
251 mModel.setAllAppsDirty();
252 mModel.setWorkspaceDirty();
253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 }
257
258 // For handling default keys
259 mDefaultKeySsb = new SpannableStringBuilder();
260 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800261
262 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
263 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700267 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
268 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 final Configuration configuration = getResources().getConfiguration();
271
Romain Guy98d01652009-06-30 16:21:04 -0700272 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 final String locale = configuration.locale.toString();
274
Romain Guy98d01652009-06-30 16:21:04 -0700275 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 final int mcc = configuration.mcc;
277
Romain Guy98d01652009-06-30 16:21:04 -0700278 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 final int mnc = configuration.mnc;
280
Romain Guy84f296c2009-11-04 15:00:44 -0800281 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
Romain Guy84f296c2009-11-04 15:00:44 -0800283 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700284 localeConfiguration.locale = locale;
285 localeConfiguration.mcc = mcc;
286 localeConfiguration.mnc = mnc;
287
288 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800289 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400290
291 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700292 }
293 }
294
295 private static class LocaleConfiguration {
296 public String locale;
297 public int mcc = -1;
298 public int mnc = -1;
299 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700300
Romain Guy98d01652009-06-30 16:21:04 -0700301 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
302 DataInputStream in = null;
303 try {
304 in = new DataInputStream(context.openFileInput(PREFERENCES));
305 configuration.locale = in.readUTF();
306 configuration.mcc = in.readInt();
307 configuration.mnc = in.readInt();
308 } catch (FileNotFoundException e) {
309 // Ignore
310 } catch (IOException e) {
311 // Ignore
312 } finally {
313 if (in != null) {
314 try {
315 in.close();
316 } catch (IOException e) {
317 // Ignore
318 }
319 }
320 }
321 }
322
323 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
324 DataOutputStream out = null;
325 try {
326 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
327 out.writeUTF(configuration.locale);
328 out.writeInt(configuration.mcc);
329 out.writeInt(configuration.mnc);
330 out.flush();
331 } catch (FileNotFoundException e) {
332 // Ignore
333 } catch (IOException e) {
334 //noinspection ResultOfMethodCallIgnored
335 context.getFileStreamPath(PREFERENCES).delete();
336 } finally {
337 if (out != null) {
338 try {
339 out.close();
340 } catch (IOException e) {
341 // Ignore
342 }
343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345 }
346
347 static int getScreen() {
348 synchronized (sLock) {
349 return sScreen;
350 }
351 }
352
353 static void setScreen(int screen) {
354 synchronized (sLock) {
355 sScreen = screen;
356 }
357 }
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700360 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361
362 Display display = getWindowManager().getDefaultDisplay();
363 boolean isPortrait = display.getWidth() < display.getHeight();
364
365 final int width = isPortrait ? display.getWidth() : display.getHeight();
366 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700367 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
369
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400370 // Note: This doesn't do all the client-id magic that BrowserProvider does
371 // in Browser. (http://b/2425179)
372 private Uri getDefaultBrowserUri() {
373 String url = getString(R.string.default_browser_url);
374 if (url.indexOf("{CID}") != -1) {
375 url = url.replace("{CID}", "android-google");
376 }
377 return Uri.parse(url);
378 }
379
380 // Load the Intent templates from arrays.xml to populate the hotseats. For
381 // each Intent, if it resolves to a single app, use that as the launch
382 // intent & use that app's label as the contentDescription. Otherwise,
383 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400384 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400385 if (mHotseatConfig == null) {
386 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
387 if (mHotseatConfig.length > 0) {
388 mHotseats = new Intent[mHotseatConfig.length];
389 mHotseatLabels = new CharSequence[mHotseatConfig.length];
390 mHotseatIcons = new Drawable[mHotseatConfig.length];
391 } else {
392 mHotseats = null;
393 mHotseatIcons = null;
394 mHotseatLabels = null;
395 }
396
397 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
398 for (int i=0; i<mHotseatConfig.length; i++) {
399 // load icon for this slot; currently unrelated to the actual activity
400 try {
401 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
402 } catch (ArrayIndexOutOfBoundsException ex) {
403 Log.w(TAG, "Missing hotseat_icons array item #" + i);
404 mHotseatIcons[i] = null;
405 }
406 }
407 hotseatIconDrawables.recycle();
408 }
409
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400410 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400411 for (int i=0; i<mHotseatConfig.length; i++) {
412 Intent intent = null;
413 if (mHotseatConfig[i].equals("*BROWSER*")) {
414 // magic value meaning "launch user's default web browser"
415 // replace it with a generic web request so we can see if there is indeed a default
416 String defaultUri = getString(R.string.default_browser_url);
417 intent = new Intent(
418 Intent.ACTION_VIEW,
419 ((defaultUri != null)
420 ? Uri.parse(defaultUri)
421 : getDefaultBrowserUri())
422 ).addCategory(Intent.CATEGORY_BROWSABLE);
423 // note: if the user launches this without a default set, she
424 // will always be taken to the default URL above; this is
425 // unavoidable as we must specify a valid URL in order for the
426 // chooser to appear, and once the user selects something, that
427 // URL is unavoidably sent to the chosen app.
428 } else {
429 try {
430 intent = Intent.parseUri(mHotseatConfig[i], 0);
431 } catch (java.net.URISyntaxException ex) {
432 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
433 // bogus; leave intent=null
434 }
435 }
436
437 if (intent == null) {
438 mHotseats[i] = null;
439 mHotseatLabels[i] = getText(R.string.activity_not_found);
440 continue;
441 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400442
443 if (LOGD) {
444 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400445 + " initial intent=["
446 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400447 + "]");
448 }
449
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400450 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
451 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
452 if (LOGD) {
453 Log.d(TAG, "Best match for intent: " + bestMatch);
454 Log.d(TAG, "All matches: ");
455 for (ResolveInfo ri : allMatches) {
456 Log.d(TAG, " --> " + ri);
457 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400458 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400459 // did this resolve to a single app, or the resolver?
460 if (allMatches.size() == 0 || bestMatch == null) {
461 // can't find any activity to handle this. let's leave the
462 // intent as-is and let Launcher show a toast when it fails
463 // to launch.
464 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400465
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400466 // set accessibility text to "Not installed"
467 mHotseatLabels[i] = getText(R.string.activity_not_found);
468 } else {
469 boolean found = false;
470 for (ResolveInfo ri : allMatches) {
471 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
472 && bestMatch.activityInfo.applicationInfo.packageName
473 .equals(ri.activityInfo.applicationInfo.packageName)) {
474 found = true;
475 break;
476 }
477 }
478
479 if (!found) {
480 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
481 // the bestMatch is probably the ResolveActivity, meaning the
482 // user has not yet selected a default
483 // so: we'll keep the original intent for now
484 mHotseats[i] = intent;
485
486 // set the accessibility text to "Select shortcut"
487 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
488 } else {
489 // we have an app!
490 // now reconstruct the intent to launch it through the front
491 // door
492 ComponentName com = new ComponentName(
493 bestMatch.activityInfo.applicationInfo.packageName,
494 bestMatch.activityInfo.name);
495 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
496
497 // load the app label for accessibility
498 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
499 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400500 }
501
502 if (LOGD) {
503 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400504 + " final intent=["
505 + ((mHotseats[i] == null)
506 ? "null"
507 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400508 + "] label=[" + mHotseatLabels[i]
509 + "]"
510 );
511 }
512 }
513 }
514
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 @Override
516 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700517 mWaitingForResult = false;
518
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 // The pattern used here is that a user PICKs a specific application,
520 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700521
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
523 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700524
Romain Guy94dabf12009-07-21 10:55:43 -0700525 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 switch (requestCode) {
527 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 break;
530 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800531 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 break;
533 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 break;
536 case REQUEST_PICK_LIVE_FOLDER:
537 addLiveFolder(data);
538 break;
539 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400540 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700542 case REQUEST_PICK_APPWIDGET:
543 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700545 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700548 case REQUEST_PICK_WALLPAPER:
549 // We just wanted the activity result here so we can clear mWaitingForResult
550 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 }
Romain Guy18042c82009-11-06 11:44:55 -0800552 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
553 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
554 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700555 // Clean up the appWidgetId if we canceled
556 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
557 if (appWidgetId != -1) {
558 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 }
562
563 @Override
564 protected void onResume() {
565 super.onResume();
566
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800567 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800568
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570 mWorkspaceLoading = true;
571 mModel.startLoader(this, true);
572 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
574 }
575
576 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700577 protected void onPause() {
578 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800579 dismissPreview(mPreviousView);
580 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800581 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700582 }
Romain Guycbb89e42009-06-08 15:52:54 -0700583
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700584 @Override
585 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400586 // Flag the loader to stop early before switching
587 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800588 mAllAppsGrid.surrender();
Romain Guycbb89e42009-06-08 15:52:54 -0700589
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700590 if (PROFILE_ROTATE) {
591 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
592 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800593 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700594 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700595
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800596 // We can't hide the IME if it was forced open. So don't bother
597 /*
598 @Override
599 public void onWindowFocusChanged(boolean hasFocus) {
600 super.onWindowFocusChanged(hasFocus);
601
602 if (hasFocus) {
603 final InputMethodManager inputManager = (InputMethodManager)
604 getSystemService(Context.INPUT_METHOD_SERVICE);
605 WindowManager.LayoutParams lp = getWindow().getAttributes();
606 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
607 android.os.Handler()) {
608 protected void onReceiveResult(int resultCode, Bundle resultData) {
609 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
610 }
611 });
612 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
613 }
614 }
615 */
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 private boolean acceptFilter() {
618 final InputMethodManager inputManager = (InputMethodManager)
619 getSystemService(Context.INPUT_METHOD_SERVICE);
620 return !inputManager.isFullscreenMode();
621 }
622
623 @Override
624 public boolean onKeyDown(int keyCode, KeyEvent event) {
625 boolean handled = super.onKeyDown(keyCode, event);
626 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
627 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
628 keyCode, event);
629 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700630 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700631 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700632 // showSearchDialog()
633 // If there are multiple keystrokes before the search dialog takes focus,
634 // onSearchRequested() will be called for every keystroke,
635 // but it is idempotent, so it's fine.
636 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
638 }
639
Joe Onorato8a9625e2010-01-28 15:55:35 -0800640 // Eat the long press event so the keyboard doesn't come up.
641 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
642 return true;
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 return handled;
646 }
647
Karl Rosaen138a0412009-04-23 19:00:21 -0700648 private String getTypedText() {
649 return mDefaultKeySsb.toString();
650 }
651
652 private void clearTypedText() {
653 mDefaultKeySsb.clear();
654 mDefaultKeySsb.clearSpans();
655 Selection.setSelection(mDefaultKeySsb, 0);
656 }
657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 /**
659 * Restores the previous state, if it exists.
660 *
661 * @param savedState The previous state.
662 */
663 private void restoreState(Bundle savedState) {
664 if (savedState == null) {
665 return;
666 }
667
Joe Onorato3a8820b2009-11-10 15:06:42 -0800668 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
669 if (allApps) {
670 showAllApps(false);
671 }
672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
674 if (currentScreen > -1) {
675 mWorkspace.setCurrentScreen(currentScreen);
676 }
677
678 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
679 if (addScreen > -1) {
680 mAddItemCellInfo = new CellLayout.CellInfo();
681 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
682 addItemCellInfo.valid = true;
683 addItemCellInfo.screen = addScreen;
684 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
685 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
686 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
687 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
688 addItemCellInfo.findVacantCellsFromOccupied(
689 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
690 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
691 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
692 mRestoring = true;
693 }
694
695 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
696 if (renameFolder) {
697 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 mRestoring = true;
700 }
701 }
702
703 /**
704 * Finds all the views we need and configure them properly.
705 */
706 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400707 DragController dragController = mDragController;
708
Romain Guyb1b69f52009-08-10 15:10:15 -0700709 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400710 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711
Joe Onorato7c312c12009-08-13 21:36:53 -0700712 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700713 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700714 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800715 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700716 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800717 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
720 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500721 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722
Joe Onorato7c312c12009-08-13 21:36:53 -0700723 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
724 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725
Joe Onorato7c312c12009-08-13 21:36:53 -0700726 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700728 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800729 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800730
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400731 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
732 hotseatLeft.setContentDescription(mHotseatLabels[0]);
733 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
734 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
735 hotseatRight.setContentDescription(mHotseatLabels[1]);
736 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400737
Romain Guy1fbc1c82009-11-09 20:43:08 -0800738 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
739 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
740
741 Drawable previous = mPreviousView.getDrawable();
742 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800743 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744
Joe Onorato0d44e942009-11-16 18:20:51 -0800745 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800746 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800747 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800748 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400751 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753
754 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400755 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400756 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
Joe Onorato00acb122009-08-04 16:04:30 -0400758 dragController.setDragScoller(workspace);
759 dragController.setDragListener(deleteZone);
760 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800761 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700762
Joe Onorato00acb122009-08-04 16:04:30 -0400763 // The order here is bottom to top.
764 dragController.addDropTarget(workspace);
765 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
767
Romain Guy8a73c512009-11-09 19:19:59 -0800768 @SuppressWarnings({"UnusedDeclaration"})
769 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800770 if (!isAllAppsVisible()) {
771 mWorkspace.scrollLeft();
772 }
Romain Guy8a73c512009-11-09 19:19:59 -0800773 }
774
775 @SuppressWarnings({"UnusedDeclaration"})
776 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800777 if (!isAllAppsVisible()) {
778 mWorkspace.scrollRight();
779 }
Romain Guy8a73c512009-11-09 19:19:59 -0800780 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400781
782 @SuppressWarnings({"UnusedDeclaration"})
783 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400784 if (isAllAppsVisible()) return;
785
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400786 int index = -1;
787 if (v.getId() == R.id.hotseat_left) {
788 index = 0;
789 } else if (v.getId() == R.id.hotseat_right) {
790 index = 1;
791 }
792
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400793 // reload these every tap; you never know when they might change
794 loadHotseats();
795 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
796 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400797 startActivitySafely(
798 mHotseats[index],
799 "hotseat"
800 );
801 }
802 }
Romain Guy8a73c512009-11-09 19:19:59 -0800803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 /**
805 * Creates a view representing a shortcut.
806 *
807 * @param info The data structure describing the shortcut.
808 *
809 * @return A View inflated from R.layout.application.
810 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800811 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 return createShortcut(R.layout.application,
813 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
814 }
815
816 /**
817 * Creates a view representing a shortcut inflated from the specified resource.
818 *
819 * @param layoutResId The id of the XML layout used to create the shortcut.
820 * @param parent The group the shortcut belongs to.
821 * @param info The data structure describing the shortcut.
822 *
823 * @return A View inflated from layoutResId.
824 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800825 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
827
Joe Onorato0589f0f2010-02-08 13:44:00 -0800828 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
829 new FastBitmapDrawable(info.getIcon(mIconCache)),
830 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 favorite.setText(info.title);
832 favorite.setTag(info);
833 favorite.setOnClickListener(this);
834
835 return favorite;
836 }
837
838 /**
839 * Add an application shortcut to the workspace.
840 *
841 * @param data The intent describing the application.
842 * @param cellInfo The position on screen where to create the shortcut.
843 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 cellInfo.screen = mWorkspace.getCurrentScreen();
846 if (!findSingleSlot(cellInfo)) return;
847
Joe Onorato0589f0f2010-02-08 13:44:00 -0800848 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
849 data, context);
850
Romain Guy73b979d2009-06-09 12:57:21 -0700851 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800852 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800854 info.container = ItemInfo.NO_ID;
855 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
856 } else {
857 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
859 }
Romain Guycbb89e42009-06-08 15:52:54 -0700860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 /**
862 * Add a shortcut to the workspace.
863 *
864 * @param data The intent describing the shortcut.
865 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 cellInfo.screen = mWorkspace.getCurrentScreen();
869 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700870
Joe Onorato0589f0f2010-02-08 13:44:00 -0800871 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872
873 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
876 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
878 }
879
Romain Guycbb89e42009-06-08 15:52:54 -0700880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700882 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 * @param data The intent describing the appWidgetId.
885 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700890
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800891 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700892
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700893 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700894
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700895 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700897 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 // Try finding open space on Launcher screen
900 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800901 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
902 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
903 return;
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700906 // Build Launcher-specific widget info and save to database
907 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 launcherInfo.spanX = spans[0];
909 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 LauncherModel.addItemToDatabase(this, launcherInfo,
912 LauncherSettings.Favorites.CONTAINER_DESKTOP,
913 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
914
915 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700919 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700920
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700921 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927 }
Romain Guycbb89e42009-06-08 15:52:54 -0700928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
930 mDesktopItems.remove(launcherInfo);
931 launcherInfo.hostView = null;
932 }
933
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934 public LauncherAppWidgetHost getAppWidgetHost() {
935 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800938 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800939 getWindow().closeAllPanels();
940
941 try {
942 dismissDialog(DIALOG_CREATE_SHORTCUT);
943 // Unlock the workspace if the dialog was showing
944 } catch (Exception e) {
945 // An exception is thrown if the dialog is not visible, which is fine
946 }
947
948 try {
949 dismissDialog(DIALOG_RENAME_FOLDER);
950 // Unlock the workspace if the dialog was showing
951 } catch (Exception e) {
952 // An exception is thrown if the dialog is not visible, which is fine
953 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800954
955 // Whatever we were doing is hereby canceled.
956 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800957 }
958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 @Override
960 protected void onNewIntent(Intent intent) {
961 super.onNewIntent(intent);
962
963 // Close the menu
964 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800965 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800966 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700967
Joe Onorato14f122b2009-11-19 14:06:36 -0800968 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
969 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
970 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800971 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800972 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800973 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800974 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700975
Joe Onorato3a8820b2009-11-10 15:06:42 -0800976 final View v = getWindow().peekDecorView();
977 if (v != null && v.getWindowToken() != null) {
978 InputMethodManager imm = (InputMethodManager)getSystemService(
979 INPUT_METHOD_SERVICE);
980 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982 }
983 }
984
985 @Override
986 protected void onRestoreInstanceState(Bundle savedInstanceState) {
987 // Do not call super here
988 mSavedInstanceState = savedInstanceState;
989 }
990
991 @Override
992 protected void onSaveInstanceState(Bundle outState) {
993 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
994
995 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
996 if (folders.size() > 0) {
997 final int count = folders.size();
998 long[] ids = new long[count];
999 for (int i = 0; i < count; i++) {
1000 final FolderInfo info = folders.get(i).getInfo();
1001 ids[i] = info.id;
1002 }
1003 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1004 } else {
1005 super.onSaveInstanceState(outState);
1006 }
1007
Joe Onoratofb0ca672009-09-14 17:55:46 -04001008 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001009 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012
1013 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1014 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1015 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1016
1017 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1018 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1019 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1020 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1021 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1022 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1023 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1024 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1025 layout.getOccupiedCells());
1026 }
1027
1028 if (mFolderInfo != null && mWaitingForResult) {
1029 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1030 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1031 }
1032 }
1033
1034 @Override
1035 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001041 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
1044 TextKeyListener.getInstance().release();
1045
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001049
1050 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001051
1052 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001053 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001054
1055 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
1058 @Override
1059 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001060 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 super.startActivityForResult(intent, requestCode);
1062 }
1063
1064 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001065 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001067
Joe Onorato7bb17492009-09-24 17:51:01 -07001068 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 if (initialQuery == null) {
1071 // Use any text typed in the launcher as the initial query
1072 initialQuery = getTypedText();
1073 clearTypedText();
1074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 if (appSearchData == null) {
1076 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001077 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 final SearchManager searchManager =
1081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001082 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001083 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085
1086 @Override
1087 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 if (isWorkspaceLocked()) {
1089 return false;
1090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091
1092 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1095 .setIcon(android.R.drawable.ic_menu_add)
1096 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 .setIcon(android.R.drawable.ic_menu_gallery)
1099 .setAlphabeticShortcut('W');
1100 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1101 .setIcon(android.R.drawable.ic_search_category_default)
1102 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1103 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1104 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1105 .setAlphabeticShortcut('N');
1106
1107 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001108 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1109 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110
1111 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1112 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1113 .setIntent(settings);
1114
1115 return true;
1116 }
1117
1118 @Override
1119 public boolean onPrepareOptionsMenu(Menu menu) {
1120 super.onPrepareOptionsMenu(menu);
1121
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001122 // If all apps is animating, don't show the menu, because we don't know
1123 // which one to show.
1124 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1125 return false;
1126 }
1127
1128 // Only show the add and wallpaper options when we're not in all apps.
1129 boolean visible = !mAllAppsGrid.isOpaque();
1130 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1131 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1132
1133 // Disable add if the workspace is full.
1134 if (visible) {
1135 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1136 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138
1139 return true;
1140 }
1141
1142 @Override
1143 public boolean onOptionsItemSelected(MenuItem item) {
1144 switch (item.getItemId()) {
1145 case MENU_ADD:
1146 addItems();
1147 return true;
1148 case MENU_WALLPAPER_SETTINGS:
1149 startWallpaper();
1150 return true;
1151 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001152 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 return true;
1154 case MENU_NOTIFICATIONS:
1155 showNotifications();
1156 return true;
1157 }
1158
1159 return super.onOptionsItemSelected(item);
1160 }
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Karl Rosaen138a0412009-04-23 19:00:21 -07001162 /**
1163 * Indicates that we want global search for this activity by setting the globalSearch
1164 * argument for {@link #startSearch} to true.
1165 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001167 @Override
1168 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001169 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001171 }
1172
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 public boolean isWorkspaceLocked() {
1174 return mWorkspaceLoading || mWaitingForResult;
1175 }
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001178 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 showAddDialog(mMenuAddInfo);
1180 }
1181
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 void addAppWidget(Intent data) {
1183 // TODO: catch bad widget exception when sent
1184 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001185 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186
Bjorn Bringert7984c942009-12-09 15:38:25 +00001187 if (appWidget.configure != null) {
1188 // Launch over to configure widget, if needed
1189 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1190 intent.setComponent(appWidget.configure);
1191 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1192
Romain Guy8e633c52010-03-04 12:51:36 -08001193 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001195 // Otherwise just add it
1196 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198 }
Romain Guycbb89e42009-06-08 15:52:54 -07001199
Joe Onoratodeb98af2010-02-19 14:59:39 -08001200 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001201 // Handle case where user selected "Applications"
1202 String applicationName = getResources().getString(R.string.group_applications);
1203 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001204
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001205 if (applicationName != null && applicationName.equals(shortcutName)) {
1206 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1207 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001208
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001209 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1210 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001211 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001212 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001213 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216
1217 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001218 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001219 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001220 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001221
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001222 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001224 } else {
1225 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
Joe Onorato9c1289c2009-08-17 11:03:03 -04001229 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 UserFolderInfo folderInfo = new UserFolderInfo();
1231 folderInfo.title = getText(R.string.folder_name);
1232
1233 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1234 cellInfo.screen = mWorkspace.getCurrentScreen();
1235 if (!findSingleSlot(cellInfo)) return;
1236
1237 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 LauncherModel.addItemToDatabase(this, folderInfo,
1239 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242
1243 // Create the view
1244 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1245 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1246 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Romain Guycbb89e42009-06-08 15:52:54 -07001249
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 void removeFolder(FolderInfo folder) {
1251 mFolders.remove(folder.id);
1252 }
1253
1254 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 cellInfo.screen = mWorkspace.getCurrentScreen();
1256 if (!findSingleSlot(cellInfo)) return;
1257
1258 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1259
1260 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1262 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1264 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
1266 }
1267
1268 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1269 CellLayout.CellInfo cellInfo, boolean notify) {
1270
1271 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1272 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1273
1274 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 Intent.ShortcutIconResource iconResource = null;
1276
1277 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1278 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1279 try {
1280 iconResource = (Intent.ShortcutIconResource) extra;
1281 final PackageManager packageManager = context.getPackageManager();
1282 Resources resources = packageManager.getResourcesForApplication(
1283 iconResource.packageName);
1284 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1285 icon = resources.getDrawable(id);
1286 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001287 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
1289 }
1290
1291 if (icon == null) {
1292 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1293 }
1294
1295 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001296 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 info.title = name;
1298 info.iconResource = iconResource;
1299 info.uri = data.getData();
1300 info.baseIntent = baseIntent;
1301 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1302 LiveFolders.DISPLAY_MODE_GRID);
1303
1304 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1305 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307
1308 return info;
1309 }
1310
1311 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1312 final int[] xy = new int[2];
1313 if (findSlot(cellInfo, xy, 1, 1)) {
1314 cellInfo.cellX = xy[0];
1315 cellInfo.cellY = xy[1];
1316 return true;
1317 }
1318 return false;
1319 }
1320
1321 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1322 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1323 boolean[] occupied = mSavedState != null ?
1324 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1325 cellInfo = mWorkspace.findAllVacantCells(occupied);
1326 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1327 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1328 return false;
1329 }
1330 }
1331 return true;
1332 }
1333
1334 private void showNotifications() {
1335 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1336 if (statusBar != null) {
1337 statusBar.expand();
1338 }
1339 }
1340
1341 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001342 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001344 Intent chooser = Intent.createChooser(pickWallpaper,
1345 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001346 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1347 // Removed in Eclair MR1
1348// WallpaperManager wm = (WallpaperManager)
1349// getSystemService(Context.WALLPAPER_SERVICE);
1350// WallpaperInfo wi = wm.getWallpaperInfo();
1351// if (wi != null && wi.getSettingsActivity() != null) {
1352// LabeledIntent li = new LabeledIntent(getPackageName(),
1353// R.string.configure_wallpaper, 0);
1354// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1355// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1356// }
Mike Clerona0618e42009-10-22 13:55:21 -07001357 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001360 /**
1361 * Registers various content observers. The current implementation registers
1362 * only a favorites observer to keep track of the favorites applications.
1363 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001364 private void registerContentObservers() {
1365 ContentResolver resolver = getContentResolver();
1366 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1367 true, mWidgetObserver);
1368 }
1369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 @Override
1371 public boolean dispatchKeyEvent(KeyEvent event) {
1372 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1373 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001375 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001376 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001377 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001378 dumpState();
1379 return true;
1380 }
1381 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001382 }
1383 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1384 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001385 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 return true;
1387 }
1388 }
1389
1390 return super.dispatchKeyEvent(event);
1391 }
1392
Joe Onorato88ec0992009-11-19 13:16:06 -08001393 @Override
1394 public void onBackPressed() {
1395 if (isAllAppsVisible()) {
1396 closeAllApps(true);
1397 } else {
1398 closeFolder();
1399 }
1400 dismissPreview(mPreviousView);
1401 dismissPreview(mNextView);
1402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 private void closeFolder() {
1405 Folder folder = mWorkspace.getOpenFolder();
1406 if (folder != null) {
1407 closeFolder(folder);
1408 }
1409 }
1410
1411 void closeFolder(Folder folder) {
1412 folder.getInfo().opened = false;
1413 ViewGroup parent = (ViewGroup) folder.getParent();
1414 if (parent != null) {
1415 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001416 if (folder instanceof DropTarget) {
1417 // Live folders aren't DropTargets.
1418 mDragController.removeDropTarget((DropTarget)folder);
1419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421 folder.onClose();
1422 }
1423
1424 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001425 * Re-listen when widgets are reset.
1426 */
1427 private void onAppWidgetReset() {
1428 mAppWidgetHost.startListening();
1429 }
1430
1431 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1433 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 private void unbindDesktopItems() {
1436 for (ItemInfo item: mDesktopItems) {
1437 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 /**
1442 * Launches the intent referred by the clicked shortcut.
1443 *
1444 * @param v The view representing the clicked shortcut.
1445 */
1446 public void onClick(View v) {
1447 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001448 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001450 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001451 int[] pos = new int[2];
1452 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001453 intent.setSourceBounds(new Rect(pos[0], pos[1],
1454 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001455 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 } else if (tag instanceof FolderInfo) {
1457 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001458 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001459 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001460 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001461 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001462 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465 }
1466
Joe Onoratof984e852010-03-25 09:47:45 -07001467 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1469 try {
1470 startActivity(intent);
1471 } catch (ActivityNotFoundException e) {
1472 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001473 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 } catch (SecurityException e) {
1475 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001476 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001478 "or use the exported attribute for this activity. "
1479 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481 }
Romain Guy8e633c52010-03-04 12:51:36 -08001482
1483 void startActivityForResultSafely(Intent intent, int requestCode) {
1484 try {
1485 startActivityForResult(intent, requestCode);
1486 } catch (ActivityNotFoundException e) {
1487 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1488 } catch (SecurityException e) {
1489 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1490 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1491 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1492 "or use the exported attribute for this activity.", e);
1493 }
1494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
1496 private void handleFolderClick(FolderInfo folderInfo) {
1497 if (!folderInfo.opened) {
1498 // Close any open folder
1499 closeFolder();
1500 // Open the requested folder
1501 openFolder(folderInfo);
1502 } else {
1503 // Find the open folder...
1504 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1505 int folderScreen;
1506 if (openFolder != null) {
1507 folderScreen = mWorkspace.getScreenForView(openFolder);
1508 // .. and close it
1509 closeFolder(openFolder);
1510 if (folderScreen != mWorkspace.getCurrentScreen()) {
1511 // Close any folder open on the current screen
1512 closeFolder();
1513 // Pull the folder onto this screen
1514 openFolder(folderInfo);
1515 }
1516 }
1517 }
1518 }
1519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 /**
1521 * Opens the user fodler described by the specified tag. The opening of the folder
1522 * is animated relative to the specified View. If the View is null, no animation
1523 * is played.
1524 *
1525 * @param folderInfo The FolderInfo describing the folder to open.
1526 */
1527 private void openFolder(FolderInfo folderInfo) {
1528 Folder openFolder;
1529
1530 if (folderInfo instanceof UserFolderInfo) {
1531 openFolder = UserFolder.fromXml(this);
1532 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001533 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 } else {
1535 return;
1536 }
1537
Joe Onorato00acb122009-08-04 16:04:30 -04001538 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 openFolder.setLauncher(this);
1540
1541 openFolder.bind(folderInfo);
1542 folderInfo.opened = true;
1543
1544 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1545 openFolder.onOpen();
1546 }
1547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001549 switch (v.getId()) {
1550 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001551 if (!isAllAppsVisible()) {
1552 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1553 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001554 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001555 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001556 return true;
1557 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001558 if (!isAllAppsVisible()) {
1559 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1560 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001561 showPreviews(v);
1562 }
1563 return true;
1564 case R.id.all_apps_button:
1565 if (!isAllAppsVisible()) {
1566 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1567 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1568 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001569 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001570 return true;
1571 }
1572
Joe Onorato9c1289c2009-08-17 11:03:03 -04001573 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 return false;
1575 }
1576
1577 if (!(v instanceof CellLayout)) {
1578 v = (View) v.getParent();
1579 }
1580
1581 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1582
1583 // This happens when long clicking an item with the dpad/trackball
1584 if (cellInfo == null) {
1585 return true;
1586 }
1587
1588 if (mWorkspace.allowLongPress()) {
1589 if (cellInfo.cell == null) {
1590 if (cellInfo.valid) {
1591 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001592 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001593 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1594 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 showAddDialog(cellInfo);
1596 }
1597 } else {
1598 if (!(cellInfo.cell instanceof Folder)) {
1599 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001600 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1601 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 mWorkspace.startDrag(cellInfo);
1603 }
1604 }
1605 }
1606 return true;
1607 }
1608
Romain Guye6b8e2f2009-11-10 11:56:55 -08001609 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001610 private void dismissPreview(final View v) {
1611 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001612 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001613 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1614 public void onDismiss() {
1615 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1616 int count = group.getChildCount();
1617 for (int i = 0; i < count; i++) {
1618 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1619 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001620 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1621 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1622
1623 v.setTag(R.id.workspace, null);
1624 v.setTag(R.id.icon, null);
1625 window.setOnDismissListener(null);
1626 }
1627 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001628 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001629 }
1630 v.setTag(null);
1631 }
1632
Romain Guyf8e6a802009-12-07 17:48:02 -08001633 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001634 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001635 }
1636
Romain Guya6abce82009-11-10 02:54:41 -08001637 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001638 final Resources resources = getResources();
1639 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001640
Romain Guya6abce82009-11-10 02:54:41 -08001641 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001642
1643 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001644
Romain Guyf8e6a802009-12-07 17:48:02 -08001645 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001646 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001647 int extraW = (int) ((r.left + r.right) * max);
1648 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001649
1650 int aW = cell.getWidth() - extraW;
1651 float w = aW / max;
1652
1653 int width = cell.getWidth();
1654 int height = cell.getHeight();
1655 int x = cell.getLeftPadding();
1656 int y = cell.getTopPadding();
1657 width -= (x + cell.getRightPadding());
1658 height -= (y + cell.getBottomPadding());
1659
1660 float scale = w / width;
1661
1662 int count = end - start;
1663
1664 final float sWidth = width * scale;
1665 float sHeight = height * scale;
1666
Romain Guye6b8e2f2009-11-10 11:56:55 -08001667 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001668
Romain Guye6b8e2f2009-11-10 11:56:55 -08001669 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1670 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001671
1672 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001673 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001674 cell = (CellLayout) workspace.getChildAt(i);
1675
Romain Guyf8e6a802009-12-07 17:48:02 -08001676 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001677 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001678
1679 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001680 c.scale(scale, scale);
1681 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1682 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001683
Romain Guy9d31e9f2009-11-11 18:54:28 -08001684 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001685 image.setImageBitmap(bitmap);
1686 image.setTag(i);
1687 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001688 image.setOnFocusChangeListener(handler);
1689 image.setFocusable(true);
1690 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001691
1692 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001693 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1694
1695 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001696 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001697
1698 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001699 p.setContentView(preview);
1700 p.setWidth((int) (sWidth * count + extraW));
1701 p.setHeight((int) (sHeight + extraH));
1702 p.setAnimationStyle(R.style.AnimationPreview);
1703 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001704 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001705 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001706 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001707
Romain Guye6b8e2f2009-11-10 11:56:55 -08001708 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1709 public void onDismiss() {
1710 dismissPreview(anchor);
1711 }
1712 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001713
1714 anchor.setTag(p);
1715 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001716 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001717 }
1718
Romain Guy9d31e9f2009-11-11 18:54:28 -08001719 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001720 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001721
Romain Guye6b8e2f2009-11-10 11:56:55 -08001722 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001723 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001724 }
1725
1726 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001727 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001728 v.post(this);
1729 }
1730
1731 public void run() {
1732 dismissPreview(mAnchor);
1733 }
1734
1735 public void onFocusChange(View v, boolean hasFocus) {
1736 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001737 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001738 }
Romain Guya6abce82009-11-10 02:54:41 -08001739 }
1740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 Workspace getWorkspace() {
1743 return mWorkspace;
1744 }
1745
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 @Override
1747 protected Dialog onCreateDialog(int id) {
1748 switch (id) {
1749 case DIALOG_CREATE_SHORTCUT:
1750 return new CreateShortcut().createDialog();
1751 case DIALOG_RENAME_FOLDER:
1752 return new RenameFolder().createDialog();
1753 }
1754
1755 return super.onCreateDialog(id);
1756 }
1757
1758 @Override
1759 protected void onPrepareDialog(int id, Dialog dialog) {
1760 switch (id) {
1761 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 break;
1763 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001764 if (mFolderInfo != null) {
1765 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1766 final CharSequence text = mFolderInfo.title;
1767 input.setText(text);
1768 input.setSelection(0, text.length());
1769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 break;
1771 }
1772 }
1773
1774 void showRenameDialog(FolderInfo info) {
1775 mFolderInfo = info;
1776 mWaitingForResult = true;
1777 showDialog(DIALOG_RENAME_FOLDER);
1778 }
1779
1780 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1781 mAddItemCellInfo = cellInfo;
1782 mWaitingForResult = true;
1783 showDialog(DIALOG_CREATE_SHORTCUT);
1784 }
1785
Joe Onoratodeb98af2010-02-19 14:59:39 -08001786 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001787 Bundle bundle = new Bundle();
1788
1789 ArrayList<String> shortcutNames = new ArrayList<String>();
1790 shortcutNames.add(getString(R.string.group_applications));
1791 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1792
1793 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1794 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1795 R.drawable.ic_launcher_application));
1796 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1797
1798 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1799 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001800 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001801 pickIntent.putExtras(bundle);
1802
Joe Onoratodeb98af2010-02-19 14:59:39 -08001803 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001804 }
1805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 private class RenameFolder {
1807 private EditText mInput;
1808
1809 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1811 mInput = (EditText) layout.findViewById(R.id.folder_name);
1812
1813 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1814 builder.setIcon(0);
1815 builder.setTitle(getString(R.string.rename_folder_title));
1816 builder.setCancelable(true);
1817 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1818 public void onCancel(DialogInterface dialog) {
1819 cleanup();
1820 }
1821 });
1822 builder.setNegativeButton(getString(R.string.cancel_action),
1823 new Dialog.OnClickListener() {
1824 public void onClick(DialogInterface dialog, int which) {
1825 cleanup();
1826 }
1827 }
1828 );
1829 builder.setPositiveButton(getString(R.string.rename_action),
1830 new Dialog.OnClickListener() {
1831 public void onClick(DialogInterface dialog, int which) {
1832 changeFolderName();
1833 }
1834 }
1835 );
1836 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001837
1838 final AlertDialog dialog = builder.create();
1839 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1840 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001841 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001842 mInput.requestFocus();
1843 InputMethodManager inputManager = (InputMethodManager)
1844 getSystemService(Context.INPUT_METHOD_SERVICE);
1845 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001846 }
1847 });
1848
1849 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
1852 private void changeFolderName() {
1853 final String name = mInput.getText().toString();
1854 if (!TextUtils.isEmpty(name)) {
1855 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001856 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 mFolderInfo.title = name;
1858 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1859
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001861 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862 mModel.setWorkspaceDirty();
1863 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 } else {
1865 final FolderIcon folderIcon = (FolderIcon)
1866 mWorkspace.getViewForTag(mFolderInfo);
1867 if (folderIcon != null) {
1868 folderIcon.setText(name);
1869 getWorkspace().requestLayout();
1870 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001871 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 mModel.setWorkspaceDirty();
1873 mWorkspaceLoading = true;
1874 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 }
1876 }
1877 }
1878 cleanup();
1879 }
1880
1881 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 dismissDialog(DIALOG_RENAME_FOLDER);
1883 mWaitingForResult = false;
1884 mFolderInfo = null;
1885 }
1886 }
1887
Joe Onoratofb0ca672009-09-14 17:55:46 -04001888 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001889 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001890 }
1891
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001892 // AllAppsView.Watcher
1893 public void zoomed(float zoom) {
1894 if (zoom == 1.0f) {
1895 mWorkspace.setVisibility(View.GONE);
1896 }
1897 }
1898
Joe Onorato3a8820b2009-11-10 15:06:42 -08001899 void showAllApps(boolean animated) {
1900 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001901
Romain Guyc16fea72010-03-12 17:17:56 -08001902 ((View) mAllAppsGrid).setFocusable(true);
1903 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001904
Joe Onorato7c312c12009-08-13 21:36:53 -07001905 // TODO: fade these two too
1906 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001907 }
1908
Joe Onoratob2061212009-11-24 16:13:54 -05001909 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001910 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001911 * - Home from workspace
1912 * - from center screen
1913 * - from other screens
1914 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001915 * - from center screen
1916 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001917 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001918 * - from center screen
1919 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001920 * - Launch app from workspace and quit
1921 * - with back
1922 * - with home
1923 * - Launch app from all apps and quit
1924 * - with back
1925 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001926 * - Go to a screen that's not the default, then all
1927 * apps, and launch and app, and go back
1928 * - with back
1929 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001930 * - On workspace, long press power and go back
1931 * - with back
1932 * - with home
1933 * - On all apps, long press power and go back
1934 * - with back
1935 * - with home
1936 * - On workspace, power off
1937 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001938 * - Launch an app and turn off the screen while in that app
1939 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001940 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001941 * - From all apps
1942 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001943 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1944 * - From all apps
1945 * - From the center workspace
1946 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001947 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001948 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001949 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001950 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001951 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001952 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001953 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001954 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001955 }
1956
Joe Onorato7c312c12009-08-13 21:36:53 -07001957 void lockAllApps() {
1958 // TODO
1959 }
1960
1961 void unlockAllApps() {
1962 // TODO
1963 }
1964
Joe Onorato7404ee42009-07-31 11:54:44 -07001965 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 * Displays the shortcut creation dialog and launches, if necessary, the
1967 * appropriate activity.
1968 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001969 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001970 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1971 DialogInterface.OnShowListener {
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1979 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001980 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 builder.setInverseBackgroundForced(true);
1983
1984 AlertDialog dialog = builder.create();
1985 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001986 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001987 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 return dialog;
1990 }
1991
1992 public void onCancel(DialogInterface dialog) {
1993 mWaitingForResult = false;
1994 cleanup();
1995 }
1996
Romain Guycbb89e42009-06-08 15:52:54 -07001997 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002001 try {
2002 dismissDialog(DIALOG_CREATE_SHORTCUT);
2003 } catch (Exception e) {
2004 // An exception is thrown if the dialog is not visible, which is fine
2005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002008 /**
2009 * Handle the action clicked in the "Add to home" dialog.
2010 */
2011 public void onClick(DialogInterface dialog, int which) {
2012 Resources res = getResources();
2013 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002014
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002015 switch (which) {
2016 case AddAdapter.ITEM_SHORTCUT: {
2017 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002018 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002019 break;
2020 }
Romain Guycbb89e42009-06-08 15:52:54 -07002021
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002022 case AddAdapter.ITEM_APPWIDGET: {
2023 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002024
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002025 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2026 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002027 // start the pick activity
2028 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2029 break;
2030 }
Romain Guycbb89e42009-06-08 15:52:54 -07002031
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002032 case AddAdapter.ITEM_LIVE_FOLDER: {
2033 // Insert extra item to handle inserting folder
2034 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002036 ArrayList<String> shortcutNames = new ArrayList<String>();
2037 shortcutNames.add(res.getString(R.string.group_folder));
2038 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002040 ArrayList<ShortcutIconResource> shortcutIcons =
2041 new ArrayList<ShortcutIconResource>();
2042 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2043 R.drawable.ic_launcher_folder));
2044 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2045
2046 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2047 pickIntent.putExtra(Intent.EXTRA_INTENT,
2048 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2049 pickIntent.putExtra(Intent.EXTRA_TITLE,
2050 getText(R.string.title_select_live_folder));
2051 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002052
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002053 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2054 break;
2055 }
2056
2057 case AddAdapter.ITEM_WALLPAPER: {
2058 startWallpaper();
2059 break;
2060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
2062 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002063
2064 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002065 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
2068
2069 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002070 * Receives notifications when applications are added/removed.
2071 */
2072 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2073 @Override
2074 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002075 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002076 String reason = intent.getStringExtra("reason");
2077 if (!"homekey".equals(reason)) {
2078 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002079 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002080 animate = false;
2081 }
Joe Onoratob2061212009-11-24 16:13:54 -05002082 closeAllApps(animate);
2083 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002084 }
2085 }
2086
2087 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002088 * Receives notifications whenever the appwidgets are reset.
2089 */
2090 private class AppWidgetResetObserver extends ContentObserver {
2091 public AppWidgetResetObserver() {
2092 super(new Handler());
2093 }
2094
2095 @Override
2096 public void onChange(boolean selfChange) {
2097 onAppWidgetReset();
2098 }
2099 }
2100
2101 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002102 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002104 public int getCurrentWorkspaceScreen() {
2105 return mWorkspace.getCurrentScreen();
2106 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002107
Joe Onorato9c1289c2009-08-17 11:03:03 -04002108 /**
2109 * Refreshes the shortcuts shown on the workspace.
2110 *
2111 * Implementation of the method from LauncherModel.Callbacks.
2112 */
2113 public void startBinding() {
2114 final Workspace workspace = mWorkspace;
2115 int count = workspace.getChildCount();
2116 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002117 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2119 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002120
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 if (DEBUG_USER_INTERFACE) {
2122 android.widget.Button finishButton = new android.widget.Button(this);
2123 finishButton.setText("Finish");
2124 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2125
2126 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2127 public void onClick(View v) {
2128 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002129 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002130 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 }
2132 }
2133
2134 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 * Bind the items start-end from the list.
2136 *
2137 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2140
2141 final Workspace workspace = mWorkspace;
2142
2143 for (int i=start; i<end; i++) {
2144 final ItemInfo item = shortcuts.get(i);
2145 mDesktopItems.add(item);
2146 switch (item.itemType) {
2147 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2148 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002149 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2151 false);
2152 break;
2153 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2154 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2155 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2156 (UserFolderInfo) item);
2157 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2158 false);
2159 break;
2160 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2161 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2162 R.layout.live_folder_icon, this,
2163 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2164 (LiveFolderInfo) item);
2165 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2166 false);
2167 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
2170
Joe Onorato9c1289c2009-08-17 11:03:03 -04002171 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 }
2173
Joe Onorato9c1289c2009-08-17 11:03:03 -04002174 /**
2175 * Implementation of the method from LauncherModel.Callbacks.
2176 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002177 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2178 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181
2182 /**
2183 * Add the views for a widget to the workspace.
2184 *
2185 * Implementation of the method from LauncherModel.Callbacks.
2186 */
2187 public void bindAppWidget(LauncherAppWidgetInfo item) {
2188 final Workspace workspace = mWorkspace;
2189
2190 final int appWidgetId = item.appWidgetId;
2191 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2192 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2193
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2195 item.hostView.setTag(item);
2196
2197 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2198 item.cellY, item.spanX, item.spanY, false);
2199
2200 workspace.requestLayout();
2201
2202 mDesktopItems.add(item);
2203 }
2204
2205 /**
2206 * Callback saying that there aren't any more items to bind.
2207 *
2208 * Implementation of the method from LauncherModel.Callbacks.
2209 */
2210 public void finishBindingItems() {
2211 if (mSavedState != null) {
2212 if (!mWorkspace.hasFocus()) {
2213 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2214 }
2215
2216 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2217 if (userFolders != null) {
2218 for (long folderId : userFolders) {
2219 final FolderInfo info = mFolders.get(folderId);
2220 if (info != null) {
2221 openFolder(info);
2222 }
2223 }
2224 final Folder openFolder = mWorkspace.getOpenFolder();
2225 if (openFolder != null) {
2226 openFolder.requestFocus();
2227 }
2228 }
2229
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 mSavedState = null;
2231 }
2232
2233 if (mSavedInstanceState != null) {
2234 super.onRestoreInstanceState(mSavedInstanceState);
2235 mSavedInstanceState = null;
2236 }
2237
Joe Onorato9c1289c2009-08-17 11:03:03 -04002238 mWorkspaceLoading = false;
2239 }
2240
2241 /**
2242 * Add the icons for all apps.
2243 *
2244 * Implementation of the method from LauncherModel.Callbacks.
2245 */
2246 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002247 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002248 }
2249
2250 /**
2251 * A package was installed.
2252 *
2253 * Implementation of the method from LauncherModel.Callbacks.
2254 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002255 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002257 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 }
2259
2260 /**
2261 * A package was updated.
2262 *
2263 * Implementation of the method from LauncherModel.Callbacks.
2264 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002265 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002266 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002267 mWorkspace.updateShortcuts(apps);
2268 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002269 }
2270
2271 /**
2272 * A package was uninstalled.
2273 *
2274 * Implementation of the method from LauncherModel.Callbacks.
2275 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002276 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002278 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002279 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002280 }
Joe Onoratobe386092009-11-17 17:32:16 -08002281
2282 /**
2283 * Prints out out state for debugging.
2284 */
2285 public void dumpState() {
2286 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002287 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002288 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2289 Log.d(TAG, "mRestoring=" + mRestoring);
2290 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2291 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2292 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2293 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002294 mModel.dumpState();
2295 mAllAppsGrid.dumpState();
2296 Log.d(TAG, "END launcher2 dump state");
2297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298}