blob: 262884baa9102e8e25c1aca266594acaa4fd9945 [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
Dianne Hackborn39772c82010-12-16 00:43:54 -0800413 public void onLoaderReset(Loader<Cursor> loader) {
414 switch (loader.getId()) {
415 case LOADER_ID_FOLDER_CONTENTS:
416 mAdapter.changeCursor(null);
417 break;
418 }
419 }
420
Leon Scroggins02081942010-11-01 17:52:42 -0400421 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500422 * Move cursor to the position that has folderToFind as its "_id".
423 * @param cursor Cursor containing folders in the bookmarks database
424 * @param folderToFind "_id" of the folder to move to.
425 * @param idIndex Index in cursor of "_id"
426 * @throws AssertionError if cursor is empty or there is no row with folderToFind
427 * as its "_id".
428 */
429 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
430 throws AssertionError {
431 if (!cursor.moveToFirst()) {
432 throw new AssertionError("No folders in the database!");
433 }
434 long folder;
435 do {
436 folder = cursor.getLong(idIndex);
437 } while (folder != folderToFind && cursor.moveToNext());
438 if (cursor.isAfterLast()) {
439 throw new AssertionError("Folder(id=" + folderToFind
440 + ") holding this bookmark does not exist!");
441 }
442 }
443
444 /**
Leon Scroggins02081942010-11-01 17:52:42 -0400445 * Update the name and image to show where the bookmark will be added
446 * @param name Name of the location to save (folder name, bookmarks, or home
447 * screen.
448 * @param drawable Image to show corresponding to the save location.
449 */
450 void updateFolderLabel(CharSequence name, Drawable drawable) {
451 mFolder.setText(name);
452 mFolder.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
453 null);
454 }
455
Leon Scroggins III052ce662010-09-13 14:44:16 -0400456 @Override
457 public void onItemClick(AdapterView<?> parent, View view, int position,
458 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400459 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400460 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400461 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400462 }
463
464 /**
465 * Shows a list of names of folders.
466 */
467 private class FolderAdapter extends CursorAdapter {
468 public FolderAdapter(Context context) {
469 super(context, null);
470 }
471
472 @Override
473 public void bindView(View view, Context context, Cursor cursor) {
474 ((TextView) view.findViewById(android.R.id.text1)).setText(
475 cursor.getString(cursor.getColumnIndexOrThrow(
476 BrowserContract.Bookmarks.TITLE)));
477 }
478
479 @Override
480 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700481 View view = LayoutInflater.from(context).inflate(
482 R.layout.folder_list_item, null);
483 view.setBackgroundDrawable(context.getResources().
484 getDrawable(android.R.drawable.list_selector_background));
485 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400486 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400487
488 @Override
489 public boolean isEmpty() {
490 // Do not show the empty view if the user is creating a new folder.
491 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
492 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400493 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494
Leon Scrogginsc1129902010-12-08 15:28:33 -0500495 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800496 protected void onCreate(Bundle icicle) {
497 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500498 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100499
500 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100501
Leon Scroggins III052ce662010-09-13 14:44:16 -0400502 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100503
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400504 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700505
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 String title = null;
507 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100508
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400509 mFakeTitle = (TextView) findViewById(R.id.fake_title);
510
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800512 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800514 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800515 mMap = b;
516 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400517 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800518 if (mEditingFolder) {
519 findViewById(R.id.row_address).setVisibility(View.GONE);
520 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400521 } else {
522 int gravity = mMap.getInt("gravity", -1);
523 if (gravity != -1) {
524 WindowManager.LayoutParams l = window.getAttributes();
525 l.gravity = gravity;
526 window.setAttributes(l);
527 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400529 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
530 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
531 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700532 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400533 }
Leon Scroggins02081942010-11-01 17:52:42 -0400534 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700535 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400536 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 }
538
539 mTitle = (EditText) findViewById(R.id.title);
540 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100541
Leon Scroggins III052ce662010-09-13 14:44:16 -0400542 mAddress = (EditText) findViewById(R.id.address);
543 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800544
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400546 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547
548 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400549 mCancelButton.setOnClickListener(this);
550
551 mFolder = (TextView) findViewById(R.id.folder);
552 mFolder.setOnClickListener(this);
553
554 mDefaultView = findViewById(R.id.default_view);
555 mFolderSelector = findViewById(R.id.folder_selector);
556
557 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
558 mFolderNamer.setOnEditorActionListener(this);
559
560 mAddNewFolder = findViewById(R.id.add_new_folder);
561 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400562 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400563
Leon Scroggins74dbe012010-10-15 10:54:27 -0400564 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
565 mCrumbs.setUseBackButton(true);
566 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400567 String name = getString(R.string.bookmarks);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500568 mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400569 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800570 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400571
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400573 mListView = (ListView) findViewById(R.id.list);
574 View empty = findViewById(R.id.empty);
575 mListView.setEmptyView(empty);
576 mListView.setAdapter(mAdapter);
577 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400578 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500579 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400580 // Find all the folders
581 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
582 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400583 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400584 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
585
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700586
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400587 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588 mButton.requestFocus();
589 }
590 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100591
Leon Scroggins8baaa632010-12-08 19:46:53 -0500592 /**
593 * Get the account name and type of the currently synced account.
594 * @param context Context to access preferences.
595 * @return null if no account name or type. Otherwise, the result will be
596 * an array of two Strings, the accountName and accountType, respectively.
597 */
598 private String[] getAccountNameAndType(Context context) {
599 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
600 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
601 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
602 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
603 return null;
604 }
605 return new String[] { accountName, accountType };
606 }
607
Leon Scroggins25230d72010-09-28 20:09:25 -0400608 // FIXME: Use a CursorLoader
609 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500610 String[] accountInfo = getAccountNameAndType(context);
611 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500612 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400613 }
614 Cursor cursor = null;
615 try {
616 cursor = context.getContentResolver().query(
617 BrowserContract.Bookmarks.CONTENT_URI,
618 new String[] { BrowserContract.Bookmarks._ID },
619 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
620 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
621 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
622 new String[] {
623 BrowserContract.ChromeSyncColumns
624 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500625 accountInfo[0],
626 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400627 null);
628 if (cursor != null && cursor.moveToFirst()) {
629 return cursor.getLong(0);
630 }
631 } finally {
632 if (cursor != null) cursor.close();
633 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500634 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400635 }
636
Leon Scroggins02065b02010-01-04 14:30:13 -0500637 /**
638 * Runnable to save a bookmark, so it can be performed in its own thread.
639 */
640 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400641 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500642 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200643 private Context mContext;
644 public SaveBookmarkRunnable(Context ctx, Message msg) {
645 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500646 mMessage = msg;
647 }
648 public void run() {
649 // Unbundle bookmark data.
650 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400651 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
652 String url = bundle.getString(BrowserContract.Bookmarks.URL);
653 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500654 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400655 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
656 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500657
658 // Save to the bookmarks DB.
659 try {
660 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400661 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
662 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500663 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200664 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500665 }
666 mMessage.arg1 = 1;
667 } catch (IllegalStateException e) {
668 mMessage.arg1 = 0;
669 }
670 mMessage.sendToTarget();
671 }
672 }
673
John Reckc8490812010-11-22 14:15:36 -0800674 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
675 Context mContext;
676 Long mId;
677
678 public UpdateBookmarkTask(Context context, long id) {
679 mContext = context;
680 mId = id;
681 }
682
683 @Override
684 protected Void doInBackground(ContentValues... params) {
685 if (params.length != 1) {
686 throw new IllegalArgumentException("No ContentValues provided!");
687 }
688 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
689 mContext.getContentResolver().update(
690 uri,
691 params[0], null, null);
692 return null;
693 }
694 }
695
Ben Murdoch1794fe22009-09-29 18:14:30 +0100696 private void createHandler() {
697 if (mHandler == null) {
698 mHandler = new Handler() {
699 @Override
700 public void handleMessage(Message msg) {
701 switch (msg.what) {
702 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500703 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100704 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
705 Toast.LENGTH_LONG).show();
706 } else {
707 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
708 Toast.LENGTH_LONG).show();
709 }
710 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400711 case TOUCH_ICON_DOWNLOADED:
712 Bundle b = msg.getData();
713 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400714 AddBookmarkPage.this,
715 b.getString(BrowserContract.Bookmarks.URL),
716 b.getString(BrowserContract.Bookmarks.TITLE),
717 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
718 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400719 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100720 }
721 }
722 };
723 }
724 }
725
The Android Open Source Project0c908882009-03-03 19:32:16 -0800726 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100727 * 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 -0800728 */
729 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100730 createHandler();
731
The Android Open Source Project0c908882009-03-03 19:32:16 -0800732 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100733 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100735
The Android Open Source Project0c908882009-03-03 19:32:16 -0800736 boolean emptyTitle = title.length() == 0;
737 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
738 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800739 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800740 if (emptyTitle) {
741 mTitle.setError(r.getText(R.string.bookmark_needs_title));
742 }
743 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400744 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800745 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400746 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100747
The Android Open Source Project0c908882009-03-03 19:32:16 -0800748 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000749 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800750 if (!mEditingFolder) {
751 try {
752 // We allow bookmarks with a javascript: scheme, but these will in most cases
753 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000754
John Reckc8490812010-11-22 14:15:36 -0800755 if (!url.toLowerCase().startsWith("javascript:")) {
756 URI uriObj = new URI(url);
757 String scheme = uriObj.getScheme();
758 if (!Bookmarks.urlHasAcceptableScheme(url)) {
759 // If the scheme was non-null, let the user know that we
760 // can't save their bookmark. If it was null, we'll assume
761 // they meant http when we parse it in the WebAddress class.
762 if (scheme != null) {
763 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
764 return false;
765 }
766 WebAddress address;
767 try {
768 address = new WebAddress(unfilteredUrl);
769 } catch (ParseException e) {
770 throw new URISyntaxException("", "");
771 }
772 if (address.getHost().length() == 0) {
773 throw new URISyntaxException("", "");
774 }
775 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000776 }
Ben Murdochde353622009-10-12 10:29:00 +0100777 }
John Reckc8490812010-11-22 14:15:36 -0800778 } catch (URISyntaxException e) {
779 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
780 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800781 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800782 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100783
Leon Scroggins88d08032010-10-21 15:17:10 -0400784 if (mSaveToHomeScreen) {
785 mEditingExisting = false;
786 }
787
788 boolean urlUnmodified = url.equals(mOriginalUrl);
789
Ben Murdoch1794fe22009-09-29 18:14:30 +0100790 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800791 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
792 ContentValues values = new ContentValues();
793 values.put(BrowserContract.Bookmarks.TITLE, title);
794 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
795 if (!mEditingFolder) {
796 values.put(BrowserContract.Bookmarks.URL, url);
797 if (!urlUnmodified) {
798 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
799 }
800 }
801 if (values.size() > 0) {
802 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
803 }
804 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100805 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400806 Bitmap thumbnail;
807 Bitmap favicon;
808 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400809 thumbnail = (Bitmap) mMap.getParcelable(
810 BrowserContract.Bookmarks.THUMBNAIL);
811 favicon = (Bitmap) mMap.getParcelable(
812 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400813 } else {
814 thumbnail = null;
815 favicon = null;
816 }
817
Ben Murdoch1794fe22009-09-29 18:14:30 +0100818 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400819 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
820 bundle.putString(BrowserContract.Bookmarks.URL, url);
821 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400822
823 if (mSaveToHomeScreen) {
824 if (mTouchIconUrl != null && urlUnmodified) {
825 Message msg = Message.obtain(mHandler,
826 TOUCH_ICON_DOWNLOADED);
827 msg.setData(bundle);
828 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400829 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400830 icon.execute(mTouchIconUrl);
831 } else {
832 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
833 title, null /*touchIcon*/, favicon));
834 }
835 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400836 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
837 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
838 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400839 // Post a message to write to the DB.
840 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
841 msg.setData(bundle);
842 // Start a new thread so as to not slow down the UI
843 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
844 t.start();
845 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100846 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000847 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800848 }
849 return true;
850 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800851}