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