AddBookmarkPage.java revision f2407c682fafef77d568deba9987b193a5f2d361
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
17package com.android.browser;
18
19import android.app.Activity;
20import android.content.ContentResolver;
21import android.content.Intent;
22import android.content.res.Resources;
23import android.database.Cursor;
24import android.net.ParseException;
25import android.net.WebAddress;
26import android.os.Bundle;
27import android.provider.Browser;
28import android.view.View;
29import android.view.Window;
30import android.widget.EditText;
31import android.widget.TextView;
32import android.widget.Toast;
33
34import java.net.URI;
35import java.net.URISyntaxException;
36import 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;
48    private String      mTouchIconUrl;
49
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");
85            mTouchIconUrl = mMap.getString("touch_icon_url");
86        }
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;
128        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();
144            }
145        } catch (URISyntaxException e) {
146            mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
147            return false;
148        }
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 {
156                final ContentResolver cr = getContentResolver();
157                Bookmarks.addBookmark(null, cr, url, title, true);
158                if (mTouchIconUrl != null) {
159                    final Cursor c =
160                            BrowserBookmarksAdapter.queryBookmarksForUrl(
161                                    cr, null, url, true);
162                    new DownloadTouchIcon(cr, c, url)
163                            .execute(mTouchIconUrl);
164                }
165                setResult(RESULT_OK);
166            }
167        } catch (IllegalStateException e) {
168            setTitle(r.getText(R.string.no_database));
169            return false;
170        }
171        return true;
172    }
173}
174