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 | 26318a1 | 2010-11-08 10:08:40 -0500 | [diff] [blame] | 232 | PopupMenu popup = new PopupMenu(this, mFolder); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 233 | popup.getMenuInflater().inflate(R.menu.folder_choice, |
| 234 | popup.getMenu()); |
| 235 | popup.setOnMenuItemClickListener(this); |
| 236 | popup.show(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 237 | } else if (v == mAddNewFolder) { |
| 238 | mFolderNamer.setVisibility(View.VISIBLE); |
| 239 | mFolderNamer.setText(R.string.new_folder); |
| 240 | mFolderNamer.requestFocus(); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 241 | updateList(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 242 | mAddNewFolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 243 | mAddSeparator.setVisibility(View.GONE); |
Bjorn Bringert | b1402a5 | 2010-10-12 10:53:12 +0100 | [diff] [blame] | 244 | getInputMethodManager().showSoftInput(mFolderNamer, |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 245 | InputMethodManager.SHOW_IMPLICIT); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 246 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 249 | @Override |
| 250 | public boolean onMenuItemClick(MenuItem item) { |
| 251 | switch(item.getItemId()) { |
| 252 | case R.id.bookmarks: |
| 253 | mCurrentFolder = getBookmarksBarId(this); |
| 254 | mFolder.setText(item.getTitle()); |
| 255 | mSaveToHomeScreen = false; |
| 256 | break; |
| 257 | case R.id.home_screen: |
| 258 | // Create a short cut to the home screen |
| 259 | mSaveToHomeScreen = true; |
| 260 | mFolder.setText(item.getTitle()); |
| 261 | break; |
| 262 | case R.id.other: |
| 263 | switchToFolderSelector(); |
| 264 | break; |
| 265 | default: |
| 266 | return false; |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 271 | // Refresh the ListView to hide or show the empty view, as necessary. |
| 272 | // Should be called after mFolderNamer is shown or hidden. |
| 273 | private void updateList() { |
| 274 | if (mAdapter.getCount() == 0) { |
| 275 | // XXX: Is there a better way to refresh the ListView? |
| 276 | mListView.setAdapter(mAdapter); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private void completeOrCancelFolderNaming(boolean cancel) { |
| 281 | if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) { |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 282 | String name = mFolderNamer.getText().toString(); |
| 283 | long id = addFolderToCurrent(mFolderNamer.getText().toString()); |
| 284 | descendInto(name, id); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 285 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 286 | mFolderNamer.setVisibility(View.GONE); |
| 287 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 288 | mAddSeparator.setVisibility(View.VISIBLE); |
| 289 | getInputMethodManager().hideSoftInputFromWindow( |
| 290 | mFolderNamer.getWindowToken(), 0); |
| 291 | updateList(); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 294 | private long addFolderToCurrent(String name) { |
| 295 | // Add the folder to the database |
| 296 | ContentValues values = new ContentValues(); |
| 297 | values.put(BrowserContract.Bookmarks.TITLE, |
| 298 | name); |
| 299 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); |
John Reck | e89daa9 | 2010-11-05 14:39:39 -0700 | [diff] [blame] | 300 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
| 301 | String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); |
| 302 | String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); |
| 303 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 304 | values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType); |
| 305 | values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName); |
| 306 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 307 | long currentFolder; |
| 308 | Object data = mCrumbs.getTopData(); |
| 309 | if (data != null) { |
| 310 | currentFolder = ((Folder) data).Id; |
| 311 | } else { |
| 312 | currentFolder = getBookmarksBarId(this); |
| 313 | } |
| 314 | values.put(BrowserContract.Bookmarks.PARENT, currentFolder); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 315 | Uri uri = getContentResolver().insert( |
| 316 | BrowserContract.Bookmarks.CONTENT_URI, values); |
| 317 | if (uri != null) { |
| 318 | return ContentUris.parseId(uri); |
| 319 | } else { |
| 320 | return -1; |
| 321 | } |
| 322 | } |
| 323 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 324 | private void switchToFolderSelector() { |
| 325 | mDefaultView.setVisibility(View.GONE); |
| 326 | mFolderSelector.setVisibility(View.VISIBLE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 327 | mCrumbHolder.setVisibility(View.VISIBLE); |
| 328 | mFakeTitle.setVisibility(View.GONE); |
| 329 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 330 | mAddSeparator.setVisibility(View.VISIBLE); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 333 | private void descendInto(String foldername, long id) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 334 | if (id != DEFAULT_FOLDER_ID) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 335 | mCrumbs.pushView(foldername, new Folder(foldername, id)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 336 | mCrumbs.notifyController(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 340 | @Override |
| 341 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 342 | String[] projection; |
| 343 | switch (id) { |
| 344 | case LOADER_ID_ALL_FOLDERS: |
| 345 | projection = new String[] { |
| 346 | BrowserContract.Bookmarks._ID, |
| 347 | BrowserContract.Bookmarks.PARENT, |
| 348 | BrowserContract.Bookmarks.TITLE, |
| 349 | BrowserContract.Bookmarks.IS_FOLDER |
| 350 | }; |
| 351 | return new CursorLoader(this, |
| 352 | BrowserContract.Bookmarks.CONTENT_URI, |
| 353 | projection, |
| 354 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 355 | null, |
| 356 | null); |
| 357 | case LOADER_ID_FOLDER_CONTENTS: |
| 358 | projection = new String[] { |
| 359 | BrowserContract.Bookmarks._ID, |
| 360 | BrowserContract.Bookmarks.TITLE, |
| 361 | BrowserContract.Bookmarks.IS_FOLDER |
| 362 | }; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 363 | return new CursorLoader(this, |
| 364 | BrowserContract.Bookmarks.buildFolderUri( |
| 365 | mCurrentFolder), |
| 366 | projection, |
| 367 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 368 | null, |
| 369 | null); |
| 370 | default: |
| 371 | throw new AssertionError("Asking for nonexistant loader!"); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 377 | switch (loader.getId()) { |
| 378 | case LOADER_ID_FOLDER_CONTENTS: |
| 379 | mAdapter.changeCursor(cursor); |
| 380 | break; |
| 381 | case LOADER_ID_ALL_FOLDERS: |
| 382 | long parent = mCurrentFolder; |
| 383 | int idIndex = cursor.getColumnIndexOrThrow( |
| 384 | BrowserContract.Bookmarks._ID); |
| 385 | int titleIndex = cursor.getColumnIndexOrThrow( |
| 386 | BrowserContract.Bookmarks.TITLE); |
| 387 | int parentIndex = cursor.getColumnIndexOrThrow( |
| 388 | BrowserContract.Bookmarks.PARENT); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 389 | Stack folderStack = new Stack(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 390 | while ((parent != BrowserProvider2.FIXED_ID_ROOT) && |
| 391 | (parent != 0)) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 392 | // First, find the folder corresponding to the current |
| 393 | // folder |
| 394 | if (!cursor.moveToFirst()) { |
| 395 | throw new AssertionError("No folders in the database!"); |
| 396 | } |
| 397 | long folder; |
| 398 | do { |
| 399 | folder = cursor.getLong(idIndex); |
| 400 | } while (folder != parent && cursor.moveToNext()); |
| 401 | if (cursor.isAfterLast()) { |
| 402 | throw new AssertionError("Folder(id=" + parent |
| 403 | + ") holding this bookmark does not exist!"); |
| 404 | } |
| 405 | String name = cursor.getString(titleIndex); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 406 | if (parent == mCurrentFolder) { |
| 407 | mFolder.setText(name); |
| 408 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 409 | folderStack.push(new Folder(name, parent)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 410 | parent = cursor.getLong(parentIndex); |
| 411 | } |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 412 | while (!folderStack.isEmpty()) { |
| 413 | Folder thisFolder = (Folder) folderStack.pop(); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 414 | mCrumbs.pushView(thisFolder.Name, thisFolder); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 415 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 416 | getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 417 | updateVisible(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 418 | break; |
| 419 | default: |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 424 | @Override |
| 425 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 426 | long id) { |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 427 | TextView tv = (TextView) view.findViewById(android.R.id.text1); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 428 | // Switch to the folder that was clicked on. |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 429 | descendInto(tv.getText().toString(), id); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Shows a list of names of folders. |
| 434 | */ |
| 435 | private class FolderAdapter extends CursorAdapter { |
| 436 | public FolderAdapter(Context context) { |
| 437 | super(context, null); |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void bindView(View view, Context context, Cursor cursor) { |
| 442 | ((TextView) view.findViewById(android.R.id.text1)).setText( |
| 443 | cursor.getString(cursor.getColumnIndexOrThrow( |
| 444 | BrowserContract.Bookmarks.TITLE))); |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 449 | View view = LayoutInflater.from(context).inflate( |
| 450 | R.layout.folder_list_item, null); |
| 451 | view.setBackgroundDrawable(context.getResources(). |
| 452 | getDrawable(android.R.drawable.list_selector_background)); |
| 453 | return view; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 454 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 455 | |
| 456 | @Override |
| 457 | public boolean isEmpty() { |
| 458 | // Do not show the empty view if the user is creating a new folder. |
| 459 | return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE; |
| 460 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 461 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 462 | |
| 463 | protected void onCreate(Bundle icicle) { |
| 464 | super.onCreate(icicle); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 465 | if (DEBUG_CRASH) { |
| 466 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 467 | } else { |
| 468 | requestWindowFeature(Window.FEATURE_LEFT_ICON); |
| 469 | } |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 470 | |
| 471 | mMap = getIntent().getExtras(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 472 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 473 | setContentView(R.layout.browser_add_bookmark); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 474 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 475 | Window window = getWindow(); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 476 | if (!DEBUG_CRASH) { |
| 477 | setTitle(R.string.bookmark_this_page); |
| 478 | window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark); |
| 479 | } |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 480 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 481 | String title = null; |
| 482 | String url = null; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 483 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 484 | mFakeTitle = (TextView) findViewById(R.id.fake_title); |
| 485 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 486 | if (mMap != null) { |
| 487 | Bundle b = mMap.getBundle("bookmark"); |
| 488 | if (b != null) { |
| 489 | mMap = b; |
| 490 | mEditingExisting = true; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 491 | mFakeTitle.setText(R.string.edit_bookmark); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 492 | if (!DEBUG_CRASH) { |
| 493 | setTitle(R.string.bookmark_this_page); |
| 494 | } |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 495 | } else { |
| 496 | int gravity = mMap.getInt("gravity", -1); |
| 497 | if (gravity != -1) { |
| 498 | WindowManager.LayoutParams l = window.getAttributes(); |
| 499 | l.gravity = gravity; |
| 500 | window.setAttributes(l); |
| 501 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 502 | } |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 503 | title = mMap.getString(BrowserContract.Bookmarks.TITLE); |
| 504 | url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL); |
| 505 | mTouchIconUrl = mMap.getString(TOUCH_ICON_URL); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 506 | mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID); |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 507 | } |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 508 | if (mCurrentFolder == DEFAULT_FOLDER_ID) { |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 509 | mCurrentFolder = getBookmarksBarId(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | mTitle = (EditText) findViewById(R.id.title); |
| 513 | mTitle.setText(title); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 514 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 515 | mAddress = (EditText) findViewById(R.id.address); |
| 516 | mAddress.setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 517 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 518 | mButton = (TextView) findViewById(R.id.OK); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 519 | mButton.setOnClickListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 520 | |
| 521 | mCancelButton = findViewById(R.id.cancel); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 522 | mCancelButton.setOnClickListener(this); |
| 523 | |
| 524 | mFolder = (TextView) findViewById(R.id.folder); |
| 525 | mFolder.setOnClickListener(this); |
| 526 | |
| 527 | mDefaultView = findViewById(R.id.default_view); |
| 528 | mFolderSelector = findViewById(R.id.folder_selector); |
| 529 | |
| 530 | mFolderNamer = (EditText) findViewById(R.id.folder_namer); |
| 531 | mFolderNamer.setOnEditorActionListener(this); |
| 532 | |
| 533 | mAddNewFolder = findViewById(R.id.add_new_folder); |
| 534 | mAddNewFolder.setOnClickListener(this); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 535 | mAddSeparator = findViewById(R.id.add_divider); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 536 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 537 | mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs); |
| 538 | mCrumbs.setUseBackButton(true); |
| 539 | mCrumbs.setController(this); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 540 | String name = getString(R.string.bookmarks); |
| 541 | mCrumbs.pushView(name, false, |
| 542 | new Folder(name, BrowserProvider2.FIXED_ID_ROOT)); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 543 | mCrumbHolder = findViewById(R.id.crumb_holder); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 544 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 545 | mAdapter = new FolderAdapter(this); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 546 | mListView = (ListView) findViewById(R.id.list); |
| 547 | View empty = findViewById(R.id.empty); |
| 548 | mListView.setEmptyView(empty); |
| 549 | mListView.setAdapter(mAdapter); |
| 550 | mListView.setOnItemClickListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 551 | LoaderManager manager = getLoaderManager(); |
Jeff Hamilton | a9bad83 | 2010-09-24 11:05:30 -0500 | [diff] [blame] | 552 | if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 553 | // Find all the folders |
| 554 | manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this); |
| 555 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 556 | // Find the contents of the current folder |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 557 | manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
| 558 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 559 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 560 | if (!window.getDecorView().isInTouchMode()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 561 | mButton.requestFocus(); |
| 562 | } |
| 563 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 564 | |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 565 | // FIXME: Use a CursorLoader |
| 566 | private long getBookmarksBarId(Context context) { |
| 567 | SharedPreferences prefs |
| 568 | = PreferenceManager.getDefaultSharedPreferences(context); |
| 569 | String accountName = |
| 570 | prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); |
| 571 | String accountType = |
| 572 | prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); |
| 573 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) { |
Jeff Hamilton | a9bad83 | 2010-09-24 11:05:30 -0500 | [diff] [blame] | 574 | return BrowserProvider2.FIXED_ID_ROOT; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 575 | } |
| 576 | Cursor cursor = null; |
| 577 | try { |
| 578 | cursor = context.getContentResolver().query( |
| 579 | BrowserContract.Bookmarks.CONTENT_URI, |
| 580 | new String[] { BrowserContract.Bookmarks._ID }, |
| 581 | BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND " |
| 582 | + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND " |
| 583 | + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?", |
| 584 | new String[] { |
| 585 | BrowserContract.ChromeSyncColumns |
| 586 | .FOLDER_NAME_BOOKMARKS_BAR, |
| 587 | accountName, |
| 588 | accountType }, |
| 589 | null); |
| 590 | if (cursor != null && cursor.moveToFirst()) { |
| 591 | return cursor.getLong(0); |
| 592 | } |
| 593 | } finally { |
| 594 | if (cursor != null) cursor.close(); |
| 595 | } |
Jeff Hamilton | a9bad83 | 2010-09-24 11:05:30 -0500 | [diff] [blame] | 596 | return BrowserProvider2.FIXED_ID_ROOT; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 597 | } |
| 598 | |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 599 | /** |
| 600 | * Runnable to save a bookmark, so it can be performed in its own thread. |
| 601 | */ |
| 602 | private class SaveBookmarkRunnable implements Runnable { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 603 | // FIXME: This should be an async task. |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 604 | private Message mMessage; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 605 | private Context mContext; |
| 606 | public SaveBookmarkRunnable(Context ctx, Message msg) { |
| 607 | mContext = ctx; |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 608 | mMessage = msg; |
| 609 | } |
| 610 | public void run() { |
| 611 | // Unbundle bookmark data. |
| 612 | Bundle bundle = mMessage.getData(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 613 | String title = bundle.getString(BrowserContract.Bookmarks.TITLE); |
| 614 | String url = bundle.getString(BrowserContract.Bookmarks.URL); |
| 615 | boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 616 | Bitmap thumbnail = invalidateThumbnail ? null |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 617 | : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL); |
| 618 | String touchIconUrl = bundle.getString(TOUCH_ICON_URL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 619 | |
| 620 | // Save to the bookmarks DB. |
| 621 | try { |
| 622 | final ContentResolver cr = getContentResolver(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 623 | Bookmarks.addBookmark(AddBookmarkPage.this, false, url, |
| 624 | title, thumbnail, true, mCurrentFolder); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 625 | if (touchIconUrl != null) { |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 626 | new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 627 | } |
| 628 | mMessage.arg1 = 1; |
| 629 | } catch (IllegalStateException e) { |
| 630 | mMessage.arg1 = 0; |
| 631 | } |
| 632 | mMessage.sendToTarget(); |
| 633 | } |
| 634 | } |
| 635 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 636 | private void createHandler() { |
| 637 | if (mHandler == null) { |
| 638 | mHandler = new Handler() { |
| 639 | @Override |
| 640 | public void handleMessage(Message msg) { |
| 641 | switch (msg.what) { |
| 642 | case SAVE_BOOKMARK: |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 643 | if (1 == msg.arg1) { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 644 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 645 | Toast.LENGTH_LONG).show(); |
| 646 | } else { |
| 647 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 648 | Toast.LENGTH_LONG).show(); |
| 649 | } |
| 650 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 651 | case TOUCH_ICON_DOWNLOADED: |
| 652 | Bundle b = msg.getData(); |
| 653 | sendBroadcast(BookmarkUtils.createAddToHomeIntent( |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 654 | AddBookmarkPage.this, |
| 655 | b.getString(BrowserContract.Bookmarks.URL), |
| 656 | b.getString(BrowserContract.Bookmarks.TITLE), |
| 657 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON), |
| 658 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON))); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 659 | break; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | }; |
| 663 | } |
| 664 | } |
| 665 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 666 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 667 | * 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] | 668 | */ |
| 669 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 670 | createHandler(); |
| 671 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 672 | String title = mTitle.getText().toString().trim(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 673 | String unfilteredUrl; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 674 | unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString()); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 675 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 676 | boolean emptyTitle = title.length() == 0; |
| 677 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 678 | Resources r = getResources(); |
| 679 | if (emptyTitle || emptyUrl) { |
| 680 | if (emptyTitle) { |
| 681 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 682 | } |
| 683 | if (emptyUrl) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 684 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 685 | } |
Leon Scroggins III | 6e3faea | 2010-09-07 15:22:14 -0400 | [diff] [blame] | 686 | return false; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 687 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 688 | } |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 689 | String url = unfilteredUrl.trim(); |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 690 | try { |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 691 | // We allow bookmarks with a javascript: scheme, but these will in most cases |
| 692 | // fail URI parsing, so don't try it if that's the kind of bookmark we have. |
| 693 | |
| 694 | if (!url.toLowerCase().startsWith("javascript:")) { |
| 695 | URI uriObj = new URI(url); |
| 696 | String scheme = uriObj.getScheme(); |
| 697 | if (!Bookmarks.urlHasAcceptableScheme(url)) { |
| 698 | // If the scheme was non-null, let the user know that we |
| 699 | // can't save their bookmark. If it was null, we'll assume |
| 700 | // they meant http when we parse it in the WebAddress class. |
| 701 | if (scheme != null) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 702 | mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 703 | return false; |
| 704 | } |
| 705 | WebAddress address; |
| 706 | try { |
| 707 | address = new WebAddress(unfilteredUrl); |
| 708 | } catch (ParseException e) { |
| 709 | throw new URISyntaxException("", ""); |
| 710 | } |
Bjorn Bringert | 131ab51 | 2010-10-12 16:25:47 +0100 | [diff] [blame] | 711 | if (address.getHost().length() == 0) { |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 712 | throw new URISyntaxException("", ""); |
| 713 | } |
| 714 | url = address.toString(); |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 715 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 716 | } |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 717 | } catch (URISyntaxException e) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 718 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 719 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 720 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 721 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 722 | if (mSaveToHomeScreen) { |
| 723 | mEditingExisting = false; |
| 724 | } |
| 725 | |
| 726 | boolean urlUnmodified = url.equals(mOriginalUrl); |
| 727 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 728 | if (mEditingExisting) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 729 | mMap.putString(BrowserContract.Bookmarks.TITLE, title); |
| 730 | mMap.putString(BrowserContract.Bookmarks.URL, url); |
| 731 | mMap.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 732 | // FIXME: This does not work yet |
| 733 | mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 734 | setResult(RESULT_OK, (new Intent()).setAction( |
| 735 | getIntent().toString()).putExtras(mMap)); |
| 736 | } else { |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 737 | Bitmap thumbnail; |
| 738 | Bitmap favicon; |
| 739 | if (urlUnmodified) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 740 | thumbnail = (Bitmap) mMap.getParcelable( |
| 741 | BrowserContract.Bookmarks.THUMBNAIL); |
| 742 | favicon = (Bitmap) mMap.getParcelable( |
| 743 | BrowserContract.Bookmarks.FAVICON); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 744 | } else { |
| 745 | thumbnail = null; |
| 746 | favicon = null; |
| 747 | } |
| 748 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 749 | Bundle bundle = new Bundle(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 750 | bundle.putString(BrowserContract.Bookmarks.TITLE, title); |
| 751 | bundle.putString(BrowserContract.Bookmarks.URL, url); |
| 752 | bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 753 | |
| 754 | if (mSaveToHomeScreen) { |
| 755 | if (mTouchIconUrl != null && urlUnmodified) { |
| 756 | Message msg = Message.obtain(mHandler, |
| 757 | TOUCH_ICON_DOWNLOADED); |
| 758 | msg.setData(bundle); |
| 759 | DownloadTouchIcon icon = new DownloadTouchIcon(this, msg, |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 760 | mMap.getString(USER_AGENT)); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 761 | icon.execute(mTouchIconUrl); |
| 762 | } else { |
| 763 | sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url, |
| 764 | title, null /*touchIcon*/, favicon)); |
| 765 | } |
| 766 | } else { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 767 | bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail); |
| 768 | bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
| 769 | bundle.putString(TOUCH_ICON_URL, mTouchIconUrl); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 770 | // Post a message to write to the DB. |
| 771 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 772 | msg.setData(bundle); |
| 773 | // Start a new thread so as to not slow down the UI |
| 774 | Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg)); |
| 775 | t.start(); |
| 776 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 777 | setResult(RESULT_OK); |
Kristian Monsen | 0a1d838 | 2010-02-01 18:41:07 +0000 | [diff] [blame] | 778 | LogTag.logBookmarkAdded(url, "bookmarkview"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 779 | } |
| 780 | return true; |
| 781 | } |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 782 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 783 | } |