blob: 87638d8744dd9f8171050433b748145a91b90890 [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
34import java.util.Date;
35
36public class AddBookmarkPage extends Activity {
37
38 private final String LOGTAG = "Bookmarks";
39
40 private EditText mTitle;
41 private EditText mAddress;
42 private TextView mButton;
43 private View mCancelButton;
44 private boolean mEditingExisting;
45 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040046 private String mTouchIconUrl;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047
48 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
49 public void onClick(View v) {
50 if (save()) {
51 finish();
52 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
53 Toast.LENGTH_LONG).show();
54 }
55 }
56 };
57
58 private View.OnClickListener mCancel = new View.OnClickListener() {
59 public void onClick(View v) {
60 finish();
61 }
62 };
63
64 protected void onCreate(Bundle icicle) {
65 super.onCreate(icicle);
66 requestWindowFeature(Window.FEATURE_LEFT_ICON);
67 setContentView(R.layout.browser_add_bookmark);
68 setTitle(R.string.save_to_bookmarks);
69 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark);
70
71 String title = null;
72 String url = null;
73 mMap = getIntent().getExtras();
74 if (mMap != null) {
75 Bundle b = mMap.getBundle("bookmark");
76 if (b != null) {
77 mMap = b;
78 mEditingExisting = true;
79 setTitle(R.string.edit_bookmark);
80 }
81 title = mMap.getString("title");
82 url = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -040083 mTouchIconUrl = mMap.getString("touch_icon_url");
The Android Open Source Project0c908882009-03-03 19:32:16 -080084 }
85
86 mTitle = (EditText) findViewById(R.id.title);
87 mTitle.setText(title);
88 mAddress = (EditText) findViewById(R.id.address);
89 mAddress.setText(url);
90
91
92 View.OnClickListener accept = mSaveBookmark;
93 mButton = (TextView) findViewById(R.id.OK);
94 mButton.setOnClickListener(accept);
95
96 mCancelButton = findViewById(R.id.cancel);
97 mCancelButton.setOnClickListener(mCancel);
98
99 if (!getWindow().getDecorView().isInTouchMode()) {
100 mButton.requestFocus();
101 }
102 }
103
104 /**
105 * Save the data to the database.
106 * Also, change the view to dialog stating
107 * that the webpage has been saved.
108 */
109 boolean save() {
110 String title = mTitle.getText().toString().trim();
111 String unfilteredUrl =
112 BrowserActivity.fixUrl(mAddress.getText().toString());
113 boolean emptyTitle = title.length() == 0;
114 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
115 Resources r = getResources();
116 if (emptyTitle || emptyUrl) {
117 if (emptyTitle) {
118 mTitle.setError(r.getText(R.string.bookmark_needs_title));
119 }
120 if (emptyUrl) {
121 mAddress.setError(r.getText(R.string.bookmark_needs_url));
122 }
123 return false;
124 }
125 String url = unfilteredUrl;
126 if (!(url.startsWith("about:") || url.startsWith("data:") || url
127 .startsWith("file:"))) {
128 WebAddress address;
129 try {
130 address = new WebAddress(unfilteredUrl);
131 } catch (ParseException e) {
132 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
133 return false;
134 }
135 if (address.mHost.length() == 0) {
136 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
137 return false;
138 }
139 url = address.toString();
140 }
141 try {
142 if (mEditingExisting) {
143 mMap.putString("title", title);
144 mMap.putString("url", url);
145 setResult(RESULT_OK, (new Intent()).setAction(
146 getIntent().toString()).putExtras(mMap));
147 } else {
Patrick Scott3918d442009-08-04 13:22:29 -0400148 final ContentResolver cr = getContentResolver();
149 Bookmarks.addBookmark(null, cr, url, title, true);
150 if (mTouchIconUrl != null) {
151 final Cursor c =
152 BrowserBookmarksAdapter.queryBookmarksForUrl(
Leon Scrogginsa5d669e2009-08-05 14:07:58 -0400153 cr, null, url, true);
Patrick Scott3918d442009-08-04 13:22:29 -0400154 new DownloadTouchIcon(cr, c, url)
155 .execute(mTouchIconUrl);
156 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800157 setResult(RESULT_OK);
158 }
159 } catch (IllegalStateException e) {
160 setTitle(r.getText(R.string.no_database));
161 return false;
162 }
163 return true;
164 }
165}