blob: 71bf481b84c2bb54c4b50c9c69843317287020ae [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;
28import android.provider.Browser;
29import android.view.View;
30import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.widget.EditText;
32import android.widget.TextView;
33import android.widget.Toast;
34
Cary Clarkf2407c62009-09-04 12:25:10 -040035import java.net.URI;
36import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import java.util.Date;
38
39public class AddBookmarkPage extends Activity {
40
41 private final String LOGTAG = "Bookmarks";
42
43 private EditText mTitle;
44 private EditText mAddress;
45 private TextView mButton;
46 private View mCancelButton;
47 private boolean mEditingExisting;
48 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040049 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010050 private Bitmap mThumbnail;
51 private String mOriginalUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
53 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
54 public void onClick(View v) {
55 if (save()) {
56 finish();
57 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
58 Toast.LENGTH_LONG).show();
59 }
60 }
61 };
62
63 private View.OnClickListener mCancel = new View.OnClickListener() {
64 public void onClick(View v) {
65 finish();
66 }
67 };
68
69 protected void onCreate(Bundle icicle) {
70 super.onCreate(icicle);
71 requestWindowFeature(Window.FEATURE_LEFT_ICON);
72 setContentView(R.layout.browser_add_bookmark);
73 setTitle(R.string.save_to_bookmarks);
74 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark);
75
76 String title = null;
77 String url = null;
78 mMap = getIntent().getExtras();
79 if (mMap != null) {
80 Bundle b = mMap.getBundle("bookmark");
81 if (b != null) {
82 mMap = b;
83 mEditingExisting = true;
84 setTitle(R.string.edit_bookmark);
85 }
86 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +010087 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -040088 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +010089 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 }
91
92 mTitle = (EditText) findViewById(R.id.title);
93 mTitle.setText(title);
94 mAddress = (EditText) findViewById(R.id.address);
95 mAddress.setText(url);
96
97
98 View.OnClickListener accept = mSaveBookmark;
99 mButton = (TextView) findViewById(R.id.OK);
100 mButton.setOnClickListener(accept);
101
102 mCancelButton = findViewById(R.id.cancel);
103 mCancelButton.setOnClickListener(mCancel);
104
105 if (!getWindow().getDecorView().isInTouchMode()) {
106 mButton.requestFocus();
107 }
108 }
109
110 /**
111 * Save the data to the database.
112 * Also, change the view to dialog stating
113 * that the webpage has been saved.
114 */
115 boolean save() {
116 String title = mTitle.getText().toString().trim();
117 String unfilteredUrl =
118 BrowserActivity.fixUrl(mAddress.getText().toString());
119 boolean emptyTitle = title.length() == 0;
120 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
121 Resources r = getResources();
122 if (emptyTitle || emptyUrl) {
123 if (emptyTitle) {
124 mTitle.setError(r.getText(R.string.bookmark_needs_title));
125 }
126 if (emptyUrl) {
127 mAddress.setError(r.getText(R.string.bookmark_needs_url));
128 }
129 return false;
130 }
131 String url = unfilteredUrl;
Cary Clarkf2407c62009-09-04 12:25:10 -0400132 try {
133 URI uriObj = new URI(url);
134 String scheme = uriObj.getScheme();
135 if (!("about".equals(scheme) || "data".equals(scheme)
136 || "javascript".equals(scheme)
137 || "file".equals(scheme) || "content".equals(scheme))) {
138 WebAddress address;
139 try {
140 address = new WebAddress(unfilteredUrl);
141 } catch (ParseException e) {
142 throw new URISyntaxException("", "");
143 }
144 if (address.mHost.length() == 0) {
145 throw new URISyntaxException("", "");
146 }
147 url = address.toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400149 } catch (URISyntaxException e) {
150 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
151 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152 }
153 try {
154 if (mEditingExisting) {
155 mMap.putString("title", title);
156 mMap.putString("url", url);
157 setResult(RESULT_OK, (new Intent()).setAction(
158 getIntent().toString()).putExtras(mMap));
159 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400160 final ContentResolver cr = getContentResolver();
Ben Murdochaac7aa62009-09-17 16:57:40 +0100161
162 // Only use mThumbnail if url and mOriginalUrl are matches.
163 // Otherwise the user edited the url and the thumbnail no longer applies.
Ben Murdochfbaf61e2009-09-28 10:29:35 +0100164 if (url.equals(mOriginalUrl)) {
Ben Murdochaac7aa62009-09-17 16:57:40 +0100165 Bookmarks.addBookmark(null, cr, url, title, mThumbnail, true);
166 } else {
167 Bookmarks.addBookmark(null, cr, url, title, null, true);
168 }
Patrick Scott3918d442009-08-04 13:22:29 -0400169 if (mTouchIconUrl != null) {
170 final Cursor c =
171 BrowserBookmarksAdapter.queryBookmarksForUrl(
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400172 cr, null, url, true);
Patrick Scott59ce8302009-09-18 16:29:38 -0400173 new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl);
Patrick Scott3918d442009-08-04 13:22:29 -0400174 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 setResult(RESULT_OK);
176 }
177 } catch (IllegalStateException e) {
178 setTitle(r.getText(R.string.no_database));
179 return false;
180 }
181 return true;
182 }
183}