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 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.app.Dialog; |
Mike LeBeau | 736cf28 | 2009-07-02 17:46:59 -0700 | [diff] [blame] | 22 | import android.app.ISearchManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.app.SearchManager; |
| 24 | import android.app.StatusBarManager; |
Dianne Hackborn | 107f839 | 2009-08-05 21:32:16 -0700 | [diff] [blame] | 25 | import android.app.WallpaperManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.content.ActivityNotFoundException; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.ComponentName; |
| 29 | import android.content.ContentResolver; |
| 30 | import android.content.Context; |
| 31 | import android.content.DialogInterface; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 34 | import android.content.Intent.ShortcutIconResource; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.content.pm.ActivityInfo; |
| 36 | import android.content.pm.PackageManager; |
| 37 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.content.res.Configuration; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 39 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | import android.database.ContentObserver; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | import android.graphics.drawable.Drawable; |
| 43 | import android.graphics.drawable.TransitionDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | import android.os.Bundle; |
| 45 | import android.os.Handler; |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 46 | import android.os.Looper; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 47 | import android.os.Message; |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 48 | import android.os.MessageQueue; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | import android.os.Parcelable; |
| 50 | import android.os.RemoteException; |
| 51 | import android.os.ServiceManager; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 52 | import android.provider.LiveFolders; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | import android.text.Selection; |
| 54 | import android.text.SpannableStringBuilder; |
| 55 | import android.text.TextUtils; |
| 56 | import android.text.method.TextKeyListener; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 57 | import static android.util.Log.*; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 58 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | import android.view.Display; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | import android.view.KeyEvent; |
| 61 | import android.view.LayoutInflater; |
| 62 | import android.view.Menu; |
| 63 | import android.view.MenuItem; |
| 64 | import android.view.View; |
| 65 | import android.view.ViewGroup; |
Romain Guy | b1b69f5 | 2009-08-10 15:10:15 -0700 | [diff] [blame] | 66 | import android.view.WindowManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 67 | import android.view.View.OnLongClickListener; |
| 68 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | import android.widget.EditText; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | import android.widget.TextView; |
| 71 | import android.widget.Toast; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 72 | import android.appwidget.AppWidgetManager; |
| 73 | import android.appwidget.AppWidgetProviderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | |
| 75 | import java.lang.ref.WeakReference; |
| 76 | import java.util.ArrayList; |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 77 | import java.util.LinkedList; |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 78 | import java.io.DataOutputStream; |
| 79 | import java.io.FileNotFoundException; |
| 80 | import java.io.IOException; |
| 81 | import java.io.DataInputStream; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Default launcher application. |
| 85 | */ |
| 86 | public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener { |
| 87 | static final String LOG_TAG = "Launcher"; |
| 88 | static final boolean LOGD = false; |
| 89 | |
| 90 | private static final boolean PROFILE_STARTUP = false; |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 91 | private static final boolean PROFILE_ROTATE = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | private static final boolean DEBUG_USER_INTERFACE = false; |
Romain Guy | 6fefcf1 | 2009-06-11 13:07:43 -0700 | [diff] [blame] | 93 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 94 | private static final int WALLPAPER_SCREENS_SPAN = 2; |
| 95 | |
| 96 | private static final int MENU_GROUP_ADD = 1; |
| 97 | private static final int MENU_ADD = Menu.FIRST + 1; |
| 98 | private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1; |
| 99 | private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1; |
| 100 | private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1; |
Romain Guy | 94dabf1 | 2009-07-21 10:55:43 -0700 | [diff] [blame] | 101 | private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | |
| 103 | private static final int REQUEST_CREATE_SHORTCUT = 1; |
| 104 | private static final int REQUEST_CREATE_LIVE_FOLDER = 4; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 105 | private static final int REQUEST_CREATE_APPWIDGET = 5; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | private static final int REQUEST_PICK_APPLICATION = 6; |
| 107 | private static final int REQUEST_PICK_SHORTCUT = 7; |
| 108 | private static final int REQUEST_PICK_LIVE_FOLDER = 8; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 109 | private static final int REQUEST_PICK_APPWIDGET = 9; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 110 | |
| 111 | static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate"; |
| 112 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 113 | static final String EXTRA_CUSTOM_WIDGET = "custom_widget"; |
| 114 | static final String SEARCH_WIDGET = "search_widget"; |
| 115 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | static final int SCREEN_COUNT = 3; |
| 117 | static final int DEFAULT_SCREN = 1; |
| 118 | static final int NUMBER_CELLS_X = 4; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 119 | static final int NUMBER_CELLS_Y = 4; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 121 | static final int DIALOG_CREATE_SHORTCUT = 1; |
| 122 | static final int DIALOG_RENAME_FOLDER = 2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 124 | private static final String PREFERENCES = "launcher.preferences"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 125 | |
| 126 | // Type: int |
| 127 | private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen"; |
| 128 | // Type: boolean |
| 129 | private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder"; |
| 130 | // Type: long |
| 131 | private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder"; |
| 132 | // Type: int |
| 133 | private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen"; |
| 134 | // Type: int |
| 135 | private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX"; |
| 136 | // Type: int |
| 137 | private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY"; |
| 138 | // Type: int |
| 139 | private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX"; |
| 140 | // Type: int |
| 141 | private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY"; |
| 142 | // Type: int |
| 143 | private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX"; |
| 144 | // Type: int |
| 145 | private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY"; |
| 146 | // Type: int[] |
| 147 | private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells"; |
| 148 | // Type: boolean |
| 149 | private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder"; |
| 150 | // Type: long |
| 151 | private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id"; |
| 152 | |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 153 | private static final LauncherModel sModel = new LauncherModel(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 154 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 155 | private static final Object sLock = new Object(); |
| 156 | private static int sScreen = DEFAULT_SCREN; |
| 157 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver(); |
| 159 | private final ContentObserver mObserver = new FavoritesChangeObserver(); |
| 160 | |
| 161 | private LayoutInflater mInflater; |
| 162 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 163 | private DragController mDragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | private Workspace mWorkspace; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 165 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 166 | private AppWidgetManager mAppWidgetManager; |
| 167 | private LauncherAppWidgetHost mAppWidgetHost; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 168 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 169 | static final int APPWIDGET_HOST_ID = 1024; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 170 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 171 | private CellLayout.CellInfo mAddItemCellInfo; |
| 172 | private CellLayout.CellInfo mMenuAddInfo; |
| 173 | private final int[] mCellCoordinates = new int[2]; |
| 174 | private FolderInfo mFolderInfo; |
| 175 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 176 | private DeleteZone mDeleteZone; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 177 | private HandleView mHandleView; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 178 | private AllAppsView mAllAppsGrid; |
| 179 | private boolean mAllAppsVisible; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 180 | |
| 181 | private boolean mDesktopLocked = true; |
| 182 | private Bundle mSavedState; |
| 183 | |
| 184 | private SpannableStringBuilder mDefaultKeySsb = null; |
| 185 | |
| 186 | private boolean mDestroyed; |
Mike LeBeau | 736cf28 | 2009-07-02 17:46:59 -0700 | [diff] [blame] | 187 | |
| 188 | private boolean mIsNewIntent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 189 | |
| 190 | private boolean mRestoring; |
| 191 | private boolean mWaitingForResult; |
| 192 | private boolean mLocaleChanged; |
| 193 | |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 194 | private boolean mHomeDown; |
| 195 | private boolean mBackDown; |
| 196 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 197 | private Bundle mSavedInstanceState; |
| 198 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 199 | private DesktopBinder mBinder; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 200 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 201 | @Override |
| 202 | protected void onCreate(Bundle savedInstanceState) { |
| 203 | super.onCreate(savedInstanceState); |
Romain Guy | b1b69f5 | 2009-08-10 15:10:15 -0700 | [diff] [blame] | 204 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 205 | mInflater = getLayoutInflater(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 206 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 207 | mAppWidgetManager = AppWidgetManager.getInstance(this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 208 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 209 | mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID); |
| 210 | mAppWidgetHost.startListening(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 211 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 212 | if (PROFILE_STARTUP) { |
| 213 | android.os.Debug.startMethodTracing("/sdcard/launcher"); |
| 214 | } |
| 215 | |
| 216 | checkForLocaleChange(); |
| 217 | setWallpaperDimension(); |
| 218 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 219 | setContentView(R.layout.launcher); |
| 220 | setupViews(); |
| 221 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 222 | lockAllApps(); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 223 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 224 | registerIntentReceivers(); |
| 225 | registerContentObservers(); |
| 226 | |
| 227 | mSavedState = savedInstanceState; |
| 228 | restoreState(mSavedState); |
| 229 | |
| 230 | if (PROFILE_STARTUP) { |
| 231 | android.os.Debug.stopMethodTracing(); |
| 232 | } |
| 233 | |
| 234 | if (!mRestoring) { |
| 235 | startLoaders(); |
| 236 | } |
| 237 | |
| 238 | // For handling default keys |
| 239 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 240 | Selection.setSelection(mDefaultKeySsb, 0); |
| 241 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 242 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 243 | private void checkForLocaleChange() { |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 244 | final LocaleConfiguration localeConfiguration = new LocaleConfiguration(); |
| 245 | readConfiguration(this, localeConfiguration); |
| 246 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 247 | final Configuration configuration = getResources().getConfiguration(); |
| 248 | |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 249 | final String previousLocale = localeConfiguration.locale; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 250 | final String locale = configuration.locale.toString(); |
| 251 | |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 252 | final int previousMcc = localeConfiguration.mcc; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 253 | final int mcc = configuration.mcc; |
| 254 | |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 255 | final int previousMnc = localeConfiguration.mnc; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 256 | final int mnc = configuration.mnc; |
| 257 | |
| 258 | mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc; |
| 259 | |
| 260 | if (mLocaleChanged) { |
Romain Guy | 98d0165 | 2009-06-30 16:21:04 -0700 | [diff] [blame] | 261 | localeConfiguration.locale = locale; |
| 262 | localeConfiguration.mcc = mcc; |
| 263 | localeConfiguration.mnc = mnc; |
| 264 | |
| 265 | writeConfiguration(this, localeConfiguration); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private static class LocaleConfiguration { |
| 270 | public String locale; |
| 271 | public int mcc = -1; |
| 272 | public int mnc = -1; |
| 273 | } |
| 274 | |
| 275 | private static void readConfiguration(Context context, LocaleConfiguration configuration) { |
| 276 | DataInputStream in = null; |
| 277 | try { |
| 278 | in = new DataInputStream(context.openFileInput(PREFERENCES)); |
| 279 | configuration.locale = in.readUTF(); |
| 280 | configuration.mcc = in.readInt(); |
| 281 | configuration.mnc = in.readInt(); |
| 282 | } catch (FileNotFoundException e) { |
| 283 | // Ignore |
| 284 | } catch (IOException e) { |
| 285 | // Ignore |
| 286 | } finally { |
| 287 | if (in != null) { |
| 288 | try { |
| 289 | in.close(); |
| 290 | } catch (IOException e) { |
| 291 | // Ignore |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private static void writeConfiguration(Context context, LocaleConfiguration configuration) { |
| 298 | DataOutputStream out = null; |
| 299 | try { |
| 300 | out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE)); |
| 301 | out.writeUTF(configuration.locale); |
| 302 | out.writeInt(configuration.mcc); |
| 303 | out.writeInt(configuration.mnc); |
| 304 | out.flush(); |
| 305 | } catch (FileNotFoundException e) { |
| 306 | // Ignore |
| 307 | } catch (IOException e) { |
| 308 | //noinspection ResultOfMethodCallIgnored |
| 309 | context.getFileStreamPath(PREFERENCES).delete(); |
| 310 | } finally { |
| 311 | if (out != null) { |
| 312 | try { |
| 313 | out.close(); |
| 314 | } catch (IOException e) { |
| 315 | // Ignore |
| 316 | } |
| 317 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | static int getScreen() { |
| 322 | synchronized (sLock) { |
| 323 | return sScreen; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | static void setScreen(int screen) { |
| 328 | synchronized (sLock) { |
| 329 | sScreen = screen; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | private void startLoaders() { |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 334 | boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged); |
| 335 | sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications); |
| 336 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 337 | mRestoring = false; |
| 338 | } |
| 339 | |
| 340 | private void setWallpaperDimension() { |
Dianne Hackborn | 107f839 | 2009-08-05 21:32:16 -0700 | [diff] [blame] | 341 | WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 342 | |
| 343 | Display display = getWindowManager().getDefaultDisplay(); |
| 344 | boolean isPortrait = display.getWidth() < display.getHeight(); |
| 345 | |
| 346 | final int width = isPortrait ? display.getWidth() : display.getHeight(); |
| 347 | final int height = isPortrait ? display.getHeight() : display.getWidth(); |
Dianne Hackborn | 6427180 | 2009-08-08 20:54:24 -0700 | [diff] [blame] | 348 | wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
| 352 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
Romain Guy | aad5ef4 | 2009-06-10 02:48:37 -0700 | [diff] [blame] | 353 | mWaitingForResult = false; |
| 354 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 355 | // The pattern used here is that a user PICKs a specific application, |
| 356 | // which, depending on the target, might need to CREATE the actual target. |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 357 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 358 | // For example, the user would PICK_SHORTCUT for "Music playlist", and we |
| 359 | // launch over to the Music app to actually CREATE_SHORTCUT. |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 360 | |
Romain Guy | 94dabf1 | 2009-07-21 10:55:43 -0700 | [diff] [blame] | 361 | if (resultCode == RESULT_OK && mAddItemCellInfo != null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 362 | switch (requestCode) { |
| 363 | case REQUEST_PICK_APPLICATION: |
| 364 | completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked); |
| 365 | break; |
| 366 | case REQUEST_PICK_SHORTCUT: |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 367 | processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 368 | break; |
| 369 | case REQUEST_CREATE_SHORTCUT: |
| 370 | completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked); |
| 371 | break; |
| 372 | case REQUEST_PICK_LIVE_FOLDER: |
| 373 | addLiveFolder(data); |
| 374 | break; |
| 375 | case REQUEST_CREATE_LIVE_FOLDER: |
| 376 | completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked); |
| 377 | break; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 378 | case REQUEST_PICK_APPWIDGET: |
| 379 | addAppWidget(data); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 380 | break; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 381 | case REQUEST_CREATE_APPWIDGET: |
| 382 | completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 383 | break; |
| 384 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 385 | } else if (requestCode == REQUEST_PICK_APPWIDGET && |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 386 | resultCode == RESULT_CANCELED && data != null) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 387 | // Clean up the appWidgetId if we canceled |
| 388 | int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
| 389 | if (appWidgetId != -1) { |
| 390 | mAppWidgetHost.deleteAppWidgetId(appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
| 396 | protected void onResume() { |
| 397 | super.onResume(); |
| 398 | |
| 399 | if (mRestoring) { |
| 400 | startLoaders(); |
| 401 | } |
Mike LeBeau | 736cf28 | 2009-07-02 17:46:59 -0700 | [diff] [blame] | 402 | |
| 403 | // If this was a new intent (i.e., the mIsNewIntent flag got set to true by |
| 404 | // onNewIntent), then close the search dialog if needed, because it probably |
| 405 | // came from the user pressing 'home' (rather than, for example, pressing 'back'). |
| 406 | if (mIsNewIntent) { |
| 407 | // Post to a handler so that this happens after the search dialog tries to open |
| 408 | // itself again. |
| 409 | mWorkspace.post(new Runnable() { |
| 410 | public void run() { |
| 411 | ISearchManager searchManagerService = ISearchManager.Stub.asInterface( |
| 412 | ServiceManager.getService(Context.SEARCH_SERVICE)); |
| 413 | try { |
| 414 | searchManagerService.stopSearch(); |
| 415 | } catch (RemoteException e) { |
| 416 | e(LOG_TAG, "error stopping search", e); |
| 417 | } |
| 418 | } |
| 419 | }); |
| 420 | } |
| 421 | |
| 422 | mIsNewIntent = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | @Override |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 426 | protected void onPause() { |
| 427 | super.onPause(); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 428 | closeAllAppsDialog(false); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 429 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 430 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 431 | @Override |
| 432 | public Object onRetainNonConfigurationInstance() { |
| 433 | // Flag any binder to stop early before switching |
| 434 | if (mBinder != null) { |
| 435 | mBinder.mTerminate = true; |
| 436 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 437 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 438 | if (PROFILE_ROTATE) { |
| 439 | android.os.Debug.startMethodTracing("/sdcard/launcher-rotate"); |
| 440 | } |
| 441 | return null; |
| 442 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 443 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 444 | private boolean acceptFilter() { |
| 445 | final InputMethodManager inputManager = (InputMethodManager) |
| 446 | getSystemService(Context.INPUT_METHOD_SERVICE); |
| 447 | return !inputManager.isFullscreenMode(); |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 452 | boolean handled = super.onKeyDown(keyCode, event); |
| 453 | if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) { |
| 454 | boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb, |
| 455 | keyCode, event); |
| 456 | if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) { |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 457 | // something usable has been typed - start a search |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 458 | // the typed text will be retrieved and cleared by |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 459 | // showSearchDialog() |
| 460 | // If there are multiple keystrokes before the search dialog takes focus, |
| 461 | // onSearchRequested() will be called for every keystroke, |
| 462 | // but it is idempotent, so it's fine. |
| 463 | return onSearchRequested(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | return handled; |
| 468 | } |
| 469 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 470 | private String getTypedText() { |
| 471 | return mDefaultKeySsb.toString(); |
| 472 | } |
| 473 | |
| 474 | private void clearTypedText() { |
| 475 | mDefaultKeySsb.clear(); |
| 476 | mDefaultKeySsb.clearSpans(); |
| 477 | Selection.setSelection(mDefaultKeySsb, 0); |
| 478 | } |
| 479 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 480 | /** |
| 481 | * Restores the previous state, if it exists. |
| 482 | * |
| 483 | * @param savedState The previous state. |
| 484 | */ |
| 485 | private void restoreState(Bundle savedState) { |
| 486 | if (savedState == null) { |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1); |
| 491 | if (currentScreen > -1) { |
| 492 | mWorkspace.setCurrentScreen(currentScreen); |
| 493 | } |
| 494 | |
| 495 | final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1); |
| 496 | if (addScreen > -1) { |
| 497 | mAddItemCellInfo = new CellLayout.CellInfo(); |
| 498 | final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; |
| 499 | addItemCellInfo.valid = true; |
| 500 | addItemCellInfo.screen = addScreen; |
| 501 | addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X); |
| 502 | addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y); |
| 503 | addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X); |
| 504 | addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y); |
| 505 | addItemCellInfo.findVacantCellsFromOccupied( |
| 506 | savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS), |
| 507 | savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X), |
| 508 | savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y)); |
| 509 | mRestoring = true; |
| 510 | } |
| 511 | |
| 512 | boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false); |
| 513 | if (renameFolder) { |
| 514 | long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID); |
| 515 | mFolderInfo = sModel.getFolderById(this, id); |
| 516 | mRestoring = true; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Finds all the views we need and configure them properly. |
| 522 | */ |
| 523 | private void setupViews() { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 524 | mDragController = new DragController(this); |
| 525 | DragController dragController = mDragController; |
| 526 | |
Romain Guy | b1b69f5 | 2009-08-10 15:10:15 -0700 | [diff] [blame] | 527 | DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 528 | dragLayer.setDragController(dragController); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 529 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 530 | mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view); |
| 531 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 532 | mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); |
| 533 | final Workspace workspace = mWorkspace; |
| 534 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 535 | DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone); |
| 536 | mDeleteZone = deleteZone; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 537 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 538 | mHandleView = (HandleView) findViewById(R.id.all_apps_button); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 539 | mHandleView.setLauncher(this); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 540 | mHandleView.setOnClickListener(this); |
Romain Guy | b1b69f5 | 2009-08-10 15:10:15 -0700 | [diff] [blame] | 541 | TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable(); |
| 542 | handleIcon.setCrossFadeEnabled(true); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 543 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 544 | workspace.setOnLongClickListener(this); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 545 | workspace.setDragController(dragController); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 546 | workspace.setLauncher(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 547 | |
| 548 | deleteZone.setLauncher(this); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 549 | deleteZone.setDragController(dragController); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 550 | deleteZone.setHandle(mHandleView); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 551 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 552 | dragController.setDragScoller(workspace); |
| 553 | dragController.setDragListener(deleteZone); |
| 554 | dragController.setScrollView(dragLayer); |
| 555 | |
| 556 | // The order here is bottom to top. |
| 557 | dragController.addDropTarget(workspace); |
| 558 | dragController.addDropTarget(deleteZone); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Creates a view representing a shortcut. |
| 563 | * |
| 564 | * @param info The data structure describing the shortcut. |
| 565 | * |
| 566 | * @return A View inflated from R.layout.application. |
| 567 | */ |
| 568 | View createShortcut(ApplicationInfo info) { |
| 569 | return createShortcut(R.layout.application, |
| 570 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Creates a view representing a shortcut inflated from the specified resource. |
| 575 | * |
| 576 | * @param layoutResId The id of the XML layout used to create the shortcut. |
| 577 | * @param parent The group the shortcut belongs to. |
| 578 | * @param info The data structure describing the shortcut. |
| 579 | * |
| 580 | * @return A View inflated from layoutResId. |
| 581 | */ |
| 582 | View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) { |
| 583 | TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false); |
| 584 | |
| 585 | if (!info.filtered) { |
| 586 | info.icon = Utilities.createIconThumbnail(info.icon, this); |
| 587 | info.filtered = true; |
| 588 | } |
| 589 | |
| 590 | favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null); |
| 591 | favorite.setText(info.title); |
| 592 | favorite.setTag(info); |
| 593 | favorite.setOnClickListener(this); |
| 594 | |
| 595 | return favorite; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Add an application shortcut to the workspace. |
| 600 | * |
| 601 | * @param data The intent describing the application. |
| 602 | * @param cellInfo The position on screen where to create the shortcut. |
| 603 | */ |
| 604 | void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo, |
| 605 | boolean insertAtFirst) { |
| 606 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 607 | if (!findSingleSlot(cellInfo)) return; |
| 608 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 609 | final ApplicationInfo info = infoFromApplicationIntent(context, data); |
| 610 | if (info != null) { |
| 611 | mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 616 | ComponentName component = data.getComponent(); |
| 617 | PackageManager packageManager = context.getPackageManager(); |
| 618 | ActivityInfo activityInfo = null; |
| 619 | try { |
| 620 | activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */); |
| 621 | } catch (NameNotFoundException e) { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 622 | e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 623 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 625 | if (activityInfo != null) { |
| 626 | ApplicationInfo itemInfo = new ApplicationInfo(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 627 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 628 | itemInfo.title = activityInfo.loadLabel(packageManager); |
| 629 | if (itemInfo.title == null) { |
| 630 | itemInfo.title = activityInfo.name; |
| 631 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 632 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 633 | itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK | |
| 634 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 635 | itemInfo.icon = activityInfo.loadIcon(packageManager); |
| 636 | itemInfo.container = ItemInfo.NO_ID; |
| 637 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 638 | return itemInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 639 | } |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 640 | |
| 641 | return null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 642 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 643 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 644 | /** |
| 645 | * Add a shortcut to the workspace. |
| 646 | * |
| 647 | * @param data The intent describing the shortcut. |
| 648 | * @param cellInfo The position on screen where to create the shortcut. |
| 649 | * @param insertAtFirst |
| 650 | */ |
| 651 | private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo, |
| 652 | boolean insertAtFirst) { |
| 653 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 654 | if (!findSingleSlot(cellInfo)) return; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 655 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 656 | final ApplicationInfo info = addShortcut(this, data, cellInfo, false); |
| 657 | |
| 658 | if (!mRestoring) { |
| 659 | sModel.addDesktopItem(info); |
| 660 | |
| 661 | final View view = createShortcut(info); |
| 662 | mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); |
| 663 | } else if (sModel.isDesktopLoaded()) { |
| 664 | sModel.addDesktopItem(info); |
| 665 | } |
| 666 | } |
| 667 | |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 668 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 669 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 670 | * Add a widget to the workspace. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 671 | * |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 672 | * @param data The intent describing the appWidgetId. |
| 673 | * @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] | 674 | */ |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 675 | private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 676 | boolean insertAtFirst) { |
| 677 | |
| 678 | Bundle extras = data.getExtras(); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 679 | int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 680 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 681 | d(LOG_TAG, "dumping extras content="+extras.toString()); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 682 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 683 | AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 684 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 685 | // Calculate the grid spans needed to fit this widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 686 | CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 687 | int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 688 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 689 | // Try finding open space on Launcher screen |
| 690 | final int[] xy = mCellCoordinates; |
| 691 | if (!findSlot(cellInfo, xy, spans[0], spans[1])) return; |
| 692 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 693 | // Build Launcher-specific widget info and save to database |
| 694 | LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 695 | launcherInfo.spanX = spans[0]; |
| 696 | launcherInfo.spanY = spans[1]; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 697 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 698 | LauncherModel.addItemToDatabase(this, launcherInfo, |
| 699 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 700 | mWorkspace.getCurrentScreen(), xy[0], xy[1], false); |
| 701 | |
| 702 | if (!mRestoring) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 703 | sModel.addDesktopAppWidget(launcherInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 704 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 705 | // Perform actual inflation because we're live |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 706 | launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 707 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 708 | launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 709 | launcherInfo.hostView.setTag(launcherInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 710 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 711 | mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], |
| 712 | launcherInfo.spanX, launcherInfo.spanY, insertAtFirst); |
| 713 | } else if (sModel.isDesktopLoaded()) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 714 | sModel.addDesktopAppWidget(launcherInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 715 | } |
| 716 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 717 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 718 | public LauncherAppWidgetHost getAppWidgetHost() { |
| 719 | return mAppWidgetHost; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 720 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 721 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 722 | static ApplicationInfo addShortcut(Context context, Intent data, |
| 723 | CellLayout.CellInfo cellInfo, boolean notify) { |
| 724 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 725 | final ApplicationInfo info = infoFromShortcutIntent(context, data); |
| 726 | LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 727 | cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); |
| 728 | |
| 729 | return info; |
| 730 | } |
| 731 | |
| 732 | private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 733 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 734 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 735 | Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); |
| 736 | |
| 737 | Drawable icon = null; |
| 738 | boolean filtered = false; |
| 739 | boolean customIcon = false; |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 740 | ShortcutIconResource iconResource = null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 741 | |
| 742 | if (bitmap != null) { |
| 743 | icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context)); |
| 744 | filtered = true; |
| 745 | customIcon = true; |
| 746 | } else { |
| 747 | Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 748 | if (extra != null && extra instanceof ShortcutIconResource) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 749 | try { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 750 | iconResource = (ShortcutIconResource) extra; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 751 | final PackageManager packageManager = context.getPackageManager(); |
| 752 | Resources resources = packageManager.getResourcesForApplication( |
| 753 | iconResource.packageName); |
| 754 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
| 755 | icon = resources.getDrawable(id); |
| 756 | } catch (Exception e) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 757 | w(LOG_TAG, "Could not load shortcut icon: " + extra); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (icon == null) { |
| 763 | icon = context.getPackageManager().getDefaultActivityIcon(); |
| 764 | } |
| 765 | |
| 766 | final ApplicationInfo info = new ApplicationInfo(); |
| 767 | info.icon = icon; |
| 768 | info.filtered = filtered; |
| 769 | info.title = name; |
| 770 | info.intent = intent; |
| 771 | info.customIcon = customIcon; |
| 772 | info.iconResource = iconResource; |
| 773 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 774 | return info; |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | protected void onNewIntent(Intent intent) { |
| 779 | super.onNewIntent(intent); |
| 780 | |
| 781 | // Close the menu |
| 782 | if (Intent.ACTION_MAIN.equals(intent.getAction())) { |
| 783 | getWindow().closeAllPanels(); |
Mike LeBeau | 736cf28 | 2009-07-02 17:46:59 -0700 | [diff] [blame] | 784 | |
| 785 | // Set this flag so that onResume knows to close the search dialog if it's open, |
| 786 | // because this was a new intent (thus a press of 'home' or some such) rather than |
| 787 | // for example onResume being called when the user pressed the 'back' button. |
| 788 | mIsNewIntent = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 789 | |
| 790 | try { |
| 791 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 792 | // Unlock the workspace if the dialog was showing |
| 793 | mWorkspace.unlock(); |
| 794 | } catch (Exception e) { |
| 795 | // An exception is thrown if the dialog is not visible, which is fine |
| 796 | } |
| 797 | |
| 798 | try { |
| 799 | dismissDialog(DIALOG_RENAME_FOLDER); |
| 800 | // Unlock the workspace if the dialog was showing |
| 801 | mWorkspace.unlock(); |
| 802 | } catch (Exception e) { |
| 803 | // An exception is thrown if the dialog is not visible, which is fine |
| 804 | } |
| 805 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 806 | if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != |
| 807 | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 808 | |
Romain Guy | 94dabf1 | 2009-07-21 10:55:43 -0700 | [diff] [blame] | 809 | if (!mWorkspace.isDefaultScreenShowing()) { |
| 810 | mWorkspace.moveToDefaultScreen(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 811 | } |
Romain Guy | 1dd3a07 | 2009-07-16 13:21:01 -0700 | [diff] [blame] | 812 | |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 813 | closeAllAppsDialog(true); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 814 | |
| 815 | final View v = getWindow().peekDecorView(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 816 | if (v != null && v.getWindowToken() != null) { |
| 817 | InputMethodManager imm = (InputMethodManager)getSystemService( |
| 818 | INPUT_METHOD_SERVICE); |
| 819 | imm.hideSoftInputFromWindow(v.getWindowToken(), 0); |
| 820 | } |
| 821 | } else { |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 822 | closeAllAppsDialog(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | @Override |
| 828 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 829 | // Do not call super here |
| 830 | mSavedInstanceState = savedInstanceState; |
| 831 | } |
| 832 | |
| 833 | @Override |
| 834 | protected void onSaveInstanceState(Bundle outState) { |
| 835 | outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen()); |
| 836 | |
| 837 | final ArrayList<Folder> folders = mWorkspace.getOpenFolders(); |
| 838 | if (folders.size() > 0) { |
| 839 | final int count = folders.size(); |
| 840 | long[] ids = new long[count]; |
| 841 | for (int i = 0; i < count; i++) { |
| 842 | final FolderInfo info = folders.get(i).getInfo(); |
| 843 | ids[i] = info.id; |
| 844 | } |
| 845 | outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids); |
| 846 | } else { |
| 847 | super.onSaveInstanceState(outState); |
| 848 | } |
| 849 | |
Romain Guy | 3cf604f | 2009-06-16 13:12:53 -0700 | [diff] [blame] | 850 | final boolean isConfigurationChange = getChangingConfigurations() != 0; |
| 851 | |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 852 | // When the drawer is opened and we are saving the state because of a |
| 853 | // configuration change |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 854 | if (mAllAppsVisible && isConfigurationChange) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 855 | outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true); |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 856 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 857 | |
| 858 | if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) { |
| 859 | final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; |
| 860 | final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen); |
| 861 | |
| 862 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen); |
| 863 | outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX); |
| 864 | outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY); |
| 865 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX); |
| 866 | outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY); |
| 867 | outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX()); |
| 868 | outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY()); |
| 869 | outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS, |
| 870 | layout.getOccupiedCells()); |
| 871 | } |
| 872 | |
| 873 | if (mFolderInfo != null && mWaitingForResult) { |
| 874 | outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true); |
| 875 | outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | @Override |
| 880 | public void onDestroy() { |
| 881 | mDestroyed = true; |
| 882 | |
| 883 | super.onDestroy(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 884 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 885 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 886 | mAppWidgetHost.stopListening(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 887 | } catch (NullPointerException ex) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 888 | w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | TextKeyListener.getInstance().release(); |
| 892 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 893 | mAllAppsGrid.setAdapter(null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 894 | sModel.unbind(); |
| 895 | sModel.abortLoaders(); |
| 896 | |
| 897 | getContentResolver().unregisterContentObserver(mObserver); |
| 898 | unregisterReceiver(mApplicationsReceiver); |
| 899 | } |
| 900 | |
| 901 | @Override |
| 902 | public void startActivityForResult(Intent intent, int requestCode) { |
Romain Guy | 08f9749 | 2009-06-29 14:41:20 -0700 | [diff] [blame] | 903 | if (requestCode >= 0) mWaitingForResult = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 904 | super.startActivityForResult(intent, requestCode); |
| 905 | } |
| 906 | |
| 907 | @Override |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 908 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 909 | Bundle appSearchData, boolean globalSearch) { |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 910 | |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 911 | closeAllAppsDialog(false); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 912 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 913 | // Slide the search widget to the top, if it's on the current screen, |
| 914 | // otherwise show the search dialog immediately. |
| 915 | Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); |
| 916 | if (searchWidget == null) { |
| 917 | showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 918 | } else { |
| 919 | searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 920 | // show the currently typed text in the search widget while sliding |
| 921 | searchWidget.setQuery(getTypedText()); |
| 922 | } |
| 923 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 924 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 925 | /** |
| 926 | * Show the search dialog immediately, without changing the search widget. |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 927 | * |
| 928 | * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean) |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 929 | */ |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 930 | void showSearchDialog(String initialQuery, boolean selectInitialQuery, |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 931 | Bundle appSearchData, boolean globalSearch) { |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 932 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 933 | if (initialQuery == null) { |
| 934 | // Use any text typed in the launcher as the initial query |
| 935 | initialQuery = getTypedText(); |
| 936 | clearTypedText(); |
| 937 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 938 | if (appSearchData == null) { |
| 939 | appSearchData = new Bundle(); |
| 940 | appSearchData.putString(SearchManager.SOURCE, "launcher-search"); |
| 941 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 942 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 943 | final SearchManager searchManager = |
| 944 | (SearchManager) getSystemService(Context.SEARCH_SERVICE); |
| 945 | |
| 946 | final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); |
| 947 | if (searchWidget != null) { |
| 948 | // This gets called when the user leaves the search dialog to go back to |
| 949 | // the Launcher. |
| 950 | searchManager.setOnCancelListener(new SearchManager.OnCancelListener() { |
| 951 | public void onCancel() { |
| 952 | searchManager.setOnCancelListener(null); |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 953 | stopSearch(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 954 | } |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 955 | }); |
| 956 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 957 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 958 | searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 959 | appSearchData, globalSearch); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 960 | } |
| 961 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 962 | /** |
| 963 | * Cancel search dialog if it is open. |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 964 | */ |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 965 | void stopSearch() { |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 966 | // Close search dialog |
| 967 | SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); |
Bjorn Bringert | 0879cef | 2009-07-08 12:38:25 +0100 | [diff] [blame] | 968 | searchManager.stopSearch(); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 969 | // Restore search widget to its normal position |
| 970 | Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); |
| 971 | if (searchWidget != null) { |
| 972 | searchWidget.stopSearch(false); |
| 973 | } |
| 974 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 975 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 976 | @Override |
| 977 | public boolean onCreateOptionsMenu(Menu menu) { |
| 978 | if (mDesktopLocked) return false; |
| 979 | |
| 980 | super.onCreateOptionsMenu(menu); |
| 981 | menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add) |
| 982 | .setIcon(android.R.drawable.ic_menu_add) |
| 983 | .setAlphabeticShortcut('A'); |
| 984 | menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper) |
| 985 | .setIcon(android.R.drawable.ic_menu_gallery) |
| 986 | .setAlphabeticShortcut('W'); |
| 987 | menu.add(0, MENU_SEARCH, 0, R.string.menu_search) |
| 988 | .setIcon(android.R.drawable.ic_search_category_default) |
| 989 | .setAlphabeticShortcut(SearchManager.MENU_KEY); |
| 990 | menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications) |
| 991 | .setIcon(com.android.internal.R.drawable.ic_menu_notifications) |
| 992 | .setAlphabeticShortcut('N'); |
| 993 | |
| 994 | final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS); |
Romain Guy | 5a94139 | 2009-04-28 15:18:25 -0700 | [diff] [blame] | 995 | settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 996 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 997 | |
| 998 | menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings) |
| 999 | .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P') |
| 1000 | .setIntent(settings); |
| 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | @Override |
| 1006 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 1007 | super.onPrepareOptionsMenu(menu); |
| 1008 | |
| 1009 | mMenuAddInfo = mWorkspace.findAllVacantCells(null); |
| 1010 | menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid); |
| 1011 | |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | @Override |
| 1016 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1017 | switch (item.getItemId()) { |
| 1018 | case MENU_ADD: |
| 1019 | addItems(); |
| 1020 | return true; |
| 1021 | case MENU_WALLPAPER_SETTINGS: |
| 1022 | startWallpaper(); |
| 1023 | return true; |
| 1024 | case MENU_SEARCH: |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1025 | onSearchRequested(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1026 | return true; |
| 1027 | case MENU_NOTIFICATIONS: |
| 1028 | showNotifications(); |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | return super.onOptionsItemSelected(item); |
| 1033 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1034 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1035 | /** |
| 1036 | * Indicates that we want global search for this activity by setting the globalSearch |
| 1037 | * argument for {@link #startSearch} to true. |
| 1038 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1039 | |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1040 | @Override |
| 1041 | public boolean onSearchRequested() { |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1042 | startSearch(null, false, null, true); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1043 | return true; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1046 | private void addItems() { |
| 1047 | showAddDialog(mMenuAddInfo); |
| 1048 | } |
| 1049 | |
| 1050 | private void removeShortcutsForPackage(String packageName) { |
| 1051 | if (packageName != null && packageName.length() > 0) { |
| 1052 | mWorkspace.removeShortcutsForPackage(packageName); |
| 1053 | } |
| 1054 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1055 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1056 | private void updateShortcutsForPackage(String packageName) { |
| 1057 | if (packageName != null && packageName.length() > 0) { |
| 1058 | mWorkspace.updateShortcutsForPackage(packageName); |
| 1059 | } |
| 1060 | } |
| 1061 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1062 | void addAppWidget(Intent data) { |
| 1063 | // TODO: catch bad widget exception when sent |
| 1064 | int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1065 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1066 | String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET); |
| 1067 | if (SEARCH_WIDGET.equals(customWidget)) { |
| 1068 | // We don't need this any more, since this isn't a real app widget. |
| 1069 | mAppWidgetHost.deleteAppWidgetId(appWidgetId); |
| 1070 | // add the search widget |
| 1071 | addSearch(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1072 | } else { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1073 | AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); |
| 1074 | |
| 1075 | if (appWidget.configure != null) { |
| 1076 | // Launch over to configure widget, if needed |
| 1077 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); |
| 1078 | intent.setComponent(appWidget.configure); |
| 1079 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 1080 | |
| 1081 | startActivityForResult(intent, REQUEST_CREATE_APPWIDGET); |
| 1082 | } else { |
| 1083 | // Otherwise just add it |
| 1084 | onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data); |
| 1085 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1086 | } |
| 1087 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1088 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1089 | void addSearch() { |
| 1090 | final Widget info = Widget.makeSearch(); |
| 1091 | final CellLayout.CellInfo cellInfo = mAddItemCellInfo; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1092 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1093 | final int[] xy = mCellCoordinates; |
| 1094 | final int spanX = info.spanX; |
| 1095 | final int spanY = info.spanY; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1096 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1097 | if (!findSlot(cellInfo, xy, spanX, spanY)) return; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1098 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1099 | sModel.addDesktopItem(info); |
| 1100 | LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 1101 | mWorkspace.getCurrentScreen(), xy[0], xy[1], false); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1102 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1103 | final View view = mInflater.inflate(info.layoutResource, null); |
| 1104 | view.setTag(info); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1105 | Search search = (Search) view.findViewById(R.id.widget_search); |
| 1106 | search.setLauncher(this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1107 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1108 | mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY); |
| 1109 | } |
| 1110 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1111 | void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) { |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1112 | // Handle case where user selected "Applications" |
| 1113 | String applicationName = getResources().getString(R.string.group_applications); |
| 1114 | String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1115 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1116 | if (applicationName != null && applicationName.equals(shortcutName)) { |
| 1117 | Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
| 1118 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1119 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1120 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1121 | pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1122 | startActivityForResult(pickIntent, requestCodeApplication); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1123 | } else { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1124 | startActivityForResult(intent, requestCodeShortcut); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1125 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | void addLiveFolder(Intent intent) { |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1129 | // Handle case where user selected "Folder" |
Jeffrey Sharkey | c4bbd0a | 2009-03-24 22:47:52 -0700 | [diff] [blame] | 1130 | String folderName = getResources().getString(R.string.group_folder); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1131 | String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1132 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1133 | if (folderName != null && folderName.equals(shortcutName)) { |
| 1134 | addFolder(!mDesktopLocked); |
| 1135 | } else { |
| 1136 | startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER); |
| 1137 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | void addFolder(boolean insertAtFirst) { |
| 1141 | UserFolderInfo folderInfo = new UserFolderInfo(); |
| 1142 | folderInfo.title = getText(R.string.folder_name); |
| 1143 | |
| 1144 | CellLayout.CellInfo cellInfo = mAddItemCellInfo; |
| 1145 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 1146 | if (!findSingleSlot(cellInfo)) return; |
| 1147 | |
| 1148 | // Update the model |
| 1149 | LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 1150 | mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false); |
| 1151 | sModel.addDesktopItem(folderInfo); |
| 1152 | sModel.addFolder(folderInfo); |
| 1153 | |
| 1154 | // Create the view |
| 1155 | FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, |
| 1156 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo); |
| 1157 | mWorkspace.addInCurrentScreen(newFolder, |
| 1158 | cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); |
| 1159 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1160 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1161 | private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo, |
| 1162 | boolean insertAtFirst) { |
| 1163 | cellInfo.screen = mWorkspace.getCurrentScreen(); |
| 1164 | if (!findSingleSlot(cellInfo)) return; |
| 1165 | |
| 1166 | final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); |
| 1167 | |
| 1168 | if (!mRestoring) { |
| 1169 | sModel.addDesktopItem(info); |
| 1170 | |
| 1171 | final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, |
| 1172 | (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); |
| 1173 | mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); |
| 1174 | } else if (sModel.isDesktopLoaded()) { |
| 1175 | sModel.addDesktopItem(info); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | static LiveFolderInfo addLiveFolder(Context context, Intent data, |
| 1180 | CellLayout.CellInfo cellInfo, boolean notify) { |
| 1181 | |
| 1182 | Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT); |
| 1183 | String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME); |
| 1184 | |
| 1185 | Drawable icon = null; |
| 1186 | boolean filtered = false; |
| 1187 | Intent.ShortcutIconResource iconResource = null; |
| 1188 | |
| 1189 | Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON); |
| 1190 | if (extra != null && extra instanceof Intent.ShortcutIconResource) { |
| 1191 | try { |
| 1192 | iconResource = (Intent.ShortcutIconResource) extra; |
| 1193 | final PackageManager packageManager = context.getPackageManager(); |
| 1194 | Resources resources = packageManager.getResourcesForApplication( |
| 1195 | iconResource.packageName); |
| 1196 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
| 1197 | icon = resources.getDrawable(id); |
| 1198 | } catch (Exception e) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1199 | w(LOG_TAG, "Could not load live folder icon: " + extra); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (icon == null) { |
| 1204 | icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder); |
| 1205 | } |
| 1206 | |
| 1207 | final LiveFolderInfo info = new LiveFolderInfo(); |
| 1208 | info.icon = icon; |
| 1209 | info.filtered = filtered; |
| 1210 | info.title = name; |
| 1211 | info.iconResource = iconResource; |
| 1212 | info.uri = data.getData(); |
| 1213 | info.baseIntent = baseIntent; |
| 1214 | info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, |
| 1215 | LiveFolders.DISPLAY_MODE_GRID); |
| 1216 | |
| 1217 | LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, |
| 1218 | cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); |
| 1219 | sModel.addFolder(info); |
| 1220 | |
| 1221 | return info; |
| 1222 | } |
| 1223 | |
| 1224 | private boolean findSingleSlot(CellLayout.CellInfo cellInfo) { |
| 1225 | final int[] xy = new int[2]; |
| 1226 | if (findSlot(cellInfo, xy, 1, 1)) { |
| 1227 | cellInfo.cellX = xy[0]; |
| 1228 | cellInfo.cellY = xy[1]; |
| 1229 | return true; |
| 1230 | } |
| 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) { |
| 1235 | if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { |
| 1236 | boolean[] occupied = mSavedState != null ? |
| 1237 | mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null; |
| 1238 | cellInfo = mWorkspace.findAllVacantCells(occupied); |
| 1239 | if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { |
| 1240 | Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show(); |
| 1241 | return false; |
| 1242 | } |
| 1243 | } |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
| 1247 | private void showNotifications() { |
| 1248 | final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE); |
| 1249 | if (statusBar != null) { |
| 1250 | statusBar.expand(); |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | private void startWallpaper() { |
| 1255 | final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 1256 | startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper))); |
| 1257 | } |
| 1258 | |
| 1259 | /** |
| 1260 | * Registers various intent receivers. The current implementation registers |
| 1261 | * only a wallpaper intent receiver to let other applications change the |
| 1262 | * wallpaper. |
| 1263 | */ |
| 1264 | private void registerIntentReceivers() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1265 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 1266 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1267 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 1268 | filter.addDataScheme("package"); |
| 1269 | registerReceiver(mApplicationsReceiver, filter); |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * Registers various content observers. The current implementation registers |
| 1274 | * only a favorites observer to keep track of the favorites applications. |
| 1275 | */ |
| 1276 | private void registerContentObservers() { |
| 1277 | ContentResolver resolver = getContentResolver(); |
| 1278 | resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver); |
| 1279 | } |
| 1280 | |
| 1281 | @Override |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1282 | public void onWindowFocusChanged(boolean hasFocus) { |
| 1283 | super.onWindowFocusChanged(hasFocus); |
| 1284 | if (!hasFocus) { |
| 1285 | mBackDown = mHomeDown = false; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1290 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 1291 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 1292 | switch (event.getKeyCode()) { |
| 1293 | case KeyEvent.KEYCODE_BACK: |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1294 | mBackDown = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1295 | return true; |
| 1296 | case KeyEvent.KEYCODE_HOME: |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1297 | mHomeDown = true; |
| 1298 | return true; |
| 1299 | } |
| 1300 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 1301 | switch (event.getKeyCode()) { |
| 1302 | case KeyEvent.KEYCODE_BACK: |
| 1303 | if (!event.isCanceled()) { |
| 1304 | mWorkspace.dispatchKeyEvent(event); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1305 | if (mAllAppsVisible) { |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1306 | closeAllAppsDialog(true); |
Dianne Hackborn | 6780086 | 2009-07-24 17:15:20 -0700 | [diff] [blame] | 1307 | } else { |
| 1308 | closeFolder(); |
| 1309 | } |
| 1310 | } |
| 1311 | mBackDown = false; |
| 1312 | return true; |
| 1313 | case KeyEvent.KEYCODE_HOME: |
| 1314 | mHomeDown = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1315 | return true; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | return super.dispatchKeyEvent(event); |
| 1320 | } |
| 1321 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1322 | private void closeFolder() { |
| 1323 | Folder folder = mWorkspace.getOpenFolder(); |
| 1324 | if (folder != null) { |
| 1325 | closeFolder(folder); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | void closeFolder(Folder folder) { |
| 1330 | folder.getInfo().opened = false; |
| 1331 | ViewGroup parent = (ViewGroup) folder.getParent(); |
| 1332 | if (parent != null) { |
| 1333 | parent.removeView(folder); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1334 | mDragController.removeDropTarget((DropTarget)folder); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1335 | } |
| 1336 | folder.onClose(); |
| 1337 | } |
| 1338 | |
| 1339 | /** |
| 1340 | * When the notification that favorites have changed is received, requests |
| 1341 | * a favorites list refresh. |
| 1342 | */ |
| 1343 | private void onFavoritesChanged() { |
| 1344 | mDesktopLocked = true; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1345 | lockAllApps(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1346 | sModel.loadUserItems(false, this, false, false); |
| 1347 | } |
| 1348 | |
| 1349 | void onDesktopItemsLoaded() { |
| 1350 | if (mDestroyed) return; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1351 | bindDesktopItems(); |
| 1352 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1353 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1354 | /** |
| 1355 | * Refreshes the shortcuts shown on the workspace. |
| 1356 | */ |
| 1357 | private void bindDesktopItems() { |
| 1358 | final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems(); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1359 | final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets(); |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1360 | final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter(); |
| 1361 | if (shortcuts == null || appWidgets == null || drawerAdapter == null) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1362 | return; |
| 1363 | } |
| 1364 | |
| 1365 | final Workspace workspace = mWorkspace; |
| 1366 | int count = workspace.getChildCount(); |
| 1367 | for (int i = 0; i < count; i++) { |
| 1368 | ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout(); |
| 1369 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1370 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1371 | if (DEBUG_USER_INTERFACE) { |
| 1372 | android.widget.Button finishButton = new android.widget.Button(this); |
| 1373 | finishButton.setText("Finish"); |
| 1374 | workspace.addInScreen(finishButton, 1, 0, 0, 1, 1); |
| 1375 | |
| 1376 | finishButton.setOnClickListener(new android.widget.Button.OnClickListener() { |
| 1377 | public void onClick(View v) { |
| 1378 | finish(); |
| 1379 | } |
| 1380 | }); |
| 1381 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1382 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1383 | // Flag any old binder to terminate early |
| 1384 | if (mBinder != null) { |
| 1385 | mBinder.mTerminate = true; |
| 1386 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1387 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1388 | mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter); |
Jeffrey Sharkey | 2d132af | 2009-03-24 21:21:09 -0700 | [diff] [blame] | 1389 | mBinder.startBindingItems(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | private void bindItems(Launcher.DesktopBinder binder, |
| 1393 | ArrayList<ItemInfo> shortcuts, int start, int count) { |
| 1394 | |
| 1395 | final Workspace workspace = mWorkspace; |
| 1396 | final boolean desktopLocked = mDesktopLocked; |
| 1397 | |
| 1398 | final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count); |
| 1399 | int i = start; |
| 1400 | |
| 1401 | for ( ; i < end; i++) { |
| 1402 | final ItemInfo item = shortcuts.get(i); |
| 1403 | switch (item.itemType) { |
| 1404 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1405 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 1406 | final View shortcut = createShortcut((ApplicationInfo) item); |
| 1407 | workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, |
| 1408 | !desktopLocked); |
| 1409 | break; |
| 1410 | case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: |
| 1411 | final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, |
| 1412 | (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), |
| 1413 | (UserFolderInfo) item); |
| 1414 | workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1, |
| 1415 | !desktopLocked); |
| 1416 | break; |
| 1417 | case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: |
| 1418 | final FolderIcon newLiveFolder = LiveFolderIcon.fromXml( |
| 1419 | R.layout.live_folder_icon, this, |
| 1420 | (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), |
| 1421 | (LiveFolderInfo) item); |
| 1422 | workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1, |
| 1423 | !desktopLocked); |
| 1424 | break; |
| 1425 | case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH: |
| 1426 | final int screen = workspace.getCurrentScreen(); |
| 1427 | final View view = mInflater.inflate(R.layout.widget_search, |
| 1428 | (ViewGroup) workspace.getChildAt(screen), false); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1429 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1430 | Search search = (Search) view.findViewById(R.id.widget_search); |
| 1431 | search.setLauncher(this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1432 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1433 | final Widget widget = (Widget) item; |
| 1434 | view.setTag(widget); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1435 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1436 | workspace.addWidget(view, widget, !desktopLocked); |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | workspace.requestLayout(); |
| 1442 | |
| 1443 | if (end >= count) { |
| 1444 | finishBindDesktopItems(); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1445 | binder.startBindingDrawer(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1446 | } else { |
| 1447 | binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget(); |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | private void finishBindDesktopItems() { |
| 1452 | if (mSavedState != null) { |
| 1453 | if (!mWorkspace.hasFocus()) { |
| 1454 | mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); |
| 1455 | } |
| 1456 | |
| 1457 | final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS); |
| 1458 | if (userFolders != null) { |
| 1459 | for (long folderId : userFolders) { |
| 1460 | final FolderInfo info = sModel.findFolderById(folderId); |
| 1461 | if (info != null) { |
| 1462 | openFolder(info); |
| 1463 | } |
| 1464 | } |
| 1465 | final Folder openFolder = mWorkspace.getOpenFolder(); |
| 1466 | if (openFolder != null) { |
| 1467 | openFolder.requestFocus(); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false); |
| 1472 | if (allApps) { |
Joe Onorato | 45d4346 | 2009-07-31 14:36:51 -0700 | [diff] [blame] | 1473 | showAllAppsDialog(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | mSavedState = null; |
| 1477 | } |
| 1478 | |
| 1479 | if (mSavedInstanceState != null) { |
| 1480 | super.onRestoreInstanceState(mSavedInstanceState); |
| 1481 | mSavedInstanceState = null; |
| 1482 | } |
| 1483 | |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1484 | /* TODO |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1485 | if (mAllAppsVisible && !mDrawer.hasFocus()) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1486 | mDrawer.requestFocus(); |
| 1487 | } |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1488 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1489 | |
| 1490 | mDesktopLocked = false; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1491 | unlockAllApps(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1492 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1493 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1494 | private void bindDrawer(Launcher.DesktopBinder binder, |
| 1495 | ApplicationsAdapter drawerAdapter) { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1496 | mAllAppsGrid.setAdapter(drawerAdapter); |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1497 | binder.startBindingAppWidgetsWhenIdle(); |
| 1498 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1499 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1500 | private void bindAppWidgets(Launcher.DesktopBinder binder, |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1501 | LinkedList<LauncherAppWidgetInfo> appWidgets) { |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1502 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1503 | final Workspace workspace = mWorkspace; |
| 1504 | final boolean desktopLocked = mDesktopLocked; |
| 1505 | |
Jeffrey Sharkey | d18299f | 2009-03-24 18:11:49 -0700 | [diff] [blame] | 1506 | if (!appWidgets.isEmpty()) { |
| 1507 | final LauncherAppWidgetInfo item = appWidgets.removeFirst(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1508 | |
Jeffrey Sharkey | d18299f | 2009-03-24 18:11:49 -0700 | [diff] [blame] | 1509 | final int appWidgetId = item.appWidgetId; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1510 | final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); |
Jeffrey Sharkey | d18299f | 2009-03-24 18:11:49 -0700 | [diff] [blame] | 1511 | item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1512 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1513 | if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo)); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1514 | |
Jeffrey Sharkey | d18299f | 2009-03-24 18:11:49 -0700 | [diff] [blame] | 1515 | item.hostView.setAppWidget(appWidgetId, appWidgetInfo); |
| 1516 | item.hostView.setTag(item); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1517 | |
Jeffrey Sharkey | d18299f | 2009-03-24 18:11:49 -0700 | [diff] [blame] | 1518 | workspace.addInScreen(item.hostView, item.screen, item.cellX, |
| 1519 | item.cellY, item.spanX, item.spanY, !desktopLocked); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1520 | |
Jeffrey Sharkey | d18299f | 2009-03-24 18:11:49 -0700 | [diff] [blame] | 1521 | workspace.requestLayout(); |
| 1522 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1523 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1524 | if (appWidgets.isEmpty()) { |
| 1525 | if (PROFILE_ROTATE) { |
| 1526 | android.os.Debug.stopMethodTracing(); |
| 1527 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1528 | } else { |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1529 | binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1530 | } |
| 1531 | } |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 1532 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1533 | /** |
| 1534 | * Launches the intent referred by the clicked shortcut. |
| 1535 | * |
| 1536 | * @param v The view representing the clicked shortcut. |
| 1537 | */ |
| 1538 | public void onClick(View v) { |
| 1539 | Object tag = v.getTag(); |
| 1540 | if (tag instanceof ApplicationInfo) { |
| 1541 | // Open shortcut |
| 1542 | final Intent intent = ((ApplicationInfo) tag).intent; |
| 1543 | startActivitySafely(intent); |
| 1544 | } else if (tag instanceof FolderInfo) { |
| 1545 | handleFolderClick((FolderInfo) tag); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1546 | } else if (v == mHandleView) { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1547 | if (mAllAppsVisible) { |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1548 | // TODO how can we be here? |
| 1549 | } else { |
Joe Onorato | 45d4346 | 2009-07-31 14:36:51 -0700 | [diff] [blame] | 1550 | showAllAppsDialog(); |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1551 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | void startActivitySafely(Intent intent) { |
| 1556 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1557 | try { |
| 1558 | startActivity(intent); |
| 1559 | } catch (ActivityNotFoundException e) { |
| 1560 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 1561 | } catch (SecurityException e) { |
| 1562 | Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1563 | e(LOG_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] | 1564 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
| 1565 | "or use the exported attribute for this activity.", e); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | private void handleFolderClick(FolderInfo folderInfo) { |
| 1570 | if (!folderInfo.opened) { |
| 1571 | // Close any open folder |
| 1572 | closeFolder(); |
| 1573 | // Open the requested folder |
| 1574 | openFolder(folderInfo); |
| 1575 | } else { |
| 1576 | // Find the open folder... |
| 1577 | Folder openFolder = mWorkspace.getFolderForTag(folderInfo); |
| 1578 | int folderScreen; |
| 1579 | if (openFolder != null) { |
| 1580 | folderScreen = mWorkspace.getScreenForView(openFolder); |
| 1581 | // .. and close it |
| 1582 | closeFolder(openFolder); |
| 1583 | if (folderScreen != mWorkspace.getCurrentScreen()) { |
| 1584 | // Close any folder open on the current screen |
| 1585 | closeFolder(); |
| 1586 | // Pull the folder onto this screen |
| 1587 | openFolder(folderInfo); |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1593 | /** |
| 1594 | * Opens the user fodler described by the specified tag. The opening of the folder |
| 1595 | * is animated relative to the specified View. If the View is null, no animation |
| 1596 | * is played. |
| 1597 | * |
| 1598 | * @param folderInfo The FolderInfo describing the folder to open. |
| 1599 | */ |
| 1600 | private void openFolder(FolderInfo folderInfo) { |
| 1601 | Folder openFolder; |
| 1602 | |
| 1603 | if (folderInfo instanceof UserFolderInfo) { |
| 1604 | openFolder = UserFolder.fromXml(this); |
| 1605 | } else if (folderInfo instanceof LiveFolderInfo) { |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 1606 | openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1607 | } else { |
| 1608 | return; |
| 1609 | } |
| 1610 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1611 | openFolder.setDragController(mDragController); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1612 | openFolder.setLauncher(this); |
| 1613 | |
| 1614 | openFolder.bind(folderInfo); |
| 1615 | folderInfo.opened = true; |
| 1616 | |
| 1617 | mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4); |
| 1618 | openFolder.onOpen(); |
| 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * Returns true if the workspace is being loaded. When the workspace is loading, |
| 1623 | * no user interaction should be allowed to avoid any conflict. |
| 1624 | * |
| 1625 | * @return True if the workspace is locked, false otherwise. |
| 1626 | */ |
| 1627 | boolean isWorkspaceLocked() { |
| 1628 | return mDesktopLocked; |
| 1629 | } |
| 1630 | |
| 1631 | public boolean onLongClick(View v) { |
| 1632 | if (mDesktopLocked) { |
| 1633 | return false; |
| 1634 | } |
| 1635 | |
| 1636 | if (!(v instanceof CellLayout)) { |
| 1637 | v = (View) v.getParent(); |
| 1638 | } |
| 1639 | |
| 1640 | CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); |
| 1641 | |
| 1642 | // This happens when long clicking an item with the dpad/trackball |
| 1643 | if (cellInfo == null) { |
| 1644 | return true; |
| 1645 | } |
| 1646 | |
| 1647 | if (mWorkspace.allowLongPress()) { |
| 1648 | if (cellInfo.cell == null) { |
| 1649 | if (cellInfo.valid) { |
| 1650 | // User long pressed on empty space |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1651 | mWorkspace.setAllowLongPress(false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1652 | showAddDialog(cellInfo); |
| 1653 | } |
| 1654 | } else { |
| 1655 | if (!(cellInfo.cell instanceof Folder)) { |
| 1656 | // User long pressed on an item |
| 1657 | mWorkspace.startDrag(cellInfo); |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | return true; |
| 1662 | } |
| 1663 | |
| 1664 | static LauncherModel getModel() { |
| 1665 | return sModel; |
| 1666 | } |
| 1667 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 1668 | View getDrawerHandle() { |
| 1669 | return mHandleView; |
| 1670 | } |
| 1671 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1672 | boolean isDrawerDown() { // TODO rename to isAllAppsVisible() |
| 1673 | return /* TODO !mDrawer.isMoving() && */ !mAllAppsVisible; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1676 | Workspace getWorkspace() { |
| 1677 | return mWorkspace; |
| 1678 | } |
| 1679 | |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1680 | /* TODO |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1681 | GridView getApplicationsGrid() { |
| 1682 | return mAllAppsGrid; |
| 1683 | } |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1684 | */ |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1685 | |
| 1686 | @Override |
| 1687 | protected Dialog onCreateDialog(int id) { |
| 1688 | switch (id) { |
| 1689 | case DIALOG_CREATE_SHORTCUT: |
| 1690 | return new CreateShortcut().createDialog(); |
| 1691 | case DIALOG_RENAME_FOLDER: |
| 1692 | return new RenameFolder().createDialog(); |
| 1693 | } |
| 1694 | |
| 1695 | return super.onCreateDialog(id); |
| 1696 | } |
| 1697 | |
| 1698 | @Override |
| 1699 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 1700 | switch (id) { |
| 1701 | case DIALOG_CREATE_SHORTCUT: |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1702 | break; |
| 1703 | case DIALOG_RENAME_FOLDER: |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1704 | if (mFolderInfo != null) { |
| 1705 | EditText input = (EditText) dialog.findViewById(R.id.folder_name); |
| 1706 | final CharSequence text = mFolderInfo.title; |
| 1707 | input.setText(text); |
| 1708 | input.setSelection(0, text.length()); |
| 1709 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | void showRenameDialog(FolderInfo info) { |
| 1715 | mFolderInfo = info; |
| 1716 | mWaitingForResult = true; |
| 1717 | showDialog(DIALOG_RENAME_FOLDER); |
| 1718 | } |
| 1719 | |
| 1720 | private void showAddDialog(CellLayout.CellInfo cellInfo) { |
| 1721 | mAddItemCellInfo = cellInfo; |
| 1722 | mWaitingForResult = true; |
| 1723 | showDialog(DIALOG_CREATE_SHORTCUT); |
| 1724 | } |
| 1725 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1726 | private void pickShortcut(int requestCode, int title) { |
| 1727 | Bundle bundle = new Bundle(); |
| 1728 | |
| 1729 | ArrayList<String> shortcutNames = new ArrayList<String>(); |
| 1730 | shortcutNames.add(getString(R.string.group_applications)); |
| 1731 | bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); |
| 1732 | |
| 1733 | ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>(); |
| 1734 | shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, |
| 1735 | R.drawable.ic_launcher_application)); |
| 1736 | bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); |
| 1737 | |
| 1738 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1739 | pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT)); |
| 1740 | pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title)); |
| 1741 | pickIntent.putExtras(bundle); |
| 1742 | |
| 1743 | startActivityForResult(pickIntent, requestCode); |
| 1744 | } |
| 1745 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1746 | private class RenameFolder { |
| 1747 | private EditText mInput; |
| 1748 | |
| 1749 | Dialog createDialog() { |
| 1750 | mWaitingForResult = true; |
| 1751 | final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null); |
| 1752 | mInput = (EditText) layout.findViewById(R.id.folder_name); |
| 1753 | |
| 1754 | AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); |
| 1755 | builder.setIcon(0); |
| 1756 | builder.setTitle(getString(R.string.rename_folder_title)); |
| 1757 | builder.setCancelable(true); |
| 1758 | builder.setOnCancelListener(new Dialog.OnCancelListener() { |
| 1759 | public void onCancel(DialogInterface dialog) { |
| 1760 | cleanup(); |
| 1761 | } |
| 1762 | }); |
| 1763 | builder.setNegativeButton(getString(R.string.cancel_action), |
| 1764 | new Dialog.OnClickListener() { |
| 1765 | public void onClick(DialogInterface dialog, int which) { |
| 1766 | cleanup(); |
| 1767 | } |
| 1768 | } |
| 1769 | ); |
| 1770 | builder.setPositiveButton(getString(R.string.rename_action), |
| 1771 | new Dialog.OnClickListener() { |
| 1772 | public void onClick(DialogInterface dialog, int which) { |
| 1773 | changeFolderName(); |
| 1774 | } |
| 1775 | } |
| 1776 | ); |
| 1777 | builder.setView(layout); |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1778 | |
| 1779 | final AlertDialog dialog = builder.create(); |
| 1780 | dialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| 1781 | public void onShow(DialogInterface dialog) { |
| 1782 | mWorkspace.lock(); |
| 1783 | } |
| 1784 | }); |
| 1785 | |
| 1786 | return dialog; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | private void changeFolderName() { |
| 1790 | final String name = mInput.getText().toString(); |
| 1791 | if (!TextUtils.isEmpty(name)) { |
| 1792 | // Make sure we have the right folder info |
| 1793 | mFolderInfo = sModel.findFolderById(mFolderInfo.id); |
| 1794 | mFolderInfo.title = name; |
| 1795 | LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo); |
| 1796 | |
| 1797 | if (mDesktopLocked) { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1798 | lockAllApps(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1799 | sModel.loadUserItems(false, Launcher.this, false, false); |
| 1800 | } else { |
| 1801 | final FolderIcon folderIcon = (FolderIcon) |
| 1802 | mWorkspace.getViewForTag(mFolderInfo); |
| 1803 | if (folderIcon != null) { |
| 1804 | folderIcon.setText(name); |
| 1805 | getWorkspace().requestLayout(); |
| 1806 | } else { |
| 1807 | mDesktopLocked = true; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1808 | lockAllApps(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1809 | sModel.loadUserItems(false, Launcher.this, false, false); |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | cleanup(); |
| 1814 | } |
| 1815 | |
| 1816 | private void cleanup() { |
| 1817 | mWorkspace.unlock(); |
| 1818 | dismissDialog(DIALOG_RENAME_FOLDER); |
| 1819 | mWaitingForResult = false; |
| 1820 | mFolderInfo = null; |
| 1821 | } |
| 1822 | } |
| 1823 | |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1824 | void showAllAppsDialog() { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1825 | mAllAppsVisible = true; |
| 1826 | mAllAppsGrid.setVisibility(View.VISIBLE); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1827 | mWorkspace.hide(); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1828 | |
| 1829 | // TODO: fade these two too |
| 1830 | mDeleteZone.setVisibility(View.GONE); |
| 1831 | mHandleView.setVisibility(View.GONE); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | void closeAllAppsDialog(boolean animated) { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1835 | if (mAllAppsVisible) { |
| 1836 | Log.d(LOG_TAG, "closing all apps"); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1837 | if (animated) { |
| 1838 | // TODO mDrawer.animateClose(); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1839 | mAllAppsGrid.setVisibility(View.GONE); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1840 | } else { |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1841 | mAllAppsGrid.setVisibility(View.GONE); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1842 | } |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1843 | mAllAppsVisible = false; |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1844 | mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); |
| 1845 | mWorkspace.show(); |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1846 | |
| 1847 | // TODO: fade these two too |
| 1848 | mDeleteZone.setVisibility(View.VISIBLE); |
| 1849 | mHandleView.setVisibility(View.VISIBLE); |
Joe Onorato | e77c08d | 2009-08-01 00:01:20 -0700 | [diff] [blame] | 1850 | } |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame^] | 1853 | void lockAllApps() { |
| 1854 | // TODO |
| 1855 | } |
| 1856 | |
| 1857 | void unlockAllApps() { |
| 1858 | // TODO |
| 1859 | } |
| 1860 | |
Joe Onorato | 7404ee4 | 2009-07-31 11:54:44 -0700 | [diff] [blame] | 1861 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1862 | * Displays the shortcut creation dialog and launches, if necessary, the |
| 1863 | * appropriate activity. |
| 1864 | */ |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1865 | private class CreateShortcut implements DialogInterface.OnClickListener, |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1866 | DialogInterface.OnCancelListener, DialogInterface.OnDismissListener, |
| 1867 | DialogInterface.OnShowListener { |
| 1868 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1869 | private AddAdapter mAdapter; |
Romain Guy | 9ffb543 | 2009-03-24 21:04:15 -0700 | [diff] [blame] | 1870 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1871 | Dialog createDialog() { |
| 1872 | mWaitingForResult = true; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1873 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1874 | mAdapter = new AddAdapter(Launcher.this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1875 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1876 | final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); |
| 1877 | builder.setTitle(getString(R.string.menu_item_add_item)); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1878 | builder.setAdapter(mAdapter, this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1879 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1880 | builder.setInverseBackgroundForced(true); |
| 1881 | |
| 1882 | AlertDialog dialog = builder.create(); |
| 1883 | dialog.setOnCancelListener(this); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1884 | dialog.setOnDismissListener(this); |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1885 | dialog.setOnShowListener(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1886 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1887 | return dialog; |
| 1888 | } |
| 1889 | |
| 1890 | public void onCancel(DialogInterface dialog) { |
| 1891 | mWaitingForResult = false; |
| 1892 | cleanup(); |
| 1893 | } |
| 1894 | |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1895 | public void onDismiss(DialogInterface dialog) { |
| 1896 | mWorkspace.unlock(); |
| 1897 | } |
| 1898 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1899 | private void cleanup() { |
| 1900 | mWorkspace.unlock(); |
| 1901 | dismissDialog(DIALOG_CREATE_SHORTCUT); |
| 1902 | } |
| 1903 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1904 | /** |
| 1905 | * Handle the action clicked in the "Add to home" dialog. |
| 1906 | */ |
| 1907 | public void onClick(DialogInterface dialog, int which) { |
| 1908 | Resources res = getResources(); |
| 1909 | cleanup(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1910 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1911 | switch (which) { |
| 1912 | case AddAdapter.ITEM_SHORTCUT: { |
| 1913 | // Insert extra item to handle picking application |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 1914 | pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut); |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1915 | break; |
| 1916 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1917 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1918 | case AddAdapter.ITEM_APPWIDGET: { |
| 1919 | int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1920 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1921 | Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); |
| 1922 | pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 1923 | // add the search widget |
| 1924 | ArrayList<AppWidgetProviderInfo> customInfo = |
| 1925 | new ArrayList<AppWidgetProviderInfo>(); |
| 1926 | AppWidgetProviderInfo info = new AppWidgetProviderInfo(); |
| 1927 | info.provider = new ComponentName(getPackageName(), "XXX.YYY"); |
| 1928 | info.label = getString(R.string.group_search); |
| 1929 | info.icon = R.drawable.ic_search_widget; |
| 1930 | customInfo.add(info); |
| 1931 | pickIntent.putParcelableArrayListExtra( |
| 1932 | AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo); |
| 1933 | ArrayList<Bundle> customExtras = new ArrayList<Bundle>(); |
| 1934 | Bundle b = new Bundle(); |
| 1935 | b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET); |
| 1936 | customExtras.add(b); |
| 1937 | pickIntent.putParcelableArrayListExtra( |
| 1938 | AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras); |
| 1939 | // start the pick activity |
| 1940 | startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET); |
| 1941 | break; |
| 1942 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1943 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1944 | case AddAdapter.ITEM_LIVE_FOLDER: { |
| 1945 | // Insert extra item to handle inserting folder |
| 1946 | Bundle bundle = new Bundle(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1947 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1948 | ArrayList<String> shortcutNames = new ArrayList<String>(); |
| 1949 | shortcutNames.add(res.getString(R.string.group_folder)); |
| 1950 | bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1951 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1952 | ArrayList<ShortcutIconResource> shortcutIcons = |
| 1953 | new ArrayList<ShortcutIconResource>(); |
| 1954 | shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, |
| 1955 | R.drawable.ic_launcher_folder)); |
| 1956 | bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); |
| 1957 | |
| 1958 | Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); |
| 1959 | pickIntent.putExtra(Intent.EXTRA_INTENT, |
| 1960 | new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER)); |
| 1961 | pickIntent.putExtra(Intent.EXTRA_TITLE, |
| 1962 | getText(R.string.title_select_live_folder)); |
| 1963 | pickIntent.putExtras(bundle); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 1964 | |
Jeffrey Sharkey | c7fdae1 | 2009-03-24 20:41:22 -0700 | [diff] [blame] | 1965 | startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER); |
| 1966 | break; |
| 1967 | } |
| 1968 | |
| 1969 | case AddAdapter.ITEM_WALLPAPER: { |
| 1970 | startWallpaper(); |
| 1971 | break; |
| 1972 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1973 | } |
| 1974 | } |
Romain Guy | 7b4ef33 | 2009-07-14 13:58:08 -0700 | [diff] [blame] | 1975 | |
| 1976 | public void onShow(DialogInterface dialog) { |
| 1977 | mWorkspace.lock(); |
| 1978 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * Receives notifications when applications are added/removed. |
| 1983 | */ |
| 1984 | private class ApplicationsIntentReceiver extends BroadcastReceiver { |
| 1985 | @Override |
| 1986 | public void onReceive(Context context, Intent intent) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1987 | final String action = intent.getAction(); |
| 1988 | final String packageName = intent.getData().getSchemeSpecificPart(); |
| 1989 | final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 1990 | |
| 1991 | if (LauncherModel.DEBUG_LOADERS) { |
| 1992 | d(LauncherModel.LOG_TAG, "application intent received: " + action + |
| 1993 | ", replacing=" + replacing); |
| 1994 | d(LauncherModel.LOG_TAG, " --> " + intent.getData()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1995 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1996 | |
| 1997 | if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) { |
| 1998 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 1999 | if (!replacing) { |
| 2000 | removeShortcutsForPackage(packageName); |
| 2001 | if (LauncherModel.DEBUG_LOADERS) { |
| 2002 | d(LauncherModel.LOG_TAG, " --> remove package"); |
| 2003 | } |
| 2004 | sModel.removePackage(Launcher.this, packageName); |
| 2005 | } |
| 2006 | // else, we are replacing the package, so a PACKAGE_ADDED will be sent |
| 2007 | // later, we will update the package at this time |
| 2008 | } else { |
| 2009 | if (!replacing) { |
| 2010 | if (LauncherModel.DEBUG_LOADERS) { |
| 2011 | d(LauncherModel.LOG_TAG, " --> add package"); |
| 2012 | } |
| 2013 | sModel.addPackage(Launcher.this, packageName); |
| 2014 | } else { |
| 2015 | if (LauncherModel.DEBUG_LOADERS) { |
| 2016 | d(LauncherModel.LOG_TAG, " --> update package " + packageName); |
| 2017 | } |
| 2018 | sModel.updatePackage(Launcher.this, packageName); |
| 2019 | updateShortcutsForPackage(packageName); |
| 2020 | } |
| 2021 | } |
| 2022 | removeDialog(DIALOG_CREATE_SHORTCUT); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2023 | } else { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2024 | if (LauncherModel.DEBUG_LOADERS) { |
| 2025 | d(LauncherModel.LOG_TAG, " --> sync package " + packageName); |
| 2026 | } |
| 2027 | sModel.syncPackage(Launcher.this, packageName); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * Receives notifications whenever the user favorites have changed. |
| 2034 | */ |
| 2035 | private class FavoritesChangeObserver extends ContentObserver { |
| 2036 | public FavoritesChangeObserver() { |
| 2037 | super(new Handler()); |
| 2038 | } |
| 2039 | |
| 2040 | @Override |
| 2041 | public void onChange(boolean selfChange) { |
| 2042 | onFavoritesChanged(); |
| 2043 | } |
| 2044 | } |
| 2045 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2046 | private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2047 | static final int MESSAGE_BIND_ITEMS = 0x1; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 2048 | static final int MESSAGE_BIND_APPWIDGETS = 0x2; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2049 | static final int MESSAGE_BIND_DRAWER = 0x3; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2050 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2051 | // Number of items to bind in every pass |
| 2052 | static final int ITEMS_COUNT = 6; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2053 | |
| 2054 | private final ArrayList<ItemInfo> mShortcuts; |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2055 | private final LinkedList<LauncherAppWidgetInfo> mAppWidgets; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2056 | private final ApplicationsAdapter mDrawerAdapter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2057 | private final WeakReference<Launcher> mLauncher; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2058 | |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2059 | public boolean mTerminate = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2060 | |
| 2061 | DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts, |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2062 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 2063 | ApplicationsAdapter drawerAdapter) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2064 | |
| 2065 | mLauncher = new WeakReference<Launcher>(launcher); |
| 2066 | mShortcuts = shortcuts; |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2067 | mDrawerAdapter = drawerAdapter; |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2068 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2069 | // Sort widgets so active workspace is bound first |
| 2070 | final int currentScreen = launcher.mWorkspace.getCurrentScreen(); |
| 2071 | final int size = appWidgets.size(); |
| 2072 | mAppWidgets = new LinkedList<LauncherAppWidgetInfo>(); |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2073 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2074 | for (int i = 0; i < size; i++) { |
| 2075 | LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i); |
| 2076 | if (appWidgetInfo.screen == currentScreen) { |
| 2077 | mAppWidgets.addFirst(appWidgetInfo); |
| 2078 | } else { |
| 2079 | mAppWidgets.addLast(appWidgetInfo); |
| 2080 | } |
| 2081 | } |
| 2082 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2083 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2084 | public void startBindingItems() { |
| 2085 | obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget(); |
| 2086 | } |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2087 | |
| 2088 | public void startBindingDrawer() { |
| 2089 | obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget(); |
| 2090 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2091 | |
Jeffrey Sharkey | 2d132af | 2009-03-24 21:21:09 -0700 | [diff] [blame] | 2092 | public void startBindingAppWidgetsWhenIdle() { |
| 2093 | // Ask for notification when message queue becomes idle |
| 2094 | final MessageQueue messageQueue = Looper.myQueue(); |
| 2095 | messageQueue.addIdleHandler(this); |
| 2096 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2097 | |
Jeffrey Sharkey | 2d132af | 2009-03-24 21:21:09 -0700 | [diff] [blame] | 2098 | public boolean queueIdle() { |
| 2099 | // Queue is idle, so start binding items |
| 2100 | startBindingAppWidgets(); |
| 2101 | return false; |
| 2102 | } |
| 2103 | |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2104 | public void startBindingAppWidgets() { |
| 2105 | obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget(); |
| 2106 | } |
| 2107 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2108 | @Override |
| 2109 | public void handleMessage(Message msg) { |
| 2110 | Launcher launcher = mLauncher.get(); |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2111 | if (launcher == null || mTerminate) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2112 | return; |
| 2113 | } |
Romain Guy | cbb89e4 | 2009-06-08 15:52:54 -0700 | [diff] [blame] | 2114 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2115 | switch (msg.what) { |
| 2116 | case MESSAGE_BIND_ITEMS: { |
| 2117 | launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2); |
| 2118 | break; |
| 2119 | } |
Karl Rosaen | 138a041 | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 2120 | case MESSAGE_BIND_DRAWER: { |
| 2121 | launcher.bindDrawer(this, mDrawerAdapter); |
| 2122 | break; |
| 2123 | } |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 2124 | case MESSAGE_BIND_APPWIDGETS: { |
Jeffrey Sharkey | 99c8758 | 2009-03-24 17:59:43 -0700 | [diff] [blame] | 2125 | launcher.bindAppWidgets(this, mAppWidgets); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2126 | break; |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | } |