2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.app.Activity;
20 import android.content.ContentResolver;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.content.res.Resources;
25 import android.database.Cursor;
26 import android.net.ParseException;
27 import android.net.WebAddress;
28 import android.os.Bundle;
29 import android.provider.Browser;
30 import android.view.View;
31 import android.view.Window;
32 import android.webkit.WebIconDatabase;
33 import android.widget.EditText;
34 import android.widget.TextView;
35 import android.widget.Toast;
37 import java.util.Date;
39 public class AddBookmarkPage extends Activity {
41 private final String LOGTAG = "Bookmarks";
43 private EditText mTitle;
44 private EditText mAddress;
45 private TextView mButton;
46 private View mCancelButton;
47 private boolean mEditingExisting;
50 private static final String[] mProjection =
51 { "_id", "url", "bookmark", "created", "title" };
52 private static final String WHERE_CLAUSE = "url = ? AND bookmark = 0";
53 private final String[] SELECTION_ARGS = new String[1];
55 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
56 public void onClick(View v) {
59 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
60 Toast.LENGTH_LONG).show();
65 private View.OnClickListener mCancel = new View.OnClickListener() {
66 public void onClick(View v) {
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);
80 mMap = getIntent().getExtras();
82 Bundle b = mMap.getBundle("bookmark");
85 mEditingExisting = true;
86 setTitle(R.string.edit_bookmark);
88 title = mMap.getString("title");
89 url = mMap.getString("url");
92 mTitle = (EditText) findViewById(R.id.title);
93 mTitle.setText(title);
94 mAddress = (EditText) findViewById(R.id.address);
95 mAddress.setText(url);
98 View.OnClickListener accept = mSaveBookmark;
99 mButton = (TextView) findViewById(R.id.OK);
100 mButton.setOnClickListener(accept);
102 mCancelButton = findViewById(R.id.cancel);
103 mCancelButton.setOnClickListener(mCancel);
105 if (!getWindow().getDecorView().isInTouchMode()) {
106 mButton.requestFocus();
111 * Save the data to the database.
112 * Also, change the view to dialog stating
113 * that the webpage has been saved.
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();
124 setTitle(r.getText(R.string.empty_bookmark));
127 setTitle(r.getText(R.string.bookmark_needs_title));
131 setTitle(r.getText(R.string.bookmark_needs_url));
134 String url = unfilteredUrl;
135 if (!(url.startsWith("about:") || url.startsWith("data:") || url
136 .startsWith("file:"))) {
139 address = new WebAddress(unfilteredUrl);
140 } catch (ParseException e) {
141 setTitle(r.getText(R.string.bookmark_url_not_valid));
144 if (address.mHost.length() == 0) {
145 setTitle(r.getText(R.string.bookmark_url_not_valid));
148 url = address.toString();
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));
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,
166 if (c.moveToFirst()) {
167 // This means we have been to this site, so convert the
168 // history item to a bookmark.
169 ContentValues map = new ContentValues();
170 map.put(Browser.BookmarkColumns.CREATED, creationTime);
171 map.put(Browser.BookmarkColumns.TITLE, title);
172 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
173 cr.update(Browser.BOOKMARKS_URI, map,
174 "_id = " + c.getInt(0), null);
176 // Adding a bookmark for a site the user has not been to.
177 ContentValues map = new ContentValues();
178 map.put(Browser.BookmarkColumns.TITLE, title);
179 map.put(Browser.BookmarkColumns.URL, url);
180 map.put(Browser.BookmarkColumns.CREATED, creationTime);
181 map.put(Browser.BookmarkColumns.BOOKMARK, 1);
182 map.put(Browser.BookmarkColumns.DATE, 0);
183 map.put(Browser.BookmarkColumns.VISITS, 0);
184 cr.insert(Browser.BOOKMARKS_URI, map);
186 WebIconDatabase.getInstance().retainIconForPageUrl(url);
188 setResult(RESULT_OK);
190 } catch (IllegalStateException e) {
191 setTitle(r.getText(R.string.no_database));