blob: 10c91f8d4223887b9e97a8e927e2438b1e6d9668 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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
17package com.android.browser;
18
19import android.app.Activity;
20import android.content.ContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.Intent;
22import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040023import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010024import android.graphics.Bitmap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.net.ParseException;
26import android.net.WebAddress;
27import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010028import android.os.Handler;
29import android.os.Message;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.provider.Browser;
31import android.view.View;
32import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.widget.EditText;
34import android.widget.TextView;
35import android.widget.Toast;
36
Cary Clarkf2407c62009-09-04 12:25:10 -040037import java.net.URI;
38import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import java.util.Date;
40
41public 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 Scott3918d442009-08-04 13:22:29 -040051 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010052 private Bitmap mThumbnail;
53 private String mOriginalUrl;
Ben Murdocheecb4e62010-07-06 16:30:38 +010054 private boolean mIsUrlEditable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055
Ben Murdoch1794fe22009-09-29 18:14:30 +010056 // Message IDs
57 private static final int SAVE_BOOKMARK = 100;
58
59 private Handler mHandler;
60
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
62 public void onClick(View v) {
63 if (save()) {
64 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 }
66 }
67 };
68
69 private View.OnClickListener mCancel = new View.OnClickListener() {
70 public void onClick(View v) {
71 finish();
72 }
73 };
74
75 protected void onCreate(Bundle icicle) {
76 super.onCreate(icicle);
77 requestWindowFeature(Window.FEATURE_LEFT_ICON);
Ben Murdocheecb4e62010-07-06 16:30:38 +010078
79 mMap = getIntent().getExtras();
80 if (mMap != null) {
81 mIsUrlEditable = mMap.getBoolean("url_editable", true);
82 }
83
84 if (mIsUrlEditable) {
85 setContentView(R.layout.browser_add_bookmark);
86 } else {
87 setContentView(R.layout.browser_add_bookmark_const_url);
88 }
89
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 setTitle(R.string.save_to_bookmarks);
Ben Murdocha753d002009-10-01 11:36:19 +010091 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
The Android Open Source Project0c908882009-03-03 19:32:16 -080092
93 String title = null;
94 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +010095
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 if (mMap != null) {
97 Bundle b = mMap.getBundle("bookmark");
98 if (b != null) {
99 mMap = b;
100 mEditingExisting = true;
101 setTitle(R.string.edit_bookmark);
102 }
103 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100104 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400105 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100106 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107 }
108
109 mTitle = (EditText) findViewById(R.id.title);
110 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100111
112 if (mIsUrlEditable) {
113 mAddress = (EditText) findViewById(R.id.address);
114 mAddress.setText(url);
115 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 View.OnClickListener accept = mSaveBookmark;
118 mButton = (TextView) findViewById(R.id.OK);
119 mButton.setOnClickListener(accept);
120
121 mCancelButton = findViewById(R.id.cancel);
122 mCancelButton.setOnClickListener(mCancel);
123
124 if (!getWindow().getDecorView().isInTouchMode()) {
125 mButton.requestFocus();
126 }
127 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100128
Leon Scroggins02065b02010-01-04 14:30:13 -0500129 /**
130 * Runnable to save a bookmark, so it can be performed in its own thread.
131 */
132 private class SaveBookmarkRunnable implements Runnable {
133 private Message mMessage;
134 public SaveBookmarkRunnable(Message msg) {
135 mMessage = msg;
136 }
137 public void run() {
138 // Unbundle bookmark data.
139 Bundle bundle = mMessage.getData();
140 String title = bundle.getString("title");
141 String url = bundle.getString("url");
142 boolean invalidateThumbnail = bundle.getBoolean(
143 "invalidateThumbnail");
144 Bitmap thumbnail = invalidateThumbnail ? null
145 : (Bitmap) bundle.getParcelable("thumbnail");
146 String touchIconUrl = bundle.getString("touchIconUrl");
147
148 // Save to the bookmarks DB.
149 try {
150 final ContentResolver cr = getContentResolver();
151 Bookmarks.addBookmark(null, cr, url, title, thumbnail, true);
152 if (touchIconUrl != null) {
Ben Murdochccb5de02010-07-19 18:38:17 +0100153 new DownloadTouchIcon(AddBookmarkPage.this, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500154 }
155 mMessage.arg1 = 1;
156 } catch (IllegalStateException e) {
157 mMessage.arg1 = 0;
158 }
159 mMessage.sendToTarget();
160 }
161 }
162
Ben Murdoch1794fe22009-09-29 18:14:30 +0100163 private void createHandler() {
164 if (mHandler == null) {
165 mHandler = new Handler() {
166 @Override
167 public void handleMessage(Message msg) {
168 switch (msg.what) {
169 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500170 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100171 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
172 Toast.LENGTH_LONG).show();
173 } else {
174 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
175 Toast.LENGTH_LONG).show();
176 }
177 break;
178 }
179 }
180 };
181 }
182 }
183
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100185 * Parse the data entered in the dialog and post a message to update the bookmarks database.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 */
187 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100188 createHandler();
189
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100191 String unfilteredUrl;
192 if (mIsUrlEditable) {
193 unfilteredUrl =
194 BrowserActivity.fixUrl(mAddress.getText().toString());
195 } else {
196 unfilteredUrl = mOriginalUrl;
197 }
198
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 boolean emptyTitle = title.length() == 0;
200 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
201 Resources r = getResources();
202 if (emptyTitle || emptyUrl) {
203 if (emptyTitle) {
204 mTitle.setError(r.getText(R.string.bookmark_needs_title));
205 }
206 if (emptyUrl) {
Ben Murdocheecb4e62010-07-06 16:30:38 +0100207 if (mIsUrlEditable) {
208 mAddress.setError(r.getText(R.string.bookmark_needs_url));
209 } else {
210 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_needs_url,
211 Toast.LENGTH_LONG).show();
212 }
213 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100215
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000217 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400218 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000219 // We allow bookmarks with a javascript: scheme, but these will in most cases
220 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
221
222 if (!url.toLowerCase().startsWith("javascript:")) {
223 URI uriObj = new URI(url);
224 String scheme = uriObj.getScheme();
225 if (!Bookmarks.urlHasAcceptableScheme(url)) {
226 // If the scheme was non-null, let the user know that we
227 // can't save their bookmark. If it was null, we'll assume
228 // they meant http when we parse it in the WebAddress class.
229 if (scheme != null) {
Ben Murdocheecb4e62010-07-06 16:30:38 +0100230 if (mIsUrlEditable) {
231 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
232 } else {
233 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_cannot_save_url,
234 Toast.LENGTH_LONG).show();
235 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000236 return false;
237 }
238 WebAddress address;
239 try {
240 address = new WebAddress(unfilteredUrl);
241 } catch (ParseException e) {
242 throw new URISyntaxException("", "");
243 }
244 if (address.mHost.length() == 0) {
245 throw new URISyntaxException("", "");
246 }
247 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100248 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400250 } catch (URISyntaxException e) {
Ben Murdocheecb4e62010-07-06 16:30:38 +0100251 if (mIsUrlEditable) {
252 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
253 } else {
254 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_url_not_valid,
255 Toast.LENGTH_LONG).show();
256 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400257 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100259
Ben Murdoch1794fe22009-09-29 18:14:30 +0100260 if (mEditingExisting) {
261 mMap.putString("title", title);
262 mMap.putString("url", url);
263 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
264 setResult(RESULT_OK, (new Intent()).setAction(
265 getIntent().toString()).putExtras(mMap));
266 } else {
267 // Post a message to write to the DB.
268 Bundle bundle = new Bundle();
269 bundle.putString("title", title);
270 bundle.putString("url", url);
271 bundle.putParcelable("thumbnail", mThumbnail);
272 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
273 bundle.putString("touchIconUrl", mTouchIconUrl);
274 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
275 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500276 // Start a new thread so as to not slow down the UI
277 Thread t = new Thread(new SaveBookmarkRunnable(msg));
278 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100279 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000280 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 }
282 return true;
283 }
284}