blob: 907021822cbea4688877f76294779ce235c36eac [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
Leon Scroggins25230d72010-09-28 20:09:25 -040019import com.android.browser.provider.BrowserProvider2;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040022import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070024import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040025import android.content.ContentValues;
26import android.content.Context;
27import android.content.CursorLoader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.Intent;
Leon Scroggins III052ce662010-09-13 14:44:16 -040029import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040030import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040032import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010033import android.graphics.Bitmap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070035import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.WebAddress;
37import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010038import android.os.Handler;
39import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040040import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040041import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040042import android.text.TextUtils;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.view.KeyEvent;
44import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040046import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.Window;
Leon Scroggins III052ce662010-09-13 14:44:16 -040048import android.view.inputmethod.EditorInfo;
49import android.view.inputmethod.InputMethodManager;
50import android.widget.AdapterView;
51import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040053import android.widget.ListView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.widget.TextView;
55import android.widget.Toast;
56
Cary Clarkf2407c62009-09-04 12:25:10 -040057import java.net.URI;
58import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040059import java.util.ArrayList;
Leon Scroggins III052ce662010-09-13 14:44:16 -040060
61public class AddBookmarkPage extends Activity
62 implements View.OnClickListener, TextView.OnEditorActionListener,
63 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
The Android Open Source Project0c908882009-03-03 19:32:16 -080064
65 private final String LOGTAG = "Bookmarks";
66
Leon Scroggins III052ce662010-09-13 14:44:16 -040067 // IDs for the CursorLoaders that are used.
68 private final int LOADER_ID_FOLDER_CONTENTS = 0;
69 private final int LOADER_ID_ALL_FOLDERS = 1;
70
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 private EditText mTitle;
72 private EditText mAddress;
73 private TextView mButton;
74 private View mCancelButton;
75 private boolean mEditingExisting;
76 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040077 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010078 private Bitmap mThumbnail;
79 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040080 private TextView mFolder;
81 private View mDefaultView;
82 private View mFolderSelector;
83 private EditText mFolderNamer;
84 private View mAddNewFolder;
85 private long mCurrentFolder = 0;
86 private FolderAdapter mAdapter;
87 private ArrayList<Folder> mPaths;
88 private TextView mPath;
89
90 private static class Folder {
91 String Name;
92 long Id;
93 Folder(String name, long id) {
94 Name = name;
95 Id = id;
96 }
97 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080098
Ben Murdoch1794fe22009-09-29 18:14:30 +010099 // Message IDs
100 private static final int SAVE_BOOKMARK = 100;
101
102 private Handler mHandler;
103
Leon Scroggins III052ce662010-09-13 14:44:16 -0400104 @Override
105 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
106 if (v == mFolderNamer) {
107 if (v.getText().length() > 0) {
108 if (actionId == EditorInfo.IME_NULL) {
109 // Only want to do this once.
110 if (event.getAction() == KeyEvent.ACTION_UP) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700111 String name = v.getText().toString();
112 long id = addFolderToCurrent(name);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400113 mFolderNamer.setVisibility(View.GONE);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700114 mAddNewFolder.setVisibility(View.VISIBLE);
115 descendInto(name,id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400116 InputMethodManager.getInstance(this)
117 .hideSoftInputFromWindow(
118 mFolderNamer.getWindowToken(), 0);
119 }
120 // Steal the key press for both up and down
121 return true;
122 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 }
124 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400125 return false;
126 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
Leon Scroggins III052ce662010-09-13 14:44:16 -0400128 @Override
129 public void onClick(View v) {
130 if (v == mButton) {
131 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700132 // We are showing the folder selector.
133 if (mFolderNamer.getVisibility() == View.VISIBLE) {
134 // Editing folder name
135 String name = mFolderNamer.getText().toString();
136 long id = addFolderToCurrent(mFolderNamer.getText().toString());
137 descendInto(name, id);
138 mFolderNamer.setVisibility(View.GONE);
139 mAddNewFolder.setVisibility(View.VISIBLE);
140 } else {
141 // User has selected a folder. Go back to the opening page
142 mFolderSelector.setVisibility(View.GONE);
143 mDefaultView.setVisibility(View.VISIBLE);
144 setTitle(R.string.bookmark_this_page);
145 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400146 } else if (save()) {
147 finish();
148 }
149 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700150 if (mFolderNamer.getVisibility() == View.VISIBLE) {
151 mFolderNamer.setVisibility(View.GONE);
152 mAddNewFolder.setVisibility(View.VISIBLE);
153 } else {
154 finish();
155 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400156 } else if (v == mFolder) {
157 switchToFolderSelector();
158 } else if (v == mAddNewFolder) {
159 mFolderNamer.setVisibility(View.VISIBLE);
160 mFolderNamer.setText(R.string.new_folder);
161 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700162 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400163 InputMethodManager.getInstance(this).showSoftInput(mFolderNamer,
164 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400166 }
167
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700168 private long addFolderToCurrent(String name) {
169 // Add the folder to the database
170 ContentValues values = new ContentValues();
171 values.put(BrowserContract.Bookmarks.TITLE,
172 name);
173 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
174 values.put(BrowserContract.Bookmarks.PARENT,
175 mCurrentFolder);
176 Uri uri = getContentResolver().insert(
177 BrowserContract.Bookmarks.CONTENT_URI, values);
178 if (uri != null) {
179 return ContentUris.parseId(uri);
180 } else {
181 return -1;
182 }
183 }
184
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 private void switchToFolderSelector() {
186 mDefaultView.setVisibility(View.GONE);
187 mFolderSelector.setVisibility(View.VISIBLE);
188 setTitle(R.string.containing_folder);
189 }
190
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700191 private void descendInto(String foldername, long id) {
192 if (id != -1) {
193 mCurrentFolder = id;
194 mPaths.add(new Folder(foldername, id));
195 updatePathString();
196 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
197 }
198 }
199
Leon Scroggins III052ce662010-09-13 14:44:16 -0400200 @Override
201 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
202 String[] projection;
203 switch (id) {
204 case LOADER_ID_ALL_FOLDERS:
205 projection = new String[] {
206 BrowserContract.Bookmarks._ID,
207 BrowserContract.Bookmarks.PARENT,
208 BrowserContract.Bookmarks.TITLE,
209 BrowserContract.Bookmarks.IS_FOLDER
210 };
211 return new CursorLoader(this,
212 BrowserContract.Bookmarks.CONTENT_URI,
213 projection,
214 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
215 null,
216 null);
217 case LOADER_ID_FOLDER_CONTENTS:
218 projection = new String[] {
219 BrowserContract.Bookmarks._ID,
220 BrowserContract.Bookmarks.TITLE,
221 BrowserContract.Bookmarks.IS_FOLDER
222 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400223 return new CursorLoader(this,
224 BrowserContract.Bookmarks.buildFolderUri(
225 mCurrentFolder),
226 projection,
227 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
228 null,
229 null);
230 default:
231 throw new AssertionError("Asking for nonexistant loader!");
232 }
233 }
234
235 @Override
236 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
237 switch (loader.getId()) {
238 case LOADER_ID_FOLDER_CONTENTS:
239 mAdapter.changeCursor(cursor);
240 break;
241 case LOADER_ID_ALL_FOLDERS:
242 long parent = mCurrentFolder;
243 int idIndex = cursor.getColumnIndexOrThrow(
244 BrowserContract.Bookmarks._ID);
245 int titleIndex = cursor.getColumnIndexOrThrow(
246 BrowserContract.Bookmarks.TITLE);
247 int parentIndex = cursor.getColumnIndexOrThrow(
248 BrowserContract.Bookmarks.PARENT);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700249 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
250 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400251 // First, find the folder corresponding to the current
252 // folder
253 if (!cursor.moveToFirst()) {
254 throw new AssertionError("No folders in the database!");
255 }
256 long folder;
257 do {
258 folder = cursor.getLong(idIndex);
259 } while (folder != parent && cursor.moveToNext());
260 if (cursor.isAfterLast()) {
261 throw new AssertionError("Folder(id=" + parent
262 + ") holding this bookmark does not exist!");
263 }
264 String name = cursor.getString(titleIndex);
265 mPaths.add(1, new Folder(name, parent));
266 parent = cursor.getLong(parentIndex);
267 }
268 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
269 updatePathString();
270 break;
271 default:
272 break;
273 }
274 }
275
276 /**
277 * Update the TextViews in both modes to display the full path of the
278 * current location to insert.
279 */
280 private void updatePathString() {
281 String path = mPaths.get(0).Name;
282 int size = mPaths.size();
283 for (int i = 1; i < size; i++) {
284 path += " / " + mPaths.get(i).Name;
285 }
286 mPath.setText(path);
287 mFolder.setText(path);
288 }
289
290 @Override
291 public void onItemClick(AdapterView<?> parent, View view, int position,
292 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400293 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400294 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400295 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400296 }
297
298 /**
299 * Shows a list of names of folders.
300 */
301 private class FolderAdapter extends CursorAdapter {
302 public FolderAdapter(Context context) {
303 super(context, null);
304 }
305
306 @Override
307 public void bindView(View view, Context context, Cursor cursor) {
308 ((TextView) view.findViewById(android.R.id.text1)).setText(
309 cursor.getString(cursor.getColumnIndexOrThrow(
310 BrowserContract.Bookmarks.TITLE)));
311 }
312
313 @Override
314 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700315 View view = LayoutInflater.from(context).inflate(
316 R.layout.folder_list_item, null);
317 view.setBackgroundDrawable(context.getResources().
318 getDrawable(android.R.drawable.list_selector_background));
319 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400320 }
321 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322
323 protected void onCreate(Bundle icicle) {
324 super.onCreate(icicle);
325 requestWindowFeature(Window.FEATURE_LEFT_ICON);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100326
327 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100328
Leon Scroggins III052ce662010-09-13 14:44:16 -0400329 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100330
Leon Scroggins III052ce662010-09-13 14:44:16 -0400331 setTitle(R.string.bookmark_this_page);
Ben Murdocha753d002009-10-01 11:36:19 +0100332 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700333
The Android Open Source Project0c908882009-03-03 19:32:16 -0800334 String title = null;
335 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100336
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 if (mMap != null) {
338 Bundle b = mMap.getBundle("bookmark");
339 if (b != null) {
340 mMap = b;
341 mEditingExisting = true;
342 setTitle(R.string.edit_bookmark);
343 }
344 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100345 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400346 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100347 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Leon Scroggins25230d72010-09-28 20:09:25 -0400348 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, -1);
349 }
350 if (mCurrentFolder == -1) {
351 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 }
353
354 mTitle = (EditText) findViewById(R.id.title);
355 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100356
Leon Scroggins III052ce662010-09-13 14:44:16 -0400357 mAddress = (EditText) findViewById(R.id.address);
358 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400361 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362
363 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400364 mCancelButton.setOnClickListener(this);
365
366 mFolder = (TextView) findViewById(R.id.folder);
367 mFolder.setOnClickListener(this);
368
369 mDefaultView = findViewById(R.id.default_view);
370 mFolderSelector = findViewById(R.id.folder_selector);
371
372 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
373 mFolderNamer.setOnEditorActionListener(this);
374
375 mAddNewFolder = findViewById(R.id.add_new_folder);
376 mAddNewFolder.setOnClickListener(this);
377
378 mPath = (TextView) findViewById(R.id.path);
379 ListView list = (ListView) findViewById(R.id.list);
380
381 mPaths = new ArrayList<Folder>();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700382 mPaths.add(0, new Folder(getString(R.string.bookmarks), BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400383 mAdapter = new FolderAdapter(this);
384 list.setAdapter(mAdapter);
385 list.setOnItemClickListener(this);
386 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500387 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400388 // Find all the folders
389 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
390 }
391 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
392
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700393
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 if (!getWindow().getDecorView().isInTouchMode()) {
395 mButton.requestFocus();
396 }
397 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100398
Leon Scroggins25230d72010-09-28 20:09:25 -0400399 // FIXME: Use a CursorLoader
400 private long getBookmarksBarId(Context context) {
401 SharedPreferences prefs
402 = PreferenceManager.getDefaultSharedPreferences(context);
403 String accountName =
404 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
405 String accountType =
406 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
407 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500408 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400409 }
410 Cursor cursor = null;
411 try {
412 cursor = context.getContentResolver().query(
413 BrowserContract.Bookmarks.CONTENT_URI,
414 new String[] { BrowserContract.Bookmarks._ID },
415 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
416 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
417 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
418 new String[] {
419 BrowserContract.ChromeSyncColumns
420 .FOLDER_NAME_BOOKMARKS_BAR,
421 accountName,
422 accountType },
423 null);
424 if (cursor != null && cursor.moveToFirst()) {
425 return cursor.getLong(0);
426 }
427 } finally {
428 if (cursor != null) cursor.close();
429 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500430 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400431 }
432
Leon Scroggins III052ce662010-09-13 14:44:16 -0400433 @Override
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700434 public boolean dispatchKeyEvent(KeyEvent event) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400435 if (mFolderSelector.getVisibility() == View.VISIBLE
436 && KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
437 if (KeyEvent.ACTION_UP == event.getAction()) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700438 if (mFolderNamer.getVisibility() == View.VISIBLE) {
439 mFolderNamer.setVisibility(View.GONE);
440 mAddNewFolder.setVisibility(View.VISIBLE);
441 InputMethodManager.getInstance(this).hideSoftInputFromWindow(
442 mFolderNamer.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400443 } else {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700444 int size = mPaths.size();
445 if (1 == size) {
446 // We have reached the top level
447 finish();
448 } else {
449 // Go up a level
450 mPaths.remove(size - 1);
451 mCurrentFolder = mPaths.get(size - 2).Id;
452 updatePathString();
453 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
454 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400455 }
456 }
457 return true;
458 }
459 return super.dispatchKeyEvent(event);
460 }
461
Leon Scroggins02065b02010-01-04 14:30:13 -0500462 /**
463 * Runnable to save a bookmark, so it can be performed in its own thread.
464 */
465 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400466 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500467 private Message mMessage;
468 public SaveBookmarkRunnable(Message msg) {
469 mMessage = msg;
470 }
471 public void run() {
472 // Unbundle bookmark data.
473 Bundle bundle = mMessage.getData();
474 String title = bundle.getString("title");
475 String url = bundle.getString("url");
476 boolean invalidateThumbnail = bundle.getBoolean(
477 "invalidateThumbnail");
478 Bitmap thumbnail = invalidateThumbnail ? null
479 : (Bitmap) bundle.getParcelable("thumbnail");
480 String touchIconUrl = bundle.getString("touchIconUrl");
481
482 // Save to the bookmarks DB.
483 try {
484 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400485 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
486 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500487 if (touchIconUrl != null) {
Ben Murdochccb5de02010-07-19 18:38:17 +0100488 new DownloadTouchIcon(AddBookmarkPage.this, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500489 }
490 mMessage.arg1 = 1;
491 } catch (IllegalStateException e) {
492 mMessage.arg1 = 0;
493 }
494 mMessage.sendToTarget();
495 }
496 }
497
Ben Murdoch1794fe22009-09-29 18:14:30 +0100498 private void createHandler() {
499 if (mHandler == null) {
500 mHandler = new Handler() {
501 @Override
502 public void handleMessage(Message msg) {
503 switch (msg.what) {
504 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500505 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100506 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
507 Toast.LENGTH_LONG).show();
508 } else {
509 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
510 Toast.LENGTH_LONG).show();
511 }
512 break;
513 }
514 }
515 };
516 }
517 }
518
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100520 * Parse the data entered in the dialog and post a message to update the bookmarks database.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 */
522 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100523 createHandler();
524
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100526 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400527 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100528
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 boolean emptyTitle = title.length() == 0;
530 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
531 Resources r = getResources();
532 if (emptyTitle || emptyUrl) {
533 if (emptyTitle) {
534 mTitle.setError(r.getText(R.string.bookmark_needs_title));
535 }
536 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400537 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400539 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100540
The Android Open Source Project0c908882009-03-03 19:32:16 -0800541 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000542 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400543 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000544 // We allow bookmarks with a javascript: scheme, but these will in most cases
545 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
546
547 if (!url.toLowerCase().startsWith("javascript:")) {
548 URI uriObj = new URI(url);
549 String scheme = uriObj.getScheme();
550 if (!Bookmarks.urlHasAcceptableScheme(url)) {
551 // If the scheme was non-null, let the user know that we
552 // can't save their bookmark. If it was null, we'll assume
553 // they meant http when we parse it in the WebAddress class.
554 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400555 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000556 return false;
557 }
558 WebAddress address;
559 try {
560 address = new WebAddress(unfilteredUrl);
561 } catch (ParseException e) {
562 throw new URISyntaxException("", "");
563 }
564 if (address.mHost.length() == 0) {
565 throw new URISyntaxException("", "");
566 }
567 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100568 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400570 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400571 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400572 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100574
Ben Murdoch1794fe22009-09-29 18:14:30 +0100575 if (mEditingExisting) {
576 mMap.putString("title", title);
577 mMap.putString("url", url);
578 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400579 // FIXME: This does not work yet
580 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100581 setResult(RESULT_OK, (new Intent()).setAction(
582 getIntent().toString()).putExtras(mMap));
583 } else {
584 // Post a message to write to the DB.
585 Bundle bundle = new Bundle();
586 bundle.putString("title", title);
587 bundle.putString("url", url);
588 bundle.putParcelable("thumbnail", mThumbnail);
589 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
590 bundle.putString("touchIconUrl", mTouchIconUrl);
591 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
592 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500593 // Start a new thread so as to not slow down the UI
594 Thread t = new Thread(new SaveBookmarkRunnable(msg));
595 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100596 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000597 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 }
599 return true;
600 }
601}