blob: e03c0f643e14c5fbf34a87eef5d4ff8f0a8d04bb [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 Scroggins162f8352010-10-18 15:02:44 -040044import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040045import android.view.KeyEvent;
46import android.view.LayoutInflater;
Leon Scroggins88d08032010-10-21 15:17:10 -040047import android.view.MenuItem;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040051import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040052import android.view.inputmethod.EditorInfo;
53import android.view.inputmethod.InputMethodManager;
54import android.widget.AdapterView;
55import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040057import android.widget.ListView;
Leon Scroggins88d08032010-10-21 15:17:10 -040058import android.widget.PopupMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059import android.widget.TextView;
60import android.widget.Toast;
61
Cary Clarkf2407c62009-09-04 12:25:10 -040062import java.net.URI;
63import java.net.URISyntaxException;
Leon Scroggins74dbe012010-10-15 10:54:27 -040064import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040065
66public class AddBookmarkPage extends Activity
67 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040068 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins88d08032010-10-21 15:17:10 -040069 BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080070
Michael Kolb370a4f32010-10-06 10:45:32 -070071 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040072 public static final String TOUCH_ICON_URL = "touch_icon_url";
73 // Place on an edited bookmark to remove the saved thumbnail
74 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
75 public static final String USER_AGENT = "user_agent";
Michael Kolb370a4f32010-10-06 10:45:32 -070076
John Reckc8490812010-11-22 14:15:36 -080077 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
78 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
79
Leon Scroggins74dbe012010-10-15 10:54:27 -040080 private static final int MAX_CRUMBS_SHOWN = 2;
81
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 private final String LOGTAG = "Bookmarks";
83
Leon Scroggins III052ce662010-09-13 14:44:16 -040084 // IDs for the CursorLoaders that are used.
85 private final int LOADER_ID_FOLDER_CONTENTS = 0;
86 private final int LOADER_ID_ALL_FOLDERS = 1;
87
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 private EditText mTitle;
89 private EditText mAddress;
90 private TextView mButton;
91 private View mCancelButton;
92 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080093 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080094 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040095 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010096 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040097 private TextView mFolder;
98 private View mDefaultView;
99 private View mFolderSelector;
100 private EditText mFolderNamer;
Leon Scroggins162f8352010-10-18 15:02:44 -0400101 private boolean mIsFolderNamerShowing;
102 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400103 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400104 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400105 private long mCurrentFolder = 0;
106 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400107 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400108 private TextView mFakeTitle;
109 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400110 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400111 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400112 private long mRootFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400113
114 private static class Folder {
115 String Name;
116 long Id;
117 Folder(String name, long id) {
118 Name = name;
119 Id = id;
120 }
121 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122
Ben Murdoch1794fe22009-09-29 18:14:30 +0100123 // Message IDs
124 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400125 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100126
127 private Handler mHandler;
128
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100129 private InputMethodManager getInputMethodManager() {
130 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
131 }
132
Leon Scroggins8baaa632010-12-08 19:46:53 -0500133 private Uri getUriForFolder(long folder) {
134 Uri uri;
135 if (folder == mRootFolder) {
136 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
137 } else {
138 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
139 }
140 String[] accountInfo = getAccountNameAndType(this);
141 if (accountInfo != null) {
142 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
143 }
144 return uri;
145 }
146
Leon Scroggins III052ce662010-09-13 14:44:16 -0400147 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400148 public void onTop(int level, Object data) {
149 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400150 Folder folderData = (Folder) data;
151 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400152 LoaderManager manager = getLoaderManager();
153 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
154 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500155 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400156 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400157 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400158 completeOrCancelFolderNaming(true);
159 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400160 }
161
Leon Scroggins74dbe012010-10-15 10:54:27 -0400162 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400163 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
164 if (v == mFolderNamer) {
165 if (v.getText().length() > 0) {
166 if (actionId == EditorInfo.IME_NULL) {
167 // Only want to do this once.
168 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400169 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400170 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400171 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 }
Michael Kolb31829b92010-10-01 11:50:21 -0700173 // Steal the key press; otherwise a newline will be added
174 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400176 return false;
177 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400179 private void switchToDefaultView(boolean changedFolder) {
180 mFolderSelector.setVisibility(View.GONE);
181 mDefaultView.setVisibility(View.VISIBLE);
182 mCrumbHolder.setVisibility(View.GONE);
183 mFakeTitle.setVisibility(View.VISIBLE);
184 if (changedFolder) {
185 Object data = mCrumbs.getTopData();
186 if (data != null) {
187 Folder folder = (Folder) data;
188 mCurrentFolder = folder.Id;
Leon Scroggins02081942010-11-01 17:52:42 -0400189 int resource = mCurrentFolder == mRootFolder ?
190 R.drawable.ic_menu_bookmarks :
191 com.android.internal.R.drawable.ic_menu_archive;
192 Drawable drawable = getResources().getDrawable(resource);
193 updateFolderLabel(folder.Name, drawable);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400194 }
195 }
196 }
197
Leon Scroggins III052ce662010-09-13 14:44:16 -0400198 @Override
199 public void onClick(View v) {
200 if (v == mButton) {
201 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400202 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400203 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400204 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700205 } else {
206 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400207 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400208 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700209 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400210 } else if (save()) {
211 finish();
212 }
213 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400214 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400215 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400216 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
217 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700218 } else {
219 finish();
220 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400221 } else if (v == mFolder) {
Leon Scroggins26318a12010-11-08 10:08:40 -0500222 PopupMenu popup = new PopupMenu(this, mFolder);
Leon Scroggins88d08032010-10-21 15:17:10 -0400223 popup.getMenuInflater().inflate(R.menu.folder_choice,
224 popup.getMenu());
John Reckc8490812010-11-22 14:15:36 -0800225 if (mEditingFolder) {
226 popup.getMenu().removeItem(R.id.home_screen);
227 }
Leon Scroggins88d08032010-10-21 15:17:10 -0400228 popup.setOnMenuItemClickListener(this);
229 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400230 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400231 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400232 mFolderNamer.setText(R.string.new_folder);
233 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700234 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400235 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400236 InputMethodManager imm = getInputMethodManager();
237 // Set the InputMethodManager to focus on the ListView so that it
238 // can transfer the focus to mFolderNamer.
239 imm.focusIn(mListView);
240 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400242 }
243
Leon Scroggins88d08032010-10-21 15:17:10 -0400244 @Override
245 public boolean onMenuItemClick(MenuItem item) {
246 switch(item.getItemId()) {
247 case R.id.bookmarks:
Leon Scroggins02081942010-11-01 17:52:42 -0400248 mCurrentFolder = mRootFolder;
249 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400250 mSaveToHomeScreen = false;
251 break;
252 case R.id.home_screen:
253 // Create a short cut to the home screen
254 mSaveToHomeScreen = true;
Leon Scroggins02081942010-11-01 17:52:42 -0400255 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400256 break;
257 case R.id.other:
258 switchToFolderSelector();
259 break;
260 default:
261 return false;
262 }
263 return true;
264 }
265
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400266 private void completeOrCancelFolderNaming(boolean cancel) {
267 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700268 String name = mFolderNamer.getText().toString();
269 long id = addFolderToCurrent(mFolderNamer.getText().toString());
270 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700271 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400272 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400273 mAddNewFolder.setVisibility(View.VISIBLE);
274 mAddSeparator.setVisibility(View.VISIBLE);
275 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400276 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700277 }
278
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700279 private long addFolderToCurrent(String name) {
280 // Add the folder to the database
281 ContentValues values = new ContentValues();
282 values.put(BrowserContract.Bookmarks.TITLE,
283 name);
284 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500285 String[] accountInfo = getAccountNameAndType(this);
286 if (accountInfo != null) {
287 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
288 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700289 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400290 long currentFolder;
291 Object data = mCrumbs.getTopData();
292 if (data != null) {
293 currentFolder = ((Folder) data).Id;
294 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400295 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400296 }
297 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700298 Uri uri = getContentResolver().insert(
299 BrowserContract.Bookmarks.CONTENT_URI, values);
300 if (uri != null) {
301 return ContentUris.parseId(uri);
302 } else {
303 return -1;
304 }
305 }
306
Leon Scroggins III052ce662010-09-13 14:44:16 -0400307 private void switchToFolderSelector() {
308 mDefaultView.setVisibility(View.GONE);
309 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400310 mCrumbHolder.setVisibility(View.VISIBLE);
311 mFakeTitle.setVisibility(View.GONE);
312 mAddNewFolder.setVisibility(View.VISIBLE);
313 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400314 }
315
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700316 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700317 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400318 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400319 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700320 }
321 }
322
Leon Scroggins III052ce662010-09-13 14:44:16 -0400323 @Override
324 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
325 String[] projection;
326 switch (id) {
327 case LOADER_ID_ALL_FOLDERS:
328 projection = new String[] {
329 BrowserContract.Bookmarks._ID,
330 BrowserContract.Bookmarks.PARENT,
331 BrowserContract.Bookmarks.TITLE,
332 BrowserContract.Bookmarks.IS_FOLDER
333 };
334 return new CursorLoader(this,
335 BrowserContract.Bookmarks.CONTENT_URI,
336 projection,
337 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
338 null,
339 null);
340 case LOADER_ID_FOLDER_CONTENTS:
341 projection = new String[] {
342 BrowserContract.Bookmarks._ID,
343 BrowserContract.Bookmarks.TITLE,
344 BrowserContract.Bookmarks.IS_FOLDER
345 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500346 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
347 if (mEditingFolder) {
348 where += " AND " + BrowserContract.Bookmarks._ID + " != "
349 + mMap.getLong(BrowserContract.Bookmarks._ID);
350 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400351 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500352 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400353 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500354 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400355 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500356 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400357 default:
358 throw new AssertionError("Asking for nonexistant loader!");
359 }
360 }
361
362 @Override
363 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
364 switch (loader.getId()) {
365 case LOADER_ID_FOLDER_CONTENTS:
366 mAdapter.changeCursor(cursor);
367 break;
368 case LOADER_ID_ALL_FOLDERS:
369 long parent = mCurrentFolder;
370 int idIndex = cursor.getColumnIndexOrThrow(
371 BrowserContract.Bookmarks._ID);
372 int titleIndex = cursor.getColumnIndexOrThrow(
373 BrowserContract.Bookmarks.TITLE);
374 int parentIndex = cursor.getColumnIndexOrThrow(
375 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500376 // If the user is editing anything inside the "Other Bookmarks"
377 // folder, we need to stop searching up when we reach its parent.
378 // Find the root folder
379 moveCursorToFolder(cursor, mRootFolder, idIndex);
380 // omniparent is the folder which contains root, and therefore
381 // also the parent of the "Other Bookmarks" folder.
382 long omniparent = cursor.getLong(parentIndex);
383 Stack<Folder> folderStack = new Stack<Folder>();
384 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400385 // First, find the folder corresponding to the current
386 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500387 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400388 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400389 if (parent == mCurrentFolder) {
Leon Scroggins02081942010-11-01 17:52:42 -0400390 Drawable draw = getResources().getDrawable(
391 com.android.internal.R.drawable.ic_menu_archive);
392 updateFolderLabel(name, draw);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400393 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400394 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400395 parent = cursor.getLong(parentIndex);
396 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400397 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500398 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400399 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400400 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400401 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400402 break;
403 default:
404 break;
405 }
406 }
407
Dianne Hackborn39772c82010-12-16 00:43:54 -0800408 public void onLoaderReset(Loader<Cursor> loader) {
409 switch (loader.getId()) {
410 case LOADER_ID_FOLDER_CONTENTS:
411 mAdapter.changeCursor(null);
412 break;
413 }
414 }
415
Leon Scroggins02081942010-11-01 17:52:42 -0400416 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500417 * Move cursor to the position that has folderToFind as its "_id".
418 * @param cursor Cursor containing folders in the bookmarks database
419 * @param folderToFind "_id" of the folder to move to.
420 * @param idIndex Index in cursor of "_id"
421 * @throws AssertionError if cursor is empty or there is no row with folderToFind
422 * as its "_id".
423 */
424 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
425 throws AssertionError {
426 if (!cursor.moveToFirst()) {
427 throw new AssertionError("No folders in the database!");
428 }
429 long folder;
430 do {
431 folder = cursor.getLong(idIndex);
432 } while (folder != folderToFind && cursor.moveToNext());
433 if (cursor.isAfterLast()) {
434 throw new AssertionError("Folder(id=" + folderToFind
435 + ") holding this bookmark does not exist!");
436 }
437 }
438
439 /**
Leon Scroggins02081942010-11-01 17:52:42 -0400440 * Update the name and image to show where the bookmark will be added
441 * @param name Name of the location to save (folder name, bookmarks, or home
442 * screen.
443 * @param drawable Image to show corresponding to the save location.
444 */
445 void updateFolderLabel(CharSequence name, Drawable drawable) {
446 mFolder.setText(name);
447 mFolder.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
448 null);
449 }
450
Leon Scroggins III052ce662010-09-13 14:44:16 -0400451 @Override
452 public void onItemClick(AdapterView<?> parent, View view, int position,
453 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400454 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400455 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400456 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400457 }
458
Leon Scroggins162f8352010-10-18 15:02:44 -0400459 private void setShowFolderNamer(boolean show) {
460 if (show != mIsFolderNamerShowing) {
461 mIsFolderNamerShowing = show;
462 if (show) {
463 // Set the selection to the folder namer so it will be in
464 // view.
465 mListView.addFooterView(mFolderNamerHolder);
466 } else {
467 mListView.removeFooterView(mFolderNamerHolder);
468 }
469 // Refresh the list.
470 mListView.setAdapter(mAdapter);
471 if (show) {
472 mListView.setSelection(mListView.getCount() - 1);
473 }
474 }
475 }
476
Leon Scroggins III052ce662010-09-13 14:44:16 -0400477 /**
478 * Shows a list of names of folders.
479 */
480 private class FolderAdapter extends CursorAdapter {
481 public FolderAdapter(Context context) {
482 super(context, null);
483 }
484
485 @Override
486 public void bindView(View view, Context context, Cursor cursor) {
487 ((TextView) view.findViewById(android.R.id.text1)).setText(
488 cursor.getString(cursor.getColumnIndexOrThrow(
489 BrowserContract.Bookmarks.TITLE)));
490 }
491
492 @Override
493 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700494 View view = LayoutInflater.from(context).inflate(
495 R.layout.folder_list_item, null);
496 view.setBackgroundDrawable(context.getResources().
497 getDrawable(android.R.drawable.list_selector_background));
498 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400499 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400500
501 @Override
502 public boolean isEmpty() {
503 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400504 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400505 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400506 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507
Leon Scrogginsc1129902010-12-08 15:28:33 -0500508 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 protected void onCreate(Bundle icicle) {
510 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500511 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100512
513 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100514
Leon Scroggins III052ce662010-09-13 14:44:16 -0400515 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100516
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400517 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700518
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 String title = null;
520 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100521
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400522 mFakeTitle = (TextView) findViewById(R.id.fake_title);
523
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800525 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800527 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 mMap = b;
529 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400530 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800531 if (mEditingFolder) {
532 findViewById(R.id.row_address).setVisibility(View.GONE);
533 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400534 } else {
535 int gravity = mMap.getInt("gravity", -1);
536 if (gravity != -1) {
537 WindowManager.LayoutParams l = window.getAttributes();
538 l.gravity = gravity;
539 window.setAttributes(l);
540 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800541 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400542 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
543 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
544 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700545 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400546 }
Leon Scroggins02081942010-11-01 17:52:42 -0400547 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700548 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400549 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800550 }
551
552 mTitle = (EditText) findViewById(R.id.title);
553 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100554
Leon Scroggins III052ce662010-09-13 14:44:16 -0400555 mAddress = (EditText) findViewById(R.id.address);
556 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557
The Android Open Source Project0c908882009-03-03 19:32:16 -0800558 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400559 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560
561 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400562 mCancelButton.setOnClickListener(this);
563
564 mFolder = (TextView) findViewById(R.id.folder);
565 mFolder.setOnClickListener(this);
566
567 mDefaultView = findViewById(R.id.default_view);
568 mFolderSelector = findViewById(R.id.folder_selector);
569
Leon Scroggins162f8352010-10-18 15:02:44 -0400570 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
571 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 mFolderNamer.setOnEditorActionListener(this);
573
574 mAddNewFolder = findViewById(R.id.add_new_folder);
575 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400576 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400577
Leon Scroggins74dbe012010-10-15 10:54:27 -0400578 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
579 mCrumbs.setUseBackButton(true);
580 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400581 String name = getString(R.string.bookmarks);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500582 mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400583 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800584 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400585
Leon Scroggins III052ce662010-09-13 14:44:16 -0400586 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400587 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400588 View empty = findViewById(R.id.empty);
589 mListView.setEmptyView(empty);
590 mListView.setAdapter(mAdapter);
591 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400592 mListView.addEditText(mFolderNamer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400593 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500594 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 // Find all the folders
596 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
597 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400598 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400599 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
600
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700601
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400602 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 mButton.requestFocus();
604 }
605 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100606
Leon Scroggins8baaa632010-12-08 19:46:53 -0500607 /**
608 * Get the account name and type of the currently synced account.
609 * @param context Context to access preferences.
610 * @return null if no account name or type. Otherwise, the result will be
611 * an array of two Strings, the accountName and accountType, respectively.
612 */
613 private String[] getAccountNameAndType(Context context) {
614 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
615 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
616 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
617 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
618 return null;
619 }
620 return new String[] { accountName, accountType };
621 }
622
Leon Scroggins25230d72010-09-28 20:09:25 -0400623 // FIXME: Use a CursorLoader
624 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500625 String[] accountInfo = getAccountNameAndType(context);
626 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500627 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400628 }
629 Cursor cursor = null;
630 try {
631 cursor = context.getContentResolver().query(
632 BrowserContract.Bookmarks.CONTENT_URI,
633 new String[] { BrowserContract.Bookmarks._ID },
634 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
635 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
636 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
637 new String[] {
638 BrowserContract.ChromeSyncColumns
639 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500640 accountInfo[0],
641 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400642 null);
643 if (cursor != null && cursor.moveToFirst()) {
644 return cursor.getLong(0);
645 }
646 } finally {
647 if (cursor != null) cursor.close();
648 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500649 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400650 }
651
Leon Scroggins02065b02010-01-04 14:30:13 -0500652 /**
653 * Runnable to save a bookmark, so it can be performed in its own thread.
654 */
655 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400656 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500657 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200658 private Context mContext;
659 public SaveBookmarkRunnable(Context ctx, Message msg) {
660 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500661 mMessage = msg;
662 }
663 public void run() {
664 // Unbundle bookmark data.
665 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400666 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
667 String url = bundle.getString(BrowserContract.Bookmarks.URL);
668 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500669 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400670 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
671 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500672
673 // Save to the bookmarks DB.
674 try {
675 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400676 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
677 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500678 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200679 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500680 }
681 mMessage.arg1 = 1;
682 } catch (IllegalStateException e) {
683 mMessage.arg1 = 0;
684 }
685 mMessage.sendToTarget();
686 }
687 }
688
John Reckc8490812010-11-22 14:15:36 -0800689 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
690 Context mContext;
691 Long mId;
692
693 public UpdateBookmarkTask(Context context, long id) {
694 mContext = context;
695 mId = id;
696 }
697
698 @Override
699 protected Void doInBackground(ContentValues... params) {
700 if (params.length != 1) {
701 throw new IllegalArgumentException("No ContentValues provided!");
702 }
703 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
704 mContext.getContentResolver().update(
705 uri,
706 params[0], null, null);
707 return null;
708 }
709 }
710
Ben Murdoch1794fe22009-09-29 18:14:30 +0100711 private void createHandler() {
712 if (mHandler == null) {
713 mHandler = new Handler() {
714 @Override
715 public void handleMessage(Message msg) {
716 switch (msg.what) {
717 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500718 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100719 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
720 Toast.LENGTH_LONG).show();
721 } else {
722 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
723 Toast.LENGTH_LONG).show();
724 }
725 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400726 case TOUCH_ICON_DOWNLOADED:
727 Bundle b = msg.getData();
728 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400729 AddBookmarkPage.this,
730 b.getString(BrowserContract.Bookmarks.URL),
731 b.getString(BrowserContract.Bookmarks.TITLE),
732 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
733 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400734 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100735 }
736 }
737 };
738 }
739 }
740
The Android Open Source Project0c908882009-03-03 19:32:16 -0800741 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100742 * 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 -0800743 */
744 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100745 createHandler();
746
The Android Open Source Project0c908882009-03-03 19:32:16 -0800747 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100748 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100750
The Android Open Source Project0c908882009-03-03 19:32:16 -0800751 boolean emptyTitle = title.length() == 0;
752 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
753 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800754 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800755 if (emptyTitle) {
756 mTitle.setError(r.getText(R.string.bookmark_needs_title));
757 }
758 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400759 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400761 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100762
The Android Open Source Project0c908882009-03-03 19:32:16 -0800763 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000764 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800765 if (!mEditingFolder) {
766 try {
767 // We allow bookmarks with a javascript: scheme, but these will in most cases
768 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000769
John Reckc8490812010-11-22 14:15:36 -0800770 if (!url.toLowerCase().startsWith("javascript:")) {
771 URI uriObj = new URI(url);
772 String scheme = uriObj.getScheme();
773 if (!Bookmarks.urlHasAcceptableScheme(url)) {
774 // If the scheme was non-null, let the user know that we
775 // can't save their bookmark. If it was null, we'll assume
776 // they meant http when we parse it in the WebAddress class.
777 if (scheme != null) {
778 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
779 return false;
780 }
781 WebAddress address;
782 try {
783 address = new WebAddress(unfilteredUrl);
784 } catch (ParseException e) {
785 throw new URISyntaxException("", "");
786 }
787 if (address.getHost().length() == 0) {
788 throw new URISyntaxException("", "");
789 }
790 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000791 }
Ben Murdochde353622009-10-12 10:29:00 +0100792 }
John Reckc8490812010-11-22 14:15:36 -0800793 } catch (URISyntaxException e) {
794 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
795 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800796 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800797 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100798
Leon Scroggins88d08032010-10-21 15:17:10 -0400799 if (mSaveToHomeScreen) {
800 mEditingExisting = false;
801 }
802
803 boolean urlUnmodified = url.equals(mOriginalUrl);
804
Ben Murdoch1794fe22009-09-29 18:14:30 +0100805 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800806 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
807 ContentValues values = new ContentValues();
808 values.put(BrowserContract.Bookmarks.TITLE, title);
809 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
810 if (!mEditingFolder) {
811 values.put(BrowserContract.Bookmarks.URL, url);
812 if (!urlUnmodified) {
813 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
814 }
815 }
816 if (values.size() > 0) {
817 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
818 }
819 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100820 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400821 Bitmap thumbnail;
822 Bitmap favicon;
823 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400824 thumbnail = (Bitmap) mMap.getParcelable(
825 BrowserContract.Bookmarks.THUMBNAIL);
826 favicon = (Bitmap) mMap.getParcelable(
827 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400828 } else {
829 thumbnail = null;
830 favicon = null;
831 }
832
Ben Murdoch1794fe22009-09-29 18:14:30 +0100833 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400834 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
835 bundle.putString(BrowserContract.Bookmarks.URL, url);
836 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400837
838 if (mSaveToHomeScreen) {
839 if (mTouchIconUrl != null && urlUnmodified) {
840 Message msg = Message.obtain(mHandler,
841 TOUCH_ICON_DOWNLOADED);
842 msg.setData(bundle);
843 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400844 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400845 icon.execute(mTouchIconUrl);
846 } else {
847 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
848 title, null /*touchIcon*/, favicon));
849 }
850 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400851 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
852 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
853 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400854 // Post a message to write to the DB.
855 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
856 msg.setData(bundle);
857 // Start a new thread so as to not slow down the UI
858 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
859 t.start();
860 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100861 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000862 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800863 }
864 return true;
865 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400866
867 /*
868 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
869 */
870 public static class CustomListView extends ListView {
871 private EditText mEditText;
872
873 public void addEditText(EditText editText) {
874 mEditText = editText;
875 }
876
877 public CustomListView(Context context) {
878 super(context);
879 }
880
881 public CustomListView(Context context, AttributeSet attrs) {
882 super(context, attrs);
883 }
884
885 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
886 super(context, attrs, defStyle);
887 }
888
889 @Override
890 public boolean checkInputConnectionProxy(View view) {
891 return view == mEditText;
892 }
893 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800894}