blob: 62aaa8be6a774cb1aa0cb9f3cc03a88bd8f6cb75 [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";
Daniel Sandler843e8602010-06-07 14:59:01 -040095 static final boolean LOGD = true;
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
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700250 // We have a new AllAppsView, we need to re-bind everything, and it could have
251 // changed in our absence.
252 mModel.setAllAppsDirty();
253 mModel.setWorkspaceDirty();
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 }
258
259 // For handling default keys
260 mDefaultKeySsb = new SpannableStringBuilder();
261 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800262
263 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
264 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
Romain Guycbb89e42009-06-08 15:52:54 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700268 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
269 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 final Configuration configuration = getResources().getConfiguration();
272
Romain Guy98d01652009-06-30 16:21:04 -0700273 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 final String locale = configuration.locale.toString();
275
Romain Guy98d01652009-06-30 16:21:04 -0700276 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 final int mcc = configuration.mcc;
278
Romain Guy98d01652009-06-30 16:21:04 -0700279 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 final int mnc = configuration.mnc;
281
Romain Guy84f296c2009-11-04 15:00:44 -0800282 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Romain Guy84f296c2009-11-04 15:00:44 -0800284 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700285 localeConfiguration.locale = locale;
286 localeConfiguration.mcc = mcc;
287 localeConfiguration.mnc = mnc;
288
289 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800290 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400291
292 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700293 }
294 }
295
296 private static class LocaleConfiguration {
297 public String locale;
298 public int mcc = -1;
299 public int mnc = -1;
300 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700301
Romain Guy98d01652009-06-30 16:21:04 -0700302 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
303 DataInputStream in = null;
304 try {
305 in = new DataInputStream(context.openFileInput(PREFERENCES));
306 configuration.locale = in.readUTF();
307 configuration.mcc = in.readInt();
308 configuration.mnc = in.readInt();
309 } catch (FileNotFoundException e) {
310 // Ignore
311 } catch (IOException e) {
312 // Ignore
313 } finally {
314 if (in != null) {
315 try {
316 in.close();
317 } catch (IOException e) {
318 // Ignore
319 }
320 }
321 }
322 }
323
324 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
325 DataOutputStream out = null;
326 try {
327 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
328 out.writeUTF(configuration.locale);
329 out.writeInt(configuration.mcc);
330 out.writeInt(configuration.mnc);
331 out.flush();
332 } catch (FileNotFoundException e) {
333 // Ignore
334 } catch (IOException e) {
335 //noinspection ResultOfMethodCallIgnored
336 context.getFileStreamPath(PREFERENCES).delete();
337 } finally {
338 if (out != null) {
339 try {
340 out.close();
341 } catch (IOException e) {
342 // Ignore
343 }
344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
346 }
347
348 static int getScreen() {
349 synchronized (sLock) {
350 return sScreen;
351 }
352 }
353
354 static void setScreen(int screen) {
355 synchronized (sLock) {
356 sScreen = screen;
357 }
358 }
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700361 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
363 Display display = getWindowManager().getDefaultDisplay();
364 boolean isPortrait = display.getWidth() < display.getHeight();
365
366 final int width = isPortrait ? display.getWidth() : display.getHeight();
367 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700368 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
370
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400371 // Note: This doesn't do all the client-id magic that BrowserProvider does
372 // in Browser. (http://b/2425179)
373 private Uri getDefaultBrowserUri() {
374 String url = getString(R.string.default_browser_url);
375 if (url.indexOf("{CID}") != -1) {
376 url = url.replace("{CID}", "android-google");
377 }
378 return Uri.parse(url);
379 }
380
381 // Load the Intent templates from arrays.xml to populate the hotseats. For
382 // each Intent, if it resolves to a single app, use that as the launch
383 // intent & use that app's label as the contentDescription. Otherwise,
384 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400385 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400386 if (mHotseatConfig == null) {
387 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
388 if (mHotseatConfig.length > 0) {
389 mHotseats = new Intent[mHotseatConfig.length];
390 mHotseatLabels = new CharSequence[mHotseatConfig.length];
391 mHotseatIcons = new Drawable[mHotseatConfig.length];
392 } else {
393 mHotseats = null;
394 mHotseatIcons = null;
395 mHotseatLabels = null;
396 }
397
398 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
399 for (int i=0; i<mHotseatConfig.length; i++) {
400 // load icon for this slot; currently unrelated to the actual activity
401 try {
402 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
403 } catch (ArrayIndexOutOfBoundsException ex) {
404 Log.w(TAG, "Missing hotseat_icons array item #" + i);
405 mHotseatIcons[i] = null;
406 }
407 }
408 hotseatIconDrawables.recycle();
409 }
410
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400411 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400412 for (int i=0; i<mHotseatConfig.length; i++) {
413 Intent intent = null;
414 if (mHotseatConfig[i].equals("*BROWSER*")) {
415 // magic value meaning "launch user's default web browser"
416 // replace it with a generic web request so we can see if there is indeed a default
417 String defaultUri = getString(R.string.default_browser_url);
418 intent = new Intent(
419 Intent.ACTION_VIEW,
420 ((defaultUri != null)
421 ? Uri.parse(defaultUri)
422 : getDefaultBrowserUri())
423 ).addCategory(Intent.CATEGORY_BROWSABLE);
424 // note: if the user launches this without a default set, she
425 // will always be taken to the default URL above; this is
426 // unavoidable as we must specify a valid URL in order for the
427 // chooser to appear, and once the user selects something, that
428 // URL is unavoidably sent to the chosen app.
429 } else {
430 try {
431 intent = Intent.parseUri(mHotseatConfig[i], 0);
432 } catch (java.net.URISyntaxException ex) {
433 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
434 // bogus; leave intent=null
435 }
436 }
437
438 if (intent == null) {
439 mHotseats[i] = null;
440 mHotseatLabels[i] = getText(R.string.activity_not_found);
441 continue;
442 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400443
444 if (LOGD) {
445 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400446 + " initial intent=["
447 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400448 + "]");
449 }
450
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400451 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
452 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
453 if (LOGD) {
454 Log.d(TAG, "Best match for intent: " + bestMatch);
455 Log.d(TAG, "All matches: ");
456 for (ResolveInfo ri : allMatches) {
457 Log.d(TAG, " --> " + ri);
458 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400459 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400460 // did this resolve to a single app, or the resolver?
461 if (allMatches.size() == 0 || bestMatch == null) {
462 // can't find any activity to handle this. let's leave the
463 // intent as-is and let Launcher show a toast when it fails
464 // to launch.
465 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400466
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400467 // set accessibility text to "Not installed"
468 mHotseatLabels[i] = getText(R.string.activity_not_found);
469 } else {
470 boolean found = false;
471 for (ResolveInfo ri : allMatches) {
472 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
473 && bestMatch.activityInfo.applicationInfo.packageName
474 .equals(ri.activityInfo.applicationInfo.packageName)) {
475 found = true;
476 break;
477 }
478 }
479
480 if (!found) {
481 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
482 // the bestMatch is probably the ResolveActivity, meaning the
483 // user has not yet selected a default
484 // so: we'll keep the original intent for now
485 mHotseats[i] = intent;
486
487 // set the accessibility text to "Select shortcut"
488 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
489 } else {
490 // we have an app!
491 // now reconstruct the intent to launch it through the front
492 // door
493 ComponentName com = new ComponentName(
494 bestMatch.activityInfo.applicationInfo.packageName,
495 bestMatch.activityInfo.name);
496 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
497
498 // load the app label for accessibility
499 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
500 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400501 }
502
503 if (LOGD) {
504 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400505 + " final intent=["
506 + ((mHotseats[i] == null)
507 ? "null"
508 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400509 + "] label=[" + mHotseatLabels[i]
510 + "]"
511 );
512 }
513 }
514 }
515
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 @Override
517 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700518 mWaitingForResult = false;
519
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 // The pattern used here is that a user PICKs a specific application,
521 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700522
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
524 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700525
Romain Guy94dabf12009-07-21 10:55:43 -0700526 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 switch (requestCode) {
528 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 break;
531 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800532 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 break;
534 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 break;
537 case REQUEST_PICK_LIVE_FOLDER:
538 addLiveFolder(data);
539 break;
540 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 case REQUEST_PICK_APPWIDGET:
544 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700549 case REQUEST_PICK_WALLPAPER:
550 // We just wanted the activity result here so we can clear mWaitingForResult
551 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 }
Romain Guy18042c82009-11-06 11:44:55 -0800553 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
554 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
555 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 // Clean up the appWidgetId if we canceled
557 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
558 if (appWidgetId != -1) {
559 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
563
564 @Override
565 protected void onResume() {
566 super.onResume();
567
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800568 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 mWorkspaceLoading = true;
572 mModel.startLoader(this, true);
573 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575 }
576
577 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700578 protected void onPause() {
579 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800580 dismissPreview(mPreviousView);
581 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800582 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 }
Romain Guycbb89e42009-06-08 15:52:54 -0700584
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700585 @Override
586 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400587 // Flag the loader to stop early before switching
588 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800589 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800590 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700591 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700592
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800593 // We can't hide the IME if it was forced open. So don't bother
594 /*
595 @Override
596 public void onWindowFocusChanged(boolean hasFocus) {
597 super.onWindowFocusChanged(hasFocus);
598
599 if (hasFocus) {
600 final InputMethodManager inputManager = (InputMethodManager)
601 getSystemService(Context.INPUT_METHOD_SERVICE);
602 WindowManager.LayoutParams lp = getWindow().getAttributes();
603 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
604 android.os.Handler()) {
605 protected void onReceiveResult(int resultCode, Bundle resultData) {
606 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
607 }
608 });
609 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
610 }
611 }
612 */
613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 private boolean acceptFilter() {
615 final InputMethodManager inputManager = (InputMethodManager)
616 getSystemService(Context.INPUT_METHOD_SERVICE);
617 return !inputManager.isFullscreenMode();
618 }
619
620 @Override
621 public boolean onKeyDown(int keyCode, KeyEvent event) {
622 boolean handled = super.onKeyDown(keyCode, event);
623 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
624 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
625 keyCode, event);
626 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700627 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700628 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700629 // showSearchDialog()
630 // If there are multiple keystrokes before the search dialog takes focus,
631 // onSearchRequested() will be called for every keystroke,
632 // but it is idempotent, so it's fine.
633 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
635 }
636
Joe Onorato8a9625e2010-01-28 15:55:35 -0800637 // Eat the long press event so the keyboard doesn't come up.
638 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
639 return true;
640 }
641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 return handled;
643 }
644
Karl Rosaen138a0412009-04-23 19:00:21 -0700645 private String getTypedText() {
646 return mDefaultKeySsb.toString();
647 }
648
649 private void clearTypedText() {
650 mDefaultKeySsb.clear();
651 mDefaultKeySsb.clearSpans();
652 Selection.setSelection(mDefaultKeySsb, 0);
653 }
654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 /**
656 * Restores the previous state, if it exists.
657 *
658 * @param savedState The previous state.
659 */
660 private void restoreState(Bundle savedState) {
661 if (savedState == null) {
662 return;
663 }
664
Joe Onorato3a8820b2009-11-10 15:06:42 -0800665 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
666 if (allApps) {
667 showAllApps(false);
668 }
669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
671 if (currentScreen > -1) {
672 mWorkspace.setCurrentScreen(currentScreen);
673 }
674
675 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
676 if (addScreen > -1) {
677 mAddItemCellInfo = new CellLayout.CellInfo();
678 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
679 addItemCellInfo.valid = true;
680 addItemCellInfo.screen = addScreen;
681 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
682 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
683 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
684 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
685 addItemCellInfo.findVacantCellsFromOccupied(
686 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
687 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
688 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
689 mRestoring = true;
690 }
691
692 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
693 if (renameFolder) {
694 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 mRestoring = true;
697 }
698 }
699
700 /**
701 * Finds all the views we need and configure them properly.
702 */
703 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400704 DragController dragController = mDragController;
705
Romain Guyb1b69f52009-08-10 15:10:15 -0700706 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400707 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708
Joe Onorato7c312c12009-08-13 21:36:53 -0700709 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700710 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700711 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800712 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700713 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800714 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
717 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500718 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719
Joe Onorato7c312c12009-08-13 21:36:53 -0700720 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
721 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722
Joe Onorato7c312c12009-08-13 21:36:53 -0700723 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700725 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800726 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800727
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400728 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
729 hotseatLeft.setContentDescription(mHotseatLabels[0]);
730 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
731 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
732 hotseatRight.setContentDescription(mHotseatLabels[1]);
733 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400734
Romain Guy1fbc1c82009-11-09 20:43:08 -0800735 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
736 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
737
738 Drawable previous = mPreviousView.getDrawable();
739 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800740 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741
Joe Onorato0d44e942009-11-16 18:20:51 -0800742 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800743 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800744 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800745 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400748 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750
751 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400752 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400753 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
Joe Onorato00acb122009-08-04 16:04:30 -0400755 dragController.setDragScoller(workspace);
756 dragController.setDragListener(deleteZone);
757 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800758 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700759
Joe Onorato00acb122009-08-04 16:04:30 -0400760 // The order here is bottom to top.
761 dragController.addDropTarget(workspace);
762 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
764
Romain Guy8a73c512009-11-09 19:19:59 -0800765 @SuppressWarnings({"UnusedDeclaration"})
766 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800767 if (!isAllAppsVisible()) {
768 mWorkspace.scrollLeft();
769 }
Romain Guy8a73c512009-11-09 19:19:59 -0800770 }
771
772 @SuppressWarnings({"UnusedDeclaration"})
773 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800774 if (!isAllAppsVisible()) {
775 mWorkspace.scrollRight();
776 }
Romain Guy8a73c512009-11-09 19:19:59 -0800777 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400778
779 @SuppressWarnings({"UnusedDeclaration"})
780 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400781 if (isAllAppsVisible()) return;
782
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400783 int index = -1;
784 if (v.getId() == R.id.hotseat_left) {
785 index = 0;
786 } else if (v.getId() == R.id.hotseat_right) {
787 index = 1;
788 }
789
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400790 // reload these every tap; you never know when they might change
791 loadHotseats();
792 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
793 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400794 startActivitySafely(
795 mHotseats[index],
796 "hotseat"
797 );
798 }
799 }
Romain Guy8a73c512009-11-09 19:19:59 -0800800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 /**
802 * Creates a view representing a shortcut.
803 *
804 * @param info The data structure describing the shortcut.
805 *
806 * @return A View inflated from R.layout.application.
807 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800808 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 return createShortcut(R.layout.application,
810 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
811 }
812
813 /**
814 * Creates a view representing a shortcut inflated from the specified resource.
815 *
816 * @param layoutResId The id of the XML layout used to create the shortcut.
817 * @param parent The group the shortcut belongs to.
818 * @param info The data structure describing the shortcut.
819 *
820 * @return A View inflated from layoutResId.
821 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800822 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
824
Joe Onorato0589f0f2010-02-08 13:44:00 -0800825 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
826 new FastBitmapDrawable(info.getIcon(mIconCache)),
827 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 favorite.setText(info.title);
829 favorite.setTag(info);
830 favorite.setOnClickListener(this);
831
832 return favorite;
833 }
834
835 /**
836 * Add an application shortcut to the workspace.
837 *
838 * @param data The intent describing the application.
839 * @param cellInfo The position on screen where to create the shortcut.
840 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 cellInfo.screen = mWorkspace.getCurrentScreen();
843 if (!findSingleSlot(cellInfo)) return;
844
Joe Onorato0589f0f2010-02-08 13:44:00 -0800845 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
846 data, context);
847
Romain Guy73b979d2009-06-09 12:57:21 -0700848 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800849 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800851 info.container = ItemInfo.NO_ID;
852 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
853 } else {
854 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
856 }
Romain Guycbb89e42009-06-08 15:52:54 -0700857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 /**
859 * Add a shortcut to the workspace.
860 *
861 * @param data The intent describing the shortcut.
862 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 cellInfo.screen = mWorkspace.getCurrentScreen();
866 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700867
Joe Onorato0589f0f2010-02-08 13:44:00 -0800868 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869
870 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
873 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
875 }
876
Romain Guycbb89e42009-06-08 15:52:54 -0700877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700879 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 * @param data The intent describing the appWidgetId.
882 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700887
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800888 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700889
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700890 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700891
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700892 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 // Try finding open space on Launcher screen
897 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800898 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
899 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
900 return;
901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700903 // Build Launcher-specific widget info and save to database
904 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 launcherInfo.spanX = spans[0];
906 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 LauncherModel.addItemToDatabase(this, launcherInfo,
909 LauncherSettings.Favorites.CONTAINER_DESKTOP,
910 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
911
912 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700916 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700918 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924 }
Romain Guycbb89e42009-06-08 15:52:54 -0700925
Joe Onorato9c1289c2009-08-17 11:03:03 -0400926 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
927 mDesktopItems.remove(launcherInfo);
928 launcherInfo.hostView = null;
929 }
930
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700931 public LauncherAppWidgetHost getAppWidgetHost() {
932 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
Romain Guycbb89e42009-06-08 15:52:54 -0700934
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800935 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800936 getWindow().closeAllPanels();
937
938 try {
939 dismissDialog(DIALOG_CREATE_SHORTCUT);
940 // Unlock the workspace if the dialog was showing
941 } catch (Exception e) {
942 // An exception is thrown if the dialog is not visible, which is fine
943 }
944
945 try {
946 dismissDialog(DIALOG_RENAME_FOLDER);
947 // Unlock the workspace if the dialog was showing
948 } catch (Exception e) {
949 // An exception is thrown if the dialog is not visible, which is fine
950 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800951
952 // Whatever we were doing is hereby canceled.
953 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800954 }
955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 @Override
957 protected void onNewIntent(Intent intent) {
958 super.onNewIntent(intent);
959
960 // Close the menu
961 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800962 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800963 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700964
Joe Onorato14f122b2009-11-19 14:06:36 -0800965 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
966 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
967 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800968 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800969 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800970 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800971 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700972
Joe Onorato3a8820b2009-11-10 15:06:42 -0800973 final View v = getWindow().peekDecorView();
974 if (v != null && v.getWindowToken() != null) {
975 InputMethodManager imm = (InputMethodManager)getSystemService(
976 INPUT_METHOD_SERVICE);
977 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
979 }
980 }
981
982 @Override
983 protected void onRestoreInstanceState(Bundle savedInstanceState) {
984 // Do not call super here
985 mSavedInstanceState = savedInstanceState;
986 }
987
988 @Override
989 protected void onSaveInstanceState(Bundle outState) {
990 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
991
992 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
993 if (folders.size() > 0) {
994 final int count = folders.size();
995 long[] ids = new long[count];
996 for (int i = 0; i < count; i++) {
997 final FolderInfo info = folders.get(i).getInfo();
998 ids[i] = info.id;
999 }
1000 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1001 } else {
1002 super.onSaveInstanceState(outState);
1003 }
1004
Joe Onoratofb0ca672009-09-14 17:55:46 -04001005 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001006 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009
1010 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1011 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1012 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1013
1014 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1015 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1016 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1017 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1018 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1019 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1020 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1021 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1022 layout.getOccupiedCells());
1023 }
1024
1025 if (mFolderInfo != null && mWaitingForResult) {
1026 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1027 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1028 }
1029 }
1030
1031 @Override
1032 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001034
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001038 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040
1041 TextKeyListener.getInstance().release();
1042
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001046
1047 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001048
1049 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001050 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001051
1052 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054
1055 @Override
1056 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001057 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 super.startActivityForResult(intent, requestCode);
1059 }
1060
1061 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001062 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001064
Joe Onorato7bb17492009-09-24 17:51:01 -07001065 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Karl Rosaen138a0412009-04-23 19:00:21 -07001067 if (initialQuery == null) {
1068 // Use any text typed in the launcher as the initial query
1069 initialQuery = getTypedText();
1070 clearTypedText();
1071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 if (appSearchData == null) {
1073 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001074 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 }
Romain Guycbb89e42009-06-08 15:52:54 -07001076
Karl Rosaen138a0412009-04-23 19:00:21 -07001077 final SearchManager searchManager =
1078 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001079 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001080 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082
1083 @Override
1084 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 if (isWorkspaceLocked()) {
1086 return false;
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
1089 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1092 .setIcon(android.R.drawable.ic_menu_add)
1093 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001094 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 .setIcon(android.R.drawable.ic_menu_gallery)
1096 .setAlphabeticShortcut('W');
1097 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1098 .setIcon(android.R.drawable.ic_search_category_default)
1099 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1100 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1101 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1102 .setAlphabeticShortcut('N');
1103
1104 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001105 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1106 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107
1108 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1109 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1110 .setIntent(settings);
1111
1112 return true;
1113 }
1114
1115 @Override
1116 public boolean onPrepareOptionsMenu(Menu menu) {
1117 super.onPrepareOptionsMenu(menu);
1118
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001119 // If all apps is animating, don't show the menu, because we don't know
1120 // which one to show.
1121 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1122 return false;
1123 }
1124
1125 // Only show the add and wallpaper options when we're not in all apps.
1126 boolean visible = !mAllAppsGrid.isOpaque();
1127 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1128 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1129
1130 // Disable add if the workspace is full.
1131 if (visible) {
1132 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1133 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135
1136 return true;
1137 }
1138
1139 @Override
1140 public boolean onOptionsItemSelected(MenuItem item) {
1141 switch (item.getItemId()) {
1142 case MENU_ADD:
1143 addItems();
1144 return true;
1145 case MENU_WALLPAPER_SETTINGS:
1146 startWallpaper();
1147 return true;
1148 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001149 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 return true;
1151 case MENU_NOTIFICATIONS:
1152 showNotifications();
1153 return true;
1154 }
1155
1156 return super.onOptionsItemSelected(item);
1157 }
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Karl Rosaen138a0412009-04-23 19:00:21 -07001159 /**
1160 * Indicates that we want global search for this activity by setting the globalSearch
1161 * argument for {@link #startSearch} to true.
1162 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001164 @Override
1165 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001166 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001167 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001168 }
1169
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 public boolean isWorkspaceLocked() {
1171 return mWorkspaceLoading || mWaitingForResult;
1172 }
1173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001175 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 showAddDialog(mMenuAddInfo);
1177 }
1178
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 void addAppWidget(Intent data) {
1180 // TODO: catch bad widget exception when sent
1181 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001182 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
Bjorn Bringert7984c942009-12-09 15:38:25 +00001184 if (appWidget.configure != null) {
1185 // Launch over to configure widget, if needed
1186 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1187 intent.setComponent(appWidget.configure);
1188 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1189
Romain Guy8e633c52010-03-04 12:51:36 -08001190 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001192 // Otherwise just add it
1193 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
1195 }
Romain Guycbb89e42009-06-08 15:52:54 -07001196
Joe Onoratodeb98af2010-02-19 14:59:39 -08001197 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001198 // Handle case where user selected "Applications"
1199 String applicationName = getResources().getString(R.string.group_applications);
1200 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001201
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001202 if (applicationName != null && applicationName.equals(shortcutName)) {
1203 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1204 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001205
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001206 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1207 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001208 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001209 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001210 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213
1214 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001215 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001216 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001217 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001219 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001221 } else {
1222 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
Joe Onorato9c1289c2009-08-17 11:03:03 -04001226 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 UserFolderInfo folderInfo = new UserFolderInfo();
1228 folderInfo.title = getText(R.string.folder_name);
1229
1230 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1231 cellInfo.screen = mWorkspace.getCurrentScreen();
1232 if (!findSingleSlot(cellInfo)) return;
1233
1234 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001235 LauncherModel.addItemToDatabase(this, folderInfo,
1236 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
1240 // Create the view
1241 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1242 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1243 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
Romain Guycbb89e42009-06-08 15:52:54 -07001246
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 void removeFolder(FolderInfo folder) {
1248 mFolders.remove(folder.id);
1249 }
1250
1251 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 cellInfo.screen = mWorkspace.getCurrentScreen();
1253 if (!findSingleSlot(cellInfo)) return;
1254
1255 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1256
1257 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1259 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001260 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1261 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263 }
1264
1265 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1266 CellLayout.CellInfo cellInfo, boolean notify) {
1267
1268 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1269 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1270
1271 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 Intent.ShortcutIconResource iconResource = null;
1273
1274 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1275 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1276 try {
1277 iconResource = (Intent.ShortcutIconResource) extra;
1278 final PackageManager packageManager = context.getPackageManager();
1279 Resources resources = packageManager.getResourcesForApplication(
1280 iconResource.packageName);
1281 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1282 icon = resources.getDrawable(id);
1283 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001284 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286 }
1287
1288 if (icon == null) {
1289 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1290 }
1291
1292 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001293 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 info.title = name;
1295 info.iconResource = iconResource;
1296 info.uri = data.getData();
1297 info.baseIntent = baseIntent;
1298 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1299 LiveFolders.DISPLAY_MODE_GRID);
1300
1301 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1302 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
1305 return info;
1306 }
1307
1308 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1309 final int[] xy = new int[2];
1310 if (findSlot(cellInfo, xy, 1, 1)) {
1311 cellInfo.cellX = xy[0];
1312 cellInfo.cellY = xy[1];
1313 return true;
1314 }
1315 return false;
1316 }
1317
1318 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1319 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1320 boolean[] occupied = mSavedState != null ?
1321 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1322 cellInfo = mWorkspace.findAllVacantCells(occupied);
1323 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1324 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1325 return false;
1326 }
1327 }
1328 return true;
1329 }
1330
1331 private void showNotifications() {
1332 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1333 if (statusBar != null) {
1334 statusBar.expand();
1335 }
1336 }
1337
1338 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001339 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001341 Intent chooser = Intent.createChooser(pickWallpaper,
1342 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001343 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1344 // Removed in Eclair MR1
1345// WallpaperManager wm = (WallpaperManager)
1346// getSystemService(Context.WALLPAPER_SERVICE);
1347// WallpaperInfo wi = wm.getWallpaperInfo();
1348// if (wi != null && wi.getSettingsActivity() != null) {
1349// LabeledIntent li = new LabeledIntent(getPackageName(),
1350// R.string.configure_wallpaper, 0);
1351// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1352// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1353// }
Mike Clerona0618e42009-10-22 13:55:21 -07001354 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001357 /**
1358 * Registers various content observers. The current implementation registers
1359 * only a favorites observer to keep track of the favorites applications.
1360 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001361 private void registerContentObservers() {
1362 ContentResolver resolver = getContentResolver();
1363 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1364 true, mWidgetObserver);
1365 }
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 @Override
1368 public boolean dispatchKeyEvent(KeyEvent event) {
1369 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1370 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001372 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001373 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001374 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001375 dumpState();
1376 return true;
1377 }
1378 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001379 }
1380 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1381 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001382 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 return true;
1384 }
1385 }
1386
1387 return super.dispatchKeyEvent(event);
1388 }
1389
Joe Onorato88ec0992009-11-19 13:16:06 -08001390 @Override
1391 public void onBackPressed() {
1392 if (isAllAppsVisible()) {
1393 closeAllApps(true);
1394 } else {
1395 closeFolder();
1396 }
1397 dismissPreview(mPreviousView);
1398 dismissPreview(mNextView);
1399 }
1400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 private void closeFolder() {
1402 Folder folder = mWorkspace.getOpenFolder();
1403 if (folder != null) {
1404 closeFolder(folder);
1405 }
1406 }
1407
1408 void closeFolder(Folder folder) {
1409 folder.getInfo().opened = false;
1410 ViewGroup parent = (ViewGroup) folder.getParent();
1411 if (parent != null) {
1412 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001413 if (folder instanceof DropTarget) {
1414 // Live folders aren't DropTargets.
1415 mDragController.removeDropTarget((DropTarget)folder);
1416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418 folder.onClose();
1419 }
1420
1421 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001422 * Re-listen when widgets are reset.
1423 */
1424 private void onAppWidgetReset() {
1425 mAppWidgetHost.startListening();
1426 }
1427
1428 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1430 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 private void unbindDesktopItems() {
1433 for (ItemInfo item: mDesktopItems) {
1434 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 /**
1439 * Launches the intent referred by the clicked shortcut.
1440 *
1441 * @param v The view representing the clicked shortcut.
1442 */
1443 public void onClick(View v) {
1444 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001445 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001447 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001448 int[] pos = new int[2];
1449 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001450 intent.setSourceBounds(new Rect(pos[0], pos[1],
1451 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001452 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 } else if (tag instanceof FolderInfo) {
1454 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001455 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001456 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001457 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001458 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001459 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462 }
1463
Joe Onoratof984e852010-03-25 09:47:45 -07001464 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1466 try {
1467 startActivity(intent);
1468 } catch (ActivityNotFoundException e) {
1469 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001470 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 } catch (SecurityException e) {
1472 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001473 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001475 "or use the exported attribute for this activity. "
1476 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478 }
Romain Guy8e633c52010-03-04 12:51:36 -08001479
1480 void startActivityForResultSafely(Intent intent, int requestCode) {
1481 try {
1482 startActivityForResult(intent, requestCode);
1483 } catch (ActivityNotFoundException e) {
1484 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1485 } catch (SecurityException e) {
1486 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1487 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1488 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1489 "or use the exported attribute for this activity.", e);
1490 }
1491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492
1493 private void handleFolderClick(FolderInfo folderInfo) {
1494 if (!folderInfo.opened) {
1495 // Close any open folder
1496 closeFolder();
1497 // Open the requested folder
1498 openFolder(folderInfo);
1499 } else {
1500 // Find the open folder...
1501 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1502 int folderScreen;
1503 if (openFolder != null) {
1504 folderScreen = mWorkspace.getScreenForView(openFolder);
1505 // .. and close it
1506 closeFolder(openFolder);
1507 if (folderScreen != mWorkspace.getCurrentScreen()) {
1508 // Close any folder open on the current screen
1509 closeFolder();
1510 // Pull the folder onto this screen
1511 openFolder(folderInfo);
1512 }
1513 }
1514 }
1515 }
1516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 /**
1518 * Opens the user fodler described by the specified tag. The opening of the folder
1519 * is animated relative to the specified View. If the View is null, no animation
1520 * is played.
1521 *
1522 * @param folderInfo The FolderInfo describing the folder to open.
1523 */
1524 private void openFolder(FolderInfo folderInfo) {
1525 Folder openFolder;
1526
1527 if (folderInfo instanceof UserFolderInfo) {
1528 openFolder = UserFolder.fromXml(this);
1529 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001530 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 } else {
1532 return;
1533 }
1534
Joe Onorato00acb122009-08-04 16:04:30 -04001535 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 openFolder.setLauncher(this);
1537
1538 openFolder.bind(folderInfo);
1539 folderInfo.opened = true;
1540
1541 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1542 openFolder.onOpen();
1543 }
1544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001546 switch (v.getId()) {
1547 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001548 if (!isAllAppsVisible()) {
1549 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1550 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001551 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001552 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001553 return true;
1554 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001555 if (!isAllAppsVisible()) {
1556 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1557 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001558 showPreviews(v);
1559 }
1560 return true;
1561 case R.id.all_apps_button:
1562 if (!isAllAppsVisible()) {
1563 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1564 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1565 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001566 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001567 return true;
1568 }
1569
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 return false;
1572 }
1573
1574 if (!(v instanceof CellLayout)) {
1575 v = (View) v.getParent();
1576 }
1577
1578 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1579
1580 // This happens when long clicking an item with the dpad/trackball
1581 if (cellInfo == null) {
1582 return true;
1583 }
1584
1585 if (mWorkspace.allowLongPress()) {
1586 if (cellInfo.cell == null) {
1587 if (cellInfo.valid) {
1588 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001589 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001590 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1591 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 showAddDialog(cellInfo);
1593 }
1594 } else {
1595 if (!(cellInfo.cell instanceof Folder)) {
1596 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001597 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1598 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 mWorkspace.startDrag(cellInfo);
1600 }
1601 }
1602 }
1603 return true;
1604 }
1605
Romain Guye6b8e2f2009-11-10 11:56:55 -08001606 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001607 private void dismissPreview(final View v) {
1608 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001609 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001610 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1611 public void onDismiss() {
1612 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1613 int count = group.getChildCount();
1614 for (int i = 0; i < count; i++) {
1615 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1616 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001617 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1618 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1619
1620 v.setTag(R.id.workspace, null);
1621 v.setTag(R.id.icon, null);
1622 window.setOnDismissListener(null);
1623 }
1624 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001625 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001626 }
1627 v.setTag(null);
1628 }
1629
Romain Guyf8e6a802009-12-07 17:48:02 -08001630 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001631 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001632 }
1633
Romain Guya6abce82009-11-10 02:54:41 -08001634 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001635 final Resources resources = getResources();
1636 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001637
Romain Guya6abce82009-11-10 02:54:41 -08001638 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001639
1640 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001641
Romain Guyf8e6a802009-12-07 17:48:02 -08001642 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001643 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001644 int extraW = (int) ((r.left + r.right) * max);
1645 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001646
1647 int aW = cell.getWidth() - extraW;
1648 float w = aW / max;
1649
1650 int width = cell.getWidth();
1651 int height = cell.getHeight();
1652 int x = cell.getLeftPadding();
1653 int y = cell.getTopPadding();
1654 width -= (x + cell.getRightPadding());
1655 height -= (y + cell.getBottomPadding());
1656
1657 float scale = w / width;
1658
1659 int count = end - start;
1660
1661 final float sWidth = width * scale;
1662 float sHeight = height * scale;
1663
Romain Guye6b8e2f2009-11-10 11:56:55 -08001664 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001665
Romain Guye6b8e2f2009-11-10 11:56:55 -08001666 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1667 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001668
1669 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001670 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001671 cell = (CellLayout) workspace.getChildAt(i);
1672
Romain Guyf8e6a802009-12-07 17:48:02 -08001673 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001674 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001675
1676 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001677 c.scale(scale, scale);
1678 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1679 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001680
Romain Guy9d31e9f2009-11-11 18:54:28 -08001681 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001682 image.setImageBitmap(bitmap);
1683 image.setTag(i);
1684 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001685 image.setOnFocusChangeListener(handler);
1686 image.setFocusable(true);
1687 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001688
1689 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001690 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1691
1692 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001693 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001694
1695 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001696 p.setContentView(preview);
1697 p.setWidth((int) (sWidth * count + extraW));
1698 p.setHeight((int) (sHeight + extraH));
1699 p.setAnimationStyle(R.style.AnimationPreview);
1700 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001701 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001702 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001703 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001704
Romain Guye6b8e2f2009-11-10 11:56:55 -08001705 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1706 public void onDismiss() {
1707 dismissPreview(anchor);
1708 }
1709 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001710
1711 anchor.setTag(p);
1712 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001713 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001714 }
1715
Romain Guy9d31e9f2009-11-11 18:54:28 -08001716 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001717 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001718
Romain Guye6b8e2f2009-11-10 11:56:55 -08001719 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001720 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001721 }
1722
1723 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001724 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001725 v.post(this);
1726 }
1727
1728 public void run() {
1729 dismissPreview(mAnchor);
1730 }
1731
1732 public void onFocusChange(View v, boolean hasFocus) {
1733 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001734 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001735 }
Romain Guya6abce82009-11-10 02:54:41 -08001736 }
1737 }
1738
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 Workspace getWorkspace() {
1740 return mWorkspace;
1741 }
1742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 @Override
1744 protected Dialog onCreateDialog(int id) {
1745 switch (id) {
1746 case DIALOG_CREATE_SHORTCUT:
1747 return new CreateShortcut().createDialog();
1748 case DIALOG_RENAME_FOLDER:
1749 return new RenameFolder().createDialog();
1750 }
1751
1752 return super.onCreateDialog(id);
1753 }
1754
1755 @Override
1756 protected void onPrepareDialog(int id, Dialog dialog) {
1757 switch (id) {
1758 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 break;
1760 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001761 if (mFolderInfo != null) {
1762 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1763 final CharSequence text = mFolderInfo.title;
1764 input.setText(text);
1765 input.setSelection(0, text.length());
1766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 break;
1768 }
1769 }
1770
1771 void showRenameDialog(FolderInfo info) {
1772 mFolderInfo = info;
1773 mWaitingForResult = true;
1774 showDialog(DIALOG_RENAME_FOLDER);
1775 }
1776
1777 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1778 mAddItemCellInfo = cellInfo;
1779 mWaitingForResult = true;
1780 showDialog(DIALOG_CREATE_SHORTCUT);
1781 }
1782
Joe Onoratodeb98af2010-02-19 14:59:39 -08001783 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001784 Bundle bundle = new Bundle();
1785
1786 ArrayList<String> shortcutNames = new ArrayList<String>();
1787 shortcutNames.add(getString(R.string.group_applications));
1788 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1789
1790 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1791 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1792 R.drawable.ic_launcher_application));
1793 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1794
1795 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1796 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001797 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001798 pickIntent.putExtras(bundle);
1799
Joe Onoratodeb98af2010-02-19 14:59:39 -08001800 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 private class RenameFolder {
1804 private EditText mInput;
1805
1806 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1808 mInput = (EditText) layout.findViewById(R.id.folder_name);
1809
1810 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1811 builder.setIcon(0);
1812 builder.setTitle(getString(R.string.rename_folder_title));
1813 builder.setCancelable(true);
1814 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1815 public void onCancel(DialogInterface dialog) {
1816 cleanup();
1817 }
1818 });
1819 builder.setNegativeButton(getString(R.string.cancel_action),
1820 new Dialog.OnClickListener() {
1821 public void onClick(DialogInterface dialog, int which) {
1822 cleanup();
1823 }
1824 }
1825 );
1826 builder.setPositiveButton(getString(R.string.rename_action),
1827 new Dialog.OnClickListener() {
1828 public void onClick(DialogInterface dialog, int which) {
1829 changeFolderName();
1830 }
1831 }
1832 );
1833 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001834
1835 final AlertDialog dialog = builder.create();
1836 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1837 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001838 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001839 mInput.requestFocus();
1840 InputMethodManager inputManager = (InputMethodManager)
1841 getSystemService(Context.INPUT_METHOD_SERVICE);
1842 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001843 }
1844 });
1845
1846 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848
1849 private void changeFolderName() {
1850 final String name = mInput.getText().toString();
1851 if (!TextUtils.isEmpty(name)) {
1852 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 mFolderInfo.title = name;
1855 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1856
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001858 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 mModel.setWorkspaceDirty();
1860 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 } else {
1862 final FolderIcon folderIcon = (FolderIcon)
1863 mWorkspace.getViewForTag(mFolderInfo);
1864 if (folderIcon != null) {
1865 folderIcon.setText(name);
1866 getWorkspace().requestLayout();
1867 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001868 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869 mModel.setWorkspaceDirty();
1870 mWorkspaceLoading = true;
1871 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873 }
1874 }
1875 cleanup();
1876 }
1877
1878 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 dismissDialog(DIALOG_RENAME_FOLDER);
1880 mWaitingForResult = false;
1881 mFolderInfo = null;
1882 }
1883 }
1884
Daniel Sandler843e8602010-06-07 14:59:01 -04001885 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1886 public boolean isAllAppsVisible() {
1887 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001888 }
1889
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001890 // AllAppsView.Watcher
1891 public void zoomed(float zoom) {
1892 if (zoom == 1.0f) {
1893 mWorkspace.setVisibility(View.GONE);
1894 }
1895 }
1896
Joe Onorato3a8820b2009-11-10 15:06:42 -08001897 void showAllApps(boolean animated) {
1898 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001899
Romain Guyc16fea72010-03-12 17:17:56 -08001900 ((View) mAllAppsGrid).setFocusable(true);
1901 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001902
Joe Onorato7c312c12009-08-13 21:36:53 -07001903 // TODO: fade these two too
1904 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001905 }
1906
Joe Onoratob2061212009-11-24 16:13:54 -05001907 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001908 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001909 * - Home from workspace
1910 * - from center screen
1911 * - from other screens
1912 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001913 * - from center screen
1914 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001915 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001916 * - from center screen
1917 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001918 * - Launch app from workspace and quit
1919 * - with back
1920 * - with home
1921 * - Launch app from all apps and quit
1922 * - with back
1923 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001924 * - Go to a screen that's not the default, then all
1925 * apps, and launch and app, and go back
1926 * - with back
1927 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001928 * - On workspace, long press power and go back
1929 * - with back
1930 * - with home
1931 * - On all apps, long press power and go back
1932 * - with back
1933 * - with home
1934 * - On workspace, power off
1935 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001936 * - Launch an app and turn off the screen while in that app
1937 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001938 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001939 * - From all apps
1940 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001941 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1942 * - From all apps
1943 * - From the center workspace
1944 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001945 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001946 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001947 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001948 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001949 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001950 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001951 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001952 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001953 }
1954
Joe Onorato7c312c12009-08-13 21:36:53 -07001955 void lockAllApps() {
1956 // TODO
1957 }
1958
1959 void unlockAllApps() {
1960 // TODO
1961 }
1962
Joe Onorato7404ee42009-07-31 11:54:44 -07001963 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 * Displays the shortcut creation dialog and launches, if necessary, the
1965 * appropriate activity.
1966 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001967 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001968 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1969 DialogInterface.OnShowListener {
1970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1977 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001978 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 builder.setInverseBackgroundForced(true);
1981
1982 AlertDialog dialog = builder.create();
1983 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001984 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001985 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 return dialog;
1988 }
1989
1990 public void onCancel(DialogInterface dialog) {
1991 mWaitingForResult = false;
1992 cleanup();
1993 }
1994
Romain Guycbb89e42009-06-08 15:52:54 -07001995 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001999 try {
2000 dismissDialog(DIALOG_CREATE_SHORTCUT);
2001 } catch (Exception e) {
2002 // An exception is thrown if the dialog is not visible, which is fine
2003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 /**
2007 * Handle the action clicked in the "Add to home" dialog.
2008 */
2009 public void onClick(DialogInterface dialog, int which) {
2010 Resources res = getResources();
2011 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002012
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002013 switch (which) {
2014 case AddAdapter.ITEM_SHORTCUT: {
2015 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002016 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002017 break;
2018 }
Romain Guycbb89e42009-06-08 15:52:54 -07002019
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002020 case AddAdapter.ITEM_APPWIDGET: {
2021 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002023 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2024 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002025 // start the pick activity
2026 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2027 break;
2028 }
Romain Guycbb89e42009-06-08 15:52:54 -07002029
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002030 case AddAdapter.ITEM_LIVE_FOLDER: {
2031 // Insert extra item to handle inserting folder
2032 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002034 ArrayList<String> shortcutNames = new ArrayList<String>();
2035 shortcutNames.add(res.getString(R.string.group_folder));
2036 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002038 ArrayList<ShortcutIconResource> shortcutIcons =
2039 new ArrayList<ShortcutIconResource>();
2040 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2041 R.drawable.ic_launcher_folder));
2042 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2043
2044 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2045 pickIntent.putExtra(Intent.EXTRA_INTENT,
2046 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2047 pickIntent.putExtra(Intent.EXTRA_TITLE,
2048 getText(R.string.title_select_live_folder));
2049 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002050
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002051 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2052 break;
2053 }
2054
2055 case AddAdapter.ITEM_WALLPAPER: {
2056 startWallpaper();
2057 break;
2058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002061
2062 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002063 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
2067 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002068 * Receives notifications when applications are added/removed.
2069 */
2070 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2071 @Override
2072 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002073 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002074 String reason = intent.getStringExtra("reason");
2075 if (!"homekey".equals(reason)) {
2076 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002077 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002078 animate = false;
2079 }
Joe Onoratob2061212009-11-24 16:13:54 -05002080 closeAllApps(animate);
2081 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002082 }
2083 }
2084
2085 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002086 * Receives notifications whenever the appwidgets are reset.
2087 */
2088 private class AppWidgetResetObserver extends ContentObserver {
2089 public AppWidgetResetObserver() {
2090 super(new Handler());
2091 }
2092
2093 @Override
2094 public void onChange(boolean selfChange) {
2095 onAppWidgetReset();
2096 }
2097 }
2098
2099 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002100 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002102 public int getCurrentWorkspaceScreen() {
2103 return mWorkspace.getCurrentScreen();
2104 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002105
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106 /**
2107 * Refreshes the shortcuts shown on the workspace.
2108 *
2109 * Implementation of the method from LauncherModel.Callbacks.
2110 */
2111 public void startBinding() {
2112 final Workspace workspace = mWorkspace;
2113 int count = workspace.getChildCount();
2114 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002115 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002116 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2117 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002118
Joe Onorato9c1289c2009-08-17 11:03:03 -04002119 if (DEBUG_USER_INTERFACE) {
2120 android.widget.Button finishButton = new android.widget.Button(this);
2121 finishButton.setText("Finish");
2122 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2123
2124 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2125 public void onClick(View v) {
2126 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 }
2130 }
2131
2132 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133 * Bind the items start-end from the list.
2134 *
2135 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2138
2139 final Workspace workspace = mWorkspace;
2140
2141 for (int i=start; i<end; i++) {
2142 final ItemInfo item = shortcuts.get(i);
2143 mDesktopItems.add(item);
2144 switch (item.itemType) {
2145 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2146 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002147 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2149 false);
2150 break;
2151 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2152 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2153 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2154 (UserFolderInfo) item);
2155 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2156 false);
2157 break;
2158 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2159 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2160 R.layout.live_folder_icon, this,
2161 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2162 (LiveFolderInfo) item);
2163 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2164 false);
2165 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 }
2168
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 }
2171
Joe Onorato9c1289c2009-08-17 11:03:03 -04002172 /**
2173 * Implementation of the method from LauncherModel.Callbacks.
2174 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002175 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2176 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179
2180 /**
2181 * Add the views for a widget to the workspace.
2182 *
2183 * Implementation of the method from LauncherModel.Callbacks.
2184 */
2185 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002186 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2187 if (DEBUG_WIDGETS) {
2188 Log.d(TAG, "bindAppWidget: " + item);
2189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 final Workspace workspace = mWorkspace;
2191
2192 final int appWidgetId = item.appWidgetId;
2193 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002194 if (DEBUG_WIDGETS) {
2195 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2196 }
2197
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2199
Joe Onorato9c1289c2009-08-17 11:03:03 -04002200 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2201 item.hostView.setTag(item);
2202
2203 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2204 item.cellY, item.spanX, item.spanY, false);
2205
2206 workspace.requestLayout();
2207
2208 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002209
2210 if (DEBUG_WIDGETS) {
2211 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2212 + (SystemClock.uptimeMillis()-start) + "ms");
2213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 }
2215
2216 /**
2217 * Callback saying that there aren't any more items to bind.
2218 *
2219 * Implementation of the method from LauncherModel.Callbacks.
2220 */
2221 public void finishBindingItems() {
2222 if (mSavedState != null) {
2223 if (!mWorkspace.hasFocus()) {
2224 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2225 }
2226
2227 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2228 if (userFolders != null) {
2229 for (long folderId : userFolders) {
2230 final FolderInfo info = mFolders.get(folderId);
2231 if (info != null) {
2232 openFolder(info);
2233 }
2234 }
2235 final Folder openFolder = mWorkspace.getOpenFolder();
2236 if (openFolder != null) {
2237 openFolder.requestFocus();
2238 }
2239 }
2240
Joe Onorato9c1289c2009-08-17 11:03:03 -04002241 mSavedState = null;
2242 }
2243
2244 if (mSavedInstanceState != null) {
2245 super.onRestoreInstanceState(mSavedInstanceState);
2246 mSavedInstanceState = null;
2247 }
2248
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 mWorkspaceLoading = false;
2250 }
2251
2252 /**
2253 * Add the icons for all apps.
2254 *
2255 * Implementation of the method from LauncherModel.Callbacks.
2256 */
2257 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002258 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 }
2260
2261 /**
2262 * A package was installed.
2263 *
2264 * Implementation of the method from LauncherModel.Callbacks.
2265 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002266 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002267 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002268 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002269 }
2270
2271 /**
2272 * A package was updated.
2273 *
2274 * Implementation of the method from LauncherModel.Callbacks.
2275 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002276 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002278 mWorkspace.updateShortcuts(apps);
2279 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002280 }
2281
2282 /**
2283 * A package was uninstalled.
2284 *
2285 * Implementation of the method from LauncherModel.Callbacks.
2286 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002287 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002288 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002289 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002290 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 }
Joe Onoratobe386092009-11-17 17:32:16 -08002292
2293 /**
2294 * Prints out out state for debugging.
2295 */
2296 public void dumpState() {
2297 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002298 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002299 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2300 Log.d(TAG, "mRestoring=" + mRestoring);
2301 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2302 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2303 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2304 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002305 mModel.dumpState();
2306 mAllAppsGrid.dumpState();
2307 Log.d(TAG, "END launcher2 dump state");
2308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309}