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