AddBookmarkPage.java revision ba6d7b853c32ad6c3be26c443daa61f322bcfdc2
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 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 = 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]; 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(); 117 String unfilteredUrl = mAddress.getText().toString(); 118 boolean emptyTitle = title.length() == 0; 119 boolean emptyUrl = unfilteredUrl.trim().length() == 0; 120 Resources r = getResources(); 121 if (emptyTitle) { 122 if (emptyUrl) { 123 setTitle(r.getText(R.string.empty_bookmark)); 124 return false; 125 } 126 setTitle(r.getText(R.string.bookmark_needs_title)); 127 return false; 128 } 129 if (emptyUrl) { 130 setTitle(r.getText(R.string.bookmark_needs_url)); 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 setTitle(r.getText(R.string.bookmark_url_not_valid)); 141 return false; 142 } 143 if (address.mHost.length() == 0) { 144 setTitle(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(); 158 SELECTION_ARGS[0] = url; 159 ContentResolver cr = getContentResolver(); 160 Cursor c = cr.query(Browser.BOOKMARKS_URI, 161 mProjection, 162 WHERE_CLAUSE, 163 SELECTION_ARGS, 164 null); 165 if (c.moveToFirst()) { 166 // This means we have been to this site, so convert the 167 // history item to a bookmark. 168 ContentValues map = new ContentValues(); 169 map.put(Browser.BookmarkColumns.CREATED, creationTime); 170 map.put(Browser.BookmarkColumns.TITLE, title); 171 map.put(Browser.BookmarkColumns.BOOKMARK, 1); 172 cr.update(Browser.BOOKMARKS_URI, map, 173 "_id = " + c.getInt(0), null); 174 } else { 175 // Adding a bookmark for a site the user has not been to. 176 ContentValues map = new ContentValues(); 177 map.put(Browser.BookmarkColumns.TITLE, title); 178 map.put(Browser.BookmarkColumns.URL, url); 179 map.put(Browser.BookmarkColumns.CREATED, creationTime); 180 map.put(Browser.BookmarkColumns.BOOKMARK, 1); 181 map.put(Browser.BookmarkColumns.DATE, 0); 182 map.put(Browser.BookmarkColumns.VISITS, 0); 183 cr.insert(Browser.BOOKMARKS_URI, map); 184 } 185 WebIconDatabase.getInstance().retainIconForPageUrl(url); 186 c.deactivate(); 187 setResult(RESULT_OK); 188 } 189 } catch (IllegalStateException e) { 190 setTitle(r.getText(R.string.no_database)); 191 return false; 192 } 193 return true; 194 } 195} 196