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