1 /* 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 17 package com.android.browser; 18 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; 36 37 import java.util.Date; 38 39 public 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 = 51 { "_id", "url", "bookmark", "created", "title", "visits" }; 52 private static final String WHERE_CLAUSE = "url = ?"; 53 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 onCreate(Bundle icicle)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 */ save()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; 132 if (!(url.startsWith("about:") || url.startsWith("data:") || url 133 .startsWith("file:"))) { 134 WebAddress address; 135 try { 136 address = new WebAddress(unfilteredUrl); 137 } catch (ParseException e) { 138 mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); 139 return false; 140 } 141 if (address.mHost.length() == 0) { 142 mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); 143 return false; 144 } 145 url = address.toString(); 146 } 147 try { 148 if (mEditingExisting) { 149 mMap.putString("title", title); 150 mMap.putString("url", url); 151 setResult(RESULT_OK, (new Intent()).setAction( 152 getIntent().toString()).putExtras(mMap)); 153 } else { 154 // Want to append to the beginning of the list 155 long creationTime = new Date().getTime(); 156 SELECTION_ARGS[0] = url; 157 ContentResolver cr = getContentResolver(); 158 Cursor c = cr.query(Browser.BOOKMARKS_URI, 159 mProjection, 160 WHERE_CLAUSE, 161 SELECTION_ARGS, 162 null); 163 ContentValues map = new ContentValues(); 164 if (c.moveToFirst() && c.getInt(c.getColumnIndexOrThrow( 165 Browser.BookmarkColumns.BOOKMARK)) == 0) { 166 // This means we have been to this site but not bookmarked 167 // it, so convert the history item to a bookmark 168 map.put(Browser.BookmarkColumns.CREATED, creationTime); 169 map.put(Browser.BookmarkColumns.TITLE, title); 170 map.put(Browser.BookmarkColumns.BOOKMARK, 1); 171 cr.update(Browser.BOOKMARKS_URI, map, 172 "_id = " + c.getInt(0), null); 173 } else { 174 int count = c.getCount(); 175 boolean matchedTitle = false; 176 for (int i = 0; i < count; i++) { 177 // One or more bookmarks already exist for this site. 178 // Check the names of each 179 c.moveToPosition(i); 180 if (c.getString(c.getColumnIndexOrThrow( 181 Browser.BookmarkColumns.TITLE)).equals(title)) { 182 // The old bookmark has the same name. 183 // Update its creation time. 184 map.put(Browser.BookmarkColumns.CREATED, 185 creationTime); 186 cr.update(Browser.BOOKMARKS_URI, map, 187 "_id = " + c.getInt(0), null); 188 matchedTitle = true; 189 } 190 } 191 if (!matchedTitle) { 192 // Adding a bookmark for a site the user has visited, 193 // or a new bookmark (with a different name) for a site 194 // the user has visited 195 map.put(Browser.BookmarkColumns.TITLE, title); 196 map.put(Browser.BookmarkColumns.URL, url); 197 map.put(Browser.BookmarkColumns.CREATED, creationTime); 198 map.put(Browser.BookmarkColumns.BOOKMARK, 1); 199 map.put(Browser.BookmarkColumns.DATE, 0); 200 int visits = 0; 201 if (count > 0) { 202 // The user has already bookmarked, and possibly 203 // visited this site. However, they are creating 204 // a new bookmark with the same url but a different 205 // name. The new bookmark should have the same 206 // number of visits as the already created bookmark. 207 visits = c.getInt(c.getColumnIndexOrThrow( 208 Browser.BookmarkColumns.VISITS)); 209 } 210 // Bookmark starts with 3 extra visits so that it will 211 // bubble up in the most visited and goto search box 212 map.put(Browser.BookmarkColumns.VISITS, visits + 3); 213 cr.insert(Browser.BOOKMARKS_URI, map); 214 } 215 } 216 WebIconDatabase.getInstance().retainIconForPageUrl(url); 217 c.deactivate(); 218 setResult(RESULT_OK); 219 } 220 } catch (IllegalStateException e) { 221 setTitle(r.getText(R.string.no_database)); 222 return false; 223 } 224 return true; 225 } 226 } 227