The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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" }; |
| 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(); |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 117 | String unfilteredUrl = |
| 118 | BrowserActivity.fixUrl(mAddress.getText().toString()); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | 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); |
| 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); |
| 175 | } else { |
| 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); |
| 185 | } |
| 186 | WebIconDatabase.getInstance().retainIconForPageUrl(url); |
| 187 | c.deactivate(); |
| 188 | setResult(RESULT_OK); |
| 189 | } |
| 190 | } catch (IllegalStateException e) { |
| 191 | setTitle(r.getText(R.string.no_database)); |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | } |