The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 19 | import com.android.browser.provider.BrowserProvider2; |
| 20 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 22 | import android.app.LoaderManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 24 | import android.content.ContentUris; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.content.CursorLoader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 28 | import android.content.Intent; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 29 | import android.content.Loader; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.content.res.Resources; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 32 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.net.ParseException; |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | import android.net.WebAddress; |
| 37 | import android.os.Bundle; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 38 | import android.os.Handler; |
| 39 | import android.os.Message; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 40 | import android.preference.PreferenceManager; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 41 | import android.provider.BrowserContract; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 42 | import android.text.TextUtils; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 43 | import android.view.KeyEvent; |
| 44 | import android.view.LayoutInflater; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 45 | import android.view.MenuItem; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 46 | import android.view.View; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 47 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 48 | import android.view.Window; |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 49 | import android.view.WindowManager; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 50 | import android.view.inputmethod.EditorInfo; |
| 51 | import android.view.inputmethod.InputMethodManager; |
| 52 | import android.widget.AdapterView; |
| 53 | import android.widget.CursorAdapter; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | import android.widget.EditText; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 55 | import android.widget.ListView; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 56 | import android.widget.PopupMenu; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 57 | import android.widget.TextView; |
| 58 | import android.widget.Toast; |
| 59 | |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 60 | import java.net.URI; |
| 61 | import java.net.URISyntaxException; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 62 | import java.util.ArrayList; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 63 | import java.util.Stack; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 64 | |
| 65 | public class AddBookmarkPage extends Activity |
| 66 | implements View.OnClickListener, TextView.OnEditorActionListener, |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 67 | AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>, |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 68 | BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 69 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 70 | public static final long DEFAULT_FOLDER_ID = -1; |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 71 | public static final String TOUCH_ICON_URL = "touch_icon_url"; |
| 72 | // Place on an edited bookmark to remove the saved thumbnail |
| 73 | public static final String REMOVE_THUMBNAIL = "remove_thumbnail"; |
| 74 | public static final String USER_AGENT = "user_agent"; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 75 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 76 | private static final int MAX_CRUMBS_SHOWN = 2; |
| 77 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | private final String LOGTAG = "Bookmarks"; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 79 | // Set to true to see the crash on the code I would like to run. |
| 80 | private final boolean DEBUG_CRASH = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 81 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 82 | // IDs for the CursorLoaders that are used. |
| 83 | private final int LOADER_ID_FOLDER_CONTENTS = 0; |
| 84 | private final int LOADER_ID_ALL_FOLDERS = 1; |
| 85 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 86 | private EditText mTitle; |
| 87 | private EditText mAddress; |
| 88 | private TextView mButton; |
| 89 | private View mCancelButton; |
| 90 | private boolean mEditingExisting; |
| 91 | private Bundle mMap; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 92 | private String mTouchIconUrl; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 93 | private String mOriginalUrl; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 94 | private TextView mFolder; |
| 95 | private View mDefaultView; |
| 96 | private View mFolderSelector; |
| 97 | private EditText mFolderNamer; |
| 98 | private View mAddNewFolder; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 99 | private View mAddSeparator; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 100 | private long mCurrentFolder = 0; |
| 101 | private FolderAdapter mAdapter; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 102 | private BreadCrumbView mCrumbs; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 103 | private TextView mFakeTitle; |
| 104 | private View mCrumbHolder; |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 105 | private ListView mListView; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 106 | private boolean mSaveToHomeScreen; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 107 | |
| 108 | private static class Folder { |
| 109 | String Name; |
| 110 | long Id; |
| 111 | Folder(String name, long id) { |
| 112 | Name = name; |
| 113 | Id = id; |
| 114 | } |
| 115 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 116 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 117 | // Message IDs |
| 118 | private static final int SAVE_BOOKMARK = 100; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 119 | private static final int TOUCH_ICON_DOWNLOADED = 101; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 120 | |
| 121 | private Handler mHandler; |
| 122 | |
Bjorn Bringert | b1402a5 | 2010-10-12 10:53:12 +0100 | [diff] [blame] | 123 | private InputMethodManager getInputMethodManager() { |
| 124 | return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); |
| 125 | } |
| 126 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 127 | @Override |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 128 | public void onTop(int level, Object data) { |
| 129 | if (null == data) return; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 130 | Folder folderData = (Folder) data; |
| 131 | long folder = folderData.Id; |
| 132 | Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 133 | LoaderManager manager = getLoaderManager(); |
| 134 | CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader( |
| 135 | LOADER_ID_FOLDER_CONTENTS)); |
| 136 | loader.setUri(uri); |
| 137 | loader.forceLoad(); |
| 138 | updateVisible(); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 139 | if (mFolderNamer.getVisibility() == View.VISIBLE) { |
| 140 | completeOrCancelFolderNaming(true); |
| 141 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Update the views shown to only show the two deepest levels of crumbs. |
| 146 | * Note that this method depends on internal knowledge of BreadCrumbView. |
| 147 | */ |
| 148 | private void updateVisible() { |
| 149 | if (MAX_CRUMBS_SHOWN > 0) { |
| 150 | int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN; |
| 151 | // This class always uses a back button, which is the first child. |
| 152 | int childIndex = 1; |
| 153 | if (invisibleCrumbs > 0) { |
| 154 | int crumbIndex = 0; |
| 155 | while (crumbIndex < invisibleCrumbs) { |
| 156 | // Set the crumb to GONE. |
| 157 | mCrumbs.getChildAt(childIndex).setVisibility(View.GONE); |
| 158 | childIndex++; |
| 159 | // Each crumb is followed by a separator (except the last |
| 160 | // one). Also make it GONE |
| 161 | mCrumbs.getChildAt(childIndex).setVisibility(View.GONE); |
| 162 | childIndex++; |
| 163 | // Move to the next crumb. |
| 164 | crumbIndex++; |
| 165 | } |
| 166 | } |
| 167 | // Make sure the last two are visible. |
| 168 | int childCount = mCrumbs.getChildCount(); |
| 169 | while (childIndex < childCount) { |
| 170 | mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE); |
| 171 | childIndex++; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Override |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 177 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 178 | if (v == mFolderNamer) { |
| 179 | if (v.getText().length() > 0) { |
| 180 | if (actionId == EditorInfo.IME_NULL) { |
| 181 | // Only want to do this once. |
| 182 | if (event.getAction() == KeyEvent.ACTION_UP) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 183 | completeOrCancelFolderNaming(false); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 184 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 185 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 186 | } |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 187 | // Steal the key press; otherwise a newline will be added |
| 188 | return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 189 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 190 | return false; |
| 191 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 192 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 193 | private void switchToDefaultView(boolean changedFolder) { |
| 194 | mFolderSelector.setVisibility(View.GONE); |
| 195 | mDefaultView.setVisibility(View.VISIBLE); |
| 196 | mCrumbHolder.setVisibility(View.GONE); |
| 197 | mFakeTitle.setVisibility(View.VISIBLE); |
| 198 | if (changedFolder) { |
| 199 | Object data = mCrumbs.getTopData(); |
| 200 | if (data != null) { |
| 201 | Folder folder = (Folder) data; |
| 202 | mCurrentFolder = folder.Id; |
| 203 | mFolder.setText(folder.Name); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 208 | @Override |
| 209 | public void onClick(View v) { |
| 210 | if (v == mButton) { |
| 211 | if (mFolderSelector.getVisibility() == View.VISIBLE) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 212 | // We are showing the folder selector. |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 213 | if (mFolderNamer.getVisibility() == View.VISIBLE) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 214 | completeOrCancelFolderNaming(false); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 215 | } else { |
| 216 | // User has selected a folder. Go back to the opening page |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 217 | mSaveToHomeScreen = false; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 218 | switchToDefaultView(true); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 219 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 220 | } else if (save()) { |
| 221 | finish(); |
| 222 | } |
| 223 | } else if (v == mCancelButton) { |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 224 | if (mFolderNamer.getVisibility() == View.VISIBLE) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 225 | completeOrCancelFolderNaming(true); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 226 | } else if (mFolderSelector.getVisibility() == View.VISIBLE) { |
| 227 | switchToDefaultView(false); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 228 | } else { |
| 229 | finish(); |
| 230 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 231 | } else if (v == mFolder) { |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 232 | // FIXME: We want to use mFolder as an anchor, but cannot until we |
| 233 | // fix the issue that the PopupMenu will not extend past the edge of |
| 234 | // the dialog. |
| 235 | PopupMenu popup = new PopupMenu(this, mFakeTitle); |
| 236 | popup.getMenuInflater().inflate(R.menu.folder_choice, |
| 237 | popup.getMenu()); |
| 238 | popup.setOnMenuItemClickListener(this); |
| 239 | popup.show(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 240 | } else if (v == mAddNewFolder) { |
| 241 | mFolderNamer.setVisibility(View.VISIBLE); |
| 242 | mFolderNamer.setText(R.string.new_folder); |
| 243 | mFolderNamer.requestFocus(); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 244 | updateList(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 245 | mAddNewFolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 246 | mAddSeparator.setVisibility(View.GONE); |
Bjorn Bringert | b1402a5 | 2010-10-12 10:53:12 +0100 | [diff] [blame] | 247 | getInputMethodManager().showSoftInput(mFolderNamer, |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 248 | InputMethodManager.SHOW_IMPLICIT); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 249 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 252 | @Override |
| 253 | public boolean onMenuItemClick(MenuItem item) { |
| 254 | switch(item.getItemId()) { |
| 255 | case R.id.bookmarks: |
| 256 | mCurrentFolder = getBookmarksBarId(this); |
| 257 | mFolder.setText(item.getTitle()); |
| 258 | mSaveToHomeScreen = false; |
| 259 | break; |
| 260 | case R.id.home_screen: |
| 261 | // Create a short cut to the home screen |
| 262 | mSaveToHomeScreen = true; |
| 263 | mFolder.setText(item.getTitle()); |
| 264 | break; |
| 265 | case R.id.other: |
| 266 | switchToFolderSelector(); |
| 267 | break; |
| 268 | default: |
| 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 274 | // Refresh the ListView to hide or show the empty view, as necessary. |
| 275 | // Should be called after mFolderNamer is shown or hidden. |
| 276 | private void updateList() { |
| 277 | if (mAdapter.getCount() == 0) { |
| 278 | // XXX: Is there a better way to refresh the ListView? |
| 279 | mListView.setAdapter(mAdapter); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private void completeOrCancelFolderNaming(boolean cancel) { |
| 284 | if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) { |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 285 | String name = mFolderNamer.getText().toString(); |
| 286 | long id = addFolderToCurrent(mFolderNamer.getText().toString()); |
| 287 | descendInto(name, id); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 288 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 289 | mFolderNamer.setVisibility(View.GONE); |
| 290 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 291 | mAddSeparator.setVisibility(View.VISIBLE); |
| 292 | getInputMethodManager().hideSoftInputFromWindow( |
| 293 | mFolderNamer.getWindowToken(), 0); |
| 294 | updateList(); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 297 | private long addFolderToCurrent(String name) { |
| 298 | // Add the folder to the database |
| 299 | ContentValues values = new ContentValues(); |
| 300 | values.put(BrowserContract.Bookmarks.TITLE, |
| 301 | name); |
| 302 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 303 | long currentFolder; |
| 304 | Object data = mCrumbs.getTopData(); |
| 305 | if (data != null) { |
| 306 | currentFolder = ((Folder) data).Id; |
| 307 | } else { |
| 308 | currentFolder = getBookmarksBarId(this); |
| 309 | } |
| 310 | values.put(BrowserContract.Bookmarks.PARENT, currentFolder); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 311 | Uri uri = getContentResolver().insert( |
| 312 | BrowserContract.Bookmarks.CONTENT_URI, values); |
| 313 | if (uri != null) { |
| 314 | return ContentUris.parseId(uri); |
| 315 | } else { |
| 316 | return -1; |
| 317 | } |
| 318 | } |
| 319 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 320 | private void switchToFolderSelector() { |
| 321 | mDefaultView.setVisibility(View.GONE); |
| 322 | mFolderSelector.setVisibility(View.VISIBLE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 323 | mCrumbHolder.setVisibility(View.VISIBLE); |
| 324 | mFakeTitle.setVisibility(View.GONE); |
| 325 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 326 | mAddSeparator.setVisibility(View.VISIBLE); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 327 | } |
| 328 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 329 | private void descendInto(String foldername, long id) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 330 | if (id != DEFAULT_FOLDER_ID) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 331 | mCrumbs.pushView(foldername, new Folder(foldername, id)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 332 | mCrumbs.notifyController(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 336 | @Override |
| 337 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 338 | String[] projection; |
| 339 | switch (id) { |
| 340 | case LOADER_ID_ALL_FOLDERS: |
| 341 | projection = new String[] { |
| 342 | BrowserContract.Bookmarks._ID, |
| 343 | BrowserContract.Bookmarks.PARENT, |
| 344 | BrowserContract.Bookmarks.TITLE, |
| 345 | BrowserContract.Bookmarks.IS_FOLDER |
| 346 | }; |
| 347 | return new CursorLoader(this, |
| 348 | BrowserContract.Bookmarks.CONTENT_URI, |
| 349 | projection, |
| 350 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 351 | null, |
| 352 | null); |
| 353 | case LOADER_ID_FOLDER_CONTENTS: |
| 354 | projection = new String[] { |
| 355 | BrowserContract.Bookmarks._ID, |
| 356 | BrowserContract.Bookmarks.TITLE, |
| 357 | BrowserContract.Bookmarks.IS_FOLDER |
| 358 | }; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 359 | return new CursorLoader(this, |
| 360 | BrowserContract.Bookmarks.buildFolderUri( |
| 361 | mCurrentFolder), |
| 362 | projection, |
| 363 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 364 | null, |
| 365 | null); |
| 366 | default: |
| 367 | throw new AssertionError("Asking for nonexistant loader!"); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 373 | switch (loader.getId()) { |
| 374 | case LOADER_ID_FOLDER_CONTENTS: |
| 375 | mAdapter.changeCursor(cursor); |
| 376 | break; |
| 377 | case LOADER_ID_ALL_FOLDERS: |
| 378 | long parent = mCurrentFolder; |
| 379 | int idIndex = cursor.getColumnIndexOrThrow( |
| 380 | BrowserContract.Bookmarks._ID); |
| 381 | int titleIndex = cursor.getColumnIndexOrThrow( |
| 382 | BrowserContract.Bookmarks.TITLE); |
| 383 | int parentIndex = cursor.getColumnIndexOrThrow( |
| 384 | BrowserContract.Bookmarks.PARENT); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 385 | Stack folderStack = new Stack(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 386 | while ((parent != BrowserProvider2.FIXED_ID_ROOT) && |
| 387 | (parent != 0)) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 388 | // First, find the folder corresponding to the current |
| 389 | // folder |
| 390 | if (!cursor.moveToFirst()) { |
| 391 | throw new AssertionError("No folders in the database!"); |
| 392 | } |
| 393 | long folder; |
| 394 | do { |
| 395 | folder = cursor.getLong(idIndex); |
| 396 | } while (folder != parent && cursor.moveToNext()); |
| 397 | if (cursor.isAfterLast()) { |
| 398 | throw new AssertionError("Folder(id=" + parent |
| 399 | + ") holding this bookmark does not exist!"); |
| 400 | } |
| 401 | String name = cursor.getString(titleIndex); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 402 | if (parent == mCurrentFolder) { |
| 403 | mFolder.setText(name); |
| 404 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 405 | folderStack.push(new Folder(name, parent)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 406 | parent = cursor.getLong(parentIndex); |
| 407 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 408 | while (!folderStack.isEmpty()) { |
| 409 | Folder thisFolder = (Folder) folderStack.pop(); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 410 | mCrumbs.pushView(thisFolder.Name, thisFolder); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 411 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 412 | getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 413 | updateVisible(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 420 | @Override |
| 421 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 422 | long id) { |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 423 | TextView tv = (TextView) view.findViewById(android.R.id.text1); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 424 | // Switch to the folder that was clicked on. |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 425 | descendInto(tv.getText().toString(), id); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Shows a list of names of folders. |
| 430 | */ |
| 431 | private class FolderAdapter extends CursorAdapter { |
| 432 | public FolderAdapter(Context context) { |
| 433 | super(context, null); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void bindView(View view, Context context, Cursor cursor) { |
| 438 | ((TextView) view.findViewById(android.R.id.text1)).setText( |
| 439 | cursor.getString(cursor.getColumnIndexOrThrow( |
| 440 | BrowserContract.Bookmarks.TITLE))); |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 445 | View view = LayoutInflater.from(context).inflate( |
| 446 | R.layout.folder_list_item, null); |
| 447 | view.setBackgroundDrawable(context.getResources(). |
| 448 | getDrawable(android.R.drawable.list_selector_background)); |
| 449 | return view; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 450 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 451 | |
| 452 | @Override |
| 453 | public boolean isEmpty() { |
| 454 | // Do not show the empty view if the user is creating a new folder. |
| 455 | return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE; |
| 456 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 457 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 458 | |
| 459 | protected void onCreate(Bundle icicle) { |
| 460 | super.onCreate(icicle); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 461 | if (DEBUG_CRASH) { |
| 462 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 463 | } else { |
| 464 | requestWindowFeature(Window.FEATURE_LEFT_ICON); |
| 465 | } |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 466 | |
| 467 | mMap = getIntent().getExtras(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 468 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 469 | setContentView(R.layout.browser_add_bookmark); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 470 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 471 | Window window = getWindow(); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 472 | if (!DEBUG_CRASH) { |
| 473 | setTitle(R.string.bookmark_this_page); |
| 474 | window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark); |
| 475 | } |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 476 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 477 | String title = null; |
| 478 | String url = null; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 479 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 480 | mFakeTitle = (TextView) findViewById(R.id.fake_title); |
| 481 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 482 | if (mMap != null) { |
| 483 | Bundle b = mMap.getBundle("bookmark"); |
| 484 | if (b != null) { |
| 485 | mMap = b; |
| 486 | mEditingExisting = true; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 487 | mFakeTitle.setText(R.string.edit_bookmark); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 488 | if (!DEBUG_CRASH) { |
| 489 | setTitle(R.string.bookmark_this_page); |
| 490 | } |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 491 | } else { |
| 492 | int gravity = mMap.getInt("gravity", -1); |
| 493 | if (gravity != -1) { |
| 494 | WindowManager.LayoutParams l = window.getAttributes(); |
| 495 | l.gravity = gravity; |
| 496 | window.setAttributes(l); |
| 497 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 498 | } |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 499 | title = mMap.getString(BrowserContract.Bookmarks.TITLE); |
| 500 | url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL); |
| 501 | mTouchIconUrl = mMap.getString(TOUCH_ICON_URL); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 502 | mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID); |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 503 | } |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 504 | if (mCurrentFolder == DEFAULT_FOLDER_ID) { |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 505 | mCurrentFolder = getBookmarksBarId(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | mTitle = (EditText) findViewById(R.id.title); |
| 509 | mTitle.setText(title); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 510 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 511 | mAddress = (EditText) findViewById(R.id.address); |
| 512 | mAddress.setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 513 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 514 | mButton = (TextView) findViewById(R.id.OK); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 515 | mButton.setOnClickListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 516 | |
| 517 | mCancelButton = findViewById(R.id.cancel); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 518 | mCancelButton.setOnClickListener(this); |
| 519 | |
| 520 | mFolder = (TextView) findViewById(R.id.folder); |
| 521 | mFolder.setOnClickListener(this); |
| 522 | |
| 523 | mDefaultView = findViewById(R.id.default_view); |
| 524 | mFolderSelector = findViewById(R.id.folder_selector); |
| 525 | |
| 526 | mFolderNamer = (EditText) findViewById(R.id.folder_namer); |
| 527 | mFolderNamer.setOnEditorActionListener(this); |
| 528 | |
| 529 | mAddNewFolder = findViewById(R.id.add_new_folder); |
| 530 | mAddNewFolder.setOnClickListener(this); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 531 | mAddSeparator = findViewById(R.id.add_divider); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 532 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 533 | mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs); |
| 534 | mCrumbs.setUseBackButton(true); |
| 535 | mCrumbs.setController(this); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 536 | String name = getString(R.string.bookmarks); |
| 537 | mCrumbs.pushView(name, false, |
| 538 | new Folder(name, BrowserProvider2.FIXED_ID_ROOT)); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 539 | mCrumbHolder = findViewById(R.id.crumb_holder); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 540 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 541 | mAdapter = new FolderAdapter(this); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 542 | mListView = (ListView) findViewById(R.id.list); |
| 543 | View empty = findViewById(R.id.empty); |
| 544 | mListView.setEmptyView(empty); |
| 545 | mListView.setAdapter(mAdapter); |
| 546 | mListView.setOnItemClickListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 547 | LoaderManager manager = getLoaderManager(); |
Jeff Hamilton | a9bad83 | 2010-09-24 11:05:30 -0500 | [diff] [blame] | 548 | if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 549 | // Find all the folders |
| 550 | manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this); |
| 551 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 552 | // Find the contents of the current folder |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 553 | manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
| 554 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 555 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 556 | if (!window.getDecorView().isInTouchMode()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 557 | mButton.requestFocus(); |
| 558 | } |
| 559 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 560 | |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 561 | // FIXME: Use a CursorLoader |
| 562 | private long getBookmarksBarId(Context context) { |
| 563 | SharedPreferences prefs |
| 564 | = PreferenceManager.getDefaultSharedPreferences(context); |
| 565 | String accountName = |
| 566 | prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); |
| 567 | String accountType = |
| 568 | prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); |
| 569 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) { |
Jeff Hamilton | a9bad83 | 2010-09-24 11:05:30 -0500 | [diff] [blame] | 570 | return BrowserProvider2.FIXED_ID_ROOT; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 571 | } |
| 572 | Cursor cursor = null; |
| 573 | try { |
| 574 | cursor = context.getContentResolver().query( |
| 575 | BrowserContract.Bookmarks.CONTENT_URI, |
| 576 | new String[] { BrowserContract.Bookmarks._ID }, |
| 577 | BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND " |
| 578 | + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND " |
| 579 | + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?", |
| 580 | new String[] { |
| 581 | BrowserContract.ChromeSyncColumns |
| 582 | .FOLDER_NAME_BOOKMARKS_BAR, |
| 583 | accountName, |
| 584 | accountType }, |
| 585 | null); |
| 586 | if (cursor != null && cursor.moveToFirst()) { |
| 587 | return cursor.getLong(0); |
| 588 | } |
| 589 | } finally { |
| 590 | if (cursor != null) cursor.close(); |
| 591 | } |
Jeff Hamilton | a9bad83 | 2010-09-24 11:05:30 -0500 | [diff] [blame] | 592 | return BrowserProvider2.FIXED_ID_ROOT; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 595 | /** |
| 596 | * Runnable to save a bookmark, so it can be performed in its own thread. |
| 597 | */ |
| 598 | private class SaveBookmarkRunnable implements Runnable { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 599 | // FIXME: This should be an async task. |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 600 | private Message mMessage; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 601 | private Context mContext; |
| 602 | public SaveBookmarkRunnable(Context ctx, Message msg) { |
| 603 | mContext = ctx; |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 604 | mMessage = msg; |
| 605 | } |
| 606 | public void run() { |
| 607 | // Unbundle bookmark data. |
| 608 | Bundle bundle = mMessage.getData(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 609 | String title = bundle.getString(BrowserContract.Bookmarks.TITLE); |
| 610 | String url = bundle.getString(BrowserContract.Bookmarks.URL); |
| 611 | boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 612 | Bitmap thumbnail = invalidateThumbnail ? null |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 613 | : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL); |
| 614 | String touchIconUrl = bundle.getString(TOUCH_ICON_URL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 615 | |
| 616 | // Save to the bookmarks DB. |
| 617 | try { |
| 618 | final ContentResolver cr = getContentResolver(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 619 | Bookmarks.addBookmark(AddBookmarkPage.this, false, url, |
| 620 | title, thumbnail, true, mCurrentFolder); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 621 | if (touchIconUrl != null) { |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 622 | new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 623 | } |
| 624 | mMessage.arg1 = 1; |
| 625 | } catch (IllegalStateException e) { |
| 626 | mMessage.arg1 = 0; |
| 627 | } |
| 628 | mMessage.sendToTarget(); |
| 629 | } |
| 630 | } |
| 631 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 632 | private void createHandler() { |
| 633 | if (mHandler == null) { |
| 634 | mHandler = new Handler() { |
| 635 | @Override |
| 636 | public void handleMessage(Message msg) { |
| 637 | switch (msg.what) { |
| 638 | case SAVE_BOOKMARK: |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 639 | if (1 == msg.arg1) { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 640 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 641 | Toast.LENGTH_LONG).show(); |
| 642 | } else { |
| 643 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 644 | Toast.LENGTH_LONG).show(); |
| 645 | } |
| 646 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 647 | case TOUCH_ICON_DOWNLOADED: |
| 648 | Bundle b = msg.getData(); |
| 649 | sendBroadcast(BookmarkUtils.createAddToHomeIntent( |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 650 | AddBookmarkPage.this, |
| 651 | b.getString(BrowserContract.Bookmarks.URL), |
| 652 | b.getString(BrowserContract.Bookmarks.TITLE), |
| 653 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON), |
| 654 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON))); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 655 | break; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | }; |
| 659 | } |
| 660 | } |
| 661 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 662 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 663 | * Parse the data entered in the dialog and post a message to update the bookmarks database. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 664 | */ |
| 665 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 666 | createHandler(); |
| 667 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 668 | String title = mTitle.getText().toString().trim(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 669 | String unfilteredUrl; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 670 | unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString()); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 671 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 672 | boolean emptyTitle = title.length() == 0; |
| 673 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 674 | Resources r = getResources(); |
| 675 | if (emptyTitle || emptyUrl) { |
| 676 | if (emptyTitle) { |
| 677 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 678 | } |
| 679 | if (emptyUrl) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 680 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 681 | } |
Leon Scroggins III | 6e3faea | 2010-09-07 15:22:14 -0400 | [diff] [blame] | 682 | return false; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 683 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 684 | } |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 685 | String url = unfilteredUrl.trim(); |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 686 | try { |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 687 | // We allow bookmarks with a javascript: scheme, but these will in most cases |
| 688 | // fail URI parsing, so don't try it if that's the kind of bookmark we have. |
| 689 | |
| 690 | if (!url.toLowerCase().startsWith("javascript:")) { |
| 691 | URI uriObj = new URI(url); |
| 692 | String scheme = uriObj.getScheme(); |
| 693 | if (!Bookmarks.urlHasAcceptableScheme(url)) { |
| 694 | // If the scheme was non-null, let the user know that we |
| 695 | // can't save their bookmark. If it was null, we'll assume |
| 696 | // they meant http when we parse it in the WebAddress class. |
| 697 | if (scheme != null) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 698 | mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 699 | return false; |
| 700 | } |
| 701 | WebAddress address; |
| 702 | try { |
| 703 | address = new WebAddress(unfilteredUrl); |
| 704 | } catch (ParseException e) { |
| 705 | throw new URISyntaxException("", ""); |
| 706 | } |
Bjorn Bringert | 131ab51 | 2010-10-12 16:25:47 +0100 | [diff] [blame] | 707 | if (address.getHost().length() == 0) { |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 708 | throw new URISyntaxException("", ""); |
| 709 | } |
| 710 | url = address.toString(); |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 711 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 712 | } |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 713 | } catch (URISyntaxException e) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 714 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 715 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 716 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 717 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 718 | if (mSaveToHomeScreen) { |
| 719 | mEditingExisting = false; |
| 720 | } |
| 721 | |
| 722 | boolean urlUnmodified = url.equals(mOriginalUrl); |
| 723 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 724 | if (mEditingExisting) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 725 | mMap.putString(BrowserContract.Bookmarks.TITLE, title); |
| 726 | mMap.putString(BrowserContract.Bookmarks.URL, url); |
| 727 | mMap.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 728 | // FIXME: This does not work yet |
| 729 | mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 730 | setResult(RESULT_OK, (new Intent()).setAction( |
| 731 | getIntent().toString()).putExtras(mMap)); |
| 732 | } else { |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 733 | Bitmap thumbnail; |
| 734 | Bitmap favicon; |
| 735 | if (urlUnmodified) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 736 | thumbnail = (Bitmap) mMap.getParcelable( |
| 737 | BrowserContract.Bookmarks.THUMBNAIL); |
| 738 | favicon = (Bitmap) mMap.getParcelable( |
| 739 | BrowserContract.Bookmarks.FAVICON); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 740 | } else { |
| 741 | thumbnail = null; |
| 742 | favicon = null; |
| 743 | } |
| 744 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 745 | Bundle bundle = new Bundle(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 746 | bundle.putString(BrowserContract.Bookmarks.TITLE, title); |
| 747 | bundle.putString(BrowserContract.Bookmarks.URL, url); |
| 748 | bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 749 | |
| 750 | if (mSaveToHomeScreen) { |
| 751 | if (mTouchIconUrl != null && urlUnmodified) { |
| 752 | Message msg = Message.obtain(mHandler, |
| 753 | TOUCH_ICON_DOWNLOADED); |
| 754 | msg.setData(bundle); |
| 755 | DownloadTouchIcon icon = new DownloadTouchIcon(this, msg, |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 756 | mMap.getString(USER_AGENT)); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 757 | icon.execute(mTouchIconUrl); |
| 758 | } else { |
| 759 | sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url, |
| 760 | title, null /*touchIcon*/, favicon)); |
| 761 | } |
| 762 | } else { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame^] | 763 | bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail); |
| 764 | bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
| 765 | bundle.putString(TOUCH_ICON_URL, mTouchIconUrl); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 766 | // Post a message to write to the DB. |
| 767 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 768 | msg.setData(bundle); |
| 769 | // Start a new thread so as to not slow down the UI |
| 770 | Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg)); |
| 771 | t.start(); |
| 772 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 773 | setResult(RESULT_OK); |
Kristian Monsen | 0a1d838 | 2010-02-01 18:41:07 +0000 | [diff] [blame] | 774 | LogTag.logBookmarkAdded(url, "bookmarkview"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 775 | } |
| 776 | return true; |
| 777 | } |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 778 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 779 | } |