The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Bjorn Bringert | 3e244cf | 2010-02-10 14:17:35 +0000 | [diff] [blame] | 19 | import com.android.common.Search; |
| 20 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.app.AlertDialog; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.app.Dialog; |
| 24 | import android.app.SearchManager; |
| 25 | import android.app.StatusBarManager; |
Dianne Hackborn | 107f839 | 2009-08-05 21:32:16 -0700 | [diff] [blame] | 26 | import android.app.WallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.content.ActivityNotFoundException; |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 29 | import android.content.ComponentName; |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 30 | import android.content.ContentResolver; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.DialogInterface; |
| 33 | import android.content.Intent; |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 34 | import android.content.Intent.ShortcutIconResource; |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 35 | import android.content.IntentFilter; |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 36 | import android.content.pm.ActivityInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 38 | import android.content.pm.ResolveInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | import android.content.res.Configuration; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 40 | import android.content.res.Resources; |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 41 | import android.content.res.TypedArray; |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 42 | import android.database.ContentObserver; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | import android.graphics.Bitmap; |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 44 | import android.graphics.Rect; |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 45 | import android.graphics.Canvas; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | import android.graphics.drawable.Drawable; |
Romain Guy | ff0c2e2 | 2009-11-10 12:09:59 -0800 | [diff] [blame] | 47 | import android.graphics.drawable.ColorDrawable; |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 48 | import android.net.Uri; |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 49 | import android.os.AsyncTask; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | import android.os.Bundle; |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 51 | import android.os.Handler; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 52 | import android.os.Parcelable; |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 53 | import android.os.SystemClock; |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 54 | import android.os.SystemProperties; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 55 | import android.provider.LiveFolders; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | import android.text.Selection; |
| 57 | import android.text.SpannableStringBuilder; |
| 58 | import android.text.TextUtils; |
| 59 | import android.text.method.TextKeyListener; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 60 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | import android.view.Display; |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 62 | import android.view.HapticFeedbackConstants; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 63 | import android.view.KeyEvent; |
| 64 | import android.view.LayoutInflater; |
| 65 | import android.view.Menu; |
| 66 | import android.view.MenuItem; |
| 67 | import android.view.View; |
| 68 | import android.view.ViewGroup; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | import android.view.View.OnLongClickListener; |
| 70 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | import android.widget.EditText; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | import android.widget.TextView; |
| 73 | import android.widget.Toast; |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 74 | import android.widget.ImageView; |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 75 | import android.widget.PopupWindow; |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 76 | import android.widget.LinearLayout; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 77 | import android.appwidget.AppWidgetManager; |
| 78 | import android.appwidget.AppWidgetProviderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | import java.util.ArrayList; |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 81 | import java.util.List; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 82 | import java.util.HashMap; |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 83 | import java.io.DataOutputStream; |
| 84 | import java.io.FileNotFoundException; |
| 85 | import java.io.IOException; |
| 86 | import java.io.DataInputStream; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 87 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 88 | import com.android.launcher.R; |
| 89 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Default launcher application. |
| 92 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 93 | public final class Launcher extends Activity |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 94 | implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 95 | static final String TAG = "Launcher"; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 96 | static final boolean LOGD = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 98 | static final boolean PROFILE_STARTUP = false; |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 99 | static final boolean DEBUG_WIDGETS = false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 100 | static final boolean DEBUG_USER_INTERFACE = false; |
Romain Guy | 6fefcf1 | 2009-06-11 13:07:43 -0700 | [diff] [blame] | 101 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | private static final int WALLPAPER_SCREENS_SPAN = 2; |
| 103 | |
| 104 | private static final int MENU_GROUP_ADD = 1; |
Joe Onorato | 2d7e7d0 | 2010-01-29 15:57:19 -0800 | [diff] [blame] | 105 | private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1; |
| 106 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | private static final int MENU_ADD = Menu.FIRST + 1; |
| 108 | private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1; |
| 109 | private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1; |
| 110 | private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1; |
Romain Guy | 94dabf1 | 2009-07-21 10:55:43 -0700 | [diff] [blame] | 111 | private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 112 | |
| 113 | private static final int REQUEST_CREATE_SHORTCUT = 1; |
| 114 | private static final int REQUEST_CREATE_LIVE_FOLDER = 4; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 115 | private static final int REQUEST_CREATE_APPWIDGET = 5; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | private static final int REQUEST_PICK_APPLICATION = 6; |
| 117 | private static final int REQUEST_PICK_SHORTCUT = 7; |
| 118 | private static final int REQUEST_PICK_LIVE_FOLDER = 8; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 119 | private static final int REQUEST_PICK_APPWIDGET = 9; |
Mike Cleron | a0618e4 | 2009-10-22 13:55:21 -0700 | [diff] [blame] | 120 | private static final int REQUEST_PICK_WALLPAPER = 10; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 121 | |
| 122 | static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate"; |
| 123 | |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 124 | static final int SCREEN_COUNT = 5; |
| 125 | static final int DEFAULT_SCREEN = 2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 126 | static final int NUMBER_CELLS_X = 4; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 127 | static final int NUMBER_CELLS_Y = 4; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 128 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 129 | static final int DIALOG_CREATE_SHORTCUT = 1; |
| 130 | static final int DIALOG_RENAME_FOLDER = 2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 131 | |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 132 | private static final String PREFERENCES = "launcher.preferences"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 133 | |
| 134 | // Type: int |
| 135 | private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen"; |
| 136 | // Type: boolean |
| 137 | private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder"; |
| 138 | // Type: long |
| 139 | private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder"; |
| 140 | // Type: int |
| 141 | private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen"; |
| 142 | // Type: int |
| 143 | private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX"; |
| 144 | // Type: int |
| 145 | private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY"; |
| 146 | // Type: int |
| 147 | private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX"; |
| 148 | // Type: int |
| 149 | private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY"; |
| 150 | // Type: int |
| 151 | private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX"; |
| 152 | // Type: int |
| 153 | private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY"; |
| 154 | // Type: int[] |
| 155 | private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells"; |
| 156 | // Type: boolean |
| 157 | private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder"; |
| 158 | // Type: long |
| 159 | private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id"; |
| 160 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 161 | static final int APPWIDGET_HOST_ID = 1024; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 162 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 163 | private static final Object sLock = new Object(); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 164 | private static int sScreen = DEFAULT_SCREEN; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 166 | private final BroadcastReceiver mCloseSystemDialogsReceiver |
| 167 | = new CloseSystemDialogsIntentReceiver(); |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 168 | private final ContentObserver mWidgetObserver = new AppWidgetResetObserver(); |
| 169 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 170 | private LayoutInflater mInflater; |
| 171 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 172 | private DragController mDragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 173 | private Workspace mWorkspace; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 174 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 175 | private AppWidgetManager mAppWidgetManager; |
| 176 | private LauncherAppWidgetHost mAppWidgetHost; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 177 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 178 | private CellLayout.CellInfo mAddItemCellInfo; |
| 179 | private CellLayout.CellInfo mMenuAddInfo; |
| 180 | private final int[] mCellCoordinates = new int[2]; |
| 181 | private FolderInfo mFolderInfo; |
| 182 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 183 | private DeleteZone mDeleteZone; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 184 | private HandleView mHandleView; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 185 | private AllAppsView mAllAppsGrid; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 186 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 187 | private Bundle mSavedState; |
| 188 | |
| 189 | private SpannableStringBuilder mDefaultKeySsb = null; |
| 190 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 191 | private boolean mWorkspaceLoading = true; |
| 192 | |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 193 | private boolean mPaused = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 194 | private boolean mRestoring; |
| 195 | private boolean mWaitingForResult; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 196 | |
| 197 | private Bundle mSavedInstanceState; |
| 198 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 199 | private LauncherModel mModel; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 200 | private IconCache mIconCache; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 201 | |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 202 | private static LocaleConfiguration sLocaleConfiguration = null; |
| 203 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 204 | private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>(); |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 205 | private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 206 | |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 207 | private ImageView mPreviousView; |
| 208 | private ImageView mNextView; |
Joe Onorato | 080d9b6 | 2009-11-02 12:01:11 -0500 | [diff] [blame] | 209 | |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 210 | // Hotseats (quick-launch icons next to AllApps) |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 211 | private static final int NUM_HOTSEATS = 2; |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 212 | private String[] mHotseatConfig = null; |
| 213 | private Intent[] mHotseats = null; |
| 214 | private Drawable[] mHotseatIcons = null; |
| 215 | private CharSequence[] mHotseatLabels = null; |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 216 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 217 | @Override |
| 218 | protected void onCreate(Bundle savedInstanceState) { |
| 219 | super.onCreate(savedInstanceState); |
Romain Guy | b1b69f5 | 2009-08-10 15:10:15 -0700 | [diff] [blame] | 220 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 221 | LauncherApplication app = ((LauncherApplication)getApplication()); |
| 222 | mModel = app.setLauncher(this); |
| 223 | mIconCache = app.getIconCache(); |
Joe Onorato | 41a12d2 | 2009-10-31 18:30:00 -0400 | [diff] [blame] | 224 | mDragController = new DragController(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 225 | mInflater = getLayoutInflater(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 226 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 227 | mAppWidgetManager = AppWidgetManager.getInstance(this); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 228 | mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID); |
| 229 | mAppWidgetHost.startListening(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 230 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 231 | if (PROFILE_STARTUP) { |
| 232 | android.os.Debug.startMethodTracing("/sdcard/launcher"); |
| 233 | } |
| 234 | |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 235 | loadHotseats(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 236 | checkForLocaleChange(); |
| 237 | setWallpaperDimension(); |
| 238 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 239 | setContentView(R.layout.launcher); |
| 240 | setupViews(); |
| 241 | |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 242 | registerContentObservers(); |
| 243 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 244 | lockAllApps(); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 245 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 246 | mSavedState = savedInstanceState; |
| 247 | restoreState(mSavedState); |
| 248 | |
| 249 | if (PROFILE_STARTUP) { |
| 250 | android.os.Debug.stopMethodTracing(); |
| 251 | } |
| 252 | |
| 253 | if (!mRestoring) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 254 | mModel.startLoader(this, true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // For handling default keys |
| 258 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 259 | Selection.setSelection(mDefaultKeySsb, 0); |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 260 | |
| 261 | IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 262 | registerReceiver(mCloseSystemDialogsReceiver, filter); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 263 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 264 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 265 | private void checkForLocaleChange() { |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 266 | if (sLocaleConfiguration == null) { |
| 267 | new AsyncTask<Void, Void, LocaleConfiguration>() { |
| 268 | @Override |
| 269 | protected LocaleConfiguration doInBackground(Void... unused) { |
| 270 | LocaleConfiguration localeConfiguration = new LocaleConfiguration(); |
| 271 | readConfiguration(Launcher.this, localeConfiguration); |
| 272 | return localeConfiguration; |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | protected void onPostExecute(LocaleConfiguration result) { |
| 277 | sLocaleConfiguration = result; |
| 278 | checkForLocaleChange(); // recursive, but now with a locale configuration |
| 279 | } |
| 280 | }.execute(); |
| 281 | return; |
| 282 | } |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 283 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 284 | final Configuration configuration = getResources().getConfiguration(); |
| 285 | |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 286 | final String previousLocale = sLocaleConfiguration.locale; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 287 | final String locale = configuration.locale.toString(); |
| 288 | |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 289 | final int previousMcc = sLocaleConfiguration.mcc; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 290 | final int mcc = configuration.mcc; |
| 291 | |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 292 | final int previousMnc = sLocaleConfiguration.mnc; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 293 | final int mnc = configuration.mnc; |
| 294 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 295 | boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 296 | |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 297 | if (localeChanged) { |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 298 | sLocaleConfiguration.locale = locale; |
| 299 | sLocaleConfiguration.mcc = mcc; |
| 300 | sLocaleConfiguration.mnc = mnc; |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 301 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 302 | mIconCache.flush(); |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 303 | loadHotseats(); |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 304 | |
| 305 | final LocaleConfiguration localeConfiguration = sLocaleConfiguration; |
| 306 | new Thread("WriteLocaleConfiguration") { |
| 307 | public void run() { |
| 308 | writeConfiguration(Launcher.this, localeConfiguration); |
| 309 | } |
| 310 | }.start(); |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | private static class LocaleConfiguration { |
| 315 | public String locale; |
| 316 | public int mcc = -1; |
| 317 | public int mnc = -1; |
| 318 | } |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 319 | |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 320 | private static void readConfiguration(Context context, LocaleConfiguration configuration) { |
| 321 | DataInputStream in = null; |
| 322 | try { |
| 323 | in = new DataInputStream(context.openFileInput(PREFERENCES)); |
| 324 | configuration.locale = in.readUTF(); |
| 325 | configuration.mcc = in.readInt(); |
| 326 | configuration.mnc = in.readInt(); |
| 327 | } catch (FileNotFoundException e) { |
| 328 | // Ignore |
| 329 | } catch (IOException e) { |
| 330 | // Ignore |
| 331 | } finally { |
| 332 | if (in != null) { |
| 333 | try { |
| 334 | in.close(); |
| 335 | } catch (IOException e) { |
| 336 | // Ignore |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | private static void writeConfiguration(Context context, LocaleConfiguration configuration) { |
| 343 | DataOutputStream out = null; |
| 344 | try { |
| 345 | out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE)); |
| 346 | out.writeUTF(configuration.locale); |
| 347 | out.writeInt(configuration.mcc); |
| 348 | out.writeInt(configuration.mnc); |
| 349 | out.flush(); |
| 350 | } catch (FileNotFoundException e) { |
| 351 | // Ignore |
| 352 | } catch (IOException e) { |
| 353 | //noinspection ResultOfMethodCallIgnored |
| 354 | context.getFileStreamPath(PREFERENCES).delete(); |
| 355 | } finally { |
| 356 | if (out != null) { |
| 357 | try { |
| 358 | out.close(); |
| 359 | } catch (IOException e) { |
| 360 | // Ignore |
| 361 | } |
| 362 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | static int getScreen() { |
| 367 | synchronized (sLock) { |
| 368 | return sScreen; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | static void setScreen(int screen) { |
| 373 | synchronized (sLock) { |
| 374 | sScreen = screen; |
| 375 | } |
| 376 | } |
| 377 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 378 | private void setWallpaperDimension() { |
Dianne Hackborn | 107f839 | 2009-08-05 21:32:16 -0700 | [diff] [blame] | 379 | WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 380 | |
| 381 | Display display = getWindowManager().getDefaultDisplay(); |
| 382 | boolean isPortrait = display.getWidth() < display.getHeight(); |
| 383 | |
| 384 | final int width = isPortrait ? display.getWidth() : display.getHeight(); |
| 385 | final int height = isPortrait ? display.getHeight() : display.getWidth(); |
Dianne Hackborn | 6427180 | 2009-08-08 20:54:24 -0700 | [diff] [blame] | 386 | wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 389 | // Note: This doesn't do all the client-id magic that BrowserProvider does |
| 390 | // in Browser. (http://b/2425179) |
| 391 | private Uri getDefaultBrowserUri() { |
| 392 | String url = getString(R.string.default_browser_url); |
| 393 | if (url.indexOf("{CID}") != -1) { |
| 394 | url = url.replace("{CID}", "android-google"); |
| 395 | } |
| 396 | return Uri.parse(url); |
| 397 | } |
| 398 | |
| 399 | // Load the Intent templates from arrays.xml to populate the hotseats. For |
| 400 | // each Intent, if it resolves to a single app, use that as the launch |
| 401 | // intent & use that app's label as the contentDescription. Otherwise, |
| 402 | // retain the ResolveActivity so the user can pick an app. |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 403 | private void loadHotseats() { |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 404 | if (mHotseatConfig == null) { |
| 405 | mHotseatConfig = getResources().getStringArray(R.array.hotseats); |
| 406 | if (mHotseatConfig.length > 0) { |
| 407 | mHotseats = new Intent[mHotseatConfig.length]; |
| 408 | mHotseatLabels = new CharSequence[mHotseatConfig.length]; |
| 409 | mHotseatIcons = new Drawable[mHotseatConfig.length]; |
| 410 | } else { |
| 411 | mHotseats = null; |
| 412 | mHotseatIcons = null; |
| 413 | mHotseatLabels = null; |
| 414 | } |
| 415 | |
| 416 | TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons); |
| 417 | for (int i=0; i<mHotseatConfig.length; i++) { |
| 418 | // load icon for this slot; currently unrelated to the actual activity |
| 419 | try { |
| 420 | mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i); |
| 421 | } catch (ArrayIndexOutOfBoundsException ex) { |
| 422 | Log.w(TAG, "Missing hotseat_icons array item #" + i); |
| 423 | mHotseatIcons[i] = null; |
| 424 | } |
| 425 | } |
| 426 | hotseatIconDrawables.recycle(); |
| 427 | } |
| 428 | |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 429 | PackageManager pm = getPackageManager(); |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 430 | for (int i=0; i<mHotseatConfig.length; i++) { |
| 431 | Intent intent = null; |
| 432 | if (mHotseatConfig[i].equals("*BROWSER*")) { |
| 433 | // magic value meaning "launch user's default web browser" |
| 434 | // replace it with a generic web request so we can see if there is indeed a default |
| 435 | String defaultUri = getString(R.string.default_browser_url); |
| 436 | intent = new Intent( |
| 437 | Intent.ACTION_VIEW, |
| 438 | ((defaultUri != null) |
| 439 | ? Uri.parse(defaultUri) |
| 440 | : getDefaultBrowserUri()) |
| 441 | ).addCategory(Intent.CATEGORY_BROWSABLE); |
| 442 | // note: if the user launches this without a default set, she |
| 443 | // will always be taken to the default URL above; this is |
| 444 | // unavoidable as we must specify a valid URL in order for the |
| 445 | // chooser to appear, and once the user selects something, that |
| 446 | // URL is unavoidably sent to the chosen app. |
| 447 | } else { |
| 448 | try { |
| 449 | intent = Intent.parseUri(mHotseatConfig[i], 0); |
| 450 | } catch (java.net.URISyntaxException ex) { |
| 451 | Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]); |
| 452 | // bogus; leave intent=null |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (intent == null) { |
| 457 | mHotseats[i] = null; |
| 458 | mHotseatLabels[i] = getText(R.string.activity_not_found); |
| 459 | continue; |
| 460 | } |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 461 | |
| 462 | if (LOGD) { |
| 463 | Log.d(TAG, "loadHotseats: hotseat " + i |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 464 | + " initial intent=[" |
| 465 | + intent.toUri(Intent.URI_INTENT_SCHEME) |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 466 | + "]"); |
| 467 | } |
| 468 | |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 469 | ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); |
| 470 | List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); |
| 471 | if (LOGD) { |
| 472 | Log.d(TAG, "Best match for intent: " + bestMatch); |
| 473 | Log.d(TAG, "All matches: "); |
| 474 | for (ResolveInfo ri : allMatches) { |
| 475 | Log.d(TAG, " --> " + ri); |
| 476 | } |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 477 | } |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 478 | // did this resolve to a single app, or the resolver? |
| 479 | if (allMatches.size() == 0 || bestMatch == null) { |
| 480 | // can't find any activity to handle this. let's leave the |
| 481 | // intent as-is and let Launcher show a toast when it fails |
| 482 | // to launch. |
| 483 | mHotseats[i] = intent; |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 484 | |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 485 | // set accessibility text to "Not installed" |
| 486 | mHotseatLabels[i] = getText(R.string.activity_not_found); |
| 487 | } else { |
| 488 | boolean found = false; |
| 489 | for (ResolveInfo ri : allMatches) { |
| 490 | if (bestMatch.activityInfo.name.equals(ri.activityInfo.name) |
| 491 | && bestMatch.activityInfo.applicationInfo.packageName |
| 492 | .equals(ri.activityInfo.applicationInfo.packageName)) { |
| 493 | found = true; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (!found) { |
| 499 | if (LOGD) Log.d(TAG, "Multiple options, no default yet"); |
| 500 | // the bestMatch is probably the ResolveActivity, meaning the |
| 501 | // user has not yet selected a default |
| 502 | // so: we'll keep the original intent for now |
| 503 | mHotseats[i] = intent; |
| 504 | |
| 505 | // set the accessibility text to "Select shortcut" |
| 506 | mHotseatLabels[i] = getText(R.string.title_select_shortcut); |
| 507 | } else { |
| 508 | // we have an app! |
| 509 | // now reconstruct the intent to launch it through the front |
| 510 | // door |
| 511 | ComponentName com = new ComponentName( |
| 512 | bestMatch.activityInfo.applicationInfo.packageName, |
| 513 | bestMatch.activityInfo.name); |
| 514 | mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com); |
| 515 | |
| 516 | // load the app label for accessibility |
| 517 | mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm); |
| 518 | } |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (LOGD) { |
| 522 | Log.d(TAG, "loadHotseats: hotseat " + i |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 523 | + " final intent=[" |
| 524 | + ((mHotseats[i] == null) |
| 525 | ? "null" |
| 526 | : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME)) |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 527 | + "] label=[" + mHotseatLabels[i] |
| 528 | + "]" |
| 529 | ); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 534 | @Override |
| 535 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
Romain Guy | aad5ef4 | 2009-06-10 02:48:37 -0700 | [diff] [blame] | 536 | mWaitingForResult = false; |
| 537 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 538 | // The pattern used here is that a user PICKs a specific application, |
| 539 | // which, depending on the target, might need to CREATE the actual target. |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 540 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 541 | // For example, the user would PICK_SHORTCUT for "Music playlist", and we |
| 542 | // launch over to the Music app to actually CREATE_SHORTCUT. |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 543 | |
Romain Guy | 94dabf1 | 2009-07-21 10:55:43 -0700 | [diff] [blame] | 544 | if (resultCode == RESULT_OK && mAddItemCellInfo != null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 545 | switch (requestCode) { |
| 546 | case REQUEST_PICK_APPLICATION: |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 547 | completeAddApplication(this, data, mAddItemCellInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 548 | break; |
| 549 | case REQUEST_PICK_SHORTCUT: |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 550 | processShortcut(data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 551 | break; |
| 552 | case REQUEST_CREATE_SHORTCUT: |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 553 | completeAddShortcut(data, mAddItemCellInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 554 | break; |
| 555 | case REQUEST_PICK_LIVE_FOLDER: |
| 556 | addLiveFolder(data); |
| 557 | break; |
| 558 | case REQUEST_CREATE_LIVE_FOLDER: |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 559 | completeAddLiveFolder(data, mAddItemCellInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 560 | break; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 561 | case REQUEST_PICK_APPWIDGET: |
| 562 | addAppWidget(data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 563 | break; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 564 | case REQUEST_CREATE_APPWIDGET: |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 565 | completeAddAppWidget(data, mAddItemCellInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 566 | break; |
Mike Cleron | a0618e4 | 2009-10-22 13:55:21 -0700 | [diff] [blame] | 567 | case REQUEST_PICK_WALLPAPER: |
| 568 | // We just wanted the activity result here so we can clear mWaitingForResult |
| 569 | break; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 570 | } |
Romain Guy | 18042c8 | 2009-11-06 11:44:55 -0800 | [diff] [blame] | 571 | } else if ((requestCode == REQUEST_PICK_APPWIDGET || |
| 572 | requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED && |
| 573 | data != null) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 574 | // Clean up the appWidgetId if we canceled |
| 575 | int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
| 576 | if (appWidgetId != -1) { |
| 577 | mAppWidgetHost.deleteAppWidgetId(appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | @Override |
| 583 | protected void onResume() { |
| 584 | super.onResume(); |
| 585 | |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 586 | mPaused = false; |
Joe Onorato | 7e4ed99 | 2009-12-03 13:10:49 -0800 | [diff] [blame] | 587 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 588 | if (mRestoring) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 589 | mWorkspaceLoading = true; |
| 590 | mModel.startLoader(this, true); |
| 591 | mRestoring = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | @Override |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 596 | protected void onPause() { |
| 597 | super.onPause(); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 598 | dismissPreview(mPreviousView); |
| 599 | dismissPreview(mNextView); |
Joe Onorato | 24b6fd8 | 2009-11-12 13:47:09 -0800 | [diff] [blame] | 600 | mDragController.cancelDrag(); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 601 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 602 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 603 | @Override |
| 604 | public Object onRetainNonConfigurationInstance() { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 605 | // Flag the loader to stop early before switching |
| 606 | mModel.stopLoader(); |
Romain Guy | 13c2e7b | 2010-03-10 19:45:00 -0800 | [diff] [blame] | 607 | mAllAppsGrid.surrender(); |
Romain Guy | 13c2e7b | 2010-03-10 19:45:00 -0800 | [diff] [blame] | 608 | return Boolean.TRUE; |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 609 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 610 | |
Joe Onorato | 2d7e7d0 | 2010-01-29 15:57:19 -0800 | [diff] [blame] | 611 | // We can't hide the IME if it was forced open. So don't bother |
| 612 | /* |
| 613 | @Override |
| 614 | public void onWindowFocusChanged(boolean hasFocus) { |
| 615 | super.onWindowFocusChanged(hasFocus); |
| 616 | |
| 617 | if (hasFocus) { |
| 618 | final InputMethodManager inputManager = (InputMethodManager) |
| 619 | getSystemService(Context.INPUT_METHOD_SERVICE); |
| 620 | WindowManager.LayoutParams lp = getWindow().getAttributes(); |
| 621 | inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new |
| 622 | android.os.Handler()) { |
| 623 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 624 | Log.d(TAG, "ResultReceiver got resultCode=" + resultCode); |
| 625 | } |
| 626 | }); |
| 627 | Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged"); |
| 628 | } |
| 629 | } |
| 630 | */ |
| 631 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 632 | private boolean acceptFilter() { |
| 633 | final InputMethodManager inputManager = (InputMethodManager) |
| 634 | getSystemService(Context.INPUT_METHOD_SERVICE); |
| 635 | return !inputManager.isFullscreenMode(); |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 640 | boolean handled = super.onKeyDown(keyCode, event); |
| 641 | if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) { |
| 642 | boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb, |
| 643 | keyCode, event); |
| 644 | if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) { |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 645 | // something usable has been typed - start a search |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 646 | // the typed text will be retrieved and cleared by |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 647 | // showSearchDialog() |
| 648 | // If there are multiple keystrokes before the search dialog takes focus, |
| 649 | // onSearchRequested() will be called for every keystroke, |
| 650 | // but it is idempotent, so it's fine. |
| 651 | return onSearchRequested(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Joe Onorato | 8a9625e | 2010-01-28 15:55:35 -0800 | [diff] [blame] | 655 | // Eat the long press event so the keyboard doesn't come up. |
| 656 | if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) { |
| 657 | return true; |
| 658 | } |
| 659 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 660 | return handled; |
| 661 | } |
| 662 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 663 | private String getTypedText() { |
| 664 | return mDefaultKeySsb.toString(); |
| 665 | } |
| 666 | |
| 667 | private void clearTypedText() { |
| 668 | mDefaultKeySsb.clear(); |
| 669 | mDefaultKeySsb.clearSpans(); |
| 670 | Selection.setSelection(mDefaultKeySsb, 0); |
| 671 | } |
| 672 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 673 | /** |
| 674 | * Restores the previous state, if it exists. |
| 675 | * |
| 676 | * @param savedState The previous state. |
| 677 | */ |
| 678 | private void restoreState(Bundle savedState) { |
| 679 | if (savedState == null) { |
| 680 | return; |
| 681 | } |
| 682 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 683 | final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false); |
| 684 | if (allApps) { |
| 685 | showAllApps(false); |
| 686 | } |
| 687 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 688 | final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1); |
| 689 | if (currentScreen > -1) { |
| 690 | mWorkspace.setCurrentScreen(currentScreen); |
| 691 | } |
| 692 | |
| 693 | final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1); |
| 694 | if (addScreen > -1) { |
| 695 | mAddItemCellInfo = new CellLayout.CellInfo(); |
| 696 | final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; |
| 697 | addItemCellInfo.valid = true; |
| 698 | addItemCellInfo.screen = addScreen; |
| 699 | addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X); |
| 700 | addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y); |
| 701 | addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X); |
| 702 | addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y); |
| 703 | addItemCellInfo.findVacantCellsFromOccupied( |
| 704 | savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS), |
| 705 | savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X), |
| 706 | savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y)); |
| 707 | mRestoring = true; |
| 708 | } |
| 709 | |
| 710 | boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false); |
| 711 | if (renameFolder) { |
| 712 | long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID); |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 713 | mFolderInfo = mModel.getFolderById(this, sFolders, id); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 714 | mRestoring = true; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Finds all the views we need and configure them properly. |
| 720 | */ |
| 721 | private void setupViews() { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 722 | DragController dragController = mDragController; |
| 723 | |
Romain Guy | b1b69f5 | 2009-08-10 15:10:15 -0700 | [diff] [blame] | 724 | DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 725 | dragLayer.setDragController(dragController); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 726 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 727 | mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 728 | mAllAppsGrid.setLauncher(this); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 729 | mAllAppsGrid.setDragController(dragController); |
Romain Guy | c16fea7 | 2010-03-12 17:17:56 -0800 | [diff] [blame] | 730 | ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window. |
Mike Cleron | b6082fa | 2009-10-19 17:03:36 -0700 | [diff] [blame] | 731 | // Manage focusability manually since this thing is always visible |
Romain Guy | c16fea7 | 2010-03-12 17:17:56 -0800 | [diff] [blame] | 732 | ((View) mAllAppsGrid).setFocusable(false); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 733 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 734 | mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); |
| 735 | final Workspace workspace = mWorkspace; |
Joe Onorato | f0dde09 | 2010-02-16 18:25:23 -0500 | [diff] [blame] | 736 | workspace.setHapticFeedbackEnabled(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 737 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 738 | DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone); |
| 739 | mDeleteZone = deleteZone; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 740 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 741 | mHandleView = (HandleView) findViewById(R.id.all_apps_button); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 742 | mHandleView.setLauncher(this); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 743 | mHandleView.setOnClickListener(this); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 744 | mHandleView.setOnLongClickListener(this); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 745 | |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 746 | ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left); |
| 747 | hotseatLeft.setContentDescription(mHotseatLabels[0]); |
| 748 | hotseatLeft.setImageDrawable(mHotseatIcons[0]); |
| 749 | ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right); |
| 750 | hotseatRight.setContentDescription(mHotseatLabels[1]); |
| 751 | hotseatRight.setImageDrawable(mHotseatIcons[1]); |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 752 | |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 753 | mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen); |
| 754 | mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen); |
| 755 | |
| 756 | Drawable previous = mPreviousView.getDrawable(); |
| 757 | Drawable next = mNextView.getDrawable(); |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 758 | mWorkspace.setIndicators(previous, next); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 759 | |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 760 | mPreviousView.setHapticFeedbackEnabled(false); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 761 | mPreviousView.setOnLongClickListener(this); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 762 | mNextView.setHapticFeedbackEnabled(false); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 763 | mNextView.setOnLongClickListener(this); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 764 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 765 | workspace.setOnLongClickListener(this); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 766 | workspace.setDragController(dragController); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 767 | workspace.setLauncher(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 768 | |
| 769 | deleteZone.setLauncher(this); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 770 | deleteZone.setDragController(dragController); |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 771 | deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 772 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 773 | dragController.setDragScoller(workspace); |
| 774 | dragController.setDragListener(deleteZone); |
| 775 | dragController.setScrollView(dragLayer); |
Romain Guy | ea3763c | 2010-01-11 18:02:04 -0800 | [diff] [blame] | 776 | dragController.setMoveTarget(workspace); |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 777 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 778 | // The order here is bottom to top. |
| 779 | dragController.addDropTarget(workspace); |
| 780 | dragController.addDropTarget(deleteZone); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 783 | @SuppressWarnings({"UnusedDeclaration"}) |
| 784 | public void previousScreen(View v) { |
Joe Onorato | 61597bd | 2009-11-19 12:51:57 -0800 | [diff] [blame] | 785 | if (!isAllAppsVisible()) { |
| 786 | mWorkspace.scrollLeft(); |
| 787 | } |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | @SuppressWarnings({"UnusedDeclaration"}) |
| 791 | public void nextScreen(View v) { |
Joe Onorato | 61597bd | 2009-11-19 12:51:57 -0800 | [diff] [blame] | 792 | if (!isAllAppsVisible()) { |
| 793 | mWorkspace.scrollRight(); |
| 794 | } |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 795 | } |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 796 | |
| 797 | @SuppressWarnings({"UnusedDeclaration"}) |
| 798 | public void launchHotSeat(View v) { |
Daniel Sandler | 3e9454a | 2010-05-24 11:22:41 -0400 | [diff] [blame] | 799 | if (isAllAppsVisible()) return; |
| 800 | |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 801 | int index = -1; |
| 802 | if (v.getId() == R.id.hotseat_left) { |
| 803 | index = 0; |
| 804 | } else if (v.getId() == R.id.hotseat_right) { |
| 805 | index = 1; |
| 806 | } |
| 807 | |
Daniel Sandler | ab1ebd7 | 2010-04-27 16:57:25 -0400 | [diff] [blame] | 808 | // reload these every tap; you never know when they might change |
| 809 | loadHotseats(); |
| 810 | if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) { |
| 811 | Intent intent = mHotseats[index]; |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 812 | startActivitySafely( |
| 813 | mHotseats[index], |
| 814 | "hotseat" |
| 815 | ); |
| 816 | } |
| 817 | } |
Romain Guy | 8a73c51 | 2009-11-09 19:19:59 -0800 | [diff] [blame] | 818 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 819 | /** |
| 820 | * Creates a view representing a shortcut. |
| 821 | * |
| 822 | * @param info The data structure describing the shortcut. |
| 823 | * |
| 824 | * @return A View inflated from R.layout.application. |
| 825 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 826 | View createShortcut(ShortcutInfo info) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 827 | return createShortcut(R.layout.application, |
| 828 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Creates a view representing a shortcut inflated from the specified resource. |
| 833 | * |
| 834 | * @param layoutResId The id of the XML layout used to create the shortcut. |
| 835 | * @param parent The group the shortcut belongs to. |
| 836 | * @param info The data structure describing the shortcut. |
| 837 | * |
| 838 | * @return A View inflated from layoutResId. |
| 839 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 840 | View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 841 | TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false); |
| 842 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 843 | favorite.setCompoundDrawablesWithIntrinsicBounds(null, |
| 844 | new FastBitmapDrawable(info.getIcon(mIconCache)), |
| 845 | null, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 846 | favorite.setText(info.title); |
| 847 | favorite.setTag(info); |
| 848 | favorite.setOnClickListener(this); |
| 849 | |
| 850 | return favorite; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Add an application shortcut to the workspace. |
| 855 | * |
| 856 | * @param data The intent describing the application. |
| 857 | * @param cellInfo The position on screen where to create the shortcut. |
| 858 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 859 | void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 860 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 861 | if (!findSingleSlot(cellInfo)) return; |
| 862 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 863 | final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(), |
| 864 | data, context); |
| 865 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 866 | if (info != null) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 867 | info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 868 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 869 | info.container = ItemInfo.NO_ID; |
| 870 | mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked()); |
| 871 | } else { |
| 872 | Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 873 | } |
| 874 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 875 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 876 | /** |
| 877 | * Add a shortcut to the workspace. |
| 878 | * |
| 879 | * @param data The intent describing the shortcut. |
| 880 | * @param cellInfo The position on screen where to create the shortcut. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 881 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 882 | private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 883 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 884 | if (!findSingleSlot(cellInfo)) return; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 885 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 886 | final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 887 | |
| 888 | if (!mRestoring) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 889 | final View view = createShortcut(info); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 890 | mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, |
| 891 | isWorkspaceLocked()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 895 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 896 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 897 | * Add a widget to the workspace. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 898 | * |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 899 | * @param data The intent describing the appWidgetId. |
| 900 | * @param cellInfo The position on screen where to create the widget. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 901 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 902 | private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 903 | Bundle extras = data.getExtras(); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 904 | int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 905 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 906 | if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString()); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 907 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 908 | AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 909 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 910 | // Calculate the grid spans needed to fit this widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 911 | CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 912 | int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 913 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 914 | // Try finding open space on Launcher screen |
| 915 | final int[] xy = mCellCoordinates; |
Romain Guy | 18042c8 | 2009-11-06 11:44:55 -0800 | [diff] [blame] | 916 | if (!findSlot(cellInfo, xy, spans[0], spans[1])) { |
| 917 | if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId); |
| 918 | return; |
| 919 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 920 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 921 | // Build Launcher-specific widget info and save to database |
| 922 | LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 923 | launcherInfo.spanX = spans[0]; |
| 924 | launcherInfo.spanY = spans[1]; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 925 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 926 | LauncherModel.addItemToDatabase(this, launcherInfo, |
| 927 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 928 | mWorkspace.getCurrentScreen(), xy[0], xy[1], false); |
| 929 | |
| 930 | if (!mRestoring) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 931 | mDesktopItems.add(launcherInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 932 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 933 | // Perform actual inflation because we're live |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 934 | launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 935 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 936 | launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 937 | launcherInfo.hostView.setTag(launcherInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 938 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 939 | mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 940 | launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 943 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 944 | public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) { |
| 945 | mDesktopItems.remove(launcherInfo); |
| 946 | launcherInfo.hostView = null; |
| 947 | } |
| 948 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 949 | public LauncherAppWidgetHost getAppWidgetHost() { |
| 950 | return mAppWidgetHost; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 951 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 952 | |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 953 | void closeSystemDialogs() { |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 954 | getWindow().closeAllPanels(); |
| 955 | |
| 956 | try { |
| 957 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 958 | // Unlock the workspace if the dialog was showing |
| 959 | } catch (Exception e) { |
| 960 | // An exception is thrown if the dialog is not visible, which is fine |
| 961 | } |
| 962 | |
| 963 | try { |
| 964 | dismissDialog(DIALOG_RENAME_FOLDER); |
| 965 | // Unlock the workspace if the dialog was showing |
| 966 | } catch (Exception e) { |
| 967 | // An exception is thrown if the dialog is not visible, which is fine |
| 968 | } |
Joe Onorato | a5c32d6 | 2009-11-19 10:28:49 -0800 | [diff] [blame] | 969 | |
| 970 | // Whatever we were doing is hereby canceled. |
| 971 | mWaitingForResult = false; |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 972 | } |
| 973 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 974 | @Override |
| 975 | protected void onNewIntent(Intent intent) { |
| 976 | super.onNewIntent(intent); |
| 977 | |
| 978 | // Close the menu |
| 979 | if (Intent.ACTION_MAIN.equals(intent.getAction())) { |
Joe Onorato | a5c32d6 | 2009-11-19 10:28:49 -0800 | [diff] [blame] | 980 | // also will cancel mWaitingForResult. |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 981 | closeSystemDialogs(); |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 982 | |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 983 | boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) |
| 984 | != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); |
| 985 | boolean allAppsVisible = isAllAppsVisible(); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 986 | if (!mWorkspace.isDefaultScreenShowing()) { |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 987 | mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 988 | } |
Joe Onorato | 14f122b | 2009-11-19 14:06:36 -0800 | [diff] [blame] | 989 | closeAllApps(alreadyOnHome && allAppsVisible); |
Romain Guy | 1dd3a07 | 2009-07-16 13:21:01 -0700 | [diff] [blame] | 990 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 991 | final View v = getWindow().peekDecorView(); |
| 992 | if (v != null && v.getWindowToken() != null) { |
| 993 | InputMethodManager imm = (InputMethodManager)getSystemService( |
| 994 | INPUT_METHOD_SERVICE); |
| 995 | imm.hideSoftInputFromWindow(v.getWindowToken(), 0); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | @Override |
| 1001 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 1002 | // Do not call super here |
| 1003 | mSavedInstanceState = savedInstanceState; |
| 1004 | } |
| 1005 | |
| 1006 | @Override |
| 1007 | protected void onSaveInstanceState(Bundle outState) { |
| 1008 | outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen()); |
| 1009 | |
| 1010 | final ArrayList<Folder> folders = mWorkspace.getOpenFolders(); |
| 1011 | if (folders.size() > 0) { |
| 1012 | final int count = folders.size(); |
| 1013 | long[] ids = new long[count]; |
| 1014 | for (int i = 0; i < count; i++) { |
| 1015 | final FolderInfo info = folders.get(i).getInfo(); |
| 1016 | ids[i] = info.id; |
| 1017 | } |
| 1018 | outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids); |
| 1019 | } else { |
| 1020 | super.onSaveInstanceState(outState); |
| 1021 | } |
| 1022 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 1023 | // TODO should not do this if the drawer is currently closing. |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1024 | if (isAllAppsVisible()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1025 | outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true); |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 1026 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1027 | |
| 1028 | if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) { |
| 1029 | final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; |
| 1030 | final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen); |
| 1031 | |
| 1032 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen); |
| 1033 | outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX); |
| 1034 | outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY); |
| 1035 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX); |
| 1036 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY); |
| 1037 | outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX()); |
| 1038 | outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY()); |
| 1039 | outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS, |
| 1040 | layout.getOccupiedCells()); |
| 1041 | } |
| 1042 | |
| 1043 | if (mFolderInfo != null && mWaitingForResult) { |
| 1044 | outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true); |
| 1045 | outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | @Override |
| 1050 | public void onDestroy() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1051 | super.onDestroy(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1052 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1053 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1054 | mAppWidgetHost.stopListening(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1055 | } catch (NullPointerException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1056 | Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | TextKeyListener.getInstance().release(); |
| 1060 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1061 | mModel.stopLoader(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1062 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1063 | unbindDesktopItems(); |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 1064 | |
| 1065 | getContentResolver().unregisterContentObserver(mWidgetObserver); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1066 | |
| 1067 | dismissPreview(mPreviousView); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1068 | dismissPreview(mNextView); |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 1069 | |
| 1070 | unregisterReceiver(mCloseSystemDialogsReceiver); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | @Override |
| 1074 | public void startActivityForResult(Intent intent, int requestCode) { |
Romain Guy | 08f9749 | 2009-06-29 14:41:20 -0700 | [diff] [blame] | 1075 | if (requestCode >= 0) mWaitingForResult = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1076 | super.startActivityForResult(intent, requestCode); |
| 1077 | } |
| 1078 | |
| 1079 | @Override |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1080 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1081 | Bundle appSearchData, boolean globalSearch) { |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1082 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 1083 | closeAllApps(true); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1084 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1085 | if (initialQuery == null) { |
| 1086 | // Use any text typed in the launcher as the initial query |
| 1087 | initialQuery = getTypedText(); |
| 1088 | clearTypedText(); |
| 1089 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1090 | if (appSearchData == null) { |
| 1091 | appSearchData = new Bundle(); |
Bjorn Bringert | 3e244cf | 2010-02-10 14:17:35 +0000 | [diff] [blame] | 1092 | appSearchData.putString(Search.SOURCE, "launcher-search"); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1093 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1094 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1095 | final SearchManager searchManager = |
| 1096 | (SearchManager) getSystemService(Context.SEARCH_SERVICE); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1097 | searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1098 | appSearchData, globalSearch); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | @Override |
| 1102 | public boolean onCreateOptionsMenu(Menu menu) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1103 | if (isWorkspaceLocked()) { |
| 1104 | return false; |
| 1105 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1106 | |
| 1107 | super.onCreateOptionsMenu(menu); |
Joe Onorato | 2d7e7d0 | 2010-01-29 15:57:19 -0800 | [diff] [blame] | 1108 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1109 | menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add) |
| 1110 | .setIcon(android.R.drawable.ic_menu_add) |
| 1111 | .setAlphabeticShortcut('A'); |
Joe Onorato | 2d7e7d0 | 2010-01-29 15:57:19 -0800 | [diff] [blame] | 1112 | menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper) |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1113 | .setIcon(android.R.drawable.ic_menu_gallery) |
| 1114 | .setAlphabeticShortcut('W'); |
| 1115 | menu.add(0, MENU_SEARCH, 0, R.string.menu_search) |
| 1116 | .setIcon(android.R.drawable.ic_search_category_default) |
| 1117 | .setAlphabeticShortcut(SearchManager.MENU_KEY); |
| 1118 | menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications) |
| 1119 | .setIcon(com.android.internal.R.drawable.ic_menu_notifications) |
| 1120 | .setAlphabeticShortcut('N'); |
| 1121 | |
| 1122 | final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS); |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 1123 | settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1124 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1125 | |
| 1126 | menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings) |
| 1127 | .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P') |
| 1128 | .setIntent(settings); |
| 1129 | |
| 1130 | return true; |
| 1131 | } |
| 1132 | |
| 1133 | @Override |
| 1134 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 1135 | super.onPrepareOptionsMenu(menu); |
| 1136 | |
Joe Onorato | 2d7e7d0 | 2010-01-29 15:57:19 -0800 | [diff] [blame] | 1137 | // If all apps is animating, don't show the menu, because we don't know |
| 1138 | // which one to show. |
| 1139 | if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) { |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | // Only show the add and wallpaper options when we're not in all apps. |
| 1144 | boolean visible = !mAllAppsGrid.isOpaque(); |
| 1145 | menu.setGroupVisible(MENU_GROUP_ADD, visible); |
| 1146 | menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible); |
| 1147 | |
| 1148 | // Disable add if the workspace is full. |
| 1149 | if (visible) { |
| 1150 | mMenuAddInfo = mWorkspace.findAllVacantCells(null); |
| 1151 | menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid); |
| 1152 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1153 | |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | @Override |
| 1158 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1159 | switch (item.getItemId()) { |
| 1160 | case MENU_ADD: |
| 1161 | addItems(); |
| 1162 | return true; |
| 1163 | case MENU_WALLPAPER_SETTINGS: |
| 1164 | startWallpaper(); |
| 1165 | return true; |
| 1166 | case MENU_SEARCH: |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1167 | onSearchRequested(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1168 | return true; |
| 1169 | case MENU_NOTIFICATIONS: |
| 1170 | showNotifications(); |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | return super.onOptionsItemSelected(item); |
| 1175 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1176 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1177 | /** |
| 1178 | * Indicates that we want global search for this activity by setting the globalSearch |
| 1179 | * argument for {@link #startSearch} to true. |
| 1180 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1181 | |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1182 | @Override |
| 1183 | public boolean onSearchRequested() { |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1184 | startSearch(null, false, null, true); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1185 | return true; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1188 | public boolean isWorkspaceLocked() { |
| 1189 | return mWorkspaceLoading || mWaitingForResult; |
| 1190 | } |
| 1191 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1192 | private void addItems() { |
Joe Onorato | e616866 | 2009-11-08 13:39:30 -0500 | [diff] [blame] | 1193 | closeAllApps(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1194 | showAddDialog(mMenuAddInfo); |
| 1195 | } |
| 1196 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1197 | void addAppWidget(Intent data) { |
| 1198 | // TODO: catch bad widget exception when sent |
| 1199 | int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1200 | AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1201 | |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1202 | if (appWidget.configure != null) { |
| 1203 | // Launch over to configure widget, if needed |
| 1204 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); |
| 1205 | intent.setComponent(appWidget.configure); |
| 1206 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 1207 | |
Romain Guy | 8e633c5 | 2010-03-04 12:51:36 -0800 | [diff] [blame] | 1208 | startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1209 | } else { |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1210 | // Otherwise just add it |
| 1211 | onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1212 | } |
| 1213 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1214 | |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 1215 | void processShortcut(Intent intent) { |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1216 | // Handle case where user selected "Applications" |
| 1217 | String applicationName = getResources().getString(R.string.group_applications); |
| 1218 | String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1219 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1220 | if (applicationName != null && applicationName.equals(shortcutName)) { |
| 1221 | Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
| 1222 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1223 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1224 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1225 | pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 1226 | startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1227 | } else { |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 1228 | startActivityForResult(intent, REQUEST_CREATE_SHORTCUT); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1229 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | void addLiveFolder(Intent intent) { |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1233 | // Handle case where user selected "Folder" |
Jeffrey Sharkey | c4bbd0a | 2009-03-24 22:47:52 -0700 | [diff] [blame] | 1234 | String folderName = getResources().getString(R.string.group_folder); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1235 | String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1236 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1237 | if (folderName != null && folderName.equals(shortcutName)) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1238 | addFolder(); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1239 | } else { |
| 1240 | startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER); |
| 1241 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1244 | void addFolder() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1245 | UserFolderInfo folderInfo = new UserFolderInfo(); |
| 1246 | folderInfo.title = getText(R.string.folder_name); |
| 1247 | |
| 1248 | CellLayout.CellInfo cellInfo = mAddItemCellInfo; |
| 1249 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 1250 | if (!findSingleSlot(cellInfo)) return; |
| 1251 | |
| 1252 | // Update the model |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1253 | LauncherModel.addItemToDatabase(this, folderInfo, |
| 1254 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1255 | mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false); |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 1256 | sFolders.put(folderInfo.id, folderInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1257 | |
| 1258 | // Create the view |
| 1259 | FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, |
| 1260 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo); |
| 1261 | mWorkspace.addInCurrentScreen(newFolder, |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1262 | cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1263 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1264 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1265 | void removeFolder(FolderInfo folder) { |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 1266 | sFolders.remove(folder.id); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1270 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 1271 | if (!findSingleSlot(cellInfo)) return; |
| 1272 | |
| 1273 | final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); |
| 1274 | |
| 1275 | if (!mRestoring) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1276 | final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, |
| 1277 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1278 | mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, |
| 1279 | isWorkspaceLocked()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | static LiveFolderInfo addLiveFolder(Context context, Intent data, |
| 1284 | CellLayout.CellInfo cellInfo, boolean notify) { |
| 1285 | |
| 1286 | Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT); |
| 1287 | String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME); |
| 1288 | |
| 1289 | Drawable icon = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1290 | Intent.ShortcutIconResource iconResource = null; |
| 1291 | |
| 1292 | Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON); |
| 1293 | if (extra != null && extra instanceof Intent.ShortcutIconResource) { |
| 1294 | try { |
| 1295 | iconResource = (Intent.ShortcutIconResource) extra; |
| 1296 | final PackageManager packageManager = context.getPackageManager(); |
| 1297 | Resources resources = packageManager.getResourcesForApplication( |
| 1298 | iconResource.packageName); |
| 1299 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
| 1300 | icon = resources.getDrawable(id); |
| 1301 | } catch (Exception e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1302 | Log.w(TAG, "Could not load live folder icon: " + extra); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | if (icon == null) { |
| 1307 | icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder); |
| 1308 | } |
| 1309 | |
| 1310 | final LiveFolderInfo info = new LiveFolderInfo(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1311 | info.icon = Utilities.createIconBitmap(icon, context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1312 | info.title = name; |
| 1313 | info.iconResource = iconResource; |
| 1314 | info.uri = data.getData(); |
| 1315 | info.baseIntent = baseIntent; |
| 1316 | info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, |
| 1317 | LiveFolders.DISPLAY_MODE_GRID); |
| 1318 | |
| 1319 | LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 1320 | cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 1321 | sFolders.put(info.id, info); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1322 | |
| 1323 | return info; |
| 1324 | } |
| 1325 | |
| 1326 | private boolean findSingleSlot(CellLayout.CellInfo cellInfo) { |
| 1327 | final int[] xy = new int[2]; |
| 1328 | if (findSlot(cellInfo, xy, 1, 1)) { |
| 1329 | cellInfo.cellX = xy[0]; |
| 1330 | cellInfo.cellY = xy[1]; |
| 1331 | return true; |
| 1332 | } |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) { |
| 1337 | if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { |
| 1338 | boolean[] occupied = mSavedState != null ? |
| 1339 | mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null; |
| 1340 | cellInfo = mWorkspace.findAllVacantCells(occupied); |
| 1341 | if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { |
| 1342 | Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show(); |
| 1343 | return false; |
| 1344 | } |
| 1345 | } |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
| 1349 | private void showNotifications() { |
| 1350 | final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE); |
| 1351 | if (statusBar != null) { |
| 1352 | statusBar.expand(); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | private void startWallpaper() { |
Joe Onorato | e616866 | 2009-11-08 13:39:30 -0500 | [diff] [blame] | 1357 | closeAllApps(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1358 | final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); |
Dianne Hackborn | 8355ae3 | 2009-09-07 21:47:51 -0700 | [diff] [blame] | 1359 | Intent chooser = Intent.createChooser(pickWallpaper, |
| 1360 | getText(R.string.chooser_wallpaper)); |
Romain Guy | f2826c7 | 2009-11-12 17:39:34 -0800 | [diff] [blame] | 1361 | // NOTE: Adds a configure option to the chooser if the wallpaper supports it |
| 1362 | // Removed in Eclair MR1 |
| 1363 | // WallpaperManager wm = (WallpaperManager) |
| 1364 | // getSystemService(Context.WALLPAPER_SERVICE); |
| 1365 | // WallpaperInfo wi = wm.getWallpaperInfo(); |
| 1366 | // if (wi != null && wi.getSettingsActivity() != null) { |
| 1367 | // LabeledIntent li = new LabeledIntent(getPackageName(), |
| 1368 | // R.string.configure_wallpaper, 0); |
| 1369 | // li.setClassName(wi.getPackageName(), wi.getSettingsActivity()); |
| 1370 | // chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li }); |
| 1371 | // } |
Mike Cleron | a0618e4 | 2009-10-22 13:55:21 -0700 | [diff] [blame] | 1372 | startActivityForResult(chooser, REQUEST_PICK_WALLPAPER); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 1375 | /** |
| 1376 | * Registers various content observers. The current implementation registers |
| 1377 | * only a favorites observer to keep track of the favorites applications. |
| 1378 | */ |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 1379 | private void registerContentObservers() { |
| 1380 | ContentResolver resolver = getContentResolver(); |
| 1381 | resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI, |
| 1382 | true, mWidgetObserver); |
| 1383 | } |
| 1384 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1385 | @Override |
| 1386 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 1387 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 1388 | switch (event.getKeyCode()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1389 | case KeyEvent.KEYCODE_HOME: |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1390 | return true; |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1391 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
Jason Sams | eb5615d | 2009-11-30 11:50:10 -0800 | [diff] [blame] | 1392 | if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) { |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1393 | dumpState(); |
| 1394 | return true; |
| 1395 | } |
| 1396 | break; |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 1399 | switch (event.getKeyCode()) { |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1400 | case KeyEvent.KEYCODE_HOME: |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1401 | return true; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | return super.dispatchKeyEvent(event); |
| 1406 | } |
| 1407 | |
Joe Onorato | 88ec099 | 2009-11-19 13:16:06 -0800 | [diff] [blame] | 1408 | @Override |
| 1409 | public void onBackPressed() { |
| 1410 | if (isAllAppsVisible()) { |
| 1411 | closeAllApps(true); |
| 1412 | } else { |
| 1413 | closeFolder(); |
| 1414 | } |
| 1415 | dismissPreview(mPreviousView); |
| 1416 | dismissPreview(mNextView); |
| 1417 | } |
| 1418 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1419 | private void closeFolder() { |
| 1420 | Folder folder = mWorkspace.getOpenFolder(); |
| 1421 | if (folder != null) { |
| 1422 | closeFolder(folder); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | void closeFolder(Folder folder) { |
| 1427 | folder.getInfo().opened = false; |
| 1428 | ViewGroup parent = (ViewGroup) folder.getParent(); |
| 1429 | if (parent != null) { |
| 1430 | parent.removeView(folder); |
Joe Onorato | b6341e9 | 2009-11-02 10:41:48 -0500 | [diff] [blame] | 1431 | if (folder instanceof DropTarget) { |
| 1432 | // Live folders aren't DropTargets. |
| 1433 | mDragController.removeDropTarget((DropTarget)folder); |
| 1434 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1435 | } |
| 1436 | folder.onClose(); |
| 1437 | } |
| 1438 | |
| 1439 | /** |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 1440 | * Re-listen when widgets are reset. |
| 1441 | */ |
| 1442 | private void onAppWidgetReset() { |
| 1443 | mAppWidgetHost.startListening(); |
| 1444 | } |
| 1445 | |
| 1446 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1447 | * Go through the and disconnect any of the callbacks in the drawables and the views or we |
| 1448 | * leak the previous Home screen on orientation change. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1449 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1450 | private void unbindDesktopItems() { |
| 1451 | for (ItemInfo item: mDesktopItems) { |
| 1452 | item.unbind(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1453 | } |
| 1454 | } |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1455 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1456 | /** |
| 1457 | * Launches the intent referred by the clicked shortcut. |
| 1458 | * |
| 1459 | * @param v The view representing the clicked shortcut. |
| 1460 | */ |
| 1461 | public void onClick(View v) { |
| 1462 | Object tag = v.getTag(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1463 | if (tag instanceof ShortcutInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1464 | // Open shortcut |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 1465 | final Intent intent = ((ShortcutInfo) tag).intent; |
Joe Onorato | 13724ea | 2009-12-02 21:16:35 -0800 | [diff] [blame] | 1466 | int[] pos = new int[2]; |
| 1467 | v.getLocationOnScreen(pos); |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 1468 | intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 1469 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 1470 | startActivitySafely(intent, tag); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1471 | } else if (tag instanceof FolderInfo) { |
| 1472 | handleFolderClick((FolderInfo) tag); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1473 | } else if (v == mHandleView) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 1474 | if (isAllAppsVisible()) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 1475 | closeAllApps(true); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1476 | } else { |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1477 | showAllApps(true); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1478 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 1482 | void startActivitySafely(Intent intent, Object tag) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1483 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1484 | try { |
| 1485 | startActivity(intent); |
| 1486 | } catch (ActivityNotFoundException e) { |
| 1487 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
Daniel Sandler | c9b1877 | 2010-04-22 14:37:59 -0400 | [diff] [blame] | 1488 | Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1489 | } catch (SecurityException e) { |
| 1490 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1491 | Log.e(TAG, "Launcher does not have the permission to launch " + intent + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1492 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 1493 | "or use the exported attribute for this activity. " |
| 1494 | + "tag="+ tag + " intent=" + intent, e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1495 | } |
| 1496 | } |
Romain Guy | 8e633c5 | 2010-03-04 12:51:36 -0800 | [diff] [blame] | 1497 | |
| 1498 | void startActivityForResultSafely(Intent intent, int requestCode) { |
| 1499 | try { |
| 1500 | startActivityForResult(intent, requestCode); |
| 1501 | } catch (ActivityNotFoundException e) { |
| 1502 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 1503 | } catch (SecurityException e) { |
| 1504 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 1505 | Log.e(TAG, "Launcher does not have the permission to launch " + intent + |
| 1506 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
| 1507 | "or use the exported attribute for this activity.", e); |
| 1508 | } |
| 1509 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1510 | |
| 1511 | private void handleFolderClick(FolderInfo folderInfo) { |
| 1512 | if (!folderInfo.opened) { |
| 1513 | // Close any open folder |
| 1514 | closeFolder(); |
| 1515 | // Open the requested folder |
| 1516 | openFolder(folderInfo); |
| 1517 | } else { |
| 1518 | // Find the open folder... |
| 1519 | Folder openFolder = mWorkspace.getFolderForTag(folderInfo); |
| 1520 | int folderScreen; |
| 1521 | if (openFolder != null) { |
| 1522 | folderScreen = mWorkspace.getScreenForView(openFolder); |
| 1523 | // .. and close it |
| 1524 | closeFolder(openFolder); |
| 1525 | if (folderScreen != mWorkspace.getCurrentScreen()) { |
| 1526 | // Close any folder open on the current screen |
| 1527 | closeFolder(); |
| 1528 | // Pull the folder onto this screen |
| 1529 | openFolder(folderInfo); |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1535 | /** |
| 1536 | * Opens the user fodler described by the specified tag. The opening of the folder |
| 1537 | * is animated relative to the specified View. If the View is null, no animation |
| 1538 | * is played. |
| 1539 | * |
| 1540 | * @param folderInfo The FolderInfo describing the folder to open. |
| 1541 | */ |
| 1542 | private void openFolder(FolderInfo folderInfo) { |
| 1543 | Folder openFolder; |
| 1544 | |
| 1545 | if (folderInfo instanceof UserFolderInfo) { |
| 1546 | openFolder = UserFolder.fromXml(this); |
| 1547 | } else if (folderInfo instanceof LiveFolderInfo) { |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 1548 | openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1549 | } else { |
| 1550 | return; |
| 1551 | } |
| 1552 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1553 | openFolder.setDragController(mDragController); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1554 | openFolder.setLauncher(this); |
| 1555 | |
| 1556 | openFolder.bind(folderInfo); |
| 1557 | folderInfo.opened = true; |
| 1558 | |
| 1559 | mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4); |
| 1560 | openFolder.onOpen(); |
| 1561 | } |
| 1562 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1563 | public boolean onLongClick(View v) { |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1564 | switch (v.getId()) { |
| 1565 | case R.id.previous_screen: |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1566 | if (!isAllAppsVisible()) { |
| 1567 | mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, |
| 1568 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1569 | showPreviews(v); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1570 | } |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1571 | return true; |
| 1572 | case R.id.next_screen: |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1573 | if (!isAllAppsVisible()) { |
| 1574 | mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, |
| 1575 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1576 | showPreviews(v); |
| 1577 | } |
| 1578 | return true; |
| 1579 | case R.id.all_apps_button: |
| 1580 | if (!isAllAppsVisible()) { |
| 1581 | mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, |
| 1582 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); |
| 1583 | showPreviews(v); |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1584 | } |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1585 | return true; |
| 1586 | } |
| 1587 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1588 | if (isWorkspaceLocked()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1589 | return false; |
| 1590 | } |
| 1591 | |
| 1592 | if (!(v instanceof CellLayout)) { |
| 1593 | v = (View) v.getParent(); |
| 1594 | } |
| 1595 | |
| 1596 | CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); |
| 1597 | |
| 1598 | // This happens when long clicking an item with the dpad/trackball |
| 1599 | if (cellInfo == null) { |
| 1600 | return true; |
| 1601 | } |
| 1602 | |
| 1603 | if (mWorkspace.allowLongPress()) { |
| 1604 | if (cellInfo.cell == null) { |
| 1605 | if (cellInfo.valid) { |
| 1606 | // User long pressed on empty space |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1607 | mWorkspace.setAllowLongPress(false); |
Joe Onorato | f0dde09 | 2010-02-16 18:25:23 -0500 | [diff] [blame] | 1608 | mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, |
| 1609 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1610 | showAddDialog(cellInfo); |
| 1611 | } |
| 1612 | } else { |
| 1613 | if (!(cellInfo.cell instanceof Folder)) { |
| 1614 | // User long pressed on an item |
Joe Onorato | 0d44e94 | 2009-11-16 18:20:51 -0800 | [diff] [blame] | 1615 | mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, |
| 1616 | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1617 | mWorkspace.startDrag(cellInfo); |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1624 | @SuppressWarnings({"unchecked"}) |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1625 | private void dismissPreview(final View v) { |
| 1626 | final PopupWindow window = (PopupWindow) v.getTag(); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1627 | if (window != null) { |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1628 | window.setOnDismissListener(new PopupWindow.OnDismissListener() { |
| 1629 | public void onDismiss() { |
| 1630 | ViewGroup group = (ViewGroup) v.getTag(R.id.workspace); |
| 1631 | int count = group.getChildCount(); |
| 1632 | for (int i = 0; i < count; i++) { |
| 1633 | ((ImageView) group.getChildAt(i)).setImageDrawable(null); |
| 1634 | } |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1635 | ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon); |
| 1636 | for (Bitmap bitmap : bitmaps) bitmap.recycle(); |
| 1637 | |
| 1638 | v.setTag(R.id.workspace, null); |
| 1639 | v.setTag(R.id.icon, null); |
| 1640 | window.setOnDismissListener(null); |
| 1641 | } |
| 1642 | }); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1643 | window.dismiss(); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1644 | } |
| 1645 | v.setTag(null); |
| 1646 | } |
| 1647 | |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1648 | private void showPreviews(View anchor) { |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1649 | showPreviews(anchor, 0, mWorkspace.getChildCount()); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1650 | } |
| 1651 | |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1652 | private void showPreviews(final View anchor, int start, int end) { |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1653 | final Resources resources = getResources(); |
| 1654 | final Workspace workspace = mWorkspace; |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1655 | |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1656 | CellLayout cell = ((CellLayout) workspace.getChildAt(start)); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1657 | |
| 1658 | float max = workspace.getChildCount(); |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1659 | |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1660 | final Rect r = new Rect(); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1661 | resources.getDrawable(R.drawable.preview_background).getPadding(r); |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1662 | int extraW = (int) ((r.left + r.right) * max); |
| 1663 | int extraH = r.top + r.bottom; |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1664 | |
| 1665 | int aW = cell.getWidth() - extraW; |
| 1666 | float w = aW / max; |
| 1667 | |
| 1668 | int width = cell.getWidth(); |
| 1669 | int height = cell.getHeight(); |
| 1670 | int x = cell.getLeftPadding(); |
| 1671 | int y = cell.getTopPadding(); |
| 1672 | width -= (x + cell.getRightPadding()); |
| 1673 | height -= (y + cell.getBottomPadding()); |
| 1674 | |
| 1675 | float scale = w / width; |
| 1676 | |
| 1677 | int count = end - start; |
| 1678 | |
| 1679 | final float sWidth = width * scale; |
| 1680 | float sHeight = height * scale; |
| 1681 | |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1682 | LinearLayout preview = new LinearLayout(this); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1683 | |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1684 | PreviewTouchHandler handler = new PreviewTouchHandler(anchor); |
| 1685 | ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1686 | |
| 1687 | for (int i = start; i < end; i++) { |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1688 | ImageView image = new ImageView(this); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1689 | cell = (CellLayout) workspace.getChildAt(i); |
| 1690 | |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1691 | final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight, |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1692 | Bitmap.Config.ARGB_8888); |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1693 | |
| 1694 | final Canvas c = new Canvas(bitmap); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1695 | c.scale(scale, scale); |
| 1696 | c.translate(-cell.getLeftPadding(), -cell.getTopPadding()); |
| 1697 | cell.dispatchDraw(c); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1698 | |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1699 | image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background)); |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1700 | image.setImageBitmap(bitmap); |
| 1701 | image.setTag(i); |
| 1702 | image.setOnClickListener(handler); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1703 | image.setOnFocusChangeListener(handler); |
| 1704 | image.setFocusable(true); |
| 1705 | if (i == mWorkspace.getCurrentScreen()) image.requestFocus(); |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1706 | |
| 1707 | preview.addView(image, |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1708 | LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); |
| 1709 | |
| 1710 | bitmaps.add(bitmap); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1711 | } |
Romain Guy | f8e6a80 | 2009-12-07 17:48:02 -0800 | [diff] [blame] | 1712 | |
| 1713 | final PopupWindow p = new PopupWindow(this); |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1714 | p.setContentView(preview); |
| 1715 | p.setWidth((int) (sWidth * count + extraW)); |
| 1716 | p.setHeight((int) (sHeight + extraH)); |
| 1717 | p.setAnimationStyle(R.style.AnimationPreview); |
| 1718 | p.setOutsideTouchable(true); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1719 | p.setFocusable(true); |
Romain Guy | ff0c2e2 | 2009-11-10 12:09:59 -0800 | [diff] [blame] | 1720 | p.setBackgroundDrawable(new ColorDrawable(0)); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1721 | p.showAsDropDown(anchor, 0, 0); |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1722 | |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1723 | p.setOnDismissListener(new PopupWindow.OnDismissListener() { |
| 1724 | public void onDismiss() { |
| 1725 | dismissPreview(anchor); |
| 1726 | } |
| 1727 | }); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1728 | |
| 1729 | anchor.setTag(p); |
| 1730 | anchor.setTag(R.id.workspace, preview); |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1731 | anchor.setTag(R.id.icon, bitmaps); |
Romain Guy | 1fbc1c8 | 2009-11-09 20:43:08 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1734 | class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener { |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1735 | private final View mAnchor; |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1736 | |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1737 | public PreviewTouchHandler(View anchor) { |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1738 | mAnchor = anchor; |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | public void onClick(View v) { |
Romain Guy | e6b8e2f | 2009-11-10 11:56:55 -0800 | [diff] [blame] | 1742 | mWorkspace.snapToScreen((Integer) v.getTag()); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1743 | v.post(this); |
| 1744 | } |
| 1745 | |
| 1746 | public void run() { |
| 1747 | dismissPreview(mAnchor); |
| 1748 | } |
| 1749 | |
| 1750 | public void onFocusChange(View v, boolean hasFocus) { |
| 1751 | if (hasFocus) { |
Romain Guy | e47f55c | 2009-11-11 19:21:22 -0800 | [diff] [blame] | 1752 | mWorkspace.snapToScreen((Integer) v.getTag()); |
Romain Guy | 9d31e9f | 2009-11-11 18:54:28 -0800 | [diff] [blame] | 1753 | } |
Romain Guy | a6abce8 | 2009-11-10 02:54:41 -0800 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1757 | Workspace getWorkspace() { |
| 1758 | return mWorkspace; |
| 1759 | } |
| 1760 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1761 | @Override |
| 1762 | protected Dialog onCreateDialog(int id) { |
| 1763 | switch (id) { |
| 1764 | case DIALOG_CREATE_SHORTCUT: |
| 1765 | return new CreateShortcut().createDialog(); |
| 1766 | case DIALOG_RENAME_FOLDER: |
| 1767 | return new RenameFolder().createDialog(); |
| 1768 | } |
| 1769 | |
| 1770 | return super.onCreateDialog(id); |
| 1771 | } |
| 1772 | |
| 1773 | @Override |
| 1774 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 1775 | switch (id) { |
| 1776 | case DIALOG_CREATE_SHORTCUT: |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1777 | break; |
| 1778 | case DIALOG_RENAME_FOLDER: |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1779 | if (mFolderInfo != null) { |
| 1780 | EditText input = (EditText) dialog.findViewById(R.id.folder_name); |
| 1781 | final CharSequence text = mFolderInfo.title; |
| 1782 | input.setText(text); |
| 1783 | input.setSelection(0, text.length()); |
| 1784 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1785 | break; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | void showRenameDialog(FolderInfo info) { |
| 1790 | mFolderInfo = info; |
| 1791 | mWaitingForResult = true; |
| 1792 | showDialog(DIALOG_RENAME_FOLDER); |
| 1793 | } |
| 1794 | |
| 1795 | private void showAddDialog(CellLayout.CellInfo cellInfo) { |
| 1796 | mAddItemCellInfo = cellInfo; |
| 1797 | mWaitingForResult = true; |
| 1798 | showDialog(DIALOG_CREATE_SHORTCUT); |
| 1799 | } |
| 1800 | |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 1801 | private void pickShortcut() { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1802 | Bundle bundle = new Bundle(); |
| 1803 | |
| 1804 | ArrayList<String> shortcutNames = new ArrayList<String>(); |
| 1805 | shortcutNames.add(getString(R.string.group_applications)); |
| 1806 | bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); |
| 1807 | |
| 1808 | ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>(); |
| 1809 | shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, |
| 1810 | R.drawable.ic_launcher_application)); |
| 1811 | bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); |
| 1812 | |
| 1813 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1814 | pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT)); |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 1815 | pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut)); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1816 | pickIntent.putExtras(bundle); |
| 1817 | |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 1818 | startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1821 | private class RenameFolder { |
| 1822 | private EditText mInput; |
| 1823 | |
| 1824 | Dialog createDialog() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1825 | final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null); |
| 1826 | mInput = (EditText) layout.findViewById(R.id.folder_name); |
| 1827 | |
| 1828 | AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); |
| 1829 | builder.setIcon(0); |
| 1830 | builder.setTitle(getString(R.string.rename_folder_title)); |
| 1831 | builder.setCancelable(true); |
| 1832 | builder.setOnCancelListener(new Dialog.OnCancelListener() { |
| 1833 | public void onCancel(DialogInterface dialog) { |
| 1834 | cleanup(); |
| 1835 | } |
| 1836 | }); |
| 1837 | builder.setNegativeButton(getString(R.string.cancel_action), |
| 1838 | new Dialog.OnClickListener() { |
| 1839 | public void onClick(DialogInterface dialog, int which) { |
| 1840 | cleanup(); |
| 1841 | } |
| 1842 | } |
| 1843 | ); |
| 1844 | builder.setPositiveButton(getString(R.string.rename_action), |
| 1845 | new Dialog.OnClickListener() { |
| 1846 | public void onClick(DialogInterface dialog, int which) { |
| 1847 | changeFolderName(); |
| 1848 | } |
| 1849 | } |
| 1850 | ); |
| 1851 | builder.setView(layout); |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1852 | |
| 1853 | final AlertDialog dialog = builder.create(); |
| 1854 | dialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| 1855 | public void onShow(DialogInterface dialog) { |
Joe Onorato | 7018d8e | 2010-04-13 20:25:47 -0700 | [diff] [blame] | 1856 | mWaitingForResult = true; |
Joe Onorato | d753b42 | 2009-11-08 13:31:11 -0500 | [diff] [blame] | 1857 | mInput.requestFocus(); |
| 1858 | InputMethodManager inputManager = (InputMethodManager) |
| 1859 | getSystemService(Context.INPUT_METHOD_SERVICE); |
| 1860 | inputManager.showSoftInput(mInput, 0); |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1861 | } |
| 1862 | }); |
| 1863 | |
| 1864 | return dialog; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | private void changeFolderName() { |
| 1868 | final String name = mInput.getText().toString(); |
| 1869 | if (!TextUtils.isEmpty(name)) { |
| 1870 | // Make sure we have the right folder info |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 1871 | mFolderInfo = sFolders.get(mFolderInfo.id); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1872 | mFolderInfo.title = name; |
| 1873 | LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo); |
| 1874 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1875 | if (mWorkspaceLoading) { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 1876 | lockAllApps(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1877 | mModel.startLoader(Launcher.this, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1878 | } else { |
| 1879 | final FolderIcon folderIcon = (FolderIcon) |
| 1880 | mWorkspace.getViewForTag(mFolderInfo); |
| 1881 | if (folderIcon != null) { |
| 1882 | folderIcon.setText(name); |
| 1883 | getWorkspace().requestLayout(); |
| 1884 | } else { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 1885 | lockAllApps(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1886 | mWorkspaceLoading = true; |
| 1887 | mModel.startLoader(Launcher.this, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | cleanup(); |
| 1892 | } |
| 1893 | |
| 1894 | private void cleanup() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1895 | dismissDialog(DIALOG_RENAME_FOLDER); |
| 1896 | mWaitingForResult = false; |
| 1897 | mFolderInfo = null; |
| 1898 | } |
| 1899 | } |
| 1900 | |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1901 | // Now a part of LauncherModel.Callbacks. Used to reorder loading steps. |
| 1902 | public boolean isAllAppsVisible() { |
| 1903 | return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false; |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 1904 | } |
| 1905 | |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 1906 | // AllAppsView.Watcher |
| 1907 | public void zoomed(float zoom) { |
| 1908 | if (zoom == 1.0f) { |
| 1909 | mWorkspace.setVisibility(View.GONE); |
| 1910 | } |
| 1911 | } |
| 1912 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1913 | void showAllApps(boolean animated) { |
| 1914 | mAllAppsGrid.zoom(1.0f, animated); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1915 | |
Romain Guy | c16fea7 | 2010-03-12 17:17:56 -0800 | [diff] [blame] | 1916 | ((View) mAllAppsGrid).setFocusable(true); |
| 1917 | ((View) mAllAppsGrid).requestFocus(); |
Mike Cleron | b6082fa | 2009-10-19 17:03:36 -0700 | [diff] [blame] | 1918 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 1919 | // TODO: fade these two too |
| 1920 | mDeleteZone.setVisibility(View.GONE); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1921 | } |
| 1922 | |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 1923 | /** |
Joe Onorato | 7e4ed99 | 2009-12-03 13:10:49 -0800 | [diff] [blame] | 1924 | * Things to test when changing this code. |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 1925 | * - Home from workspace |
| 1926 | * - from center screen |
| 1927 | * - from other screens |
| 1928 | * - Home from all apps |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 1929 | * - from center screen |
| 1930 | * - from other screens |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 1931 | * - Back from all apps |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 1932 | * - from center screen |
| 1933 | * - from other screens |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 1934 | * - Launch app from workspace and quit |
| 1935 | * - with back |
| 1936 | * - with home |
| 1937 | * - Launch app from all apps and quit |
| 1938 | * - with back |
| 1939 | * - with home |
Joe Onorato | 7e4ed99 | 2009-12-03 13:10:49 -0800 | [diff] [blame] | 1940 | * - Go to a screen that's not the default, then all |
| 1941 | * apps, and launch and app, and go back |
| 1942 | * - with back |
| 1943 | * -with home |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 1944 | * - On workspace, long press power and go back |
| 1945 | * - with back |
| 1946 | * - with home |
| 1947 | * - On all apps, long press power and go back |
| 1948 | * - with back |
| 1949 | * - with home |
| 1950 | * - On workspace, power off |
| 1951 | * - On all apps, power off |
Joe Onorato | 7e4ed99 | 2009-12-03 13:10:49 -0800 | [diff] [blame] | 1952 | * - Launch an app and turn off the screen while in that app |
| 1953 | * - Go back with home key |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 1954 | * - Go back with back key TODO: make this not go to workspace |
Joe Onorato | 7e4ed99 | 2009-12-03 13:10:49 -0800 | [diff] [blame] | 1955 | * - From all apps |
| 1956 | * - From workspace |
Joe Onorato | effc4a8 | 2010-04-15 11:48:13 -0700 | [diff] [blame] | 1957 | * - Enter and exit car mode (becuase it causes an extra configuration changed) |
| 1958 | * - From all apps |
| 1959 | * - From the center workspace |
| 1960 | * - From another workspace |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 1961 | */ |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 1962 | void closeAllApps(boolean animated) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 1963 | if (mAllAppsGrid.isVisible()) { |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 1964 | mWorkspace.setVisibility(View.VISIBLE); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1965 | mAllAppsGrid.zoom(0.0f, animated); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 1966 | ((View)mAllAppsGrid).setFocusable(false); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1967 | mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1968 | } |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 1971 | void lockAllApps() { |
| 1972 | // TODO |
| 1973 | } |
| 1974 | |
| 1975 | void unlockAllApps() { |
| 1976 | // TODO |
| 1977 | } |
| 1978 | |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1979 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1980 | * Displays the shortcut creation dialog and launches, if necessary, the |
| 1981 | * appropriate activity. |
| 1982 | */ |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1983 | private class CreateShortcut implements DialogInterface.OnClickListener, |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1984 | DialogInterface.OnCancelListener, DialogInterface.OnDismissListener, |
| 1985 | DialogInterface.OnShowListener { |
| 1986 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1987 | private AddAdapter mAdapter; |
Romain Guy | 9ffb543 | 2009-03-24 21:04:15 -0700 | [diff] [blame] | 1988 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1989 | Dialog createDialog() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1990 | mAdapter = new AddAdapter(Launcher.this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1991 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1992 | final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); |
| 1993 | builder.setTitle(getString(R.string.menu_item_add_item)); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1994 | builder.setAdapter(mAdapter, this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1995 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1996 | builder.setInverseBackgroundForced(true); |
| 1997 | |
| 1998 | AlertDialog dialog = builder.create(); |
| 1999 | dialog.setOnCancelListener(this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2000 | dialog.setOnDismissListener(this); |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 2001 | dialog.setOnShowListener(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2002 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2003 | return dialog; |
| 2004 | } |
| 2005 | |
| 2006 | public void onCancel(DialogInterface dialog) { |
| 2007 | mWaitingForResult = false; |
| 2008 | cleanup(); |
| 2009 | } |
| 2010 | |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2011 | public void onDismiss(DialogInterface dialog) { |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2014 | private void cleanup() { |
Joe Onorato | cc19a53 | 2009-11-19 14:19:17 -0800 | [diff] [blame] | 2015 | try { |
| 2016 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 2017 | } catch (Exception e) { |
| 2018 | // An exception is thrown if the dialog is not visible, which is fine |
| 2019 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2020 | } |
| 2021 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2022 | /** |
| 2023 | * Handle the action clicked in the "Add to home" dialog. |
| 2024 | */ |
| 2025 | public void onClick(DialogInterface dialog, int which) { |
| 2026 | Resources res = getResources(); |
| 2027 | cleanup(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2028 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2029 | switch (which) { |
| 2030 | case AddAdapter.ITEM_SHORTCUT: { |
| 2031 | // Insert extra item to handle picking application |
Joe Onorato | deb98af | 2010-02-19 14:59:39 -0800 | [diff] [blame] | 2032 | pickShortcut(); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2033 | break; |
| 2034 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2035 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2036 | case AddAdapter.ITEM_APPWIDGET: { |
| 2037 | int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2038 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2039 | Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); |
| 2040 | pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2041 | // start the pick activity |
| 2042 | startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET); |
| 2043 | break; |
| 2044 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2045 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2046 | case AddAdapter.ITEM_LIVE_FOLDER: { |
| 2047 | // Insert extra item to handle inserting folder |
| 2048 | Bundle bundle = new Bundle(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2049 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2050 | ArrayList<String> shortcutNames = new ArrayList<String>(); |
| 2051 | shortcutNames.add(res.getString(R.string.group_folder)); |
| 2052 | bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2053 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2054 | ArrayList<ShortcutIconResource> shortcutIcons = |
| 2055 | new ArrayList<ShortcutIconResource>(); |
| 2056 | shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, |
| 2057 | R.drawable.ic_launcher_folder)); |
| 2058 | bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); |
| 2059 | |
| 2060 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 2061 | pickIntent.putExtra(Intent.EXTRA_INTENT, |
| 2062 | new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER)); |
| 2063 | pickIntent.putExtra(Intent.EXTRA_TITLE, |
| 2064 | getText(R.string.title_select_live_folder)); |
| 2065 | pickIntent.putExtras(bundle); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2066 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 2067 | startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER); |
| 2068 | break; |
| 2069 | } |
| 2070 | |
| 2071 | case AddAdapter.ITEM_WALLPAPER: { |
| 2072 | startWallpaper(); |
| 2073 | break; |
| 2074 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2075 | } |
| 2076 | } |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 2077 | |
| 2078 | public void onShow(DialogInterface dialog) { |
Romain Guy | 406d703 | 2010-03-04 18:59:31 -0800 | [diff] [blame] | 2079 | mWaitingForResult = true; |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 2080 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | /** |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 2084 | * Receives notifications when applications are added/removed. |
| 2085 | */ |
| 2086 | private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver { |
| 2087 | @Override |
| 2088 | public void onReceive(Context context, Intent intent) { |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 2089 | closeSystemDialogs(); |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 2090 | String reason = intent.getStringExtra("reason"); |
| 2091 | if (!"homekey".equals(reason)) { |
| 2092 | boolean animate = true; |
Joe Onorato | 34a0e1b | 2009-12-14 17:44:51 -0800 | [diff] [blame] | 2093 | if (mPaused || "lock".equals(reason)) { |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 2094 | animate = false; |
| 2095 | } |
Joe Onorato | b206121 | 2009-11-24 16:13:54 -0500 | [diff] [blame] | 2096 | closeAllApps(animate); |
| 2097 | } |
Joe Onorato | 2ca0ae7 | 2009-11-10 13:14:13 -0800 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | /** |
Jeff Sharkey | 1e2efc8 | 2009-11-06 15:17:59 -0800 | [diff] [blame] | 2102 | * Receives notifications whenever the appwidgets are reset. |
| 2103 | */ |
| 2104 | private class AppWidgetResetObserver extends ContentObserver { |
| 2105 | public AppWidgetResetObserver() { |
| 2106 | super(new Handler()); |
| 2107 | } |
| 2108 | |
| 2109 | @Override |
| 2110 | public void onChange(boolean selfChange) { |
| 2111 | onAppWidgetReset(); |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2116 | * Implementation of the method from LauncherModel.Callbacks. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2117 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2118 | public int getCurrentWorkspaceScreen() { |
Joe Onorato | d0afc87 | 2010-06-11 00:03:15 -0700 | [diff] [blame] | 2119 | if (mWorkspace != null) { |
| 2120 | return mWorkspace.getCurrentScreen(); |
| 2121 | } else { |
| 2122 | return SCREEN_COUNT / 2; |
| 2123 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2124 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2125 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2126 | /** |
| 2127 | * Refreshes the shortcuts shown on the workspace. |
| 2128 | * |
| 2129 | * Implementation of the method from LauncherModel.Callbacks. |
| 2130 | */ |
| 2131 | public void startBinding() { |
| 2132 | final Workspace workspace = mWorkspace; |
| 2133 | int count = workspace.getChildCount(); |
| 2134 | for (int i = 0; i < count; i++) { |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 2135 | // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate(). |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2136 | ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout(); |
| 2137 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2138 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2139 | if (DEBUG_USER_INTERFACE) { |
| 2140 | android.widget.Button finishButton = new android.widget.Button(this); |
| 2141 | finishButton.setText("Finish"); |
| 2142 | workspace.addInScreen(finishButton, 1, 0, 0, 1, 1); |
| 2143 | |
| 2144 | finishButton.setOnClickListener(new android.widget.Button.OnClickListener() { |
| 2145 | public void onClick(View v) { |
| 2146 | finish(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2147 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2148 | }); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2153 | * Bind the items start-end from the list. |
| 2154 | * |
| 2155 | * Implementation of the method from LauncherModel.Callbacks. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2156 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2157 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) { |
| 2158 | |
| 2159 | final Workspace workspace = mWorkspace; |
| 2160 | |
| 2161 | for (int i=start; i<end; i++) { |
| 2162 | final ItemInfo item = shortcuts.get(i); |
| 2163 | mDesktopItems.add(item); |
| 2164 | switch (item.itemType) { |
| 2165 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 2166 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2167 | final View shortcut = createShortcut((ShortcutInfo)item); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2168 | workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, |
| 2169 | false); |
| 2170 | break; |
| 2171 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
| 2172 | final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, |
| 2173 | (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), |
| 2174 | (UserFolderInfo) item); |
| 2175 | workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1, |
| 2176 | false); |
| 2177 | break; |
| 2178 | case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: |
| 2179 | final FolderIcon newLiveFolder = LiveFolderIcon.fromXml( |
| 2180 | R.layout.live_folder_icon, this, |
| 2181 | (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), |
| 2182 | (LiveFolderInfo) item); |
| 2183 | workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1, |
| 2184 | false); |
| 2185 | break; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2186 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2187 | } |
| 2188 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2189 | workspace.requestLayout(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2190 | } |
| 2191 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2192 | /** |
| 2193 | * Implementation of the method from LauncherModel.Callbacks. |
| 2194 | */ |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 2195 | public void bindFolders(HashMap<Long, FolderInfo> folders) { |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 2196 | sFolders.clear(); |
| 2197 | sFolders.putAll(folders); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2198 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2199 | |
| 2200 | /** |
| 2201 | * Add the views for a widget to the workspace. |
| 2202 | * |
| 2203 | * Implementation of the method from LauncherModel.Callbacks. |
| 2204 | */ |
| 2205 | public void bindAppWidget(LauncherAppWidgetInfo item) { |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 2206 | final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0; |
| 2207 | if (DEBUG_WIDGETS) { |
| 2208 | Log.d(TAG, "bindAppWidget: " + item); |
| 2209 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2210 | final Workspace workspace = mWorkspace; |
| 2211 | |
| 2212 | final int appWidgetId = item.appWidgetId; |
| 2213 | final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 2214 | if (DEBUG_WIDGETS) { |
| 2215 | Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider); |
| 2216 | } |
| 2217 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2218 | item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); |
| 2219 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2220 | item.hostView.setAppWidget(appWidgetId, appWidgetInfo); |
| 2221 | item.hostView.setTag(item); |
| 2222 | |
| 2223 | workspace.addInScreen(item.hostView, item.screen, item.cellX, |
| 2224 | item.cellY, item.spanX, item.spanY, false); |
| 2225 | |
| 2226 | workspace.requestLayout(); |
| 2227 | |
| 2228 | mDesktopItems.add(item); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 2229 | |
| 2230 | if (DEBUG_WIDGETS) { |
| 2231 | Log.d(TAG, "bound widget id="+item.appWidgetId+" in " |
| 2232 | + (SystemClock.uptimeMillis()-start) + "ms"); |
| 2233 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * Callback saying that there aren't any more items to bind. |
| 2238 | * |
| 2239 | * Implementation of the method from LauncherModel.Callbacks. |
| 2240 | */ |
| 2241 | public void finishBindingItems() { |
| 2242 | if (mSavedState != null) { |
| 2243 | if (!mWorkspace.hasFocus()) { |
| 2244 | mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); |
| 2245 | } |
| 2246 | |
| 2247 | final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS); |
| 2248 | if (userFolders != null) { |
| 2249 | for (long folderId : userFolders) { |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 2250 | final FolderInfo info = sFolders.get(folderId); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2251 | if (info != null) { |
| 2252 | openFolder(info); |
| 2253 | } |
| 2254 | } |
| 2255 | final Folder openFolder = mWorkspace.getOpenFolder(); |
| 2256 | if (openFolder != null) { |
| 2257 | openFolder.requestFocus(); |
| 2258 | } |
| 2259 | } |
| 2260 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2261 | mSavedState = null; |
| 2262 | } |
| 2263 | |
| 2264 | if (mSavedInstanceState != null) { |
| 2265 | super.onRestoreInstanceState(mSavedInstanceState); |
| 2266 | mSavedInstanceState = null; |
| 2267 | } |
| 2268 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2269 | mWorkspaceLoading = false; |
| 2270 | } |
| 2271 | |
| 2272 | /** |
| 2273 | * Add the icons for all apps. |
| 2274 | * |
| 2275 | * Implementation of the method from LauncherModel.Callbacks. |
| 2276 | */ |
| 2277 | public void bindAllApplications(ArrayList<ApplicationInfo> apps) { |
Romain Guy | 84f296c | 2009-11-04 15:00:44 -0800 | [diff] [blame] | 2278 | mAllAppsGrid.setApps(apps); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * A package was installed. |
| 2283 | * |
| 2284 | * Implementation of the method from LauncherModel.Callbacks. |
| 2285 | */ |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 2286 | public void bindAppsAdded(ArrayList<ApplicationInfo> apps) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2287 | removeDialog(DIALOG_CREATE_SHORTCUT); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 2288 | mAllAppsGrid.addApps(apps); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | /** |
| 2292 | * A package was updated. |
| 2293 | * |
| 2294 | * Implementation of the method from LauncherModel.Callbacks. |
| 2295 | */ |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 2296 | public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2297 | removeDialog(DIALOG_CREATE_SHORTCUT); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 2298 | mWorkspace.updateShortcuts(apps); |
| 2299 | mAllAppsGrid.updateApps(apps); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /** |
| 2303 | * A package was uninstalled. |
| 2304 | * |
| 2305 | * Implementation of the method from LauncherModel.Callbacks. |
| 2306 | */ |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2307 | public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2308 | removeDialog(DIALOG_CREATE_SHORTCUT); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2309 | if (permanent) { |
| 2310 | mWorkspace.removeItems(apps); |
| 2311 | } |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 2312 | mAllAppsGrid.removeApps(apps); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2313 | } |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2314 | |
| 2315 | /** |
| 2316 | * Prints out out state for debugging. |
| 2317 | */ |
| 2318 | public void dumpState() { |
| 2319 | Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this); |
Joe Onorato | 39bfc13 | 2009-11-18 17:22:01 -0800 | [diff] [blame] | 2320 | Log.d(TAG, "mSavedState=" + mSavedState); |
Joe Onorato | 39bfc13 | 2009-11-18 17:22:01 -0800 | [diff] [blame] | 2321 | Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading); |
| 2322 | Log.d(TAG, "mRestoring=" + mRestoring); |
| 2323 | Log.d(TAG, "mWaitingForResult=" + mWaitingForResult); |
| 2324 | Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState); |
| 2325 | Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size()); |
Brad Fitzpatrick | 319226a | 2010-09-01 13:45:16 -0700 | [diff] [blame^] | 2326 | Log.d(TAG, "sFolders.size=" + sFolders.size()); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2327 | mModel.dumpState(); |
| 2328 | mAllAppsGrid.dumpState(); |
| 2329 | Log.d(TAG, "END launcher2 dump state"); |
| 2330 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2331 | } |