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