The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [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; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.content.res.Resources; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 23 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.net.ParseException; |
| 26 | import android.net.WebAddress; |
| 27 | import android.os.Bundle; |
| 28 | import android.provider.Browser; |
| 29 | import android.view.View; |
| 30 | import android.view.Window; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.widget.EditText; |
| 32 | import android.widget.TextView; |
| 33 | import android.widget.Toast; |
| 34 | |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 35 | import java.net.URI; |
| 36 | import java.net.URISyntaxException; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 49 | private String mTouchIconUrl; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 50 | private Bitmap mThumbnail; |
| 51 | private String mOriginalUrl; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 52 | |
| 53 | private View.OnClickListener mSaveBookmark = new View.OnClickListener() { |
| 54 | public void onClick(View v) { |
| 55 | if (save()) { |
| 56 | finish(); |
| 57 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 58 | Toast.LENGTH_LONG).show(); |
| 59 | } |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | private View.OnClickListener mCancel = new View.OnClickListener() { |
| 64 | public void onClick(View v) { |
| 65 | finish(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | protected void onCreate(Bundle icicle) { |
| 70 | super.onCreate(icicle); |
| 71 | requestWindowFeature(Window.FEATURE_LEFT_ICON); |
| 72 | setContentView(R.layout.browser_add_bookmark); |
| 73 | setTitle(R.string.save_to_bookmarks); |
| 74 | getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark); |
| 75 | |
| 76 | String title = null; |
| 77 | String url = null; |
| 78 | mMap = getIntent().getExtras(); |
| 79 | if (mMap != null) { |
| 80 | Bundle b = mMap.getBundle("bookmark"); |
| 81 | if (b != null) { |
| 82 | mMap = b; |
| 83 | mEditingExisting = true; |
| 84 | setTitle(R.string.edit_bookmark); |
| 85 | } |
| 86 | title = mMap.getString("title"); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 87 | url = mOriginalUrl = mMap.getString("url"); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 88 | mTouchIconUrl = mMap.getString("touch_icon_url"); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 89 | mThumbnail = (Bitmap) mMap.getParcelable("thumbnail"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 = |
| 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; |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 132 | try { |
| 133 | URI uriObj = new URI(url); |
| 134 | String scheme = uriObj.getScheme(); |
| 135 | if (!("about".equals(scheme) || "data".equals(scheme) |
| 136 | || "javascript".equals(scheme) |
| 137 | || "file".equals(scheme) || "content".equals(scheme))) { |
| 138 | WebAddress address; |
| 139 | try { |
| 140 | address = new WebAddress(unfilteredUrl); |
| 141 | } catch (ParseException e) { |
| 142 | throw new URISyntaxException("", ""); |
| 143 | } |
| 144 | if (address.mHost.length() == 0) { |
| 145 | throw new URISyntaxException("", ""); |
| 146 | } |
| 147 | url = address.toString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 148 | } |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 149 | } catch (URISyntaxException e) { |
| 150 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
| 151 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 152 | } |
| 153 | try { |
| 154 | if (mEditingExisting) { |
| 155 | mMap.putString("title", title); |
| 156 | mMap.putString("url", url); |
| 157 | setResult(RESULT_OK, (new Intent()).setAction( |
| 158 | getIntent().toString()).putExtras(mMap)); |
| 159 | } else { |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 160 | final ContentResolver cr = getContentResolver(); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 161 | |
| 162 | // Only use mThumbnail if url and mOriginalUrl are matches. |
| 163 | // Otherwise the user edited the url and the thumbnail no longer applies. |
| 164 | if (mOriginalUrl.equals(url)) { |
| 165 | Bookmarks.addBookmark(null, cr, url, title, mThumbnail, true); |
| 166 | } else { |
| 167 | Bookmarks.addBookmark(null, cr, url, title, null, true); |
| 168 | } |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 169 | if (mTouchIconUrl != null) { |
| 170 | final Cursor c = |
| 171 | BrowserBookmarksAdapter.queryBookmarksForUrl( |
Leon Scroggins | a5d669e | 2009-08-05 14:07:58 -0400 | [diff] [blame] | 172 | cr, null, url, true); |
Patrick Scott | 59ce830 | 2009-09-18 16:29:38 -0400 | [diff] [blame] | 173 | new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 174 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 175 | setResult(RESULT_OK); |
| 176 | } |
| 177 | } catch (IllegalStateException e) { |
| 178 | setTitle(r.getText(R.string.no_database)); |
| 179 | return false; |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | } |