blob: c8251f92bc2e3aaadfdda5abb0bc5a8dbbe4be69 [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 III76a0e9c2010-10-05 16:10:01 -040048import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
51import android.widget.AdapterView;
52import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040054import android.widget.ListView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.TextView;
56import android.widget.Toast;
57
Cary Clarkf2407c62009-09-04 12:25:10 -040058import java.net.URI;
59import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040060import java.util.ArrayList;
Leon Scroggins III052ce662010-09-13 14:44:16 -040061
62public class AddBookmarkPage extends Activity
63 implements View.OnClickListener, TextView.OnEditorActionListener,
64 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
The Android Open Source Project0c908882009-03-03 19:32:16 -080065
66 private final String LOGTAG = "Bookmarks";
67
Leon Scroggins III052ce662010-09-13 14:44:16 -040068 // IDs for the CursorLoaders that are used.
69 private final int LOADER_ID_FOLDER_CONTENTS = 0;
70 private final int LOADER_ID_ALL_FOLDERS = 1;
71
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private EditText mTitle;
73 private EditText mAddress;
74 private TextView mButton;
75 private View mCancelButton;
76 private boolean mEditingExisting;
77 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040078 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010079 private Bitmap mThumbnail;
80 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040081 private TextView mFolder;
82 private View mDefaultView;
83 private View mFolderSelector;
84 private EditText mFolderNamer;
85 private View mAddNewFolder;
86 private long mCurrentFolder = 0;
87 private FolderAdapter mAdapter;
88 private ArrayList<Folder> mPaths;
89 private TextView mPath;
90
91 private static class Folder {
92 String Name;
93 long Id;
94 Folder(String name, long id) {
95 Name = name;
96 Id = id;
97 }
98 }
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
Ben Murdoch1794fe22009-09-29 18:14:30 +0100100 // Message IDs
101 private static final int SAVE_BOOKMARK = 100;
102
103 private Handler mHandler;
104
Leon Scroggins III052ce662010-09-13 14:44:16 -0400105 @Override
106 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
107 if (v == mFolderNamer) {
108 if (v.getText().length() > 0) {
109 if (actionId == EditorInfo.IME_NULL) {
110 // Only want to do this once.
111 if (event.getAction() == KeyEvent.ACTION_UP) {
Michael Kolb31829b92010-10-01 11:50:21 -0700112 completeFolderNaming();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400113 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400114 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115 }
Michael Kolb31829b92010-10-01 11:50:21 -0700116 // Steal the key press; otherwise a newline will be added
117 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 return false;
120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121
Leon Scroggins III052ce662010-09-13 14:44:16 -0400122 @Override
123 public void onClick(View v) {
124 if (v == mButton) {
125 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700126 // We are showing the folder selector.
127 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Michael Kolb31829b92010-10-01 11:50:21 -0700128 completeFolderNaming();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700129 } else {
130 // User has selected a folder. Go back to the opening page
131 mFolderSelector.setVisibility(View.GONE);
132 mDefaultView.setVisibility(View.VISIBLE);
133 setTitle(R.string.bookmark_this_page);
134 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400135 } else if (save()) {
136 finish();
137 }
138 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700139 if (mFolderNamer.getVisibility() == View.VISIBLE) {
140 mFolderNamer.setVisibility(View.GONE);
141 mAddNewFolder.setVisibility(View.VISIBLE);
142 } else {
143 finish();
144 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400145 } else if (v == mFolder) {
146 switchToFolderSelector();
147 } else if (v == mAddNewFolder) {
148 mFolderNamer.setVisibility(View.VISIBLE);
149 mFolderNamer.setText(R.string.new_folder);
150 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700151 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400152 InputMethodManager.getInstance(this).showSoftInput(mFolderNamer,
153 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400155 }
156
Michael Kolb31829b92010-10-01 11:50:21 -0700157 private void completeFolderNaming() {
158 if (!TextUtils.isEmpty(mFolderNamer.getText())) {
159 String name = mFolderNamer.getText().toString();
160 long id = addFolderToCurrent(mFolderNamer.getText().toString());
161 descendInto(name, id);
162 mFolderNamer.setVisibility(View.GONE);
163 mAddNewFolder.setVisibility(View.VISIBLE);
164 InputMethodManager.getInstance(this).hideSoftInputFromWindow(
165 mFolderNamer.getWindowToken(), 0);
166 }
167 }
168
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700169 private long addFolderToCurrent(String name) {
170 // Add the folder to the database
171 ContentValues values = new ContentValues();
172 values.put(BrowserContract.Bookmarks.TITLE,
173 name);
174 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
175 values.put(BrowserContract.Bookmarks.PARENT,
176 mCurrentFolder);
177 Uri uri = getContentResolver().insert(
178 BrowserContract.Bookmarks.CONTENT_URI, values);
179 if (uri != null) {
180 return ContentUris.parseId(uri);
181 } else {
182 return -1;
183 }
184 }
185
Leon Scroggins III052ce662010-09-13 14:44:16 -0400186 private void switchToFolderSelector() {
187 mDefaultView.setVisibility(View.GONE);
188 mFolderSelector.setVisibility(View.VISIBLE);
189 setTitle(R.string.containing_folder);
190 }
191
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700192 private void descendInto(String foldername, long id) {
193 if (id != -1) {
194 mCurrentFolder = id;
195 mPaths.add(new Folder(foldername, id));
196 updatePathString();
197 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
198 }
199 }
200
Leon Scroggins III052ce662010-09-13 14:44:16 -0400201 @Override
202 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
203 String[] projection;
204 switch (id) {
205 case LOADER_ID_ALL_FOLDERS:
206 projection = new String[] {
207 BrowserContract.Bookmarks._ID,
208 BrowserContract.Bookmarks.PARENT,
209 BrowserContract.Bookmarks.TITLE,
210 BrowserContract.Bookmarks.IS_FOLDER
211 };
212 return new CursorLoader(this,
213 BrowserContract.Bookmarks.CONTENT_URI,
214 projection,
215 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
216 null,
217 null);
218 case LOADER_ID_FOLDER_CONTENTS:
219 projection = new String[] {
220 BrowserContract.Bookmarks._ID,
221 BrowserContract.Bookmarks.TITLE,
222 BrowserContract.Bookmarks.IS_FOLDER
223 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400224 return new CursorLoader(this,
225 BrowserContract.Bookmarks.buildFolderUri(
226 mCurrentFolder),
227 projection,
228 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
229 null,
230 null);
231 default:
232 throw new AssertionError("Asking for nonexistant loader!");
233 }
234 }
235
236 @Override
237 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
238 switch (loader.getId()) {
239 case LOADER_ID_FOLDER_CONTENTS:
240 mAdapter.changeCursor(cursor);
241 break;
242 case LOADER_ID_ALL_FOLDERS:
243 long parent = mCurrentFolder;
244 int idIndex = cursor.getColumnIndexOrThrow(
245 BrowserContract.Bookmarks._ID);
246 int titleIndex = cursor.getColumnIndexOrThrow(
247 BrowserContract.Bookmarks.TITLE);
248 int parentIndex = cursor.getColumnIndexOrThrow(
249 BrowserContract.Bookmarks.PARENT);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700250 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
251 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400252 // First, find the folder corresponding to the current
253 // folder
254 if (!cursor.moveToFirst()) {
255 throw new AssertionError("No folders in the database!");
256 }
257 long folder;
258 do {
259 folder = cursor.getLong(idIndex);
260 } while (folder != parent && cursor.moveToNext());
261 if (cursor.isAfterLast()) {
262 throw new AssertionError("Folder(id=" + parent
263 + ") holding this bookmark does not exist!");
264 }
265 String name = cursor.getString(titleIndex);
266 mPaths.add(1, new Folder(name, parent));
267 parent = cursor.getLong(parentIndex);
268 }
269 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
270 updatePathString();
271 break;
272 default:
273 break;
274 }
275 }
276
277 /**
278 * Update the TextViews in both modes to display the full path of the
279 * current location to insert.
280 */
281 private void updatePathString() {
282 String path = mPaths.get(0).Name;
283 int size = mPaths.size();
284 for (int i = 1; i < size; i++) {
285 path += " / " + mPaths.get(i).Name;
286 }
287 mPath.setText(path);
288 mFolder.setText(path);
289 }
290
291 @Override
292 public void onItemClick(AdapterView<?> parent, View view, int position,
293 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400294 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400295 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400296 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400297 }
298
299 /**
300 * Shows a list of names of folders.
301 */
302 private class FolderAdapter extends CursorAdapter {
303 public FolderAdapter(Context context) {
304 super(context, null);
305 }
306
307 @Override
308 public void bindView(View view, Context context, Cursor cursor) {
309 ((TextView) view.findViewById(android.R.id.text1)).setText(
310 cursor.getString(cursor.getColumnIndexOrThrow(
311 BrowserContract.Bookmarks.TITLE)));
312 }
313
314 @Override
315 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700316 View view = LayoutInflater.from(context).inflate(
317 R.layout.folder_list_item, null);
318 view.setBackgroundDrawable(context.getResources().
319 getDrawable(android.R.drawable.list_selector_background));
320 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400321 }
322 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323
324 protected void onCreate(Bundle icicle) {
325 super.onCreate(icicle);
326 requestWindowFeature(Window.FEATURE_LEFT_ICON);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100327
328 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100329
Leon Scroggins III052ce662010-09-13 14:44:16 -0400330 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100331
Leon Scroggins III052ce662010-09-13 14:44:16 -0400332 setTitle(R.string.bookmark_this_page);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400333 Window window = getWindow();
334 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700335
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 String title = null;
337 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100338
The Android Open Source Project0c908882009-03-03 19:32:16 -0800339 if (mMap != null) {
340 Bundle b = mMap.getBundle("bookmark");
341 if (b != null) {
342 mMap = b;
343 mEditingExisting = true;
344 setTitle(R.string.edit_bookmark);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400345 } else {
346 int gravity = mMap.getInt("gravity", -1);
347 if (gravity != -1) {
348 WindowManager.LayoutParams l = window.getAttributes();
349 l.gravity = gravity;
350 window.setAttributes(l);
351 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 }
353 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100354 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400355 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100356 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Leon Scroggins25230d72010-09-28 20:09:25 -0400357 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, -1);
358 }
359 if (mCurrentFolder == -1) {
360 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 }
362
363 mTitle = (EditText) findViewById(R.id.title);
364 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100365
Leon Scroggins III052ce662010-09-13 14:44:16 -0400366 mAddress = (EditText) findViewById(R.id.address);
367 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400370 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371
372 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400373 mCancelButton.setOnClickListener(this);
374
375 mFolder = (TextView) findViewById(R.id.folder);
376 mFolder.setOnClickListener(this);
377
378 mDefaultView = findViewById(R.id.default_view);
379 mFolderSelector = findViewById(R.id.folder_selector);
380
381 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
382 mFolderNamer.setOnEditorActionListener(this);
383
384 mAddNewFolder = findViewById(R.id.add_new_folder);
385 mAddNewFolder.setOnClickListener(this);
386
387 mPath = (TextView) findViewById(R.id.path);
388 ListView list = (ListView) findViewById(R.id.list);
389
390 mPaths = new ArrayList<Folder>();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700391 mPaths.add(0, new Folder(getString(R.string.bookmarks), BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400392 mAdapter = new FolderAdapter(this);
393 list.setAdapter(mAdapter);
394 list.setOnItemClickListener(this);
395 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500396 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400397 // Find all the folders
398 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
399 }
400 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
401
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700402
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400403 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800404 mButton.requestFocus();
405 }
406 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100407
Leon Scroggins25230d72010-09-28 20:09:25 -0400408 // FIXME: Use a CursorLoader
409 private long getBookmarksBarId(Context context) {
410 SharedPreferences prefs
411 = PreferenceManager.getDefaultSharedPreferences(context);
412 String accountName =
413 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
414 String accountType =
415 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
416 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500417 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400418 }
419 Cursor cursor = null;
420 try {
421 cursor = context.getContentResolver().query(
422 BrowserContract.Bookmarks.CONTENT_URI,
423 new String[] { BrowserContract.Bookmarks._ID },
424 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
425 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
426 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
427 new String[] {
428 BrowserContract.ChromeSyncColumns
429 .FOLDER_NAME_BOOKMARKS_BAR,
430 accountName,
431 accountType },
432 null);
433 if (cursor != null && cursor.moveToFirst()) {
434 return cursor.getLong(0);
435 }
436 } finally {
437 if (cursor != null) cursor.close();
438 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500439 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400440 }
441
Leon Scroggins III052ce662010-09-13 14:44:16 -0400442 @Override
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700443 public boolean dispatchKeyEvent(KeyEvent event) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400444 if (mFolderSelector.getVisibility() == View.VISIBLE
445 && KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
446 if (KeyEvent.ACTION_UP == event.getAction()) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700447 if (mFolderNamer.getVisibility() == View.VISIBLE) {
448 mFolderNamer.setVisibility(View.GONE);
449 mAddNewFolder.setVisibility(View.VISIBLE);
450 InputMethodManager.getInstance(this).hideSoftInputFromWindow(
451 mFolderNamer.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400452 } else {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700453 int size = mPaths.size();
454 if (1 == size) {
455 // We have reached the top level
456 finish();
457 } else {
458 // Go up a level
459 mPaths.remove(size - 1);
460 mCurrentFolder = mPaths.get(size - 2).Id;
461 updatePathString();
462 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
463 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400464 }
465 }
466 return true;
467 }
468 return super.dispatchKeyEvent(event);
469 }
470
Leon Scroggins02065b02010-01-04 14:30:13 -0500471 /**
472 * Runnable to save a bookmark, so it can be performed in its own thread.
473 */
474 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400475 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500476 private Message mMessage;
477 public SaveBookmarkRunnable(Message msg) {
478 mMessage = msg;
479 }
480 public void run() {
481 // Unbundle bookmark data.
482 Bundle bundle = mMessage.getData();
483 String title = bundle.getString("title");
484 String url = bundle.getString("url");
485 boolean invalidateThumbnail = bundle.getBoolean(
486 "invalidateThumbnail");
487 Bitmap thumbnail = invalidateThumbnail ? null
488 : (Bitmap) bundle.getParcelable("thumbnail");
489 String touchIconUrl = bundle.getString("touchIconUrl");
490
491 // Save to the bookmarks DB.
492 try {
493 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400494 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
495 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500496 if (touchIconUrl != null) {
Ben Murdochccb5de02010-07-19 18:38:17 +0100497 new DownloadTouchIcon(AddBookmarkPage.this, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500498 }
499 mMessage.arg1 = 1;
500 } catch (IllegalStateException e) {
501 mMessage.arg1 = 0;
502 }
503 mMessage.sendToTarget();
504 }
505 }
506
Ben Murdoch1794fe22009-09-29 18:14:30 +0100507 private void createHandler() {
508 if (mHandler == null) {
509 mHandler = new Handler() {
510 @Override
511 public void handleMessage(Message msg) {
512 switch (msg.what) {
513 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500514 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100515 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
516 Toast.LENGTH_LONG).show();
517 } else {
518 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
519 Toast.LENGTH_LONG).show();
520 }
521 break;
522 }
523 }
524 };
525 }
526 }
527
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100529 * 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 -0800530 */
531 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100532 createHandler();
533
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100535 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400536 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100537
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 boolean emptyTitle = title.length() == 0;
539 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
540 Resources r = getResources();
541 if (emptyTitle || emptyUrl) {
542 if (emptyTitle) {
543 mTitle.setError(r.getText(R.string.bookmark_needs_title));
544 }
545 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400546 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400548 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100549
The Android Open Source Project0c908882009-03-03 19:32:16 -0800550 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000551 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400552 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000553 // We allow bookmarks with a javascript: scheme, but these will in most cases
554 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
555
556 if (!url.toLowerCase().startsWith("javascript:")) {
557 URI uriObj = new URI(url);
558 String scheme = uriObj.getScheme();
559 if (!Bookmarks.urlHasAcceptableScheme(url)) {
560 // If the scheme was non-null, let the user know that we
561 // can't save their bookmark. If it was null, we'll assume
562 // they meant http when we parse it in the WebAddress class.
563 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400564 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000565 return false;
566 }
567 WebAddress address;
568 try {
569 address = new WebAddress(unfilteredUrl);
570 } catch (ParseException e) {
571 throw new URISyntaxException("", "");
572 }
573 if (address.mHost.length() == 0) {
574 throw new URISyntaxException("", "");
575 }
576 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100577 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800578 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400579 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400580 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400581 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100583
Ben Murdoch1794fe22009-09-29 18:14:30 +0100584 if (mEditingExisting) {
585 mMap.putString("title", title);
586 mMap.putString("url", url);
587 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400588 // FIXME: This does not work yet
589 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100590 setResult(RESULT_OK, (new Intent()).setAction(
591 getIntent().toString()).putExtras(mMap));
592 } else {
593 // Post a message to write to the DB.
594 Bundle bundle = new Bundle();
595 bundle.putString("title", title);
596 bundle.putString("url", url);
597 bundle.putParcelable("thumbnail", mThumbnail);
598 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
599 bundle.putString("touchIconUrl", mTouchIconUrl);
600 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
601 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500602 // Start a new thread so as to not slow down the UI
603 Thread t = new Thread(new SaveBookmarkRunnable(msg));
604 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100605 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000606 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 }
608 return true;
609 }
Michael Kolb31829b92010-10-01 11:50:21 -0700610
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611}