blob: 1104d5e254f80ab1a03e9c8eb711671b9c1b0f8d [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054
Ben Murdoch1794fe22009-09-29 18:14:30 +010055 // Message IDs
56 private static final int SAVE_BOOKMARK = 100;
57
58 private Handler mHandler;
59
The Android Open Source Project0c908882009-03-03 19:32:16 -080060 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
61 public void onClick(View v) {
62 if (save()) {
63 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 }
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);
Ben Murdocha753d002009-10-01 11:36:19 +010079 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
The Android Open Source Project0c908882009-03-03 19:32:16 -080080
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 Murdochaac7aa62009-09-17 16:57:40 +010092 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -040093 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +010094 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 }
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 Project0c908882009-03-03 19:32:16 -0800102 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 Murdoch1794fe22009-09-29 18:14:30 +0100113
Leon Scroggins02065b02010-01-04 14:30:13 -0500114 /**
115 * Runnable to save a bookmark, so it can be performed in its own thread.
116 */
117 private class SaveBookmarkRunnable implements Runnable {
118 private Message mMessage;
119 public SaveBookmarkRunnable(Message msg) {
120 mMessage = msg;
121 }
122 public void run() {
123 // Unbundle bookmark data.
124 Bundle bundle = mMessage.getData();
125 String title = bundle.getString("title");
126 String url = bundle.getString("url");
127 boolean invalidateThumbnail = bundle.getBoolean(
128 "invalidateThumbnail");
129 Bitmap thumbnail = invalidateThumbnail ? null
130 : (Bitmap) bundle.getParcelable("thumbnail");
131 String touchIconUrl = bundle.getString("touchIconUrl");
132
133 // Save to the bookmarks DB.
134 try {
135 final ContentResolver cr = getContentResolver();
136 Bookmarks.addBookmark(null, cr, url, title, thumbnail, true);
137 if (touchIconUrl != null) {
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400138 new DownloadTouchIcon(cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500139 }
140 mMessage.arg1 = 1;
141 } catch (IllegalStateException e) {
142 mMessage.arg1 = 0;
143 }
144 mMessage.sendToTarget();
145 }
146 }
147
Ben Murdoch1794fe22009-09-29 18:14:30 +0100148 private void createHandler() {
149 if (mHandler == null) {
150 mHandler = new Handler() {
151 @Override
152 public void handleMessage(Message msg) {
153 switch (msg.what) {
154 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500155 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100156 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
157 Toast.LENGTH_LONG).show();
158 } else {
159 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
160 Toast.LENGTH_LONG).show();
161 }
162 break;
163 }
164 }
165 };
166 }
167 }
168
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100170 * 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 -0800171 */
172 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100173 createHandler();
174
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 String title = mTitle.getText().toString().trim();
176 String unfilteredUrl =
177 BrowserActivity.fixUrl(mAddress.getText().toString());
178 boolean emptyTitle = title.length() == 0;
179 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
180 Resources r = getResources();
181 if (emptyTitle || emptyUrl) {
182 if (emptyTitle) {
183 mTitle.setError(r.getText(R.string.bookmark_needs_title));
184 }
185 if (emptyUrl) {
186 mAddress.setError(r.getText(R.string.bookmark_needs_url));
187 }
188 return false;
189 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000190 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400191 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000192 // We allow bookmarks with a javascript: scheme, but these will in most cases
193 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
194
195 if (!url.toLowerCase().startsWith("javascript:")) {
196 URI uriObj = new URI(url);
197 String scheme = uriObj.getScheme();
198 if (!Bookmarks.urlHasAcceptableScheme(url)) {
199 // If the scheme was non-null, let the user know that we
200 // can't save their bookmark. If it was null, we'll assume
201 // they meant http when we parse it in the WebAddress class.
202 if (scheme != null) {
203 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
204 return false;
205 }
206 WebAddress address;
207 try {
208 address = new WebAddress(unfilteredUrl);
209 } catch (ParseException e) {
210 throw new URISyntaxException("", "");
211 }
212 if (address.mHost.length() == 0) {
213 throw new URISyntaxException("", "");
214 }
215 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100216 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400218 } catch (URISyntaxException e) {
219 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
220 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100222
Ben Murdoch1794fe22009-09-29 18:14:30 +0100223 if (mEditingExisting) {
224 mMap.putString("title", title);
225 mMap.putString("url", url);
226 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
227 setResult(RESULT_OK, (new Intent()).setAction(
228 getIntent().toString()).putExtras(mMap));
229 } else {
230 // Post a message to write to the DB.
231 Bundle bundle = new Bundle();
232 bundle.putString("title", title);
233 bundle.putString("url", url);
234 bundle.putParcelable("thumbnail", mThumbnail);
235 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
236 bundle.putString("touchIconUrl", mTouchIconUrl);
237 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
238 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500239 // Start a new thread so as to not slow down the UI
240 Thread t = new Thread(new SaveBookmarkRunnable(msg));
241 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100242 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000243 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244 }
245 return true;
246 }
247}