blob: f773d0629b35fc8c3170cab9159a8b45f8a82176 [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;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Intent;
24import android.content.res.Resources;
25import android.database.Cursor;
26import android.net.ParseException;
27import android.net.WebAddress;
28import android.os.Bundle;
29import android.provider.Browser;
30import android.view.View;
31import android.view.Window;
32import android.webkit.WebIconDatabase;
33import android.widget.EditText;
34import android.widget.TextView;
35import android.widget.Toast;
36
37import 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;
49
50 private static final String[] mProjection =
Leon Scrogginse75001e2009-05-08 18:17:08 -040051 { "_id", "url", "bookmark", "created", "title", "visits" };
52 private static final String WHERE_CLAUSE
53 = "url = ? OR url = ? OR url = ? OR url = ?";
54 private static final String WHERE_CLAUSE_SECURE = "url = ? OR url = ?";
55 private String[] SELECTION_ARGS;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056
57 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
58 public void onClick(View v) {
59 if (save()) {
60 finish();
61 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
62 Toast.LENGTH_LONG).show();
63 }
64 }
65 };
66
67 private View.OnClickListener mCancel = new View.OnClickListener() {
68 public void onClick(View v) {
69 finish();
70 }
71 };
72
73 protected void onCreate(Bundle icicle) {
74 super.onCreate(icicle);
75 requestWindowFeature(Window.FEATURE_LEFT_ICON);
76 setContentView(R.layout.browser_add_bookmark);
77 setTitle(R.string.save_to_bookmarks);
78 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark);
79
80 String title = null;
81 String url = null;
82 mMap = getIntent().getExtras();
83 if (mMap != null) {
84 Bundle b = mMap.getBundle("bookmark");
85 if (b != null) {
86 mMap = b;
87 mEditingExisting = true;
88 setTitle(R.string.edit_bookmark);
89 }
90 title = mMap.getString("title");
91 url = mMap.getString("url");
92 }
93
94 mTitle = (EditText) findViewById(R.id.title);
95 mTitle.setText(title);
96 mAddress = (EditText) findViewById(R.id.address);
97 mAddress.setText(url);
98
99
100 View.OnClickListener accept = mSaveBookmark;
101 mButton = (TextView) findViewById(R.id.OK);
102 mButton.setOnClickListener(accept);
103
104 mCancelButton = findViewById(R.id.cancel);
105 mCancelButton.setOnClickListener(mCancel);
106
107 if (!getWindow().getDecorView().isInTouchMode()) {
108 mButton.requestFocus();
109 }
110 }
111
112 /**
113 * Save the data to the database.
114 * Also, change the view to dialog stating
115 * that the webpage has been saved.
116 */
117 boolean save() {
118 String title = mTitle.getText().toString().trim();
119 String unfilteredUrl =
120 BrowserActivity.fixUrl(mAddress.getText().toString());
121 boolean emptyTitle = title.length() == 0;
122 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
123 Resources r = getResources();
124 if (emptyTitle || emptyUrl) {
125 if (emptyTitle) {
126 mTitle.setError(r.getText(R.string.bookmark_needs_title));
127 }
128 if (emptyUrl) {
129 mAddress.setError(r.getText(R.string.bookmark_needs_url));
130 }
131 return false;
132 }
133 String url = unfilteredUrl;
134 if (!(url.startsWith("about:") || url.startsWith("data:") || url
135 .startsWith("file:"))) {
136 WebAddress address;
137 try {
138 address = new WebAddress(unfilteredUrl);
139 } catch (ParseException e) {
140 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
141 return false;
142 }
143 if (address.mHost.length() == 0) {
144 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
145 return false;
146 }
147 url = address.toString();
148 }
149 try {
150 if (mEditingExisting) {
151 mMap.putString("title", title);
152 mMap.putString("url", url);
153 setResult(RESULT_OK, (new Intent()).setAction(
154 getIntent().toString()).putExtras(mMap));
155 } else {
156 // Want to append to the beginning of the list
157 long creationTime = new Date().getTime();
Leon Scrogginse75001e2009-05-08 18:17:08 -0400158 // First we check to see if the user has already visited this
159 // site. They may have bookmarked it in a different way from
160 // how it's stored in the database, so allow different combos
161 // to map to the same url.
162 boolean secure = false;
163 if (url.startsWith("http://")) {
164 url = url.substring(7);
165 } else if (url.startsWith("https://")) {
166 url = url.substring(8);
167 secure = true;
168 }
169 if (url.startsWith("www.")) {
170 url = url.substring(4);
171 }
172 if (secure) {
173 SELECTION_ARGS = new String[2];
174 SELECTION_ARGS[0] = "https://" + url;
175 SELECTION_ARGS[1] = "https://www." + url;
176 } else {
177 SELECTION_ARGS = new String[4];
178 SELECTION_ARGS[0] = url;
179 SELECTION_ARGS[1] = "www." + url;
180 SELECTION_ARGS[2] = "http://" + url;
181 SELECTION_ARGS[3] = "http://" + SELECTION_ARGS[1];
182 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 ContentResolver cr = getContentResolver();
184 Cursor c = cr.query(Browser.BOOKMARKS_URI,
185 mProjection,
Leon Scrogginse75001e2009-05-08 18:17:08 -0400186 secure ? WHERE_CLAUSE_SECURE : WHERE_CLAUSE,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 SELECTION_ARGS,
188 null);
189 ContentValues map = new ContentValues();
190 if (c.moveToFirst() && c.getInt(c.getColumnIndexOrThrow(
191 Browser.BookmarkColumns.BOOKMARK)) == 0) {
192 // This means we have been to this site but not bookmarked
193 // it, so convert the history item to a bookmark
194 map.put(Browser.BookmarkColumns.CREATED, creationTime);
195 map.put(Browser.BookmarkColumns.TITLE, title);
196 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
197 cr.update(Browser.BOOKMARKS_URI, map,
198 "_id = " + c.getInt(0), null);
199 } else {
200 int count = c.getCount();
201 boolean matchedTitle = false;
202 for (int i = 0; i < count; i++) {
203 // One or more bookmarks already exist for this site.
204 // Check the names of each
205 c.moveToPosition(i);
206 if (c.getString(c.getColumnIndexOrThrow(
207 Browser.BookmarkColumns.TITLE)).equals(title)) {
208 // The old bookmark has the same name.
209 // Update its creation time.
210 map.put(Browser.BookmarkColumns.CREATED,
211 creationTime);
212 cr.update(Browser.BOOKMARKS_URI, map,
213 "_id = " + c.getInt(0), null);
214 matchedTitle = true;
Leon Scrogginse75001e2009-05-08 18:17:08 -0400215 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 }
217 }
218 if (!matchedTitle) {
219 // Adding a bookmark for a site the user has visited,
220 // or a new bookmark (with a different name) for a site
221 // the user has visited
222 map.put(Browser.BookmarkColumns.TITLE, title);
223 map.put(Browser.BookmarkColumns.URL, url);
224 map.put(Browser.BookmarkColumns.CREATED, creationTime);
225 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
226 map.put(Browser.BookmarkColumns.DATE, 0);
227 int visits = 0;
228 if (count > 0) {
229 // The user has already bookmarked, and possibly
230 // visited this site. However, they are creating
231 // a new bookmark with the same url but a different
232 // name. The new bookmark should have the same
233 // number of visits as the already created bookmark.
234 visits = c.getInt(c.getColumnIndexOrThrow(
235 Browser.BookmarkColumns.VISITS));
236 }
237 // Bookmark starts with 3 extra visits so that it will
238 // bubble up in the most visited and goto search box
239 map.put(Browser.BookmarkColumns.VISITS, visits + 3);
240 cr.insert(Browser.BOOKMARKS_URI, map);
241 }
242 }
243 WebIconDatabase.getInstance().retainIconForPageUrl(url);
244 c.deactivate();
245 setResult(RESULT_OK);
246 }
247 } catch (IllegalStateException e) {
248 setTitle(r.getText(R.string.no_database));
249 return false;
250 }
251 return true;
252 }
253}