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