blob: 2a92dce0c0126a5ac09c06d3c5dcb23b5a04a5f4 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.Intent;
22import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040023import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.net.ParseException;
25import android.net.WebAddress;
26import android.os.Bundle;
27import android.provider.Browser;
28import android.view.View;
29import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.widget.EditText;
31import android.widget.TextView;
32import android.widget.Toast;
33
Cary Clarkf2407c62009-09-04 12:25:10 -040034import java.net.URI;
35import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import java.util.Date;
37
38public class AddBookmarkPage extends Activity {
39
40 private final String LOGTAG = "Bookmarks";
41
42 private EditText mTitle;
43 private EditText mAddress;
44 private TextView mButton;
45 private View mCancelButton;
46 private boolean mEditingExisting;
47 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040048 private String mTouchIconUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049
50 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
51 public void onClick(View v) {
52 if (save()) {
53 finish();
54 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
55 Toast.LENGTH_LONG).show();
56 }
57 }
58 };
59
60 private View.OnClickListener mCancel = new View.OnClickListener() {
61 public void onClick(View v) {
62 finish();
63 }
64 };
65
66 protected void onCreate(Bundle icicle) {
67 super.onCreate(icicle);
68 requestWindowFeature(Window.FEATURE_LEFT_ICON);
69 setContentView(R.layout.browser_add_bookmark);
70 setTitle(R.string.save_to_bookmarks);
71 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark);
72
73 String title = null;
74 String url = null;
75 mMap = getIntent().getExtras();
76 if (mMap != null) {
77 Bundle b = mMap.getBundle("bookmark");
78 if (b != null) {
79 mMap = b;
80 mEditingExisting = true;
81 setTitle(R.string.edit_bookmark);
82 }
83 title = mMap.getString("title");
84 url = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -040085 mTouchIconUrl = mMap.getString("touch_icon_url");
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 }
87
88 mTitle = (EditText) findViewById(R.id.title);
89 mTitle.setText(title);
90 mAddress = (EditText) findViewById(R.id.address);
91 mAddress.setText(url);
92
93
94 View.OnClickListener accept = mSaveBookmark;
95 mButton = (TextView) findViewById(R.id.OK);
96 mButton.setOnClickListener(accept);
97
98 mCancelButton = findViewById(R.id.cancel);
99 mCancelButton.setOnClickListener(mCancel);
100
101 if (!getWindow().getDecorView().isInTouchMode()) {
102 mButton.requestFocus();
103 }
104 }
105
106 /**
107 * Save the data to the database.
108 * Also, change the view to dialog stating
109 * that the webpage has been saved.
110 */
111 boolean save() {
112 String title = mTitle.getText().toString().trim();
113 String unfilteredUrl =
114 BrowserActivity.fixUrl(mAddress.getText().toString());
115 boolean emptyTitle = title.length() == 0;
116 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
117 Resources r = getResources();
118 if (emptyTitle || emptyUrl) {
119 if (emptyTitle) {
120 mTitle.setError(r.getText(R.string.bookmark_needs_title));
121 }
122 if (emptyUrl) {
123 mAddress.setError(r.getText(R.string.bookmark_needs_url));
124 }
125 return false;
126 }
127 String url = unfilteredUrl;
Cary Clarkf2407c62009-09-04 12:25:10 -0400128 try {
129 URI uriObj = new URI(url);
130 String scheme = uriObj.getScheme();
131 if (!("about".equals(scheme) || "data".equals(scheme)
132 || "javascript".equals(scheme)
133 || "file".equals(scheme) || "content".equals(scheme))) {
134 WebAddress address;
135 try {
136 address = new WebAddress(unfilteredUrl);
137 } catch (ParseException e) {
138 throw new URISyntaxException("", "");
139 }
140 if (address.mHost.length() == 0) {
141 throw new URISyntaxException("", "");
142 }
143 url = address.toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400145 } catch (URISyntaxException e) {
146 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
147 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 }
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 {
Patrick Scott3918d442009-08-04 13:22:29 -0400156 final ContentResolver cr = getContentResolver();
157 Bookmarks.addBookmark(null, cr, url, title, true);
158 if (mTouchIconUrl != null) {
159 final Cursor c =
160 BrowserBookmarksAdapter.queryBookmarksForUrl(
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400161 cr, null, url, true);
Patrick Scott59ce8302009-09-18 16:29:38 -0400162 new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl);
Patrick Scott3918d442009-08-04 13:22:29 -0400163 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 setResult(RESULT_OK);
165 }
166 } catch (IllegalStateException e) {
167 setTitle(r.getText(R.string.no_database));
168 return false;
169 }
170 return true;
171 }
172}