blob: ecd038f0377af3743a605807d0f1839cda7107b4 [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;
Leon Scroggins III052ce662010-09-13 14:44:16 -040028import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040029import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040031import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010032import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040033import android.graphics.drawable.Drawable;
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;
John Reckc8490812010-11-22 14:15:36 -080037import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010039import android.os.Handler;
40import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040041import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040042import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040043import android.text.TextUtils;
Leon Scroggins III052ce662010-09-13 14:44:16 -040044import android.view.KeyEvent;
45import android.view.LayoutInflater;
Leon Scroggins88d08032010-10-21 15:17:10 -040046import android.view.MenuItem;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040048import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040050import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040051import android.view.inputmethod.EditorInfo;
52import android.view.inputmethod.InputMethodManager;
53import android.widget.AdapterView;
54import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040056import android.widget.ListView;
Leon Scroggins88d08032010-10-21 15:17:10 -040057import android.widget.PopupMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.widget.TextView;
59import android.widget.Toast;
60
Cary Clarkf2407c62009-09-04 12:25:10 -040061import java.net.URI;
62import java.net.URISyntaxException;
Leon Scroggins74dbe012010-10-15 10:54:27 -040063import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040064
65public class AddBookmarkPage extends Activity
66 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040067 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins88d08032010-10-21 15:17:10 -040068 BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080069
Michael Kolb370a4f32010-10-06 10:45:32 -070070 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040071 public static final String TOUCH_ICON_URL = "touch_icon_url";
72 // Place on an edited bookmark to remove the saved thumbnail
73 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
74 public static final String USER_AGENT = "user_agent";
Michael Kolb370a4f32010-10-06 10:45:32 -070075
John Reckc8490812010-11-22 14:15:36 -080076 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
77 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
78
Leon Scroggins74dbe012010-10-15 10:54:27 -040079 private static final int MAX_CRUMBS_SHOWN = 2;
80
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 private final String LOGTAG = "Bookmarks";
82
Leon Scroggins III052ce662010-09-13 14:44:16 -040083 // IDs for the CursorLoaders that are used.
84 private final int LOADER_ID_FOLDER_CONTENTS = 0;
85 private final int LOADER_ID_ALL_FOLDERS = 1;
86
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 private EditText mTitle;
88 private EditText mAddress;
89 private TextView mButton;
90 private View mCancelButton;
91 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080092 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040094 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010095 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040096 private TextView mFolder;
97 private View mDefaultView;
98 private View mFolderSelector;
99 private EditText mFolderNamer;
100 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400101 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400102 private long mCurrentFolder = 0;
103 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400104 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400105 private TextView mFakeTitle;
106 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400107 private ListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400108 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400109 private long mRootFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400110
111 private static class Folder {
112 String Name;
113 long Id;
114 Folder(String name, long id) {
115 Name = name;
116 Id = id;
117 }
118 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119
Ben Murdoch1794fe22009-09-29 18:14:30 +0100120 // Message IDs
121 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400122 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100123
124 private Handler mHandler;
125
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100126 private InputMethodManager getInputMethodManager() {
127 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
128 }
129
Leon Scroggins8baaa632010-12-08 19:46:53 -0500130 private Uri getUriForFolder(long folder) {
131 Uri uri;
132 if (folder == mRootFolder) {
133 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
134 } else {
135 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
136 }
137 String[] accountInfo = getAccountNameAndType(this);
138 if (accountInfo != null) {
139 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
140 }
141 return uri;
142 }
143
Leon Scroggins III052ce662010-09-13 14:44:16 -0400144 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400145 public void onTop(int level, Object data) {
146 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400147 Folder folderData = (Folder) data;
148 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400149 LoaderManager manager = getLoaderManager();
150 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
151 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500152 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400153 loader.forceLoad();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400154 if (mFolderNamer.getVisibility() == View.VISIBLE) {
155 completeOrCancelFolderNaming(true);
156 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400157 }
158
Leon Scroggins74dbe012010-10-15 10:54:27 -0400159 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400160 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
161 if (v == mFolderNamer) {
162 if (v.getText().length() > 0) {
163 if (actionId == EditorInfo.IME_NULL) {
164 // Only want to do this once.
165 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400166 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400167 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400168 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 }
Michael Kolb31829b92010-10-01 11:50:21 -0700170 // Steal the key press; otherwise a newline will be added
171 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400173 return false;
174 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400176 private void switchToDefaultView(boolean changedFolder) {
177 mFolderSelector.setVisibility(View.GONE);
178 mDefaultView.setVisibility(View.VISIBLE);
179 mCrumbHolder.setVisibility(View.GONE);
180 mFakeTitle.setVisibility(View.VISIBLE);
181 if (changedFolder) {
182 Object data = mCrumbs.getTopData();
183 if (data != null) {
184 Folder folder = (Folder) data;
185 mCurrentFolder = folder.Id;
Leon Scroggins02081942010-11-01 17:52:42 -0400186 int resource = mCurrentFolder == mRootFolder ?
187 R.drawable.ic_menu_bookmarks :
188 com.android.internal.R.drawable.ic_menu_archive;
189 Drawable drawable = getResources().getDrawable(resource);
190 updateFolderLabel(folder.Name, drawable);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400191 }
192 }
193 }
194
Leon Scroggins III052ce662010-09-13 14:44:16 -0400195 @Override
196 public void onClick(View v) {
197 if (v == mButton) {
198 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400199 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700200 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400201 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700202 } else {
203 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400204 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400205 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700206 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400207 } else if (save()) {
208 finish();
209 }
210 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700211 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400212 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400213 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
214 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700215 } else {
216 finish();
217 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400218 } else if (v == mFolder) {
Leon Scroggins26318a12010-11-08 10:08:40 -0500219 PopupMenu popup = new PopupMenu(this, mFolder);
Leon Scroggins88d08032010-10-21 15:17:10 -0400220 popup.getMenuInflater().inflate(R.menu.folder_choice,
221 popup.getMenu());
John Reckc8490812010-11-22 14:15:36 -0800222 if (mEditingFolder) {
223 popup.getMenu().removeItem(R.id.home_screen);
224 }
Leon Scroggins88d08032010-10-21 15:17:10 -0400225 popup.setOnMenuItemClickListener(this);
226 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400227 } else if (v == mAddNewFolder) {
228 mFolderNamer.setVisibility(View.VISIBLE);
229 mFolderNamer.setText(R.string.new_folder);
230 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400231 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700232 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400233 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100234 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400235 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400237 }
238
Leon Scroggins88d08032010-10-21 15:17:10 -0400239 @Override
240 public boolean onMenuItemClick(MenuItem item) {
241 switch(item.getItemId()) {
242 case R.id.bookmarks:
Leon Scroggins02081942010-11-01 17:52:42 -0400243 mCurrentFolder = mRootFolder;
244 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400245 mSaveToHomeScreen = false;
246 break;
247 case R.id.home_screen:
248 // Create a short cut to the home screen
249 mSaveToHomeScreen = true;
Leon Scroggins02081942010-11-01 17:52:42 -0400250 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400251 break;
252 case R.id.other:
253 switchToFolderSelector();
254 break;
255 default:
256 return false;
257 }
258 return true;
259 }
260
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400261 // Refresh the ListView to hide or show the empty view, as necessary.
262 // Should be called after mFolderNamer is shown or hidden.
263 private void updateList() {
264 if (mAdapter.getCount() == 0) {
265 // XXX: Is there a better way to refresh the ListView?
266 mListView.setAdapter(mAdapter);
267 }
268 }
269
270 private void completeOrCancelFolderNaming(boolean cancel) {
271 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700272 String name = mFolderNamer.getText().toString();
273 long id = addFolderToCurrent(mFolderNamer.getText().toString());
274 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700275 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400276 mFolderNamer.setVisibility(View.GONE);
277 mAddNewFolder.setVisibility(View.VISIBLE);
278 mAddSeparator.setVisibility(View.VISIBLE);
279 getInputMethodManager().hideSoftInputFromWindow(
280 mFolderNamer.getWindowToken(), 0);
281 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700282 }
283
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700284 private long addFolderToCurrent(String name) {
285 // Add the folder to the database
286 ContentValues values = new ContentValues();
287 values.put(BrowserContract.Bookmarks.TITLE,
288 name);
289 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500290 String[] accountInfo = getAccountNameAndType(this);
291 if (accountInfo != null) {
292 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
293 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700294 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400295 long currentFolder;
296 Object data = mCrumbs.getTopData();
297 if (data != null) {
298 currentFolder = ((Folder) data).Id;
299 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400300 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400301 }
302 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700303 Uri uri = getContentResolver().insert(
304 BrowserContract.Bookmarks.CONTENT_URI, values);
305 if (uri != null) {
306 return ContentUris.parseId(uri);
307 } else {
308 return -1;
309 }
310 }
311
Leon Scroggins III052ce662010-09-13 14:44:16 -0400312 private void switchToFolderSelector() {
313 mDefaultView.setVisibility(View.GONE);
314 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400315 mCrumbHolder.setVisibility(View.VISIBLE);
316 mFakeTitle.setVisibility(View.GONE);
317 mAddNewFolder.setVisibility(View.VISIBLE);
318 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400319 }
320
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700321 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700322 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400323 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400324 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700325 }
326 }
327
Leon Scroggins III052ce662010-09-13 14:44:16 -0400328 @Override
329 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
330 String[] projection;
331 switch (id) {
332 case LOADER_ID_ALL_FOLDERS:
333 projection = new String[] {
334 BrowserContract.Bookmarks._ID,
335 BrowserContract.Bookmarks.PARENT,
336 BrowserContract.Bookmarks.TITLE,
337 BrowserContract.Bookmarks.IS_FOLDER
338 };
339 return new CursorLoader(this,
340 BrowserContract.Bookmarks.CONTENT_URI,
341 projection,
342 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
343 null,
344 null);
345 case LOADER_ID_FOLDER_CONTENTS:
346 projection = new String[] {
347 BrowserContract.Bookmarks._ID,
348 BrowserContract.Bookmarks.TITLE,
349 BrowserContract.Bookmarks.IS_FOLDER
350 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500351 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
352 if (mEditingFolder) {
353 where += " AND " + BrowserContract.Bookmarks._ID + " != "
354 + mMap.getLong(BrowserContract.Bookmarks._ID);
355 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400356 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500357 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400358 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500359 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400360 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500361 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400362 default:
363 throw new AssertionError("Asking for nonexistant loader!");
364 }
365 }
366
367 @Override
368 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
369 switch (loader.getId()) {
370 case LOADER_ID_FOLDER_CONTENTS:
371 mAdapter.changeCursor(cursor);
372 break;
373 case LOADER_ID_ALL_FOLDERS:
374 long parent = mCurrentFolder;
375 int idIndex = cursor.getColumnIndexOrThrow(
376 BrowserContract.Bookmarks._ID);
377 int titleIndex = cursor.getColumnIndexOrThrow(
378 BrowserContract.Bookmarks.TITLE);
379 int parentIndex = cursor.getColumnIndexOrThrow(
380 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500381 // If the user is editing anything inside the "Other Bookmarks"
382 // folder, we need to stop searching up when we reach its parent.
383 // Find the root folder
384 moveCursorToFolder(cursor, mRootFolder, idIndex);
385 // omniparent is the folder which contains root, and therefore
386 // also the parent of the "Other Bookmarks" folder.
387 long omniparent = cursor.getLong(parentIndex);
388 Stack<Folder> folderStack = new Stack<Folder>();
389 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400390 // First, find the folder corresponding to the current
391 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500392 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400393 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400394 if (parent == mCurrentFolder) {
Leon Scroggins02081942010-11-01 17:52:42 -0400395 Drawable draw = getResources().getDrawable(
396 com.android.internal.R.drawable.ic_menu_archive);
397 updateFolderLabel(name, draw);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400398 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400399 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400400 parent = cursor.getLong(parentIndex);
401 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400402 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500403 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400404 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400405 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400406 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400407 break;
408 default:
409 break;
410 }
411 }
412
Leon Scroggins02081942010-11-01 17:52:42 -0400413 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500414 * Move cursor to the position that has folderToFind as its "_id".
415 * @param cursor Cursor containing folders in the bookmarks database
416 * @param folderToFind "_id" of the folder to move to.
417 * @param idIndex Index in cursor of "_id"
418 * @throws AssertionError if cursor is empty or there is no row with folderToFind
419 * as its "_id".
420 */
421 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
422 throws AssertionError {
423 if (!cursor.moveToFirst()) {
424 throw new AssertionError("No folders in the database!");
425 }
426 long folder;
427 do {
428 folder = cursor.getLong(idIndex);
429 } while (folder != folderToFind && cursor.moveToNext());
430 if (cursor.isAfterLast()) {
431 throw new AssertionError("Folder(id=" + folderToFind
432 + ") holding this bookmark does not exist!");
433 }
434 }
435
436 /**
Leon Scroggins02081942010-11-01 17:52:42 -0400437 * Update the name and image to show where the bookmark will be added
438 * @param name Name of the location to save (folder name, bookmarks, or home
439 * screen.
440 * @param drawable Image to show corresponding to the save location.
441 */
442 void updateFolderLabel(CharSequence name, Drawable drawable) {
443 mFolder.setText(name);
444 mFolder.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
445 null);
446 }
447
Leon Scroggins III052ce662010-09-13 14:44:16 -0400448 @Override
449 public void onItemClick(AdapterView<?> parent, View view, int position,
450 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400451 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400452 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400453 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400454 }
455
456 /**
457 * Shows a list of names of folders.
458 */
459 private class FolderAdapter extends CursorAdapter {
460 public FolderAdapter(Context context) {
461 super(context, null);
462 }
463
464 @Override
465 public void bindView(View view, Context context, Cursor cursor) {
466 ((TextView) view.findViewById(android.R.id.text1)).setText(
467 cursor.getString(cursor.getColumnIndexOrThrow(
468 BrowserContract.Bookmarks.TITLE)));
469 }
470
471 @Override
472 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700473 View view = LayoutInflater.from(context).inflate(
474 R.layout.folder_list_item, null);
475 view.setBackgroundDrawable(context.getResources().
476 getDrawable(android.R.drawable.list_selector_background));
477 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400478 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400479
480 @Override
481 public boolean isEmpty() {
482 // Do not show the empty view if the user is creating a new folder.
483 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
484 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400485 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486
Leon Scrogginsc1129902010-12-08 15:28:33 -0500487 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488 protected void onCreate(Bundle icicle) {
489 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500490 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100491
492 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100493
Leon Scroggins III052ce662010-09-13 14:44:16 -0400494 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100495
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400496 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700497
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 String title = null;
499 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100500
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400501 mFakeTitle = (TextView) findViewById(R.id.fake_title);
502
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800504 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800506 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 mMap = b;
508 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400509 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800510 if (mEditingFolder) {
511 findViewById(R.id.row_address).setVisibility(View.GONE);
512 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400513 } else {
514 int gravity = mMap.getInt("gravity", -1);
515 if (gravity != -1) {
516 WindowManager.LayoutParams l = window.getAttributes();
517 l.gravity = gravity;
518 window.setAttributes(l);
519 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400521 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
522 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
523 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700524 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400525 }
Leon Scroggins02081942010-11-01 17:52:42 -0400526 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700527 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400528 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 }
530
531 mTitle = (EditText) findViewById(R.id.title);
532 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100533
Leon Scroggins III052ce662010-09-13 14:44:16 -0400534 mAddress = (EditText) findViewById(R.id.address);
535 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400538 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539
540 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400541 mCancelButton.setOnClickListener(this);
542
543 mFolder = (TextView) findViewById(R.id.folder);
544 mFolder.setOnClickListener(this);
545
546 mDefaultView = findViewById(R.id.default_view);
547 mFolderSelector = findViewById(R.id.folder_selector);
548
549 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
550 mFolderNamer.setOnEditorActionListener(this);
551
552 mAddNewFolder = findViewById(R.id.add_new_folder);
553 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400554 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400555
Leon Scroggins74dbe012010-10-15 10:54:27 -0400556 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
557 mCrumbs.setUseBackButton(true);
558 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400559 String name = getString(R.string.bookmarks);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500560 mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400561 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800562 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400563
Leon Scroggins III052ce662010-09-13 14:44:16 -0400564 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400565 mListView = (ListView) findViewById(R.id.list);
566 View empty = findViewById(R.id.empty);
567 mListView.setEmptyView(empty);
568 mListView.setAdapter(mAdapter);
569 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400570 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500571 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 // Find all the folders
573 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
574 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400575 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400576 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
577
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700578
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400579 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 mButton.requestFocus();
581 }
582 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100583
Leon Scroggins8baaa632010-12-08 19:46:53 -0500584 /**
585 * Get the account name and type of the currently synced account.
586 * @param context Context to access preferences.
587 * @return null if no account name or type. Otherwise, the result will be
588 * an array of two Strings, the accountName and accountType, respectively.
589 */
590 private String[] getAccountNameAndType(Context context) {
591 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
592 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
593 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
594 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
595 return null;
596 }
597 return new String[] { accountName, accountType };
598 }
599
Leon Scroggins25230d72010-09-28 20:09:25 -0400600 // FIXME: Use a CursorLoader
601 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500602 String[] accountInfo = getAccountNameAndType(context);
603 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500604 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400605 }
606 Cursor cursor = null;
607 try {
608 cursor = context.getContentResolver().query(
609 BrowserContract.Bookmarks.CONTENT_URI,
610 new String[] { BrowserContract.Bookmarks._ID },
611 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
612 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
613 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
614 new String[] {
615 BrowserContract.ChromeSyncColumns
616 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500617 accountInfo[0],
618 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400619 null);
620 if (cursor != null && cursor.moveToFirst()) {
621 return cursor.getLong(0);
622 }
623 } finally {
624 if (cursor != null) cursor.close();
625 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500626 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400627 }
628
Leon Scroggins02065b02010-01-04 14:30:13 -0500629 /**
630 * Runnable to save a bookmark, so it can be performed in its own thread.
631 */
632 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400633 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500634 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200635 private Context mContext;
636 public SaveBookmarkRunnable(Context ctx, Message msg) {
637 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500638 mMessage = msg;
639 }
640 public void run() {
641 // Unbundle bookmark data.
642 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400643 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
644 String url = bundle.getString(BrowserContract.Bookmarks.URL);
645 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500646 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400647 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
648 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500649
650 // Save to the bookmarks DB.
651 try {
652 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400653 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
654 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500655 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200656 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500657 }
658 mMessage.arg1 = 1;
659 } catch (IllegalStateException e) {
660 mMessage.arg1 = 0;
661 }
662 mMessage.sendToTarget();
663 }
664 }
665
John Reckc8490812010-11-22 14:15:36 -0800666 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
667 Context mContext;
668 Long mId;
669
670 public UpdateBookmarkTask(Context context, long id) {
671 mContext = context;
672 mId = id;
673 }
674
675 @Override
676 protected Void doInBackground(ContentValues... params) {
677 if (params.length != 1) {
678 throw new IllegalArgumentException("No ContentValues provided!");
679 }
680 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
681 mContext.getContentResolver().update(
682 uri,
683 params[0], null, null);
684 return null;
685 }
686 }
687
Ben Murdoch1794fe22009-09-29 18:14:30 +0100688 private void createHandler() {
689 if (mHandler == null) {
690 mHandler = new Handler() {
691 @Override
692 public void handleMessage(Message msg) {
693 switch (msg.what) {
694 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500695 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100696 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
697 Toast.LENGTH_LONG).show();
698 } else {
699 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
700 Toast.LENGTH_LONG).show();
701 }
702 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400703 case TOUCH_ICON_DOWNLOADED:
704 Bundle b = msg.getData();
705 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400706 AddBookmarkPage.this,
707 b.getString(BrowserContract.Bookmarks.URL),
708 b.getString(BrowserContract.Bookmarks.TITLE),
709 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
710 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400711 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100712 }
713 }
714 };
715 }
716 }
717
The Android Open Source Project0c908882009-03-03 19:32:16 -0800718 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100719 * 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 -0800720 */
721 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100722 createHandler();
723
The Android Open Source Project0c908882009-03-03 19:32:16 -0800724 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100725 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100727
The Android Open Source Project0c908882009-03-03 19:32:16 -0800728 boolean emptyTitle = title.length() == 0;
729 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
730 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800731 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800732 if (emptyTitle) {
733 mTitle.setError(r.getText(R.string.bookmark_needs_title));
734 }
735 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400736 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800737 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400738 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100739
The Android Open Source Project0c908882009-03-03 19:32:16 -0800740 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000741 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800742 if (!mEditingFolder) {
743 try {
744 // We allow bookmarks with a javascript: scheme, but these will in most cases
745 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000746
John Reckc8490812010-11-22 14:15:36 -0800747 if (!url.toLowerCase().startsWith("javascript:")) {
748 URI uriObj = new URI(url);
749 String scheme = uriObj.getScheme();
750 if (!Bookmarks.urlHasAcceptableScheme(url)) {
751 // If the scheme was non-null, let the user know that we
752 // can't save their bookmark. If it was null, we'll assume
753 // they meant http when we parse it in the WebAddress class.
754 if (scheme != null) {
755 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
756 return false;
757 }
758 WebAddress address;
759 try {
760 address = new WebAddress(unfilteredUrl);
761 } catch (ParseException e) {
762 throw new URISyntaxException("", "");
763 }
764 if (address.getHost().length() == 0) {
765 throw new URISyntaxException("", "");
766 }
767 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000768 }
Ben Murdochde353622009-10-12 10:29:00 +0100769 }
John Reckc8490812010-11-22 14:15:36 -0800770 } catch (URISyntaxException e) {
771 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
772 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800773 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800774 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100775
Leon Scroggins88d08032010-10-21 15:17:10 -0400776 if (mSaveToHomeScreen) {
777 mEditingExisting = false;
778 }
779
780 boolean urlUnmodified = url.equals(mOriginalUrl);
781
Ben Murdoch1794fe22009-09-29 18:14:30 +0100782 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800783 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
784 ContentValues values = new ContentValues();
785 values.put(BrowserContract.Bookmarks.TITLE, title);
786 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
787 if (!mEditingFolder) {
788 values.put(BrowserContract.Bookmarks.URL, url);
789 if (!urlUnmodified) {
790 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
791 }
792 }
793 if (values.size() > 0) {
794 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
795 }
796 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100797 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400798 Bitmap thumbnail;
799 Bitmap favicon;
800 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400801 thumbnail = (Bitmap) mMap.getParcelable(
802 BrowserContract.Bookmarks.THUMBNAIL);
803 favicon = (Bitmap) mMap.getParcelable(
804 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400805 } else {
806 thumbnail = null;
807 favicon = null;
808 }
809
Ben Murdoch1794fe22009-09-29 18:14:30 +0100810 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400811 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
812 bundle.putString(BrowserContract.Bookmarks.URL, url);
813 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400814
815 if (mSaveToHomeScreen) {
816 if (mTouchIconUrl != null && urlUnmodified) {
817 Message msg = Message.obtain(mHandler,
818 TOUCH_ICON_DOWNLOADED);
819 msg.setData(bundle);
820 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400821 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400822 icon.execute(mTouchIconUrl);
823 } else {
824 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
825 title, null /*touchIcon*/, favicon));
826 }
827 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400828 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
829 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
830 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400831 // Post a message to write to the DB.
832 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
833 msg.setData(bundle);
834 // Start a new thread so as to not slow down the UI
835 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
836 t.start();
837 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100838 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000839 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800840 }
841 return true;
842 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800843}