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