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; |
| 20 | import android.content.ContentResolver; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.content.res.Resources; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 23 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.net.ParseException; |
| 26 | import android.net.WebAddress; |
| 27 | import android.os.Bundle; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame^] | 28 | import android.os.Handler; |
| 29 | import android.os.Message; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.provider.Browser; |
| 31 | import android.view.View; |
| 32 | import android.view.Window; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.widget.EditText; |
| 34 | import android.widget.TextView; |
| 35 | import android.widget.Toast; |
| 36 | |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 37 | import java.net.URI; |
| 38 | import java.net.URISyntaxException; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import java.util.Date; |
| 40 | |
| 41 | public class AddBookmarkPage extends Activity { |
| 42 | |
| 43 | private final String LOGTAG = "Bookmarks"; |
| 44 | |
| 45 | private EditText mTitle; |
| 46 | private EditText mAddress; |
| 47 | private TextView mButton; |
| 48 | private View mCancelButton; |
| 49 | private boolean mEditingExisting; |
| 50 | private Bundle mMap; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 51 | private String mTouchIconUrl; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 52 | private Bitmap mThumbnail; |
| 53 | private String mOriginalUrl; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame^] | 55 | // Message IDs |
| 56 | private static final int SAVE_BOOKMARK = 100; |
| 57 | |
| 58 | private Handler mHandler; |
| 59 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 60 | private View.OnClickListener mSaveBookmark = new View.OnClickListener() { |
| 61 | public void onClick(View v) { |
| 62 | if (save()) { |
| 63 | finish(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | private View.OnClickListener mCancel = new View.OnClickListener() { |
| 69 | public void onClick(View v) { |
| 70 | finish(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | protected void onCreate(Bundle icicle) { |
| 75 | super.onCreate(icicle); |
| 76 | requestWindowFeature(Window.FEATURE_LEFT_ICON); |
| 77 | setContentView(R.layout.browser_add_bookmark); |
| 78 | setTitle(R.string.save_to_bookmarks); |
| 79 | getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark); |
| 80 | |
| 81 | String title = null; |
| 82 | String url = null; |
| 83 | mMap = getIntent().getExtras(); |
| 84 | if (mMap != null) { |
| 85 | Bundle b = mMap.getBundle("bookmark"); |
| 86 | if (b != null) { |
| 87 | mMap = b; |
| 88 | mEditingExisting = true; |
| 89 | setTitle(R.string.edit_bookmark); |
| 90 | } |
| 91 | title = mMap.getString("title"); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 92 | url = mOriginalUrl = mMap.getString("url"); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 93 | mTouchIconUrl = mMap.getString("touch_icon_url"); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 94 | mThumbnail = (Bitmap) mMap.getParcelable("thumbnail"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | mTitle = (EditText) findViewById(R.id.title); |
| 98 | mTitle.setText(title); |
| 99 | mAddress = (EditText) findViewById(R.id.address); |
| 100 | mAddress.setText(url); |
| 101 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 102 | View.OnClickListener accept = mSaveBookmark; |
| 103 | mButton = (TextView) findViewById(R.id.OK); |
| 104 | mButton.setOnClickListener(accept); |
| 105 | |
| 106 | mCancelButton = findViewById(R.id.cancel); |
| 107 | mCancelButton.setOnClickListener(mCancel); |
| 108 | |
| 109 | if (!getWindow().getDecorView().isInTouchMode()) { |
| 110 | mButton.requestFocus(); |
| 111 | } |
| 112 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame^] | 113 | |
| 114 | private void createHandler() { |
| 115 | if (mHandler == null) { |
| 116 | mHandler = new Handler() { |
| 117 | @Override |
| 118 | public void handleMessage(Message msg) { |
| 119 | switch (msg.what) { |
| 120 | case SAVE_BOOKMARK: |
| 121 | // Unbundle bookmark data. |
| 122 | Bundle bundle = msg.getData(); |
| 123 | String title = bundle.getString("title"); |
| 124 | String url = bundle.getString("url"); |
| 125 | boolean invalidateThumbnail = bundle.getBoolean("invalidateThumbnail"); |
| 126 | Bitmap thumbnail = invalidateThumbnail |
| 127 | ? null : (Bitmap) bundle.getParcelable("thumbnail"); |
| 128 | String touchIconUrl = bundle.getString("touchIconUrl"); |
| 129 | |
| 130 | // Save to the bookmarks DB. |
| 131 | if (updateBookmarksDB(title, url, thumbnail, touchIconUrl)) { |
| 132 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 133 | Toast.LENGTH_LONG).show(); |
| 134 | } else { |
| 135 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 136 | Toast.LENGTH_LONG).show(); |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | }; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private boolean updateBookmarksDB(String title, String url, Bitmap thumbnail, String touchIconUrl) { |
| 146 | try { |
| 147 | final ContentResolver cr = getContentResolver(); |
| 148 | Bookmarks.addBookmark(null, cr, url, title, thumbnail, true); |
| 149 | if (touchIconUrl != null) { |
| 150 | final Cursor c = |
| 151 | BrowserBookmarksAdapter.queryBookmarksForUrl(cr, null, url, true); |
| 152 | new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl); |
| 153 | } |
| 154 | } catch (IllegalStateException e) { |
| 155 | return false; |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 160 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame^] | 161 | * 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] | 162 | */ |
| 163 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame^] | 164 | createHandler(); |
| 165 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 166 | String title = mTitle.getText().toString().trim(); |
| 167 | String unfilteredUrl = |
| 168 | BrowserActivity.fixUrl(mAddress.getText().toString()); |
| 169 | boolean emptyTitle = title.length() == 0; |
| 170 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 171 | Resources r = getResources(); |
| 172 | if (emptyTitle || emptyUrl) { |
| 173 | if (emptyTitle) { |
| 174 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 175 | } |
| 176 | if (emptyUrl) { |
| 177 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | String url = unfilteredUrl; |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 182 | try { |
| 183 | URI uriObj = new URI(url); |
| 184 | String scheme = uriObj.getScheme(); |
| 185 | if (!("about".equals(scheme) || "data".equals(scheme) |
| 186 | || "javascript".equals(scheme) |
| 187 | || "file".equals(scheme) || "content".equals(scheme))) { |
| 188 | WebAddress address; |
| 189 | try { |
| 190 | address = new WebAddress(unfilteredUrl); |
| 191 | } catch (ParseException e) { |
| 192 | throw new URISyntaxException("", ""); |
| 193 | } |
| 194 | if (address.mHost.length() == 0) { |
| 195 | throw new URISyntaxException("", ""); |
| 196 | } |
| 197 | url = address.toString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 198 | } |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 199 | } catch (URISyntaxException e) { |
| 200 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
| 201 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 202 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 203 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame^] | 204 | if (mEditingExisting) { |
| 205 | mMap.putString("title", title); |
| 206 | mMap.putString("url", url); |
| 207 | mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl)); |
| 208 | setResult(RESULT_OK, (new Intent()).setAction( |
| 209 | getIntent().toString()).putExtras(mMap)); |
| 210 | } else { |
| 211 | // Post a message to write to the DB. |
| 212 | Bundle bundle = new Bundle(); |
| 213 | bundle.putString("title", title); |
| 214 | bundle.putString("url", url); |
| 215 | bundle.putParcelable("thumbnail", mThumbnail); |
| 216 | bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl)); |
| 217 | bundle.putString("touchIconUrl", mTouchIconUrl); |
| 218 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 219 | msg.setData(bundle); |
| 220 | mHandler.sendMessage(msg); |
| 221 | setResult(RESULT_OK); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | } |