PageRenderTime 31ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/WebVox/src/com/marvin/webvox/AddBookmarkPage.java

http://eyes-free.googlecode.com/
Java | 229 lines | 171 code | 23 blank | 35 comment | 26 complexity | 05965ad1f2838cd491e0de134f208905 MD5 | raw file
  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.marvin.webvox;
 18
 19import com.marvin.webvox.R;
 20
 21import android.app.Activity;
 22import android.content.ContentResolver;
 23import android.content.Intent;
 24import android.content.res.Resources;
 25import android.database.Cursor;
 26import android.graphics.Bitmap;
 27import android.net.ParseException;
 28//import android.net.WebAddress;
 29import android.os.Bundle;
 30import android.os.Handler;
 31import android.os.Message;
 32import android.provider.Browser;
 33import android.view.View;
 34import android.view.Window;
 35import android.widget.EditText;
 36import android.widget.TextView;
 37import android.widget.Toast;
 38
 39import java.net.URI;
 40import java.net.URISyntaxException;
 41import java.util.Date;
 42
 43public class AddBookmarkPage extends Activity {
 44
 45    private final String LOGTAG = "Bookmarks";
 46
 47    private EditText    mTitle;
 48    private EditText    mAddress;
 49    private TextView    mButton;
 50    private View        mCancelButton;
 51    private boolean     mEditingExisting;
 52    private Bundle      mMap;
 53    private String      mTouchIconUrl;
 54    private Bitmap      mThumbnail;
 55    private String      mOriginalUrl;
 56
 57    // Message IDs
 58    private static final int SAVE_BOOKMARK = 100;
 59
 60    private Handler mHandler;
 61
 62    private View.OnClickListener mSaveBookmark = new View.OnClickListener() {
 63        public void onClick(View v) {
 64            if (save()) {
 65                finish();
 66            }
 67        }
 68    };
 69
 70    private View.OnClickListener mCancel = new View.OnClickListener() {
 71        public void onClick(View v) {
 72            finish();
 73        }
 74    };
 75
 76    protected void onCreate(Bundle icicle) {
 77        super.onCreate(icicle);
 78        requestWindowFeature(Window.FEATURE_LEFT_ICON);
 79        setContentView(R.layout.browser_add_bookmark);
 80        setTitle(R.string.save_to_bookmarks);
 81        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
 82        
 83        String title = null;
 84        String url = null;
 85        mMap = getIntent().getExtras();
 86        if (mMap != null) {
 87            Bundle b = mMap.getBundle("bookmark");
 88            if (b != null) {
 89                mMap = b;
 90                mEditingExisting = true;
 91                setTitle(R.string.edit_bookmark);
 92            }
 93            title = mMap.getString("title");
 94            url = mOriginalUrl = mMap.getString("url");
 95            mTouchIconUrl = mMap.getString("touch_icon_url");
 96            mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
 97        }
 98
 99        mTitle = (EditText) findViewById(R.id.title);
100        mTitle.setText(title);
101        mAddress = (EditText) findViewById(R.id.address);
102        mAddress.setText(url);
103
104        View.OnClickListener accept = mSaveBookmark;
105        mButton = (TextView) findViewById(R.id.OK);
106        mButton.setOnClickListener(accept);
107
108        mCancelButton = findViewById(R.id.cancel);
109        mCancelButton.setOnClickListener(mCancel);
110        
111        if (!getWindow().getDecorView().isInTouchMode()) {
112            mButton.requestFocus();
113        }
114    }
115
116    private void createHandler() {
117        if (mHandler == null) {
118            mHandler = new Handler() {
119                @Override
120                public void handleMessage(Message msg) {
121                    switch (msg.what) {
122                        case SAVE_BOOKMARK:
123                            // Unbundle bookmark data.
124                            Bundle bundle = msg.getData();
125                            String title = bundle.getString("title");
126                            String url = bundle.getString("url");
127                            boolean invalidateThumbnail = bundle.getBoolean("invalidateThumbnail");
128                            Bitmap thumbnail = invalidateThumbnail
129                                    ? null : (Bitmap) bundle.getParcelable("thumbnail");
130                            String touchIconUrl = bundle.getString("touchIconUrl");
131
132                            // Save to the bookmarks DB.
133                            if (updateBookmarksDB(title, url, thumbnail, touchIconUrl)) {
134                                Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
135                                        Toast.LENGTH_LONG).show();
136                            } else {
137                                Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
138                                        Toast.LENGTH_LONG).show();
139                            }
140                            break;
141                    }
142                }
143            };
144        }
145    }
146
147    private boolean updateBookmarksDB(String title, String url, Bitmap thumbnail, String touchIconUrl) {
148        try {
149            final ContentResolver cr = getContentResolver();
150            Bookmarks.addBookmark(null, cr, url, title, thumbnail, true);
151            if (touchIconUrl != null) {
152                final Cursor c =
153                        BrowserBookmarksAdapter.queryBookmarksForUrl(cr, null, url, true);
154                new DownloadTouchIcon(cr, c, url).execute(mTouchIconUrl);
155            }
156        } catch (IllegalStateException e) {
157            return false;
158        }
159        return true;
160    }
161
162    /**
163     * Parse the data entered in the dialog and post a message to update the bookmarks database.
164     */
165    boolean save() {
166        createHandler();
167
168        String title = mTitle.getText().toString().trim();
169        String unfilteredUrl = 
170                BrowserActivity.fixUrl(mAddress.getText().toString());
171        boolean emptyTitle = title.length() == 0;
172        boolean emptyUrl = unfilteredUrl.trim().length() == 0;
173        Resources r = getResources();
174        if (emptyTitle || emptyUrl) {
175            if (emptyTitle) {
176                mTitle.setError(r.getText(R.string.bookmark_needs_title));
177            }
178            if (emptyUrl) {
179                mAddress.setError(r.getText(R.string.bookmark_needs_url));
180            }
181            return false;
182        }
183        String url = unfilteredUrl;
184        try {
185            URI uriObj = new URI(url);
186            String scheme = uriObj.getScheme();
187            if (!("about".equals(scheme) || "data".equals(scheme)
188                    || "javascript".equals(scheme)
189                    || "file".equals(scheme) || "content".equals(scheme))) {
190/*                
191                WebAddress address;
192                try {
193                    address = new WebAddress(unfilteredUrl);
194                } catch (ParseException e) {
195                    throw new URISyntaxException("", "");
196                }
197                if (address.mHost.length() == 0) {
198                    throw new URISyntaxException("", "");
199                }
200                url = address.toString();
201*/                
202            }
203        } catch (URISyntaxException e) {
204            mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
205            return false;
206        }
207
208        if (mEditingExisting) {
209            mMap.putString("title", title);
210            mMap.putString("url", url);
211            mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
212            setResult(RESULT_OK, (new Intent()).setAction(
213                    getIntent().toString()).putExtras(mMap));
214        } else {
215            // Post a message to write to the DB.
216            Bundle bundle = new Bundle();
217            bundle.putString("title", title);
218            bundle.putString("url", url);
219            bundle.putParcelable("thumbnail", mThumbnail);
220            bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
221            bundle.putString("touchIconUrl", mTouchIconUrl);
222            Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
223            msg.setData(bundle);
224            mHandler.sendMessage(msg);
225            setResult(RESULT_OK);
226        }
227        return true;
228    }
229}