public void run() { // Unbundle bookmark data. Bundle bundle = mMessage.getData(); String title = bundle.getString("title"); String url = bundle.getString("url"); boolean invalidateThumbnail = bundle.getBoolean("invalidateThumbnail"); Bitmap thumbnail = invalidateThumbnail ? null : (Bitmap) bundle.getParcelable("thumbnail"); String touchIconUrl = bundle.getString(TOUCH_ICON_URL); // Save to the bookmarks DB. try { final ContentResolver cr = getContentResolver(); MyBookmarks.addBookmark(null, cr, url, title, thumbnail, true); if (touchIconUrl != null) { // new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); } mMessage.arg1 = 1; } catch (IllegalStateException e) { mMessage.arg1 = 0; } mMessage.sendToTarget(); }
/** Parse the data entered in the dialog and post a message to update the bookmarks database. */ boolean save() { createHandler(); String title = mTitle.getText().toString().trim(); String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString()); boolean emptyTitle = title.length() == 0; boolean emptyUrl = unfilteredUrl.trim().length() == 0; Resources r = getResources(); if (emptyTitle || emptyUrl) { if (emptyTitle) { mTitle.setError(r.getText(R.string.bookmark_needs_title)); } if (emptyUrl) { mAddress.setError(r.getText(R.string.bookmark_needs_url)); } return false; } String url = unfilteredUrl.trim(); try { // We allow bookmarks with a javascript: scheme, but these will in most cases // fail URI parsing, so don't try it if that's the kind of bookmark we have. if (!url.toLowerCase().startsWith("javascript:")) { URI uriObj = new URI(url); String scheme = uriObj.getScheme(); if (!MyBookmarks.urlHasAcceptableScheme(url)) { // If the scheme was non-null, let the user know that we // can't save their bookmark. If it was null, we'll assume // they meant http when we parse it in the WebAddress class. if (scheme != null) { mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); return false; } WebAddress address; try { address = new WebAddress(unfilteredUrl); } catch (ParseException e) { throw new URISyntaxException("", ""); } if (address.mHost.length() == 0) { throw new URISyntaxException("", ""); } url = address.toString(); } } } catch (URISyntaxException e) { mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); return false; } if (mEditingExisting) { mMap.putString("title", title); mMap.putString("url", url); mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl)); setResult(RESULT_OK, (new Intent()).setAction(getIntent().toString()).putExtras(mMap)); } else { // Post a message to write to the DB. Bundle bundle = new Bundle(); bundle.putString("title", title); bundle.putString("url", url); bundle.putParcelable("thumbnail", mThumbnail); bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl)); bundle.putString(TOUCH_ICON_URL, mTouchIconUrl); Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); msg.setData(bundle); // Start a new thread so as to not slow down the UI Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg)); t.start(); setResult(RESULT_OK); } return true; }