blob: 13a39a3a309d31eccda7530072607327c4ddcc48 [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;
Daniel Sandler843e8602010-06-07 14:59:01 -040052import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080053import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.text.Selection;
56import android.text.SpannableStringBuilder;
57import android.text.TextUtils;
58import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070059import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080061import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.KeyEvent;
63import android.view.LayoutInflater;
64import android.view.Menu;
65import android.view.MenuItem;
66import android.view.View;
67import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.View.OnLongClickListener;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.widget.TextView;
72import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080073import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080074import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080075import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070076import android.appwidget.AppWidgetManager;
77import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import java.util.ArrayList;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040080import java.util.List;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070082import java.io.DataOutputStream;
83import java.io.FileNotFoundException;
84import java.io.IOException;
85import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Romain Guyedcce092010-03-04 13:03:17 -080087import com.android.launcher.R;
88
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089/**
90 * Default launcher application.
91 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040092public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050093 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080094 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -070095 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -040098 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 private static final int WALLPAPER_SCREENS_SPAN = 2;
102
103 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800104 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int MENU_ADD = Menu.FIRST + 1;
107 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
108 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
109 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700110 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 private static final int REQUEST_CREATE_SHORTCUT = 1;
113 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 private static final int REQUEST_PICK_APPLICATION = 6;
116 private static final int REQUEST_PICK_SHORTCUT = 7;
117 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700118 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700119 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
121 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
122
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800123 static final int SCREEN_COUNT = 5;
124 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700126 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
Joe Onorato7c312c12009-08-13 21:36:53 -0700128 static final int DIALOG_CREATE_SHORTCUT = 1;
129 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Romain Guy98d01652009-06-30 16:21:04 -0700131 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 // Type: int
134 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
135 // Type: boolean
136 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
137 // Type: long
138 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
153 // Type: int[]
154 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
155 // Type: boolean
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
157 // Type: long
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800163 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800165 private final BroadcastReceiver mCloseSystemDialogsReceiver
166 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800167 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private LayoutInflater mInflater;
170
Joe Onorato00acb122009-08-04 16:04:30 -0400171 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700173
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700174 private AppWidgetManager mAppWidgetManager;
175 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private CellLayout.CellInfo mAddItemCellInfo;
178 private CellLayout.CellInfo mMenuAddInfo;
179 private final int[] mCellCoordinates = new int[2];
180 private FolderInfo mFolderInfo;
181
Joe Onorato7c312c12009-08-13 21:36:53 -0700182 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700183 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700184 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private Bundle mSavedState;
187
188 private SpannableStringBuilder mDefaultKeySsb = null;
189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 private boolean mWorkspaceLoading = true;
191
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800192 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private boolean mRestoring;
194 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
196 private Bundle mSavedInstanceState;
197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800199 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200
Romain Guy84f296c2009-11-04 15:00:44 -0800201 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
202 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700203
Romain Guy1fbc1c82009-11-09 20:43:08 -0800204 private ImageView mPreviousView;
205 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500206
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400207 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400208 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400209 private String[] mHotseatConfig = null;
210 private Intent[] mHotseats = null;
211 private Drawable[] mHotseatIcons = null;
212 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 @Override
215 protected void onCreate(Bundle savedInstanceState) {
216 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700217
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 LauncherApplication app = ((LauncherApplication)getApplication());
219 mModel = app.setLauncher(this);
220 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400221 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700223
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700224 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
226 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 if (PROFILE_STARTUP) {
229 android.os.Debug.startMethodTracing("/sdcard/launcher");
230 }
231
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400232 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 checkForLocaleChange();
234 setWallpaperDimension();
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 setContentView(R.layout.launcher);
237 setupViews();
238
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800239 registerContentObservers();
240
Joe Onorato7c312c12009-08-13 21:36:53 -0700241 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 mSavedState = savedInstanceState;
244 restoreState(mSavedState);
245
246 if (PROFILE_STARTUP) {
247 android.os.Debug.stopMethodTracing();
248 }
249
250 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
253
254 // For handling default keys
255 mDefaultKeySsb = new SpannableStringBuilder();
256 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800257
258 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
259 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 }
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700263 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
264 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final Configuration configuration = getResources().getConfiguration();
267
Romain Guy98d01652009-06-30 16:21:04 -0700268 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 final String locale = configuration.locale.toString();
270
Romain Guy98d01652009-06-30 16:21:04 -0700271 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 final int mcc = configuration.mcc;
273
Romain Guy98d01652009-06-30 16:21:04 -0700274 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 final int mnc = configuration.mnc;
276
Romain Guy84f296c2009-11-04 15:00:44 -0800277 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Romain Guy84f296c2009-11-04 15:00:44 -0800279 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700280 localeConfiguration.locale = locale;
281 localeConfiguration.mcc = mcc;
282 localeConfiguration.mnc = mnc;
283
284 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400286
287 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700288 }
289 }
290
291 private static class LocaleConfiguration {
292 public String locale;
293 public int mcc = -1;
294 public int mnc = -1;
295 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700296
Romain Guy98d01652009-06-30 16:21:04 -0700297 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
298 DataInputStream in = null;
299 try {
300 in = new DataInputStream(context.openFileInput(PREFERENCES));
301 configuration.locale = in.readUTF();
302 configuration.mcc = in.readInt();
303 configuration.mnc = in.readInt();
304 } catch (FileNotFoundException e) {
305 // Ignore
306 } catch (IOException e) {
307 // Ignore
308 } finally {
309 if (in != null) {
310 try {
311 in.close();
312 } catch (IOException e) {
313 // Ignore
314 }
315 }
316 }
317 }
318
319 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
320 DataOutputStream out = null;
321 try {
322 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
323 out.writeUTF(configuration.locale);
324 out.writeInt(configuration.mcc);
325 out.writeInt(configuration.mnc);
326 out.flush();
327 } catch (FileNotFoundException e) {
328 // Ignore
329 } catch (IOException e) {
330 //noinspection ResultOfMethodCallIgnored
331 context.getFileStreamPath(PREFERENCES).delete();
332 } finally {
333 if (out != null) {
334 try {
335 out.close();
336 } catch (IOException e) {
337 // Ignore
338 }
339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341 }
342
343 static int getScreen() {
344 synchronized (sLock) {
345 return sScreen;
346 }
347 }
348
349 static void setScreen(int screen) {
350 synchronized (sLock) {
351 sScreen = screen;
352 }
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700356 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357
358 Display display = getWindowManager().getDefaultDisplay();
359 boolean isPortrait = display.getWidth() < display.getHeight();
360
361 final int width = isPortrait ? display.getWidth() : display.getHeight();
362 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700363 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
365
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400366 // Note: This doesn't do all the client-id magic that BrowserProvider does
367 // in Browser. (http://b/2425179)
368 private Uri getDefaultBrowserUri() {
369 String url = getString(R.string.default_browser_url);
370 if (url.indexOf("{CID}") != -1) {
371 url = url.replace("{CID}", "android-google");
372 }
373 return Uri.parse(url);
374 }
375
376 // Load the Intent templates from arrays.xml to populate the hotseats. For
377 // each Intent, if it resolves to a single app, use that as the launch
378 // intent & use that app's label as the contentDescription. Otherwise,
379 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400380 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400381 if (mHotseatConfig == null) {
382 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
383 if (mHotseatConfig.length > 0) {
384 mHotseats = new Intent[mHotseatConfig.length];
385 mHotseatLabels = new CharSequence[mHotseatConfig.length];
386 mHotseatIcons = new Drawable[mHotseatConfig.length];
387 } else {
388 mHotseats = null;
389 mHotseatIcons = null;
390 mHotseatLabels = null;
391 }
392
393 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
394 for (int i=0; i<mHotseatConfig.length; i++) {
395 // load icon for this slot; currently unrelated to the actual activity
396 try {
397 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
398 } catch (ArrayIndexOutOfBoundsException ex) {
399 Log.w(TAG, "Missing hotseat_icons array item #" + i);
400 mHotseatIcons[i] = null;
401 }
402 }
403 hotseatIconDrawables.recycle();
404 }
405
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400406 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400407 for (int i=0; i<mHotseatConfig.length; i++) {
408 Intent intent = null;
409 if (mHotseatConfig[i].equals("*BROWSER*")) {
410 // magic value meaning "launch user's default web browser"
411 // replace it with a generic web request so we can see if there is indeed a default
412 String defaultUri = getString(R.string.default_browser_url);
413 intent = new Intent(
414 Intent.ACTION_VIEW,
415 ((defaultUri != null)
416 ? Uri.parse(defaultUri)
417 : getDefaultBrowserUri())
418 ).addCategory(Intent.CATEGORY_BROWSABLE);
419 // note: if the user launches this without a default set, she
420 // will always be taken to the default URL above; this is
421 // unavoidable as we must specify a valid URL in order for the
422 // chooser to appear, and once the user selects something, that
423 // URL is unavoidably sent to the chosen app.
424 } else {
425 try {
426 intent = Intent.parseUri(mHotseatConfig[i], 0);
427 } catch (java.net.URISyntaxException ex) {
428 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
429 // bogus; leave intent=null
430 }
431 }
432
433 if (intent == null) {
434 mHotseats[i] = null;
435 mHotseatLabels[i] = getText(R.string.activity_not_found);
436 continue;
437 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400438
439 if (LOGD) {
440 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400441 + " initial intent=["
442 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400443 + "]");
444 }
445
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400446 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
447 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
448 if (LOGD) {
449 Log.d(TAG, "Best match for intent: " + bestMatch);
450 Log.d(TAG, "All matches: ");
451 for (ResolveInfo ri : allMatches) {
452 Log.d(TAG, " --> " + ri);
453 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400454 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400455 // did this resolve to a single app, or the resolver?
456 if (allMatches.size() == 0 || bestMatch == null) {
457 // can't find any activity to handle this. let's leave the
458 // intent as-is and let Launcher show a toast when it fails
459 // to launch.
460 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400461
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400462 // set accessibility text to "Not installed"
463 mHotseatLabels[i] = getText(R.string.activity_not_found);
464 } else {
465 boolean found = false;
466 for (ResolveInfo ri : allMatches) {
467 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
468 && bestMatch.activityInfo.applicationInfo.packageName
469 .equals(ri.activityInfo.applicationInfo.packageName)) {
470 found = true;
471 break;
472 }
473 }
474
475 if (!found) {
476 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
477 // the bestMatch is probably the ResolveActivity, meaning the
478 // user has not yet selected a default
479 // so: we'll keep the original intent for now
480 mHotseats[i] = intent;
481
482 // set the accessibility text to "Select shortcut"
483 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
484 } else {
485 // we have an app!
486 // now reconstruct the intent to launch it through the front
487 // door
488 ComponentName com = new ComponentName(
489 bestMatch.activityInfo.applicationInfo.packageName,
490 bestMatch.activityInfo.name);
491 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
492
493 // load the app label for accessibility
494 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
495 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400496 }
497
498 if (LOGD) {
499 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400500 + " final intent=["
501 + ((mHotseats[i] == null)
502 ? "null"
503 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400504 + "] label=[" + mHotseatLabels[i]
505 + "]"
506 );
507 }
508 }
509 }
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 @Override
512 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700513 mWaitingForResult = false;
514
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 // The pattern used here is that a user PICKs a specific application,
516 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
519 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700520
Romain Guy94dabf12009-07-21 10:55:43 -0700521 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 switch (requestCode) {
523 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 break;
526 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800527 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 break;
529 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 break;
532 case REQUEST_PICK_LIVE_FOLDER:
533 addLiveFolder(data);
534 break;
535 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700538 case REQUEST_PICK_APPWIDGET:
539 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700541 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700544 case REQUEST_PICK_WALLPAPER:
545 // We just wanted the activity result here so we can clear mWaitingForResult
546 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
Romain Guy18042c82009-11-06 11:44:55 -0800548 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
549 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
550 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700551 // Clean up the appWidgetId if we canceled
552 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
553 if (appWidgetId != -1) {
554 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558
559 @Override
560 protected void onResume() {
561 super.onResume();
562
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800563 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800564
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 mWorkspaceLoading = true;
567 mModel.startLoader(this, true);
568 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 }
570 }
571
572 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700573 protected void onPause() {
574 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800575 dismissPreview(mPreviousView);
576 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800577 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700578 }
Romain Guycbb89e42009-06-08 15:52:54 -0700579
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700580 @Override
581 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400582 // Flag the loader to stop early before switching
583 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800584 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800585 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700586 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700587
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800588 // We can't hide the IME if it was forced open. So don't bother
589 /*
590 @Override
591 public void onWindowFocusChanged(boolean hasFocus) {
592 super.onWindowFocusChanged(hasFocus);
593
594 if (hasFocus) {
595 final InputMethodManager inputManager = (InputMethodManager)
596 getSystemService(Context.INPUT_METHOD_SERVICE);
597 WindowManager.LayoutParams lp = getWindow().getAttributes();
598 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
599 android.os.Handler()) {
600 protected void onReceiveResult(int resultCode, Bundle resultData) {
601 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
602 }
603 });
604 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
605 }
606 }
607 */
608
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 private boolean acceptFilter() {
610 final InputMethodManager inputManager = (InputMethodManager)
611 getSystemService(Context.INPUT_METHOD_SERVICE);
612 return !inputManager.isFullscreenMode();
613 }
614
615 @Override
616 public boolean onKeyDown(int keyCode, KeyEvent event) {
617 boolean handled = super.onKeyDown(keyCode, event);
618 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
619 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
620 keyCode, event);
621 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700622 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700623 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700624 // showSearchDialog()
625 // If there are multiple keystrokes before the search dialog takes focus,
626 // onSearchRequested() will be called for every keystroke,
627 // but it is idempotent, so it's fine.
628 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 }
630 }
631
Joe Onorato8a9625e2010-01-28 15:55:35 -0800632 // Eat the long press event so the keyboard doesn't come up.
633 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
634 return true;
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 return handled;
638 }
639
Karl Rosaen138a0412009-04-23 19:00:21 -0700640 private String getTypedText() {
641 return mDefaultKeySsb.toString();
642 }
643
644 private void clearTypedText() {
645 mDefaultKeySsb.clear();
646 mDefaultKeySsb.clearSpans();
647 Selection.setSelection(mDefaultKeySsb, 0);
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 /**
651 * Restores the previous state, if it exists.
652 *
653 * @param savedState The previous state.
654 */
655 private void restoreState(Bundle savedState) {
656 if (savedState == null) {
657 return;
658 }
659
Joe Onorato3a8820b2009-11-10 15:06:42 -0800660 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
661 if (allApps) {
662 showAllApps(false);
663 }
664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
666 if (currentScreen > -1) {
667 mWorkspace.setCurrentScreen(currentScreen);
668 }
669
670 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
671 if (addScreen > -1) {
672 mAddItemCellInfo = new CellLayout.CellInfo();
673 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
674 addItemCellInfo.valid = true;
675 addItemCellInfo.screen = addScreen;
676 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
677 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
678 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
679 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
680 addItemCellInfo.findVacantCellsFromOccupied(
681 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
682 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
683 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
684 mRestoring = true;
685 }
686
687 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
688 if (renameFolder) {
689 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 mRestoring = true;
692 }
693 }
694
695 /**
696 * Finds all the views we need and configure them properly.
697 */
698 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400699 DragController dragController = mDragController;
700
Romain Guyb1b69f52009-08-10 15:10:15 -0700701 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400702 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703
Joe Onorato7c312c12009-08-13 21:36:53 -0700704 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700705 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700706 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800707 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700708 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800709 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
712 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500713 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714
Joe Onorato7c312c12009-08-13 21:36:53 -0700715 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
716 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717
Joe Onorato7c312c12009-08-13 21:36:53 -0700718 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700720 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800721 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800722
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400723 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
724 hotseatLeft.setContentDescription(mHotseatLabels[0]);
725 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
726 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
727 hotseatRight.setContentDescription(mHotseatLabels[1]);
728 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400729
Romain Guy1fbc1c82009-11-09 20:43:08 -0800730 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
731 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
732
733 Drawable previous = mPreviousView.getDrawable();
734 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800735 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736
Joe Onorato0d44e942009-11-16 18:20:51 -0800737 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800738 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800739 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800740 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400743 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
746 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400747 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400748 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749
Joe Onorato00acb122009-08-04 16:04:30 -0400750 dragController.setDragScoller(workspace);
751 dragController.setDragListener(deleteZone);
752 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800753 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700754
Joe Onorato00acb122009-08-04 16:04:30 -0400755 // The order here is bottom to top.
756 dragController.addDropTarget(workspace);
757 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759
Romain Guy8a73c512009-11-09 19:19:59 -0800760 @SuppressWarnings({"UnusedDeclaration"})
761 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800762 if (!isAllAppsVisible()) {
763 mWorkspace.scrollLeft();
764 }
Romain Guy8a73c512009-11-09 19:19:59 -0800765 }
766
767 @SuppressWarnings({"UnusedDeclaration"})
768 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800769 if (!isAllAppsVisible()) {
770 mWorkspace.scrollRight();
771 }
Romain Guy8a73c512009-11-09 19:19:59 -0800772 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400773
774 @SuppressWarnings({"UnusedDeclaration"})
775 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400776 if (isAllAppsVisible()) return;
777
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400778 int index = -1;
779 if (v.getId() == R.id.hotseat_left) {
780 index = 0;
781 } else if (v.getId() == R.id.hotseat_right) {
782 index = 1;
783 }
784
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400785 // reload these every tap; you never know when they might change
786 loadHotseats();
787 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
788 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400789 startActivitySafely(
790 mHotseats[index],
791 "hotseat"
792 );
793 }
794 }
Romain Guy8a73c512009-11-09 19:19:59 -0800795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 /**
797 * Creates a view representing a shortcut.
798 *
799 * @param info The data structure describing the shortcut.
800 *
801 * @return A View inflated from R.layout.application.
802 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800803 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 return createShortcut(R.layout.application,
805 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
806 }
807
808 /**
809 * Creates a view representing a shortcut inflated from the specified resource.
810 *
811 * @param layoutResId The id of the XML layout used to create the shortcut.
812 * @param parent The group the shortcut belongs to.
813 * @param info The data structure describing the shortcut.
814 *
815 * @return A View inflated from layoutResId.
816 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800817 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
819
Joe Onorato0589f0f2010-02-08 13:44:00 -0800820 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
821 new FastBitmapDrawable(info.getIcon(mIconCache)),
822 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 favorite.setText(info.title);
824 favorite.setTag(info);
825 favorite.setOnClickListener(this);
826
827 return favorite;
828 }
829
830 /**
831 * Add an application shortcut to the workspace.
832 *
833 * @param data The intent describing the application.
834 * @param cellInfo The position on screen where to create the shortcut.
835 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 cellInfo.screen = mWorkspace.getCurrentScreen();
838 if (!findSingleSlot(cellInfo)) return;
839
Joe Onorato0589f0f2010-02-08 13:44:00 -0800840 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
841 data, context);
842
Romain Guy73b979d2009-06-09 12:57:21 -0700843 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800844 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800846 info.container = ItemInfo.NO_ID;
847 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
848 } else {
849 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
851 }
Romain Guycbb89e42009-06-08 15:52:54 -0700852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 /**
854 * Add a shortcut to the workspace.
855 *
856 * @param data The intent describing the shortcut.
857 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 cellInfo.screen = mWorkspace.getCurrentScreen();
861 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700862
Joe Onorato0589f0f2010-02-08 13:44:00 -0800863 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864
865 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
868 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
870 }
871
Romain Guycbb89e42009-06-08 15:52:54 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700874 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700876 * @param data The intent describing the appWidgetId.
877 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700882
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800883 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700884
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700885 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700887 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 // Try finding open space on Launcher screen
892 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800893 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
894 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
895 return;
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700898 // Build Launcher-specific widget info and save to database
899 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 launcherInfo.spanX = spans[0];
901 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 LauncherModel.addItemToDatabase(this, launcherInfo,
904 LauncherSettings.Favorites.CONTAINER_DESKTOP,
905 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
906
907 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700909
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700915
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919 }
Romain Guycbb89e42009-06-08 15:52:54 -0700920
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
922 mDesktopItems.remove(launcherInfo);
923 launcherInfo.hostView = null;
924 }
925
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700926 public LauncherAppWidgetHost getAppWidgetHost() {
927 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800930 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800931 getWindow().closeAllPanels();
932
933 try {
934 dismissDialog(DIALOG_CREATE_SHORTCUT);
935 // Unlock the workspace if the dialog was showing
936 } catch (Exception e) {
937 // An exception is thrown if the dialog is not visible, which is fine
938 }
939
940 try {
941 dismissDialog(DIALOG_RENAME_FOLDER);
942 // Unlock the workspace if the dialog was showing
943 } catch (Exception e) {
944 // An exception is thrown if the dialog is not visible, which is fine
945 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800946
947 // Whatever we were doing is hereby canceled.
948 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800949 }
950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 @Override
952 protected void onNewIntent(Intent intent) {
953 super.onNewIntent(intent);
954
955 // Close the menu
956 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800957 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800958 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700959
Joe Onorato14f122b2009-11-19 14:06:36 -0800960 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
961 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
962 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800963 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800964 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800965 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800966 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700967
Joe Onorato3a8820b2009-11-10 15:06:42 -0800968 final View v = getWindow().peekDecorView();
969 if (v != null && v.getWindowToken() != null) {
970 InputMethodManager imm = (InputMethodManager)getSystemService(
971 INPUT_METHOD_SERVICE);
972 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974 }
975 }
976
977 @Override
978 protected void onRestoreInstanceState(Bundle savedInstanceState) {
979 // Do not call super here
980 mSavedInstanceState = savedInstanceState;
981 }
982
983 @Override
984 protected void onSaveInstanceState(Bundle outState) {
985 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
986
987 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
988 if (folders.size() > 0) {
989 final int count = folders.size();
990 long[] ids = new long[count];
991 for (int i = 0; i < count; i++) {
992 final FolderInfo info = folders.get(i).getInfo();
993 ids[i] = info.id;
994 }
995 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
996 } else {
997 super.onSaveInstanceState(outState);
998 }
999
Joe Onoratofb0ca672009-09-14 17:55:46 -04001000 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001001 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004
1005 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1006 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1007 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1008
1009 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1010 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1011 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1012 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1013 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1014 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1015 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1016 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1017 layout.getOccupiedCells());
1018 }
1019
1020 if (mFolderInfo != null && mWaitingForResult) {
1021 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1022 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1023 }
1024 }
1025
1026 @Override
1027 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001033 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035
1036 TextKeyListener.getInstance().release();
1037
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001041
1042 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001043
1044 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001045 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001046
1047 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 }
1049
1050 @Override
1051 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001052 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 super.startActivityForResult(intent, requestCode);
1054 }
1055
1056 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001057 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001059
Joe Onorato7bb17492009-09-24 17:51:01 -07001060 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001061
Karl Rosaen138a0412009-04-23 19:00:21 -07001062 if (initialQuery == null) {
1063 // Use any text typed in the launcher as the initial query
1064 initialQuery = getTypedText();
1065 clearTypedText();
1066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 if (appSearchData == null) {
1068 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001069 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 }
Romain Guycbb89e42009-06-08 15:52:54 -07001071
Karl Rosaen138a0412009-04-23 19:00:21 -07001072 final SearchManager searchManager =
1073 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001074 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001075 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 }
1077
1078 @Override
1079 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001080 if (isWorkspaceLocked()) {
1081 return false;
1082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
1084 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1087 .setIcon(android.R.drawable.ic_menu_add)
1088 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001089 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 .setIcon(android.R.drawable.ic_menu_gallery)
1091 .setAlphabeticShortcut('W');
1092 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1093 .setIcon(android.R.drawable.ic_search_category_default)
1094 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1095 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1096 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1097 .setAlphabeticShortcut('N');
1098
1099 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001100 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1101 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102
1103 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1104 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1105 .setIntent(settings);
1106
1107 return true;
1108 }
1109
1110 @Override
1111 public boolean onPrepareOptionsMenu(Menu menu) {
1112 super.onPrepareOptionsMenu(menu);
1113
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001114 // If all apps is animating, don't show the menu, because we don't know
1115 // which one to show.
1116 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1117 return false;
1118 }
1119
1120 // Only show the add and wallpaper options when we're not in all apps.
1121 boolean visible = !mAllAppsGrid.isOpaque();
1122 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1123 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1124
1125 // Disable add if the workspace is full.
1126 if (visible) {
1127 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1128 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
1131 return true;
1132 }
1133
1134 @Override
1135 public boolean onOptionsItemSelected(MenuItem item) {
1136 switch (item.getItemId()) {
1137 case MENU_ADD:
1138 addItems();
1139 return true;
1140 case MENU_WALLPAPER_SETTINGS:
1141 startWallpaper();
1142 return true;
1143 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001144 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 return true;
1146 case MENU_NOTIFICATIONS:
1147 showNotifications();
1148 return true;
1149 }
1150
1151 return super.onOptionsItemSelected(item);
1152 }
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 /**
1155 * Indicates that we want global search for this activity by setting the globalSearch
1156 * argument for {@link #startSearch} to true.
1157 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001159 @Override
1160 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001161 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001162 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001163 }
1164
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 public boolean isWorkspaceLocked() {
1166 return mWorkspaceLoading || mWaitingForResult;
1167 }
1168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001170 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 showAddDialog(mMenuAddInfo);
1172 }
1173
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 void addAppWidget(Intent data) {
1175 // TODO: catch bad widget exception when sent
1176 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001177 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Bjorn Bringert7984c942009-12-09 15:38:25 +00001179 if (appWidget.configure != null) {
1180 // Launch over to configure widget, if needed
1181 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1182 intent.setComponent(appWidget.configure);
1183 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1184
Romain Guy8e633c52010-03-04 12:51:36 -08001185 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001187 // Otherwise just add it
1188 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
Joe Onoratodeb98af2010-02-19 14:59:39 -08001192 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001193 // Handle case where user selected "Applications"
1194 String applicationName = getResources().getString(R.string.group_applications);
1195 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001196
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001197 if (applicationName != null && applicationName.equals(shortcutName)) {
1198 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1199 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001200
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001201 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1202 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001203 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001204 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001205 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208
1209 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001210 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001211 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001212 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001213
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001214 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001216 } else {
1217 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
1220
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 UserFolderInfo folderInfo = new UserFolderInfo();
1223 folderInfo.title = getText(R.string.folder_name);
1224
1225 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1226 cellInfo.screen = mWorkspace.getCurrentScreen();
1227 if (!findSingleSlot(cellInfo)) return;
1228
1229 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 LauncherModel.addItemToDatabase(this, folderInfo,
1231 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234
1235 // Create the view
1236 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1237 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1238 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
Romain Guycbb89e42009-06-08 15:52:54 -07001241
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242 void removeFolder(FolderInfo folder) {
1243 mFolders.remove(folder.id);
1244 }
1245
1246 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 cellInfo.screen = mWorkspace.getCurrentScreen();
1248 if (!findSingleSlot(cellInfo)) return;
1249
1250 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1251
1252 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1254 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1256 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
1258 }
1259
1260 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1261 CellLayout.CellInfo cellInfo, boolean notify) {
1262
1263 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1264 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1265
1266 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 Intent.ShortcutIconResource iconResource = null;
1268
1269 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1270 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1271 try {
1272 iconResource = (Intent.ShortcutIconResource) extra;
1273 final PackageManager packageManager = context.getPackageManager();
1274 Resources resources = packageManager.getResourcesForApplication(
1275 iconResource.packageName);
1276 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1277 icon = resources.getDrawable(id);
1278 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001279 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281 }
1282
1283 if (icon == null) {
1284 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1285 }
1286
1287 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001288 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 info.title = name;
1290 info.iconResource = iconResource;
1291 info.uri = data.getData();
1292 info.baseIntent = baseIntent;
1293 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1294 LiveFolders.DISPLAY_MODE_GRID);
1295
1296 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1297 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299
1300 return info;
1301 }
1302
1303 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1304 final int[] xy = new int[2];
1305 if (findSlot(cellInfo, xy, 1, 1)) {
1306 cellInfo.cellX = xy[0];
1307 cellInfo.cellY = xy[1];
1308 return true;
1309 }
1310 return false;
1311 }
1312
1313 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1314 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1315 boolean[] occupied = mSavedState != null ?
1316 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1317 cellInfo = mWorkspace.findAllVacantCells(occupied);
1318 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1319 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1320 return false;
1321 }
1322 }
1323 return true;
1324 }
1325
1326 private void showNotifications() {
1327 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1328 if (statusBar != null) {
1329 statusBar.expand();
1330 }
1331 }
1332
1333 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001334 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001336 Intent chooser = Intent.createChooser(pickWallpaper,
1337 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001338 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1339 // Removed in Eclair MR1
1340// WallpaperManager wm = (WallpaperManager)
1341// getSystemService(Context.WALLPAPER_SERVICE);
1342// WallpaperInfo wi = wm.getWallpaperInfo();
1343// if (wi != null && wi.getSettingsActivity() != null) {
1344// LabeledIntent li = new LabeledIntent(getPackageName(),
1345// R.string.configure_wallpaper, 0);
1346// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1347// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1348// }
Mike Clerona0618e42009-10-22 13:55:21 -07001349 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001352 /**
1353 * Registers various content observers. The current implementation registers
1354 * only a favorites observer to keep track of the favorites applications.
1355 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001356 private void registerContentObservers() {
1357 ContentResolver resolver = getContentResolver();
1358 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1359 true, mWidgetObserver);
1360 }
1361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 @Override
1363 public boolean dispatchKeyEvent(KeyEvent event) {
1364 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1365 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001367 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001368 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001369 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001370 dumpState();
1371 return true;
1372 }
1373 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001374 }
1375 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1376 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001377 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 return true;
1379 }
1380 }
1381
1382 return super.dispatchKeyEvent(event);
1383 }
1384
Joe Onorato88ec0992009-11-19 13:16:06 -08001385 @Override
1386 public void onBackPressed() {
1387 if (isAllAppsVisible()) {
1388 closeAllApps(true);
1389 } else {
1390 closeFolder();
1391 }
1392 dismissPreview(mPreviousView);
1393 dismissPreview(mNextView);
1394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 private void closeFolder() {
1397 Folder folder = mWorkspace.getOpenFolder();
1398 if (folder != null) {
1399 closeFolder(folder);
1400 }
1401 }
1402
1403 void closeFolder(Folder folder) {
1404 folder.getInfo().opened = false;
1405 ViewGroup parent = (ViewGroup) folder.getParent();
1406 if (parent != null) {
1407 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001408 if (folder instanceof DropTarget) {
1409 // Live folders aren't DropTargets.
1410 mDragController.removeDropTarget((DropTarget)folder);
1411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413 folder.onClose();
1414 }
1415
1416 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001417 * Re-listen when widgets are reset.
1418 */
1419 private void onAppWidgetReset() {
1420 mAppWidgetHost.startListening();
1421 }
1422
1423 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1425 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 private void unbindDesktopItems() {
1428 for (ItemInfo item: mDesktopItems) {
1429 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 /**
1434 * Launches the intent referred by the clicked shortcut.
1435 *
1436 * @param v The view representing the clicked shortcut.
1437 */
1438 public void onClick(View v) {
1439 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001440 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001442 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001443 int[] pos = new int[2];
1444 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001445 intent.setSourceBounds(new Rect(pos[0], pos[1],
1446 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001447 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 } else if (tag instanceof FolderInfo) {
1449 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001450 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001451 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001452 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001453 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001454 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457 }
1458
Joe Onoratof984e852010-03-25 09:47:45 -07001459 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1461 try {
1462 startActivity(intent);
1463 } catch (ActivityNotFoundException e) {
1464 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001465 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 } catch (SecurityException e) {
1467 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001468 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001470 "or use the exported attribute for this activity. "
1471 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473 }
Romain Guy8e633c52010-03-04 12:51:36 -08001474
1475 void startActivityForResultSafely(Intent intent, int requestCode) {
1476 try {
1477 startActivityForResult(intent, requestCode);
1478 } catch (ActivityNotFoundException e) {
1479 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1480 } catch (SecurityException e) {
1481 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1482 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1483 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1484 "or use the exported attribute for this activity.", e);
1485 }
1486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
1488 private void handleFolderClick(FolderInfo folderInfo) {
1489 if (!folderInfo.opened) {
1490 // Close any open folder
1491 closeFolder();
1492 // Open the requested folder
1493 openFolder(folderInfo);
1494 } else {
1495 // Find the open folder...
1496 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1497 int folderScreen;
1498 if (openFolder != null) {
1499 folderScreen = mWorkspace.getScreenForView(openFolder);
1500 // .. and close it
1501 closeFolder(openFolder);
1502 if (folderScreen != mWorkspace.getCurrentScreen()) {
1503 // Close any folder open on the current screen
1504 closeFolder();
1505 // Pull the folder onto this screen
1506 openFolder(folderInfo);
1507 }
1508 }
1509 }
1510 }
1511
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 /**
1513 * Opens the user fodler described by the specified tag. The opening of the folder
1514 * is animated relative to the specified View. If the View is null, no animation
1515 * is played.
1516 *
1517 * @param folderInfo The FolderInfo describing the folder to open.
1518 */
1519 private void openFolder(FolderInfo folderInfo) {
1520 Folder openFolder;
1521
1522 if (folderInfo instanceof UserFolderInfo) {
1523 openFolder = UserFolder.fromXml(this);
1524 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001525 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 } else {
1527 return;
1528 }
1529
Joe Onorato00acb122009-08-04 16:04:30 -04001530 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 openFolder.setLauncher(this);
1532
1533 openFolder.bind(folderInfo);
1534 folderInfo.opened = true;
1535
1536 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1537 openFolder.onOpen();
1538 }
1539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001541 switch (v.getId()) {
1542 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001543 if (!isAllAppsVisible()) {
1544 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1545 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001546 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001547 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001548 return true;
1549 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001550 if (!isAllAppsVisible()) {
1551 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1552 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001553 showPreviews(v);
1554 }
1555 return true;
1556 case R.id.all_apps_button:
1557 if (!isAllAppsVisible()) {
1558 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1559 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1560 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001561 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001562 return true;
1563 }
1564
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 return false;
1567 }
1568
1569 if (!(v instanceof CellLayout)) {
1570 v = (View) v.getParent();
1571 }
1572
1573 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1574
1575 // This happens when long clicking an item with the dpad/trackball
1576 if (cellInfo == null) {
1577 return true;
1578 }
1579
1580 if (mWorkspace.allowLongPress()) {
1581 if (cellInfo.cell == null) {
1582 if (cellInfo.valid) {
1583 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001584 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001585 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1586 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 showAddDialog(cellInfo);
1588 }
1589 } else {
1590 if (!(cellInfo.cell instanceof Folder)) {
1591 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001592 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1593 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 mWorkspace.startDrag(cellInfo);
1595 }
1596 }
1597 }
1598 return true;
1599 }
1600
Romain Guye6b8e2f2009-11-10 11:56:55 -08001601 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001602 private void dismissPreview(final View v) {
1603 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001604 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001605 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1606 public void onDismiss() {
1607 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1608 int count = group.getChildCount();
1609 for (int i = 0; i < count; i++) {
1610 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1611 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001612 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1613 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1614
1615 v.setTag(R.id.workspace, null);
1616 v.setTag(R.id.icon, null);
1617 window.setOnDismissListener(null);
1618 }
1619 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001620 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001621 }
1622 v.setTag(null);
1623 }
1624
Romain Guyf8e6a802009-12-07 17:48:02 -08001625 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001626 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001627 }
1628
Romain Guya6abce82009-11-10 02:54:41 -08001629 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001630 final Resources resources = getResources();
1631 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001632
Romain Guya6abce82009-11-10 02:54:41 -08001633 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001634
1635 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001636
Romain Guyf8e6a802009-12-07 17:48:02 -08001637 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001638 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001639 int extraW = (int) ((r.left + r.right) * max);
1640 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001641
1642 int aW = cell.getWidth() - extraW;
1643 float w = aW / max;
1644
1645 int width = cell.getWidth();
1646 int height = cell.getHeight();
1647 int x = cell.getLeftPadding();
1648 int y = cell.getTopPadding();
1649 width -= (x + cell.getRightPadding());
1650 height -= (y + cell.getBottomPadding());
1651
1652 float scale = w / width;
1653
1654 int count = end - start;
1655
1656 final float sWidth = width * scale;
1657 float sHeight = height * scale;
1658
Romain Guye6b8e2f2009-11-10 11:56:55 -08001659 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001660
Romain Guye6b8e2f2009-11-10 11:56:55 -08001661 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1662 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001663
1664 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001665 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001666 cell = (CellLayout) workspace.getChildAt(i);
1667
Romain Guyf8e6a802009-12-07 17:48:02 -08001668 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001669 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001670
1671 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001672 c.scale(scale, scale);
1673 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1674 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001675
Romain Guy9d31e9f2009-11-11 18:54:28 -08001676 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001677 image.setImageBitmap(bitmap);
1678 image.setTag(i);
1679 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001680 image.setOnFocusChangeListener(handler);
1681 image.setFocusable(true);
1682 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001683
1684 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001685 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1686
1687 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001688 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001689
1690 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001691 p.setContentView(preview);
1692 p.setWidth((int) (sWidth * count + extraW));
1693 p.setHeight((int) (sHeight + extraH));
1694 p.setAnimationStyle(R.style.AnimationPreview);
1695 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001696 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001697 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001698 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001699
Romain Guye6b8e2f2009-11-10 11:56:55 -08001700 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1701 public void onDismiss() {
1702 dismissPreview(anchor);
1703 }
1704 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001705
1706 anchor.setTag(p);
1707 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001708 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001709 }
1710
Romain Guy9d31e9f2009-11-11 18:54:28 -08001711 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001712 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001713
Romain Guye6b8e2f2009-11-10 11:56:55 -08001714 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001715 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001716 }
1717
1718 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001719 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001720 v.post(this);
1721 }
1722
1723 public void run() {
1724 dismissPreview(mAnchor);
1725 }
1726
1727 public void onFocusChange(View v, boolean hasFocus) {
1728 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001729 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001730 }
Romain Guya6abce82009-11-10 02:54:41 -08001731 }
1732 }
1733
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 Workspace getWorkspace() {
1735 return mWorkspace;
1736 }
1737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 @Override
1739 protected Dialog onCreateDialog(int id) {
1740 switch (id) {
1741 case DIALOG_CREATE_SHORTCUT:
1742 return new CreateShortcut().createDialog();
1743 case DIALOG_RENAME_FOLDER:
1744 return new RenameFolder().createDialog();
1745 }
1746
1747 return super.onCreateDialog(id);
1748 }
1749
1750 @Override
1751 protected void onPrepareDialog(int id, Dialog dialog) {
1752 switch (id) {
1753 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 break;
1755 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001756 if (mFolderInfo != null) {
1757 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1758 final CharSequence text = mFolderInfo.title;
1759 input.setText(text);
1760 input.setSelection(0, text.length());
1761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 break;
1763 }
1764 }
1765
1766 void showRenameDialog(FolderInfo info) {
1767 mFolderInfo = info;
1768 mWaitingForResult = true;
1769 showDialog(DIALOG_RENAME_FOLDER);
1770 }
1771
1772 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1773 mAddItemCellInfo = cellInfo;
1774 mWaitingForResult = true;
1775 showDialog(DIALOG_CREATE_SHORTCUT);
1776 }
1777
Joe Onoratodeb98af2010-02-19 14:59:39 -08001778 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001779 Bundle bundle = new Bundle();
1780
1781 ArrayList<String> shortcutNames = new ArrayList<String>();
1782 shortcutNames.add(getString(R.string.group_applications));
1783 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1784
1785 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1786 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1787 R.drawable.ic_launcher_application));
1788 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1789
1790 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1791 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001792 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001793 pickIntent.putExtras(bundle);
1794
Joe Onoratodeb98af2010-02-19 14:59:39 -08001795 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001796 }
1797
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 private class RenameFolder {
1799 private EditText mInput;
1800
1801 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1803 mInput = (EditText) layout.findViewById(R.id.folder_name);
1804
1805 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1806 builder.setIcon(0);
1807 builder.setTitle(getString(R.string.rename_folder_title));
1808 builder.setCancelable(true);
1809 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1810 public void onCancel(DialogInterface dialog) {
1811 cleanup();
1812 }
1813 });
1814 builder.setNegativeButton(getString(R.string.cancel_action),
1815 new Dialog.OnClickListener() {
1816 public void onClick(DialogInterface dialog, int which) {
1817 cleanup();
1818 }
1819 }
1820 );
1821 builder.setPositiveButton(getString(R.string.rename_action),
1822 new Dialog.OnClickListener() {
1823 public void onClick(DialogInterface dialog, int which) {
1824 changeFolderName();
1825 }
1826 }
1827 );
1828 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001829
1830 final AlertDialog dialog = builder.create();
1831 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1832 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001833 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001834 mInput.requestFocus();
1835 InputMethodManager inputManager = (InputMethodManager)
1836 getSystemService(Context.INPUT_METHOD_SERVICE);
1837 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001838 }
1839 });
1840
1841 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
1843
1844 private void changeFolderName() {
1845 final String name = mInput.getText().toString();
1846 if (!TextUtils.isEmpty(name)) {
1847 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 mFolderInfo.title = name;
1850 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1851
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001853 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 } else {
1856 final FolderIcon folderIcon = (FolderIcon)
1857 mWorkspace.getViewForTag(mFolderInfo);
1858 if (folderIcon != null) {
1859 folderIcon.setText(name);
1860 getWorkspace().requestLayout();
1861 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001862 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 mWorkspaceLoading = true;
1864 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866 }
1867 }
1868 cleanup();
1869 }
1870
1871 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 dismissDialog(DIALOG_RENAME_FOLDER);
1873 mWaitingForResult = false;
1874 mFolderInfo = null;
1875 }
1876 }
1877
Daniel Sandler843e8602010-06-07 14:59:01 -04001878 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1879 public boolean isAllAppsVisible() {
1880 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001881 }
1882
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001883 // AllAppsView.Watcher
1884 public void zoomed(float zoom) {
1885 if (zoom == 1.0f) {
1886 mWorkspace.setVisibility(View.GONE);
1887 }
1888 }
1889
Joe Onorato3a8820b2009-11-10 15:06:42 -08001890 void showAllApps(boolean animated) {
1891 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001892
Romain Guyc16fea72010-03-12 17:17:56 -08001893 ((View) mAllAppsGrid).setFocusable(true);
1894 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001895
Joe Onorato7c312c12009-08-13 21:36:53 -07001896 // TODO: fade these two too
1897 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001898 }
1899
Joe Onoratob2061212009-11-24 16:13:54 -05001900 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001901 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001902 * - Home from workspace
1903 * - from center screen
1904 * - from other screens
1905 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001906 * - from center screen
1907 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001908 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001909 * - from center screen
1910 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001911 * - Launch app from workspace and quit
1912 * - with back
1913 * - with home
1914 * - Launch app from all apps and quit
1915 * - with back
1916 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001917 * - Go to a screen that's not the default, then all
1918 * apps, and launch and app, and go back
1919 * - with back
1920 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001921 * - On workspace, long press power and go back
1922 * - with back
1923 * - with home
1924 * - On all apps, long press power and go back
1925 * - with back
1926 * - with home
1927 * - On workspace, power off
1928 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001929 * - Launch an app and turn off the screen while in that app
1930 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001931 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001932 * - From all apps
1933 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001934 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1935 * - From all apps
1936 * - From the center workspace
1937 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001938 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001939 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001940 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001941 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001942 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001943 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001944 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001945 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001946 }
1947
Joe Onorato7c312c12009-08-13 21:36:53 -07001948 void lockAllApps() {
1949 // TODO
1950 }
1951
1952 void unlockAllApps() {
1953 // TODO
1954 }
1955
Joe Onorato7404ee42009-07-31 11:54:44 -07001956 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 * Displays the shortcut creation dialog and launches, if necessary, the
1958 * appropriate activity.
1959 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001960 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001961 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1962 DialogInterface.OnShowListener {
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1970 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001971 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 builder.setInverseBackgroundForced(true);
1974
1975 AlertDialog dialog = builder.create();
1976 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001977 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001978 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 return dialog;
1981 }
1982
1983 public void onCancel(DialogInterface dialog) {
1984 mWaitingForResult = false;
1985 cleanup();
1986 }
1987
Romain Guycbb89e42009-06-08 15:52:54 -07001988 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001992 try {
1993 dismissDialog(DIALOG_CREATE_SHORTCUT);
1994 } catch (Exception e) {
1995 // An exception is thrown if the dialog is not visible, which is fine
1996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001999 /**
2000 * Handle the action clicked in the "Add to home" dialog.
2001 */
2002 public void onClick(DialogInterface dialog, int which) {
2003 Resources res = getResources();
2004 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002005
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 switch (which) {
2007 case AddAdapter.ITEM_SHORTCUT: {
2008 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002009 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002010 break;
2011 }
Romain Guycbb89e42009-06-08 15:52:54 -07002012
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002013 case AddAdapter.ITEM_APPWIDGET: {
2014 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002015
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002016 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2017 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002018 // start the pick activity
2019 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2020 break;
2021 }
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002023 case AddAdapter.ITEM_LIVE_FOLDER: {
2024 // Insert extra item to handle inserting folder
2025 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002026
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002027 ArrayList<String> shortcutNames = new ArrayList<String>();
2028 shortcutNames.add(res.getString(R.string.group_folder));
2029 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002030
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002031 ArrayList<ShortcutIconResource> shortcutIcons =
2032 new ArrayList<ShortcutIconResource>();
2033 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2034 R.drawable.ic_launcher_folder));
2035 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2036
2037 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2038 pickIntent.putExtra(Intent.EXTRA_INTENT,
2039 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2040 pickIntent.putExtra(Intent.EXTRA_TITLE,
2041 getText(R.string.title_select_live_folder));
2042 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002044 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2045 break;
2046 }
2047
2048 case AddAdapter.ITEM_WALLPAPER: {
2049 startWallpaper();
2050 break;
2051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
2053 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002054
2055 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002056 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
2059
2060 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002061 * Receives notifications when applications are added/removed.
2062 */
2063 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2064 @Override
2065 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002066 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002067 String reason = intent.getStringExtra("reason");
2068 if (!"homekey".equals(reason)) {
2069 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002070 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002071 animate = false;
2072 }
Joe Onoratob2061212009-11-24 16:13:54 -05002073 closeAllApps(animate);
2074 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002075 }
2076 }
2077
2078 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002079 * Receives notifications whenever the appwidgets are reset.
2080 */
2081 private class AppWidgetResetObserver extends ContentObserver {
2082 public AppWidgetResetObserver() {
2083 super(new Handler());
2084 }
2085
2086 @Override
2087 public void onChange(boolean selfChange) {
2088 onAppWidgetReset();
2089 }
2090 }
2091
2092 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 public int getCurrentWorkspaceScreen() {
2096 return mWorkspace.getCurrentScreen();
2097 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002098
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099 /**
2100 * Refreshes the shortcuts shown on the workspace.
2101 *
2102 * Implementation of the method from LauncherModel.Callbacks.
2103 */
2104 public void startBinding() {
2105 final Workspace workspace = mWorkspace;
2106 int count = workspace.getChildCount();
2107 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002108 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002109 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2110 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002111
Joe Onorato9c1289c2009-08-17 11:03:03 -04002112 if (DEBUG_USER_INTERFACE) {
2113 android.widget.Button finishButton = new android.widget.Button(this);
2114 finishButton.setText("Finish");
2115 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2116
2117 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2118 public void onClick(View v) {
2119 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002121 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123 }
2124
2125 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126 * Bind the items start-end from the list.
2127 *
2128 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002130 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2131
2132 final Workspace workspace = mWorkspace;
2133
2134 for (int i=start; i<end; i++) {
2135 final ItemInfo item = shortcuts.get(i);
2136 mDesktopItems.add(item);
2137 switch (item.itemType) {
2138 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2139 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002140 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002141 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2142 false);
2143 break;
2144 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2145 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2146 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2147 (UserFolderInfo) item);
2148 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2149 false);
2150 break;
2151 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2152 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2153 R.layout.live_folder_icon, this,
2154 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2155 (LiveFolderInfo) item);
2156 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2157 false);
2158 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 }
2161
Joe Onorato9c1289c2009-08-17 11:03:03 -04002162 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 /**
2166 * Implementation of the method from LauncherModel.Callbacks.
2167 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002168 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2169 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002172
2173 /**
2174 * Add the views for a widget to the workspace.
2175 *
2176 * Implementation of the method from LauncherModel.Callbacks.
2177 */
2178 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002179 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2180 if (DEBUG_WIDGETS) {
2181 Log.d(TAG, "bindAppWidget: " + item);
2182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002183 final Workspace workspace = mWorkspace;
2184
2185 final int appWidgetId = item.appWidgetId;
2186 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002187 if (DEBUG_WIDGETS) {
2188 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2189 }
2190
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2192
Joe Onorato9c1289c2009-08-17 11:03:03 -04002193 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2194 item.hostView.setTag(item);
2195
2196 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2197 item.cellY, item.spanX, item.spanY, false);
2198
2199 workspace.requestLayout();
2200
2201 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002202
2203 if (DEBUG_WIDGETS) {
2204 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2205 + (SystemClock.uptimeMillis()-start) + "ms");
2206 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002207 }
2208
2209 /**
2210 * Callback saying that there aren't any more items to bind.
2211 *
2212 * Implementation of the method from LauncherModel.Callbacks.
2213 */
2214 public void finishBindingItems() {
2215 if (mSavedState != null) {
2216 if (!mWorkspace.hasFocus()) {
2217 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2218 }
2219
2220 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2221 if (userFolders != null) {
2222 for (long folderId : userFolders) {
2223 final FolderInfo info = mFolders.get(folderId);
2224 if (info != null) {
2225 openFolder(info);
2226 }
2227 }
2228 final Folder openFolder = mWorkspace.getOpenFolder();
2229 if (openFolder != null) {
2230 openFolder.requestFocus();
2231 }
2232 }
2233
Joe Onorato9c1289c2009-08-17 11:03:03 -04002234 mSavedState = null;
2235 }
2236
2237 if (mSavedInstanceState != null) {
2238 super.onRestoreInstanceState(mSavedInstanceState);
2239 mSavedInstanceState = null;
2240 }
2241
Joe Onorato9c1289c2009-08-17 11:03:03 -04002242 mWorkspaceLoading = false;
2243 }
2244
2245 /**
2246 * Add the icons for all apps.
2247 *
2248 * Implementation of the method from LauncherModel.Callbacks.
2249 */
2250 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002251 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 }
2253
2254 /**
2255 * A package was installed.
2256 *
2257 * Implementation of the method from LauncherModel.Callbacks.
2258 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002259 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002261 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002262 }
2263
2264 /**
2265 * A package was updated.
2266 *
2267 * Implementation of the method from LauncherModel.Callbacks.
2268 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002269 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002270 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002271 mWorkspace.updateShortcuts(apps);
2272 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 }
2274
2275 /**
2276 * A package was uninstalled.
2277 *
2278 * Implementation of the method from LauncherModel.Callbacks.
2279 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002280 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002281 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002282 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002283 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002284 }
Joe Onoratobe386092009-11-17 17:32:16 -08002285
2286 /**
2287 * Prints out out state for debugging.
2288 */
2289 public void dumpState() {
2290 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002291 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002292 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2293 Log.d(TAG, "mRestoring=" + mRestoring);
2294 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2295 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2296 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2297 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002298 mModel.dumpState();
2299 mAllAppsGrid.dumpState();
2300 Log.d(TAG, "END launcher2 dump state");
2301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302}