blob: 594f985a725af4ac97122f1bd2069814e37a6275 [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;
Henrik Baard980e9952010-09-06 18:13:23 +020021import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.Intent;
23import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040024import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010025import android.graphics.Bitmap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.net.ParseException;
27import android.net.WebAddress;
28import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010029import android.os.Handler;
30import android.os.Message;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.provider.Browser;
32import android.view.View;
33import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.widget.EditText;
35import android.widget.TextView;
36import android.widget.Toast;
37
Cary Clarkf2407c62009-09-04 12:25:10 -040038import java.net.URI;
39import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import java.util.Date;
41
42public class AddBookmarkPage extends Activity {
43
44 private final String LOGTAG = "Bookmarks";
45
46 private EditText mTitle;
47 private EditText mAddress;
48 private TextView mButton;
49 private View mCancelButton;
50 private boolean mEditingExisting;
51 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040052 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010053 private Bitmap mThumbnail;
54 private String mOriginalUrl;
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);
78 setContentView(R.layout.browser_add_bookmark);
79 setTitle(R.string.save_to_bookmarks);
Ben Murdocha753d002009-10-01 11:36:19 +010080 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
The Android Open Source Project0c908882009-03-03 19:32:16 -080081
82 String title = null;
83 String url = null;
84 mMap = getIntent().getExtras();
85 if (mMap != null) {
86 Bundle b = mMap.getBundle("bookmark");
87 if (b != null) {
88 mMap = b;
89 mEditingExisting = true;
90 setTitle(R.string.edit_bookmark);
91 }
92 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +010093 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -040094 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +010095 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 }
97
98 mTitle = (EditText) findViewById(R.id.title);
99 mTitle.setText(title);
100 mAddress = (EditText) findViewById(R.id.address);
101 mAddress.setText(url);
102
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103 View.OnClickListener accept = mSaveBookmark;
104 mButton = (TextView) findViewById(R.id.OK);
105 mButton.setOnClickListener(accept);
106
107 mCancelButton = findViewById(R.id.cancel);
108 mCancelButton.setOnClickListener(mCancel);
109
110 if (!getWindow().getDecorView().isInTouchMode()) {
111 mButton.requestFocus();
112 }
113 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100114
Leon Scroggins02065b02010-01-04 14:30:13 -0500115 /**
116 * Runnable to save a bookmark, so it can be performed in its own thread.
117 */
118 private class SaveBookmarkRunnable implements Runnable {
119 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200120 private Context mContext;
121 public SaveBookmarkRunnable(Context ctx, Message msg) {
122 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500123 mMessage = msg;
124 }
125 public void run() {
126 // Unbundle bookmark data.
127 Bundle bundle = mMessage.getData();
128 String title = bundle.getString("title");
129 String url = bundle.getString("url");
130 boolean invalidateThumbnail = bundle.getBoolean(
131 "invalidateThumbnail");
132 Bitmap thumbnail = invalidateThumbnail ? null
133 : (Bitmap) bundle.getParcelable("thumbnail");
134 String touchIconUrl = bundle.getString("touchIconUrl");
135
136 // Save to the bookmarks DB.
137 try {
138 final ContentResolver cr = getContentResolver();
139 Bookmarks.addBookmark(null, cr, url, title, thumbnail, true);
140 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200141 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500142 }
143 mMessage.arg1 = 1;
144 } catch (IllegalStateException e) {
145 mMessage.arg1 = 0;
146 }
147 mMessage.sendToTarget();
148 }
149 }
150
Ben Murdoch1794fe22009-09-29 18:14:30 +0100151 private void createHandler() {
152 if (mHandler == null) {
153 mHandler = new Handler() {
154 @Override
155 public void handleMessage(Message msg) {
156 switch (msg.what) {
157 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500158 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100159 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
160 Toast.LENGTH_LONG).show();
161 } else {
162 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
163 Toast.LENGTH_LONG).show();
164 }
165 break;
166 }
167 }
168 };
169 }
170 }
171
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100173 * 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 -0800174 */
175 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100176 createHandler();
177
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 String title = mTitle.getText().toString().trim();
179 String unfilteredUrl =
180 BrowserActivity.fixUrl(mAddress.getText().toString());
181 boolean emptyTitle = title.length() == 0;
182 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
183 Resources r = getResources();
184 if (emptyTitle || emptyUrl) {
185 if (emptyTitle) {
186 mTitle.setError(r.getText(R.string.bookmark_needs_title));
187 }
188 if (emptyUrl) {
189 mAddress.setError(r.getText(R.string.bookmark_needs_url));
190 }
191 return false;
192 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000193 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400194 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000195 // We allow bookmarks with a javascript: scheme, but these will in most cases
196 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
197
198 if (!url.toLowerCase().startsWith("javascript:")) {
199 URI uriObj = new URI(url);
200 String scheme = uriObj.getScheme();
201 if (!Bookmarks.urlHasAcceptableScheme(url)) {
202 // If the scheme was non-null, let the user know that we
203 // can't save their bookmark. If it was null, we'll assume
204 // they meant http when we parse it in the WebAddress class.
205 if (scheme != null) {
206 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
207 return false;
208 }
209 WebAddress address;
210 try {
211 address = new WebAddress(unfilteredUrl);
212 } catch (ParseException e) {
213 throw new URISyntaxException("", "");
214 }
215 if (address.mHost.length() == 0) {
216 throw new URISyntaxException("", "");
217 }
218 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100219 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400221 } catch (URISyntaxException e) {
222 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
223 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100225
Ben Murdoch1794fe22009-09-29 18:14:30 +0100226 if (mEditingExisting) {
227 mMap.putString("title", title);
228 mMap.putString("url", url);
229 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
230 setResult(RESULT_OK, (new Intent()).setAction(
231 getIntent().toString()).putExtras(mMap));
232 } else {
233 // Post a message to write to the DB.
234 Bundle bundle = new Bundle();
235 bundle.putString("title", title);
236 bundle.putString("url", url);
237 bundle.putParcelable("thumbnail", mThumbnail);
238 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
239 bundle.putString("touchIconUrl", mTouchIconUrl);
240 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
241 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500242 // Start a new thread so as to not slow down the UI
Henrik Baard980e9952010-09-06 18:13:23 +0200243 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
Leon Scroggins02065b02010-01-04 14:30:13 -0500244 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100245 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000246 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 }
248 return true;
249 }
250}