blob: 191659a907e685fb021020e74e33d51778c7e8fe [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.net.ParseException;
24import android.net.WebAddress;
25import android.os.Bundle;
26import android.provider.Browser;
27import android.view.View;
28import android.view.Window;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.widget.EditText;
30import android.widget.TextView;
31import android.widget.Toast;
32
33import java.util.Date;
34
35public class AddBookmarkPage extends Activity {
36
37 private final String LOGTAG = "Bookmarks";
38
39 private EditText mTitle;
40 private EditText mAddress;
41 private TextView mButton;
42 private View mCancelButton;
43 private boolean mEditingExisting;
44 private Bundle mMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045
46 private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
47 public void onClick(View v) {
48 if (save()) {
49 finish();
50 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
51 Toast.LENGTH_LONG).show();
52 }
53 }
54 };
55
56 private View.OnClickListener mCancel = new View.OnClickListener() {
57 public void onClick(View v) {
58 finish();
59 }
60 };
61
62 protected void onCreate(Bundle icicle) {
63 super.onCreate(icicle);
64 requestWindowFeature(Window.FEATURE_LEFT_ICON);
65 setContentView(R.layout.browser_add_bookmark);
66 setTitle(R.string.save_to_bookmarks);
67 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_dialog_bookmark);
68
69 String title = null;
70 String url = null;
71 mMap = getIntent().getExtras();
72 if (mMap != null) {
73 Bundle b = mMap.getBundle("bookmark");
74 if (b != null) {
75 mMap = b;
76 mEditingExisting = true;
77 setTitle(R.string.edit_bookmark);
78 }
79 title = mMap.getString("title");
80 url = mMap.getString("url");
81 }
82
83 mTitle = (EditText) findViewById(R.id.title);
84 mTitle.setText(title);
85 mAddress = (EditText) findViewById(R.id.address);
86 mAddress.setText(url);
87
88
89 View.OnClickListener accept = mSaveBookmark;
90 mButton = (TextView) findViewById(R.id.OK);
91 mButton.setOnClickListener(accept);
92
93 mCancelButton = findViewById(R.id.cancel);
94 mCancelButton.setOnClickListener(mCancel);
95
96 if (!getWindow().getDecorView().isInTouchMode()) {
97 mButton.requestFocus();
98 }
99 }
100
101 /**
102 * Save the data to the database.
103 * Also, change the view to dialog stating
104 * that the webpage has been saved.
105 */
106 boolean save() {
107 String title = mTitle.getText().toString().trim();
108 String unfilteredUrl =
109 BrowserActivity.fixUrl(mAddress.getText().toString());
110 boolean emptyTitle = title.length() == 0;
111 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
112 Resources r = getResources();
113 if (emptyTitle || emptyUrl) {
114 if (emptyTitle) {
115 mTitle.setError(r.getText(R.string.bookmark_needs_title));
116 }
117 if (emptyUrl) {
118 mAddress.setError(r.getText(R.string.bookmark_needs_url));
119 }
120 return false;
121 }
122 String url = unfilteredUrl;
123 if (!(url.startsWith("about:") || url.startsWith("data:") || url
124 .startsWith("file:"))) {
125 WebAddress address;
126 try {
127 address = new WebAddress(unfilteredUrl);
128 } catch (ParseException e) {
129 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
130 return false;
131 }
132 if (address.mHost.length() == 0) {
133 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
134 return false;
135 }
136 url = address.toString();
137 }
138 try {
139 if (mEditingExisting) {
140 mMap.putString("title", title);
141 mMap.putString("url", url);
142 setResult(RESULT_OK, (new Intent()).setAction(
143 getIntent().toString()).putExtras(mMap));
144 } else {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700145 Bookmarks.addBookmark(null, getContentResolver(), url, title, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 setResult(RESULT_OK);
147 }
148 } catch (IllegalStateException e) {
149 setTitle(r.getText(R.string.no_database));
150 return false;
151 }
152 return true;
153 }
154}