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 | |
| 19 | import android.app.Activity; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 20 | import android.app.LoaderManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.content.CursorLoader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 26 | import android.content.Loader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 27 | import android.content.res.Resources; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 28 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.net.ParseException; |
| 31 | import android.net.WebAddress; |
| 32 | import android.os.Bundle; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.Message; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 35 | import android.provider.BrowserContract; |
| 36 | import android.view.KeyEvent; |
| 37 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.view.View; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 39 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | import android.view.Window; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 41 | import android.view.inputmethod.EditorInfo; |
| 42 | import android.view.inputmethod.InputMethodManager; |
| 43 | import android.widget.AdapterView; |
| 44 | import android.widget.CursorAdapter; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | import android.widget.EditText; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 46 | import android.widget.ListView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | import android.widget.TextView; |
| 48 | import android.widget.Toast; |
| 49 | |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 50 | import java.net.URI; |
| 51 | import java.net.URISyntaxException; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 52 | import java.util.ArrayList; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 53 | import java.util.Date; |
| 54 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 55 | import android.util.Log; |
| 56 | |
| 57 | public class AddBookmarkPage extends Activity |
| 58 | implements View.OnClickListener, TextView.OnEditorActionListener, |
| 59 | AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 60 | |
| 61 | private final String LOGTAG = "Bookmarks"; |
| 62 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 63 | // IDs for the CursorLoaders that are used. |
| 64 | private final int LOADER_ID_FOLDER_CONTENTS = 0; |
| 65 | private final int LOADER_ID_ALL_FOLDERS = 1; |
| 66 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 67 | private EditText mTitle; |
| 68 | private EditText mAddress; |
| 69 | private TextView mButton; |
| 70 | private View mCancelButton; |
| 71 | private boolean mEditingExisting; |
| 72 | private Bundle mMap; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 73 | private String mTouchIconUrl; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 74 | private Bitmap mThumbnail; |
| 75 | private String mOriginalUrl; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 76 | private TextView mFolder; |
| 77 | private View mDefaultView; |
| 78 | private View mFolderSelector; |
| 79 | private EditText mFolderNamer; |
| 80 | private View mAddNewFolder; |
| 81 | private long mCurrentFolder = 0; |
| 82 | private FolderAdapter mAdapter; |
| 83 | private ArrayList<Folder> mPaths; |
| 84 | private TextView mPath; |
| 85 | |
| 86 | private static class Folder { |
| 87 | String Name; |
| 88 | long Id; |
| 89 | Folder(String name, long id) { |
| 90 | Name = name; |
| 91 | Id = id; |
| 92 | } |
| 93 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 94 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 95 | // Message IDs |
| 96 | private static final int SAVE_BOOKMARK = 100; |
| 97 | |
| 98 | private Handler mHandler; |
| 99 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 100 | @Override |
| 101 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 102 | if (v == mFolderNamer) { |
| 103 | if (v.getText().length() > 0) { |
| 104 | if (actionId == EditorInfo.IME_NULL) { |
| 105 | // Only want to do this once. |
| 106 | if (event.getAction() == KeyEvent.ACTION_UP) { |
| 107 | // Add the folder to the database |
| 108 | ContentValues values = new ContentValues(); |
| 109 | values.put(BrowserContract.Bookmarks.TITLE, |
| 110 | v.getText().toString()); |
| 111 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); |
| 112 | values.put(BrowserContract.Bookmarks.PARENT, |
| 113 | mCurrentFolder); |
| 114 | getContentResolver().insert( |
| 115 | BrowserContract.Bookmarks.CONTENT_URI, values); |
| 116 | |
| 117 | mFolderNamer.setVisibility(View.GONE); |
| 118 | InputMethodManager.getInstance(this) |
| 119 | .hideSoftInputFromWindow( |
| 120 | mFolderNamer.getWindowToken(), 0); |
| 121 | } |
| 122 | // Steal the key press for both up and down |
| 123 | return true; |
| 124 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 127 | return false; |
| 128 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 129 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 130 | @Override |
| 131 | public void onClick(View v) { |
| 132 | if (v == mButton) { |
| 133 | if (mFolderSelector.getVisibility() == View.VISIBLE) { |
| 134 | // We are showing the folder selector. This means that the user |
| 135 | // has selected a folder. Go back to the opening page |
| 136 | mFolderSelector.setVisibility(View.GONE); |
| 137 | mDefaultView.setVisibility(View.VISIBLE); |
| 138 | setTitle(R.string.bookmark_this_page); |
| 139 | } else if (save()) { |
| 140 | finish(); |
| 141 | } |
| 142 | } else if (v == mCancelButton) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 143 | finish(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 144 | } else if (v == mFolder) { |
| 145 | switchToFolderSelector(); |
| 146 | } else if (v == mAddNewFolder) { |
| 147 | mFolderNamer.setVisibility(View.VISIBLE); |
| 148 | mFolderNamer.setText(R.string.new_folder); |
| 149 | mFolderNamer.requestFocus(); |
| 150 | InputMethodManager.getInstance(this).showSoftInput(mFolderNamer, |
| 151 | InputMethodManager.SHOW_IMPLICIT); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 152 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 153 | } |
| 154 | |
| 155 | private void switchToFolderSelector() { |
| 156 | mDefaultView.setVisibility(View.GONE); |
| 157 | mFolderSelector.setVisibility(View.VISIBLE); |
| 158 | setTitle(R.string.containing_folder); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 163 | String[] projection; |
| 164 | switch (id) { |
| 165 | case LOADER_ID_ALL_FOLDERS: |
| 166 | projection = new String[] { |
| 167 | BrowserContract.Bookmarks._ID, |
| 168 | BrowserContract.Bookmarks.PARENT, |
| 169 | BrowserContract.Bookmarks.TITLE, |
| 170 | BrowserContract.Bookmarks.IS_FOLDER |
| 171 | }; |
| 172 | return new CursorLoader(this, |
| 173 | BrowserContract.Bookmarks.CONTENT_URI, |
| 174 | projection, |
| 175 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 176 | null, |
| 177 | null); |
| 178 | case LOADER_ID_FOLDER_CONTENTS: |
| 179 | projection = new String[] { |
| 180 | BrowserContract.Bookmarks._ID, |
| 181 | BrowserContract.Bookmarks.TITLE, |
| 182 | BrowserContract.Bookmarks.IS_FOLDER |
| 183 | }; |
| 184 | |
| 185 | return new CursorLoader(this, |
| 186 | BrowserContract.Bookmarks.buildFolderUri( |
| 187 | mCurrentFolder), |
| 188 | projection, |
| 189 | BrowserContract.Bookmarks.IS_FOLDER + " != 0", |
| 190 | null, |
| 191 | null); |
| 192 | default: |
| 193 | throw new AssertionError("Asking for nonexistant loader!"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 199 | switch (loader.getId()) { |
| 200 | case LOADER_ID_FOLDER_CONTENTS: |
| 201 | mAdapter.changeCursor(cursor); |
| 202 | break; |
| 203 | case LOADER_ID_ALL_FOLDERS: |
| 204 | long parent = mCurrentFolder; |
| 205 | int idIndex = cursor.getColumnIndexOrThrow( |
| 206 | BrowserContract.Bookmarks._ID); |
| 207 | int titleIndex = cursor.getColumnIndexOrThrow( |
| 208 | BrowserContract.Bookmarks.TITLE); |
| 209 | int parentIndex = cursor.getColumnIndexOrThrow( |
| 210 | BrowserContract.Bookmarks.PARENT); |
| 211 | while (parent != 0) { |
| 212 | // First, find the folder corresponding to the current |
| 213 | // folder |
| 214 | if (!cursor.moveToFirst()) { |
| 215 | throw new AssertionError("No folders in the database!"); |
| 216 | } |
| 217 | long folder; |
| 218 | do { |
| 219 | folder = cursor.getLong(idIndex); |
| 220 | } while (folder != parent && cursor.moveToNext()); |
| 221 | if (cursor.isAfterLast()) { |
| 222 | throw new AssertionError("Folder(id=" + parent |
| 223 | + ") holding this bookmark does not exist!"); |
| 224 | } |
| 225 | String name = cursor.getString(titleIndex); |
| 226 | mPaths.add(1, new Folder(name, parent)); |
| 227 | parent = cursor.getLong(parentIndex); |
| 228 | } |
| 229 | getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS); |
| 230 | updatePathString(); |
| 231 | break; |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Update the TextViews in both modes to display the full path of the |
| 239 | * current location to insert. |
| 240 | */ |
| 241 | private void updatePathString() { |
| 242 | String path = mPaths.get(0).Name; |
| 243 | int size = mPaths.size(); |
| 244 | for (int i = 1; i < size; i++) { |
| 245 | path += " / " + mPaths.get(i).Name; |
| 246 | } |
| 247 | mPath.setText(path); |
| 248 | mFolder.setText(path); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 253 | long id) { |
| 254 | // Switch to the folder that was clicked on. |
| 255 | mCurrentFolder = id; |
| 256 | mPaths.add(new Folder(((TextView) view).getText().toString(), id)); |
| 257 | updatePathString(); |
| 258 | |
| 259 | getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Shows a list of names of folders. |
| 264 | */ |
| 265 | private class FolderAdapter extends CursorAdapter { |
| 266 | public FolderAdapter(Context context) { |
| 267 | super(context, null); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void bindView(View view, Context context, Cursor cursor) { |
| 272 | ((TextView) view.findViewById(android.R.id.text1)).setText( |
| 273 | cursor.getString(cursor.getColumnIndexOrThrow( |
| 274 | BrowserContract.Bookmarks.TITLE))); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 279 | return LayoutInflater.from(context).inflate( |
| 280 | android.R.layout.simple_list_item_1, null); |
| 281 | } |
| 282 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 283 | |
| 284 | protected void onCreate(Bundle icicle) { |
| 285 | super.onCreate(icicle); |
| 286 | requestWindowFeature(Window.FEATURE_LEFT_ICON); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 287 | |
| 288 | mMap = getIntent().getExtras(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 289 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 290 | setContentView(R.layout.browser_add_bookmark); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 291 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 292 | setTitle(R.string.bookmark_this_page); |
Ben Murdoch | a753d00 | 2009-10-01 11:36:19 +0100 | [diff] [blame] | 293 | getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 294 | |
| 295 | String title = null; |
| 296 | String url = null; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 297 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 298 | if (mMap != null) { |
| 299 | Bundle b = mMap.getBundle("bookmark"); |
| 300 | if (b != null) { |
| 301 | mMap = b; |
| 302 | mEditingExisting = true; |
| 303 | setTitle(R.string.edit_bookmark); |
| 304 | } |
| 305 | title = mMap.getString("title"); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 306 | url = mOriginalUrl = mMap.getString("url"); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 307 | mTouchIconUrl = mMap.getString("touch_icon_url"); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 308 | mThumbnail = (Bitmap) mMap.getParcelable("thumbnail"); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 309 | mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | mTitle = (EditText) findViewById(R.id.title); |
| 313 | mTitle.setText(title); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 314 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 315 | mAddress = (EditText) findViewById(R.id.address); |
| 316 | mAddress.setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 317 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 318 | mButton = (TextView) findViewById(R.id.OK); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 319 | mButton.setOnClickListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 320 | |
| 321 | mCancelButton = findViewById(R.id.cancel); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 322 | mCancelButton.setOnClickListener(this); |
| 323 | |
| 324 | mFolder = (TextView) findViewById(R.id.folder); |
| 325 | mFolder.setOnClickListener(this); |
| 326 | |
| 327 | mDefaultView = findViewById(R.id.default_view); |
| 328 | mFolderSelector = findViewById(R.id.folder_selector); |
| 329 | |
| 330 | mFolderNamer = (EditText) findViewById(R.id.folder_namer); |
| 331 | mFolderNamer.setOnEditorActionListener(this); |
| 332 | |
| 333 | mAddNewFolder = findViewById(R.id.add_new_folder); |
| 334 | mAddNewFolder.setOnClickListener(this); |
| 335 | |
| 336 | mPath = (TextView) findViewById(R.id.path); |
| 337 | ListView list = (ListView) findViewById(R.id.list); |
| 338 | |
| 339 | mPaths = new ArrayList<Folder>(); |
| 340 | mPaths.add(0, new Folder(getString(R.string.bookmarks), 0)); |
| 341 | mAdapter = new FolderAdapter(this); |
| 342 | list.setAdapter(mAdapter); |
| 343 | list.setOnItemClickListener(this); |
| 344 | LoaderManager manager = getLoaderManager(); |
| 345 | if (mCurrentFolder != 0) { |
| 346 | // Find all the folders |
| 347 | manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this); |
| 348 | } |
| 349 | manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
| 350 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 351 | |
| 352 | if (!getWindow().getDecorView().isInTouchMode()) { |
| 353 | mButton.requestFocus(); |
| 354 | } |
| 355 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 356 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 357 | @Override |
| 358 | public boolean dispatchKeyEvent (KeyEvent event) { |
| 359 | if (mFolderSelector.getVisibility() == View.VISIBLE |
| 360 | && KeyEvent.KEYCODE_BACK == event.getKeyCode()) { |
| 361 | if (KeyEvent.ACTION_UP == event.getAction()) { |
| 362 | int size = mPaths.size(); |
| 363 | if (1 == size) { |
| 364 | // We have reached the top level |
| 365 | finish(); |
| 366 | } else { |
| 367 | // Go up a level |
| 368 | mPaths.remove(size - 1); |
| 369 | mCurrentFolder = mPaths.get(size - 2).Id; |
| 370 | updatePathString(); |
| 371 | getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, |
| 372 | null, this); |
| 373 | } |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | return super.dispatchKeyEvent(event); |
| 378 | } |
| 379 | |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 380 | /** |
| 381 | * Runnable to save a bookmark, so it can be performed in its own thread. |
| 382 | */ |
| 383 | private class SaveBookmarkRunnable implements Runnable { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 384 | // FIXME: This should be an async task. |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 385 | private Message mMessage; |
| 386 | public SaveBookmarkRunnable(Message msg) { |
| 387 | mMessage = msg; |
| 388 | } |
| 389 | public void run() { |
| 390 | // Unbundle bookmark data. |
| 391 | Bundle bundle = mMessage.getData(); |
| 392 | String title = bundle.getString("title"); |
| 393 | String url = bundle.getString("url"); |
| 394 | boolean invalidateThumbnail = bundle.getBoolean( |
| 395 | "invalidateThumbnail"); |
| 396 | Bitmap thumbnail = invalidateThumbnail ? null |
| 397 | : (Bitmap) bundle.getParcelable("thumbnail"); |
| 398 | String touchIconUrl = bundle.getString("touchIconUrl"); |
| 399 | |
| 400 | // Save to the bookmarks DB. |
| 401 | try { |
| 402 | final ContentResolver cr = getContentResolver(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 403 | Bookmarks.addBookmark(AddBookmarkPage.this, false, url, |
| 404 | title, thumbnail, true, mCurrentFolder); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 405 | if (touchIconUrl != null) { |
Ben Murdoch | ccb5de0 | 2010-07-19 18:38:17 +0100 | [diff] [blame] | 406 | new DownloadTouchIcon(AddBookmarkPage.this, cr, url).execute(mTouchIconUrl); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 407 | } |
| 408 | mMessage.arg1 = 1; |
| 409 | } catch (IllegalStateException e) { |
| 410 | mMessage.arg1 = 0; |
| 411 | } |
| 412 | mMessage.sendToTarget(); |
| 413 | } |
| 414 | } |
| 415 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 416 | private void createHandler() { |
| 417 | if (mHandler == null) { |
| 418 | mHandler = new Handler() { |
| 419 | @Override |
| 420 | public void handleMessage(Message msg) { |
| 421 | switch (msg.what) { |
| 422 | case SAVE_BOOKMARK: |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 423 | if (1 == msg.arg1) { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 424 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 425 | Toast.LENGTH_LONG).show(); |
| 426 | } else { |
| 427 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 428 | Toast.LENGTH_LONG).show(); |
| 429 | } |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | }; |
| 434 | } |
| 435 | } |
| 436 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 437 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 438 | * 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] | 439 | */ |
| 440 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 441 | createHandler(); |
| 442 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 443 | String title = mTitle.getText().toString().trim(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 444 | String unfilteredUrl; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 445 | unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString()); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 446 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 447 | boolean emptyTitle = title.length() == 0; |
| 448 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 449 | Resources r = getResources(); |
| 450 | if (emptyTitle || emptyUrl) { |
| 451 | if (emptyTitle) { |
| 452 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 453 | } |
| 454 | if (emptyUrl) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 455 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 456 | } |
Leon Scroggins III | 6e3faea | 2010-09-07 15:22:14 -0400 | [diff] [blame] | 457 | return false; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 458 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 459 | } |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 460 | String url = unfilteredUrl.trim(); |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 461 | try { |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 462 | // We allow bookmarks with a javascript: scheme, but these will in most cases |
| 463 | // fail URI parsing, so don't try it if that's the kind of bookmark we have. |
| 464 | |
| 465 | if (!url.toLowerCase().startsWith("javascript:")) { |
| 466 | URI uriObj = new URI(url); |
| 467 | String scheme = uriObj.getScheme(); |
| 468 | if (!Bookmarks.urlHasAcceptableScheme(url)) { |
| 469 | // If the scheme was non-null, let the user know that we |
| 470 | // can't save their bookmark. If it was null, we'll assume |
| 471 | // they meant http when we parse it in the WebAddress class. |
| 472 | if (scheme != null) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 473 | mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 474 | return false; |
| 475 | } |
| 476 | WebAddress address; |
| 477 | try { |
| 478 | address = new WebAddress(unfilteredUrl); |
| 479 | } catch (ParseException e) { |
| 480 | throw new URISyntaxException("", ""); |
| 481 | } |
| 482 | if (address.mHost.length() == 0) { |
| 483 | throw new URISyntaxException("", ""); |
| 484 | } |
| 485 | url = address.toString(); |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 486 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 487 | } |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 488 | } catch (URISyntaxException e) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 489 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 490 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 491 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 492 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 493 | if (mEditingExisting) { |
| 494 | mMap.putString("title", title); |
| 495 | mMap.putString("url", url); |
| 496 | mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl)); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame^] | 497 | // FIXME: This does not work yet |
| 498 | mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 499 | setResult(RESULT_OK, (new Intent()).setAction( |
| 500 | getIntent().toString()).putExtras(mMap)); |
| 501 | } else { |
| 502 | // Post a message to write to the DB. |
| 503 | Bundle bundle = new Bundle(); |
| 504 | bundle.putString("title", title); |
| 505 | bundle.putString("url", url); |
| 506 | bundle.putParcelable("thumbnail", mThumbnail); |
| 507 | bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl)); |
| 508 | bundle.putString("touchIconUrl", mTouchIconUrl); |
| 509 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 510 | msg.setData(bundle); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 511 | // Start a new thread so as to not slow down the UI |
| 512 | Thread t = new Thread(new SaveBookmarkRunnable(msg)); |
| 513 | t.start(); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 514 | setResult(RESULT_OK); |
Kristian Monsen | 0a1d838 | 2010-02-01 18:41:07 +0000 | [diff] [blame] | 515 | LogTag.logBookmarkAdded(url, "bookmarkview"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | } |