blob: 6e3f8bc06daf66a48b48ca3e9abc20bda12e8381 [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";
Leon Scroggins74dbe012010-10-15 10:54:27 -040082 // Set to true to see the crash on the code I would like to run.
83 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
Leon Scroggins III052ce662010-09-13 14:44:16 -040085 // IDs for the CursorLoaders that are used.
86 private final int LOADER_ID_FOLDER_CONTENTS = 0;
87 private final int LOADER_ID_ALL_FOLDERS = 1;
88
The Android Open Source Project0c908882009-03-03 19:32:16 -080089 private EditText mTitle;
90 private EditText mAddress;
91 private TextView mButton;
92 private View mCancelButton;
93 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080094 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040096 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010097 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040098 private TextView mFolder;
99 private View mDefaultView;
100 private View mFolderSelector;
101 private EditText mFolderNamer;
102 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400103 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400104 private long mCurrentFolder = 0;
105 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400106 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400107 private TextView mFakeTitle;
108 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400109 private ListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400110 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400111 private long mRootFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400112
113 private static class Folder {
114 String Name;
115 long Id;
116 Folder(String name, long id) {
117 Name = name;
118 Id = id;
119 }
120 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121
Ben Murdoch1794fe22009-09-29 18:14:30 +0100122 // Message IDs
123 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400124 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100125
126 private Handler mHandler;
127
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100128 private InputMethodManager getInputMethodManager() {
129 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
130 }
131
Leon Scroggins8baaa632010-12-08 19:46:53 -0500132 private Uri getUriForFolder(long folder) {
133 Uri uri;
134 if (folder == mRootFolder) {
135 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
136 } else {
137 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
138 }
139 String[] accountInfo = getAccountNameAndType(this);
140 if (accountInfo != null) {
141 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
142 }
143 return uri;
144 }
145
Leon Scroggins III052ce662010-09-13 14:44:16 -0400146 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400147 public void onTop(int level, Object data) {
148 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400149 Folder folderData = (Folder) data;
150 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400151 LoaderManager manager = getLoaderManager();
152 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
153 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500154 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400155 loader.forceLoad();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400156 if (mFolderNamer.getVisibility() == View.VISIBLE) {
157 completeOrCancelFolderNaming(true);
158 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400159 }
160
Leon Scroggins74dbe012010-10-15 10:54:27 -0400161 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400162 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
163 if (v == mFolderNamer) {
164 if (v.getText().length() > 0) {
165 if (actionId == EditorInfo.IME_NULL) {
166 // Only want to do this once.
167 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400168 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400169 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400170 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800171 }
Michael Kolb31829b92010-10-01 11:50:21 -0700172 // Steal the key press; otherwise a newline will be added
173 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400175 return false;
176 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400178 private void switchToDefaultView(boolean changedFolder) {
179 mFolderSelector.setVisibility(View.GONE);
180 mDefaultView.setVisibility(View.VISIBLE);
181 mCrumbHolder.setVisibility(View.GONE);
182 mFakeTitle.setVisibility(View.VISIBLE);
183 if (changedFolder) {
184 Object data = mCrumbs.getTopData();
185 if (data != null) {
186 Folder folder = (Folder) data;
187 mCurrentFolder = folder.Id;
Leon Scroggins02081942010-11-01 17:52:42 -0400188 int resource = mCurrentFolder == mRootFolder ?
189 R.drawable.ic_menu_bookmarks :
190 com.android.internal.R.drawable.ic_menu_archive;
191 Drawable drawable = getResources().getDrawable(resource);
192 updateFolderLabel(folder.Name, drawable);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400193 }
194 }
195 }
196
Leon Scroggins III052ce662010-09-13 14:44:16 -0400197 @Override
198 public void onClick(View v) {
199 if (v == mButton) {
200 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400201 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700202 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400203 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700204 } else {
205 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400206 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400207 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700208 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400209 } else if (save()) {
210 finish();
211 }
212 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700213 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400214 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400215 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
216 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700217 } else {
218 finish();
219 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400220 } else if (v == mFolder) {
Leon Scroggins26318a12010-11-08 10:08:40 -0500221 PopupMenu popup = new PopupMenu(this, mFolder);
Leon Scroggins88d08032010-10-21 15:17:10 -0400222 popup.getMenuInflater().inflate(R.menu.folder_choice,
223 popup.getMenu());
John Reckc8490812010-11-22 14:15:36 -0800224 if (mEditingFolder) {
225 popup.getMenu().removeItem(R.id.home_screen);
226 }
Leon Scroggins88d08032010-10-21 15:17:10 -0400227 popup.setOnMenuItemClickListener(this);
228 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400229 } else if (v == mAddNewFolder) {
230 mFolderNamer.setVisibility(View.VISIBLE);
231 mFolderNamer.setText(R.string.new_folder);
232 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400233 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700234 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400235 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100236 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400237 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400239 }
240
Leon Scroggins88d08032010-10-21 15:17:10 -0400241 @Override
242 public boolean onMenuItemClick(MenuItem item) {
243 switch(item.getItemId()) {
244 case R.id.bookmarks:
Leon Scroggins02081942010-11-01 17:52:42 -0400245 mCurrentFolder = mRootFolder;
246 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400247 mSaveToHomeScreen = false;
248 break;
249 case R.id.home_screen:
250 // Create a short cut to the home screen
251 mSaveToHomeScreen = true;
Leon Scroggins02081942010-11-01 17:52:42 -0400252 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400253 break;
254 case R.id.other:
255 switchToFolderSelector();
256 break;
257 default:
258 return false;
259 }
260 return true;
261 }
262
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400263 // Refresh the ListView to hide or show the empty view, as necessary.
264 // Should be called after mFolderNamer is shown or hidden.
265 private void updateList() {
266 if (mAdapter.getCount() == 0) {
267 // XXX: Is there a better way to refresh the ListView?
268 mListView.setAdapter(mAdapter);
269 }
270 }
271
272 private void completeOrCancelFolderNaming(boolean cancel) {
273 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700274 String name = mFolderNamer.getText().toString();
275 long id = addFolderToCurrent(mFolderNamer.getText().toString());
276 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700277 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400278 mFolderNamer.setVisibility(View.GONE);
279 mAddNewFolder.setVisibility(View.VISIBLE);
280 mAddSeparator.setVisibility(View.VISIBLE);
281 getInputMethodManager().hideSoftInputFromWindow(
282 mFolderNamer.getWindowToken(), 0);
283 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700284 }
285
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700286 private long addFolderToCurrent(String name) {
287 // Add the folder to the database
288 ContentValues values = new ContentValues();
289 values.put(BrowserContract.Bookmarks.TITLE,
290 name);
291 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500292 String[] accountInfo = getAccountNameAndType(this);
293 if (accountInfo != null) {
294 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
295 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700296 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400297 long currentFolder;
298 Object data = mCrumbs.getTopData();
299 if (data != null) {
300 currentFolder = ((Folder) data).Id;
301 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400302 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400303 }
304 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700305 Uri uri = getContentResolver().insert(
306 BrowserContract.Bookmarks.CONTENT_URI, values);
307 if (uri != null) {
308 return ContentUris.parseId(uri);
309 } else {
310 return -1;
311 }
312 }
313
Leon Scroggins III052ce662010-09-13 14:44:16 -0400314 private void switchToFolderSelector() {
315 mDefaultView.setVisibility(View.GONE);
316 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400317 mCrumbHolder.setVisibility(View.VISIBLE);
318 mFakeTitle.setVisibility(View.GONE);
319 mAddNewFolder.setVisibility(View.VISIBLE);
320 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400321 }
322
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700323 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700324 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400325 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400326 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700327 }
328 }
329
Leon Scroggins III052ce662010-09-13 14:44:16 -0400330 @Override
331 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
332 String[] projection;
333 switch (id) {
334 case LOADER_ID_ALL_FOLDERS:
335 projection = new String[] {
336 BrowserContract.Bookmarks._ID,
337 BrowserContract.Bookmarks.PARENT,
338 BrowserContract.Bookmarks.TITLE,
339 BrowserContract.Bookmarks.IS_FOLDER
340 };
341 return new CursorLoader(this,
342 BrowserContract.Bookmarks.CONTENT_URI,
343 projection,
344 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
345 null,
346 null);
347 case LOADER_ID_FOLDER_CONTENTS:
348 projection = new String[] {
349 BrowserContract.Bookmarks._ID,
350 BrowserContract.Bookmarks.TITLE,
351 BrowserContract.Bookmarks.IS_FOLDER
352 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500353 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
354 if (mEditingFolder) {
355 where += " AND " + BrowserContract.Bookmarks._ID + " != "
356 + mMap.getLong(BrowserContract.Bookmarks._ID);
357 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400358 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500359 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400360 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500361 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400362 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500363 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400364 default:
365 throw new AssertionError("Asking for nonexistant loader!");
366 }
367 }
368
369 @Override
370 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
371 switch (loader.getId()) {
372 case LOADER_ID_FOLDER_CONTENTS:
373 mAdapter.changeCursor(cursor);
374 break;
375 case LOADER_ID_ALL_FOLDERS:
376 long parent = mCurrentFolder;
377 int idIndex = cursor.getColumnIndexOrThrow(
378 BrowserContract.Bookmarks._ID);
379 int titleIndex = cursor.getColumnIndexOrThrow(
380 BrowserContract.Bookmarks.TITLE);
381 int parentIndex = cursor.getColumnIndexOrThrow(
382 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500383 // If the user is editing anything inside the "Other Bookmarks"
384 // folder, we need to stop searching up when we reach its parent.
385 // Find the root folder
386 moveCursorToFolder(cursor, mRootFolder, idIndex);
387 // omniparent is the folder which contains root, and therefore
388 // also the parent of the "Other Bookmarks" folder.
389 long omniparent = cursor.getLong(parentIndex);
390 Stack<Folder> folderStack = new Stack<Folder>();
391 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400392 // First, find the folder corresponding to the current
393 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500394 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400395 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400396 if (parent == mCurrentFolder) {
Leon Scroggins02081942010-11-01 17:52:42 -0400397 Drawable draw = getResources().getDrawable(
398 com.android.internal.R.drawable.ic_menu_archive);
399 updateFolderLabel(name, draw);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400400 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400401 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400402 parent = cursor.getLong(parentIndex);
403 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400404 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500405 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400406 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400407 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400408 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400409 break;
410 default:
411 break;
412 }
413 }
414
Leon Scroggins02081942010-11-01 17:52:42 -0400415 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500416 * Move cursor to the position that has folderToFind as its "_id".
417 * @param cursor Cursor containing folders in the bookmarks database
418 * @param folderToFind "_id" of the folder to move to.
419 * @param idIndex Index in cursor of "_id"
420 * @throws AssertionError if cursor is empty or there is no row with folderToFind
421 * as its "_id".
422 */
423 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
424 throws AssertionError {
425 if (!cursor.moveToFirst()) {
426 throw new AssertionError("No folders in the database!");
427 }
428 long folder;
429 do {
430 folder = cursor.getLong(idIndex);
431 } while (folder != folderToFind && cursor.moveToNext());
432 if (cursor.isAfterLast()) {
433 throw new AssertionError("Folder(id=" + folderToFind
434 + ") holding this bookmark does not exist!");
435 }
436 }
437
438 /**
Leon Scroggins02081942010-11-01 17:52:42 -0400439 * Update the name and image to show where the bookmark will be added
440 * @param name Name of the location to save (folder name, bookmarks, or home
441 * screen.
442 * @param drawable Image to show corresponding to the save location.
443 */
444 void updateFolderLabel(CharSequence name, Drawable drawable) {
445 mFolder.setText(name);
446 mFolder.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
447 null);
448 }
449
Leon Scroggins III052ce662010-09-13 14:44:16 -0400450 @Override
451 public void onItemClick(AdapterView<?> parent, View view, int position,
452 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400453 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400454 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400455 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400456 }
457
458 /**
459 * Shows a list of names of folders.
460 */
461 private class FolderAdapter extends CursorAdapter {
462 public FolderAdapter(Context context) {
463 super(context, null);
464 }
465
466 @Override
467 public void bindView(View view, Context context, Cursor cursor) {
468 ((TextView) view.findViewById(android.R.id.text1)).setText(
469 cursor.getString(cursor.getColumnIndexOrThrow(
470 BrowserContract.Bookmarks.TITLE)));
471 }
472
473 @Override
474 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700475 View view = LayoutInflater.from(context).inflate(
476 R.layout.folder_list_item, null);
477 view.setBackgroundDrawable(context.getResources().
478 getDrawable(android.R.drawable.list_selector_background));
479 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400480 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400481
482 @Override
483 public boolean isEmpty() {
484 // Do not show the empty view if the user is creating a new folder.
485 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
486 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400487 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488
Leon Scrogginsc1129902010-12-08 15:28:33 -0500489 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 protected void onCreate(Bundle icicle) {
491 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400492 if (DEBUG_CRASH) {
493 requestWindowFeature(Window.FEATURE_NO_TITLE);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400494 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100495
496 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100497
Leon Scroggins III052ce662010-09-13 14:44:16 -0400498 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100499
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400500 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400501 if (!DEBUG_CRASH) {
Leon Scroggins02081942010-11-01 17:52:42 -0400502 setTitle("");
Leon Scroggins74dbe012010-10-15 10:54:27 -0400503 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700504
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 String title = null;
506 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100507
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400508 mFakeTitle = (TextView) findViewById(R.id.fake_title);
509
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800511 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800513 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800514 mMap = b;
515 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400516 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800517 if (mEditingFolder) {
518 findViewById(R.id.row_address).setVisibility(View.GONE);
519 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400520 } else {
521 int gravity = mMap.getInt("gravity", -1);
522 if (gravity != -1) {
523 WindowManager.LayoutParams l = window.getAttributes();
524 l.gravity = gravity;
525 window.setAttributes(l);
526 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400528 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
529 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
530 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700531 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400532 }
Leon Scroggins02081942010-11-01 17:52:42 -0400533 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700534 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400535 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 }
537
538 mTitle = (EditText) findViewById(R.id.title);
539 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100540
Leon Scroggins III052ce662010-09-13 14:44:16 -0400541 mAddress = (EditText) findViewById(R.id.address);
542 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543
The Android Open Source Project0c908882009-03-03 19:32:16 -0800544 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400545 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800546
547 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400548 mCancelButton.setOnClickListener(this);
549
550 mFolder = (TextView) findViewById(R.id.folder);
551 mFolder.setOnClickListener(this);
552
553 mDefaultView = findViewById(R.id.default_view);
554 mFolderSelector = findViewById(R.id.folder_selector);
555
556 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
557 mFolderNamer.setOnEditorActionListener(this);
558
559 mAddNewFolder = findViewById(R.id.add_new_folder);
560 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400561 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400562
Leon Scroggins74dbe012010-10-15 10:54:27 -0400563 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
564 mCrumbs.setUseBackButton(true);
565 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400566 String name = getString(R.string.bookmarks);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500567 mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400568 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800569 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400570
Leon Scroggins III052ce662010-09-13 14:44:16 -0400571 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400572 mListView = (ListView) findViewById(R.id.list);
573 View empty = findViewById(R.id.empty);
574 mListView.setEmptyView(empty);
575 mListView.setAdapter(mAdapter);
576 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400577 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500578 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400579 // Find all the folders
580 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
581 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400582 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400583 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
584
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700585
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400586 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 mButton.requestFocus();
588 }
589 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100590
Leon Scroggins8baaa632010-12-08 19:46:53 -0500591 /**
592 * Get the account name and type of the currently synced account.
593 * @param context Context to access preferences.
594 * @return null if no account name or type. Otherwise, the result will be
595 * an array of two Strings, the accountName and accountType, respectively.
596 */
597 private String[] getAccountNameAndType(Context context) {
598 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
599 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
600 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
601 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
602 return null;
603 }
604 return new String[] { accountName, accountType };
605 }
606
Leon Scroggins25230d72010-09-28 20:09:25 -0400607 // FIXME: Use a CursorLoader
608 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500609 String[] accountInfo = getAccountNameAndType(context);
610 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500611 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400612 }
613 Cursor cursor = null;
614 try {
615 cursor = context.getContentResolver().query(
616 BrowserContract.Bookmarks.CONTENT_URI,
617 new String[] { BrowserContract.Bookmarks._ID },
618 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
619 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
620 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
621 new String[] {
622 BrowserContract.ChromeSyncColumns
623 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500624 accountInfo[0],
625 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400626 null);
627 if (cursor != null && cursor.moveToFirst()) {
628 return cursor.getLong(0);
629 }
630 } finally {
631 if (cursor != null) cursor.close();
632 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500633 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400634 }
635
Leon Scroggins02065b02010-01-04 14:30:13 -0500636 /**
637 * Runnable to save a bookmark, so it can be performed in its own thread.
638 */
639 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400640 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500641 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200642 private Context mContext;
643 public SaveBookmarkRunnable(Context ctx, Message msg) {
644 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500645 mMessage = msg;
646 }
647 public void run() {
648 // Unbundle bookmark data.
649 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400650 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
651 String url = bundle.getString(BrowserContract.Bookmarks.URL);
652 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500653 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400654 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
655 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500656
657 // Save to the bookmarks DB.
658 try {
659 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400660 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
661 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500662 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200663 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500664 }
665 mMessage.arg1 = 1;
666 } catch (IllegalStateException e) {
667 mMessage.arg1 = 0;
668 }
669 mMessage.sendToTarget();
670 }
671 }
672
John Reckc8490812010-11-22 14:15:36 -0800673 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
674 Context mContext;
675 Long mId;
676
677 public UpdateBookmarkTask(Context context, long id) {
678 mContext = context;
679 mId = id;
680 }
681
682 @Override
683 protected Void doInBackground(ContentValues... params) {
684 if (params.length != 1) {
685 throw new IllegalArgumentException("No ContentValues provided!");
686 }
687 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
688 mContext.getContentResolver().update(
689 uri,
690 params[0], null, null);
691 return null;
692 }
693 }
694
Ben Murdoch1794fe22009-09-29 18:14:30 +0100695 private void createHandler() {
696 if (mHandler == null) {
697 mHandler = new Handler() {
698 @Override
699 public void handleMessage(Message msg) {
700 switch (msg.what) {
701 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500702 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100703 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
704 Toast.LENGTH_LONG).show();
705 } else {
706 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
707 Toast.LENGTH_LONG).show();
708 }
709 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400710 case TOUCH_ICON_DOWNLOADED:
711 Bundle b = msg.getData();
712 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400713 AddBookmarkPage.this,
714 b.getString(BrowserContract.Bookmarks.URL),
715 b.getString(BrowserContract.Bookmarks.TITLE),
716 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
717 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400718 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100719 }
720 }
721 };
722 }
723 }
724
The Android Open Source Project0c908882009-03-03 19:32:16 -0800725 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100726 * 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 -0800727 */
728 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100729 createHandler();
730
The Android Open Source Project0c908882009-03-03 19:32:16 -0800731 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100732 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100734
The Android Open Source Project0c908882009-03-03 19:32:16 -0800735 boolean emptyTitle = title.length() == 0;
736 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
737 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800738 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800739 if (emptyTitle) {
740 mTitle.setError(r.getText(R.string.bookmark_needs_title));
741 }
742 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400743 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800744 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400745 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100746
The Android Open Source Project0c908882009-03-03 19:32:16 -0800747 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000748 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800749 if (!mEditingFolder) {
750 try {
751 // We allow bookmarks with a javascript: scheme, but these will in most cases
752 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000753
John Reckc8490812010-11-22 14:15:36 -0800754 if (!url.toLowerCase().startsWith("javascript:")) {
755 URI uriObj = new URI(url);
756 String scheme = uriObj.getScheme();
757 if (!Bookmarks.urlHasAcceptableScheme(url)) {
758 // If the scheme was non-null, let the user know that we
759 // can't save their bookmark. If it was null, we'll assume
760 // they meant http when we parse it in the WebAddress class.
761 if (scheme != null) {
762 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
763 return false;
764 }
765 WebAddress address;
766 try {
767 address = new WebAddress(unfilteredUrl);
768 } catch (ParseException e) {
769 throw new URISyntaxException("", "");
770 }
771 if (address.getHost().length() == 0) {
772 throw new URISyntaxException("", "");
773 }
774 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000775 }
Ben Murdochde353622009-10-12 10:29:00 +0100776 }
John Reckc8490812010-11-22 14:15:36 -0800777 } catch (URISyntaxException e) {
778 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
779 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800780 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800781 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100782
Leon Scroggins88d08032010-10-21 15:17:10 -0400783 if (mSaveToHomeScreen) {
784 mEditingExisting = false;
785 }
786
787 boolean urlUnmodified = url.equals(mOriginalUrl);
788
Ben Murdoch1794fe22009-09-29 18:14:30 +0100789 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800790 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
791 ContentValues values = new ContentValues();
792 values.put(BrowserContract.Bookmarks.TITLE, title);
793 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
794 if (!mEditingFolder) {
795 values.put(BrowserContract.Bookmarks.URL, url);
796 if (!urlUnmodified) {
797 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
798 }
799 }
800 if (values.size() > 0) {
801 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
802 }
803 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100804 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400805 Bitmap thumbnail;
806 Bitmap favicon;
807 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400808 thumbnail = (Bitmap) mMap.getParcelable(
809 BrowserContract.Bookmarks.THUMBNAIL);
810 favicon = (Bitmap) mMap.getParcelable(
811 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400812 } else {
813 thumbnail = null;
814 favicon = null;
815 }
816
Ben Murdoch1794fe22009-09-29 18:14:30 +0100817 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400818 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
819 bundle.putString(BrowserContract.Bookmarks.URL, url);
820 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400821
822 if (mSaveToHomeScreen) {
823 if (mTouchIconUrl != null && urlUnmodified) {
824 Message msg = Message.obtain(mHandler,
825 TOUCH_ICON_DOWNLOADED);
826 msg.setData(bundle);
827 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400828 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400829 icon.execute(mTouchIconUrl);
830 } else {
831 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
832 title, null /*touchIcon*/, favicon));
833 }
834 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400835 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
836 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
837 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400838 // Post a message to write to the DB.
839 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
840 msg.setData(bundle);
841 // Start a new thread so as to not slow down the UI
842 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
843 t.start();
844 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100845 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000846 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800847 }
848 return true;
849 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850}