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