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