blob: 23c0facb24d5369a571f66679b754f3799adb7db [file] [log] [blame]
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -07001/*
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 =
The Android Open Source Projectb7775e12009-02-10 15:44:04 -080051 { "_id", "url", "bookmark", "created", "title", "visits" };
52 private static final String WHERE_CLAUSE = "url = ?";
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070053 private final String[] SELECTION_ARGS = new String[1];
54
55 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
56 public void onClick(View v) {
57 if (save()) {
58 finish();
59 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
60 Toast.LENGTH_LONG).show();
61 }
62 }
63 };
64
65 private View.OnClickListener mCancel = new View.OnClickListener() {
66 public void onClick(View v) {
67 finish();
68 }
69 };
70
71 protected void onCreate(Bundle icicle) {
72 super.onCreate(icicle);
73 requestWindowFeature(Window.FEATURE_LEFT_ICON);
74 setContentView(R.layout.browser_add_bookmark);
75 setTitle(R.string.save_to_bookmarks);
76 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark);
77
78 String title = null;
79 String url = null;
80 mMap = getIntent().getExtras();
81 if (mMap != null) {
82 Bundle b = mMap.getBundle("bookmark");
83 if (b != null) {
84 mMap = b;
85 mEditingExisting = true;
86 setTitle(R.string.edit_bookmark);
87 }
88 title = mMap.getString("title");
89 url = mMap.getString("url");
90 }
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();
The Android Open Source Projected217d92008-12-17 18:05:52 -0800117 String unfilteredUrl =
118 BrowserActivity.fixUrl(mAddress.getText().toString());
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700119 boolean emptyTitle = title.length() == 0;
120 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
121 Resources r = getResources();
122 if (emptyTitle) {
123 if (emptyUrl) {
124 setTitle(r.getText(R.string.empty_bookmark));
125 return false;
126 }
127 setTitle(r.getText(R.string.bookmark_needs_title));
128 return false;
129 }
130 if (emptyUrl) {
131 setTitle(r.getText(R.string.bookmark_needs_url));
132 return false;
133 }
134 String url = unfilteredUrl;
135 if (!(url.startsWith("about:") || url.startsWith("data:") || url
136 .startsWith("file:"))) {
137 WebAddress address;
138 try {
139 address = new WebAddress(unfilteredUrl);
140 } catch (ParseException e) {
141 setTitle(r.getText(R.string.bookmark_url_not_valid));
142 return false;
143 }
144 if (address.mHost.length() == 0) {
145 setTitle(r.getText(R.string.bookmark_url_not_valid));
146 return false;
147 }
148 url = address.toString();
149 }
150 try {
151 if (mEditingExisting) {
152 mMap.putString("title", title);
153 mMap.putString("url", url);
154 setResult(RESULT_OK, (new Intent()).setAction(
155 getIntent().toString()).putExtras(mMap));
156 } else {
157 // Want to append to the beginning of the list
158 long creationTime = new Date().getTime();
159 SELECTION_ARGS[0] = url;
160 ContentResolver cr = getContentResolver();
161 Cursor c = cr.query(Browser.BOOKMARKS_URI,
162 mProjection,
163 WHERE_CLAUSE,
164 SELECTION_ARGS,
165 null);
The Android Open Source Projectb7775e12009-02-10 15:44:04 -0800166 ContentValues map = new ContentValues();
167 if (c.moveToFirst() && c.getInt(c.getColumnIndexOrThrow(
168 Browser.BookmarkColumns.BOOKMARK)) == 0) {
169 // This means we have been to this site but not bookmarked
170 // it, so convert the history item to a bookmark
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700171 map.put(Browser.BookmarkColumns.CREATED, creationTime);
172 map.put(Browser.BookmarkColumns.TITLE, title);
173 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
174 cr.update(Browser.BOOKMARKS_URI, map,
175 "_id = " + c.getInt(0), null);
176 } else {
The Android Open Source Projectb7775e12009-02-10 15:44:04 -0800177 int count = c.getCount();
178 boolean matchedTitle = false;
179 for (int i = 0; i < count; i++) {
180 // One or more bookmarks already exist for this site.
181 // Check the names of each
182 c.moveToPosition(i);
183 if (c.getString(c.getColumnIndexOrThrow(
184 Browser.BookmarkColumns.TITLE)).equals(title)) {
185 // The old bookmark has the same name.
186 // Update its creation time.
187 map.put(Browser.BookmarkColumns.CREATED,
188 creationTime);
189 cr.update(Browser.BOOKMARKS_URI, map,
190 "_id = " + c.getInt(0), null);
191 matchedTitle = true;
192 }
193 }
194 if (!matchedTitle) {
195 // Adding a bookmark for a site the user has visited,
196 // or a new bookmark (with a different name) for a site
197 // the user has visited
198 map.put(Browser.BookmarkColumns.TITLE, title);
199 map.put(Browser.BookmarkColumns.URL, url);
200 map.put(Browser.BookmarkColumns.CREATED, creationTime);
201 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
202 map.put(Browser.BookmarkColumns.DATE, 0);
203 int visits = 0;
204 if (count > 0) {
205 // The user has already bookmarked, and possibly
206 // visited this site. However, they are creating
207 // a new bookmark with the same url but a different
208 // name. The new bookmark should have the same
209 // number of visits as the already created bookmark.
210 visits = c.getInt(c.getColumnIndexOrThrow(
211 Browser.BookmarkColumns.VISITS));
212 }
213 // Bookmark starts with 3 extra visits so that it will
214 // bubble up in the most visited and goto search box
215 map.put(Browser.BookmarkColumns.VISITS, visits + 3);
216 cr.insert(Browser.BOOKMARKS_URI, map);
217 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700218 }
219 WebIconDatabase.getInstance().retainIconForPageUrl(url);
220 c.deactivate();
221 setResult(RESULT_OK);
222 }
223 } catch (IllegalStateException e) {
224 setTitle(r.getText(R.string.no_database));
225 return false;
226 }
227 return true;
228 }
229}