blob: 5a55f6745cceaedbe52006ba8f5a97eceb1c11b7 [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;
Leon Scroggins504433a2011-01-13 12:26:52 -050020import com.android.browser.addbookmark.FolderSpinner;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -050021import com.android.browser.addbookmark.FolderSpinnerAdapter;
Leon Scroggins25230d72010-09-28 20:09:25 -040022
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040024import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070026import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040027import android.content.ContentValues;
28import android.content.Context;
29import android.content.CursorLoader;
Leon Scroggins III052ce662010-09-13 14:44:16 -040030import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040031import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040033import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010034import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040035import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070037import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.WebAddress;
John Reckc8490812010-11-22 14:15:36 -080039import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010041import android.os.Handler;
42import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040043import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040044import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040045import android.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040046import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040047import android.view.KeyEvent;
48import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040050import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040052import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040053import android.view.inputmethod.EditorInfo;
54import android.view.inputmethod.InputMethodManager;
55import android.widget.AdapterView;
56import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040058import android.widget.ListView;
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 Scroggins504433a2011-01-13 12:26:52 -050069 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener {
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;
Leon Scroggins504433a2011-01-13 12:26:52 -050087 private final int LOADER_ID_FIND_ROOT = 2;
88 private final int LOADER_ID_CHECK_FOR_DUPE = 3;
Leon Scroggins III052ce662010-09-13 14:44:16 -040089
The Android Open Source Project0c908882009-03-03 19:32:16 -080090 private EditText mTitle;
91 private EditText mAddress;
92 private TextView mButton;
93 private View mCancelButton;
94 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080095 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040097 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010098 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -050099 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400100 private View mDefaultView;
101 private View mFolderSelector;
102 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500103 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400104 private boolean mIsFolderNamerShowing;
105 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400106 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400107 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400108 private long mCurrentFolder = 0;
109 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400110 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400111 private TextView mFakeTitle;
112 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400113 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400114 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400115 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500116 private TextView mTopLevelLabel;
117 private Drawable mHeaderIcon;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400118 private static class Folder {
119 String Name;
120 long Id;
121 Folder(String name, long id) {
122 Name = name;
123 Id = id;
124 }
125 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126
Ben Murdoch1794fe22009-09-29 18:14:30 +0100127 // Message IDs
128 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400129 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100130
131 private Handler mHandler;
132
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100133 private InputMethodManager getInputMethodManager() {
134 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
135 }
136
Leon Scroggins8baaa632010-12-08 19:46:53 -0500137 private Uri getUriForFolder(long folder) {
138 Uri uri;
139 if (folder == mRootFolder) {
140 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
141 } else {
142 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
143 }
144 String[] accountInfo = getAccountNameAndType(this);
145 if (accountInfo != null) {
146 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
147 }
148 return uri;
149 }
150
Leon Scroggins III052ce662010-09-13 14:44:16 -0400151 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400152 public void onTop(int level, Object data) {
153 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400154 Folder folderData = (Folder) data;
155 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400156 LoaderManager manager = getLoaderManager();
157 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
158 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500159 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400160 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400161 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400162 completeOrCancelFolderNaming(true);
163 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500164 setShowBookmarkIcon(level == 1);
165 }
166
167 /**
168 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
169 * @param show True if the icon should visible, false otherwise.
170 */
171 private void setShowBookmarkIcon(boolean show) {
172 Drawable drawable = show ? mHeaderIcon: null;
173 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400174 }
175
Leon Scroggins74dbe012010-10-15 10:54:27 -0400176 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400177 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
178 if (v == mFolderNamer) {
179 if (v.getText().length() > 0) {
180 if (actionId == EditorInfo.IME_NULL) {
181 // Only want to do this once.
182 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400183 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
Michael Kolb31829b92010-10-01 11:50:21 -0700187 // Steal the key press; otherwise a newline will be added
188 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400190 return false;
191 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400193 private void switchToDefaultView(boolean changedFolder) {
194 mFolderSelector.setVisibility(View.GONE);
195 mDefaultView.setVisibility(View.VISIBLE);
196 mCrumbHolder.setVisibility(View.GONE);
197 mFakeTitle.setVisibility(View.VISIBLE);
198 if (changedFolder) {
199 Object data = mCrumbs.getTopData();
200 if (data != null) {
201 Folder folder = (Folder) data;
202 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500203 if (mCurrentFolder == mRootFolder) {
204 // The Spinner changed to show "Other folder ..." Change
205 // it back to "Bookmarks", which is position 0 if we are
206 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500207 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500208 } else {
209 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
210 }
211 }
212 } else {
213 // The user canceled selecting a folder. Revert back to the earlier
214 // selection.
215 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500216 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500217 } else {
Leon Scroggins504433a2011-01-13 12:26:52 -0500218 // FIXME: Need to find the actual folder.
219 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400220 }
221 }
222 }
223
Leon Scroggins III052ce662010-09-13 14:44:16 -0400224 @Override
225 public void onClick(View v) {
226 if (v == mButton) {
227 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400228 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400229 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400230 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700231 } else {
232 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400233 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400234 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700235 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400236 } else if (save()) {
237 finish();
238 }
239 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400240 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400241 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400242 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
243 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700244 } else {
245 finish();
246 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500247 } else if (v == mFolderCancel) {
248 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400249 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400250 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400251 mFolderNamer.setText(R.string.new_folder);
252 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700253 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400254 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400255 InputMethodManager imm = getInputMethodManager();
256 // Set the InputMethodManager to focus on the ListView so that it
257 // can transfer the focus to mFolderNamer.
258 imm.focusIn(mListView);
259 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400261 }
262
Leon Scroggins504433a2011-01-13 12:26:52 -0500263 // FolderSpinner.OnSetSelectionListener
264
Leon Scroggins88d08032010-10-21 15:17:10 -0400265 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500266 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500267 int intId = (int) id;
268 switch (intId) {
269 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400270 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400271 mSaveToHomeScreen = false;
272 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500273 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400274 // Create a short cut to the home screen
275 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400276 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500277 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400278 switchToFolderSelector();
279 break;
280 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500281 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400282 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500283 }
284
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500285 /**
286 * Finish naming a folder, and close the IME
287 * @param cancel If true, the new folder is not created. If false, the new
288 * folder is created and the user is taken inside it.
289 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400290 private void completeOrCancelFolderNaming(boolean cancel) {
291 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700292 String name = mFolderNamer.getText().toString();
293 long id = addFolderToCurrent(mFolderNamer.getText().toString());
294 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700295 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400296 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400297 mAddNewFolder.setVisibility(View.VISIBLE);
298 mAddSeparator.setVisibility(View.VISIBLE);
299 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400300 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700301 }
302
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700303 private long addFolderToCurrent(String name) {
304 // Add the folder to the database
305 ContentValues values = new ContentValues();
306 values.put(BrowserContract.Bookmarks.TITLE,
307 name);
308 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500309 String[] accountInfo = getAccountNameAndType(this);
310 if (accountInfo != null) {
311 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
312 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700313 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400314 long currentFolder;
315 Object data = mCrumbs.getTopData();
316 if (data != null) {
317 currentFolder = ((Folder) data).Id;
318 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400319 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400320 }
321 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700322 Uri uri = getContentResolver().insert(
323 BrowserContract.Bookmarks.CONTENT_URI, values);
324 if (uri != null) {
325 return ContentUris.parseId(uri);
326 } else {
327 return -1;
328 }
329 }
330
Leon Scroggins III052ce662010-09-13 14:44:16 -0400331 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500332 // Set the list to the top in case it is scrolled.
333 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400334 mDefaultView.setVisibility(View.GONE);
335 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400336 mCrumbHolder.setVisibility(View.VISIBLE);
337 mFakeTitle.setVisibility(View.GONE);
338 mAddNewFolder.setVisibility(View.VISIBLE);
339 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400340 }
341
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700342 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700343 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400344 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400345 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700346 }
347 }
348
Leon Scroggins III052ce662010-09-13 14:44:16 -0400349 @Override
350 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
351 String[] projection;
352 switch (id) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500353 case LOADER_ID_CHECK_FOR_DUPE:
354 projection = new String[] {
355 BrowserContract.Bookmarks._ID,
356 BrowserContract.Bookmarks.PARENT,
357 BrowserContract.Bookmarks.TITLE
358 };
359 return new CursorLoader(this,
360 BookmarkUtils.getBookmarksUri(this),
361 projection,
362 BrowserContract.Bookmarks.URL + " = ?",
363 new String[] { mOriginalUrl },
364 null);
365 case LOADER_ID_FIND_ROOT:
366 String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME);
367 String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE);
368
369 projection = new String[] { BrowserContract.Bookmarks._ID };
370 String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
371 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
372 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?";
373 String[] selArgs = new String[] {
374 BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
375 name,
376 type
377 };
378 return new CursorLoader(this,
379 BrowserContract.Bookmarks.CONTENT_URI,
380 projection,
381 selection,
382 selArgs,
383 null);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400384 case LOADER_ID_ALL_FOLDERS:
385 projection = new String[] {
386 BrowserContract.Bookmarks._ID,
387 BrowserContract.Bookmarks.PARENT,
388 BrowserContract.Bookmarks.TITLE,
389 BrowserContract.Bookmarks.IS_FOLDER
390 };
391 return new CursorLoader(this,
392 BrowserContract.Bookmarks.CONTENT_URI,
393 projection,
394 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
395 null,
396 null);
397 case LOADER_ID_FOLDER_CONTENTS:
398 projection = new String[] {
399 BrowserContract.Bookmarks._ID,
400 BrowserContract.Bookmarks.TITLE,
401 BrowserContract.Bookmarks.IS_FOLDER
402 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500403 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
404 if (mEditingFolder) {
405 where += " AND " + BrowserContract.Bookmarks._ID + " != "
406 + mMap.getLong(BrowserContract.Bookmarks._ID);
407 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400408 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500409 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400410 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500411 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400412 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500413 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400414 default:
415 throw new AssertionError("Asking for nonexistant loader!");
416 }
417 }
418
419 @Override
420 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
421 switch (loader.getId()) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500422 case LOADER_ID_CHECK_FOR_DUPE:
423 if (cursor != null && cursor.moveToFirst()) {
424 // Site is bookmarked.
425 mEditingExisting = true;
426 mFakeTitle.setText(R.string.edit_bookmark);
427 int index = cursor.getColumnIndexOrThrow(
428 BrowserContract.Bookmarks.PARENT);
429 mCurrentFolder = cursor.getLong(index);
430 index = cursor.getColumnIndexOrThrow(
431 BrowserContract.Bookmarks.TITLE);
432 String title = cursor.getString(index);
433 mTitle.setText(title);
434 index = cursor.getColumnIndexOrThrow(
435 BrowserContract.Bookmarks._ID);
436 long id = cursor.getLong(index);
437 mMap.putLong(BrowserContract.Bookmarks._ID, id);
438 }
439 onCurrentFolderFound();
440 getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE);
441 break;
442 case LOADER_ID_FIND_ROOT:
443 long root;
444 if (cursor != null && cursor.moveToFirst()) {
445 root = cursor.getLong(0);
446 } else {
447 root = BrowserProvider2.FIXED_ID_ROOT;
448 }
449 onRootFolderFound(root);
450 getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT);
451 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400452 case LOADER_ID_FOLDER_CONTENTS:
453 mAdapter.changeCursor(cursor);
454 break;
455 case LOADER_ID_ALL_FOLDERS:
456 long parent = mCurrentFolder;
457 int idIndex = cursor.getColumnIndexOrThrow(
458 BrowserContract.Bookmarks._ID);
459 int titleIndex = cursor.getColumnIndexOrThrow(
460 BrowserContract.Bookmarks.TITLE);
461 int parentIndex = cursor.getColumnIndexOrThrow(
462 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500463 // If the user is editing anything inside the "Other Bookmarks"
464 // folder, we need to stop searching up when we reach its parent.
465 // Find the root folder
466 moveCursorToFolder(cursor, mRootFolder, idIndex);
467 // omniparent is the folder which contains root, and therefore
468 // also the parent of the "Other Bookmarks" folder.
469 long omniparent = cursor.getLong(parentIndex);
470 Stack<Folder> folderStack = new Stack<Folder>();
471 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400472 // First, find the folder corresponding to the current
473 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500474 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400475 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400476 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500477 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400478 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400479 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400480 parent = cursor.getLong(parentIndex);
481 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400482 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500483 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400484 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400485 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800486 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400487 break;
488 default:
489 break;
490 }
491 }
492
Dianne Hackborn39772c82010-12-16 00:43:54 -0800493 public void onLoaderReset(Loader<Cursor> loader) {
494 switch (loader.getId()) {
495 case LOADER_ID_FOLDER_CONTENTS:
496 mAdapter.changeCursor(null);
497 break;
498 }
499 }
500
Leon Scroggins02081942010-11-01 17:52:42 -0400501 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500502 * Move cursor to the position that has folderToFind as its "_id".
503 * @param cursor Cursor containing folders in the bookmarks database
504 * @param folderToFind "_id" of the folder to move to.
505 * @param idIndex Index in cursor of "_id"
506 * @throws AssertionError if cursor is empty or there is no row with folderToFind
507 * as its "_id".
508 */
509 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
510 throws AssertionError {
511 if (!cursor.moveToFirst()) {
512 throw new AssertionError("No folders in the database!");
513 }
514 long folder;
515 do {
516 folder = cursor.getLong(idIndex);
517 } while (folder != folderToFind && cursor.moveToNext());
518 if (cursor.isAfterLast()) {
519 throw new AssertionError("Folder(id=" + folderToFind
520 + ") holding this bookmark does not exist!");
521 }
522 }
523
Leon Scroggins III052ce662010-09-13 14:44:16 -0400524 @Override
525 public void onItemClick(AdapterView<?> parent, View view, int position,
526 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400527 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400528 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400529 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400530 }
531
Leon Scroggins162f8352010-10-18 15:02:44 -0400532 private void setShowFolderNamer(boolean show) {
533 if (show != mIsFolderNamerShowing) {
534 mIsFolderNamerShowing = show;
535 if (show) {
536 // Set the selection to the folder namer so it will be in
537 // view.
538 mListView.addFooterView(mFolderNamerHolder);
539 } else {
540 mListView.removeFooterView(mFolderNamerHolder);
541 }
542 // Refresh the list.
543 mListView.setAdapter(mAdapter);
544 if (show) {
545 mListView.setSelection(mListView.getCount() - 1);
546 }
547 }
548 }
549
Leon Scroggins III052ce662010-09-13 14:44:16 -0400550 /**
551 * Shows a list of names of folders.
552 */
553 private class FolderAdapter extends CursorAdapter {
554 public FolderAdapter(Context context) {
555 super(context, null);
556 }
557
558 @Override
559 public void bindView(View view, Context context, Cursor cursor) {
560 ((TextView) view.findViewById(android.R.id.text1)).setText(
561 cursor.getString(cursor.getColumnIndexOrThrow(
562 BrowserContract.Bookmarks.TITLE)));
563 }
564
565 @Override
566 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700567 View view = LayoutInflater.from(context).inflate(
568 R.layout.folder_list_item, null);
569 view.setBackgroundDrawable(context.getResources().
570 getDrawable(android.R.drawable.list_selector_background));
571 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400572 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400573
574 @Override
575 public boolean isEmpty() {
576 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400577 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400578 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400579 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580
Leon Scrogginsc1129902010-12-08 15:28:33 -0500581 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 protected void onCreate(Bundle icicle) {
583 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500584 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100585
586 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100587
Leon Scroggins III052ce662010-09-13 14:44:16 -0400588 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100589
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400590 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700591
The Android Open Source Project0c908882009-03-03 19:32:16 -0800592 String title = null;
593 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100594
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400595 mFakeTitle = (TextView) findViewById(R.id.fake_title);
596
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800598 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800600 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800601 mMap = b;
602 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400603 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800604 if (mEditingFolder) {
605 findViewById(R.id.row_address).setVisibility(View.GONE);
606 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400607 } else {
608 int gravity = mMap.getInt("gravity", -1);
609 if (gravity != -1) {
610 WindowManager.LayoutParams l = window.getAttributes();
611 l.gravity = gravity;
612 window.setAttributes(l);
613 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400615 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
616 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
617 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700618 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400619 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620
621 mTitle = (EditText) findViewById(R.id.title);
622 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100623
Leon Scroggins III052ce662010-09-13 14:44:16 -0400624 mAddress = (EditText) findViewById(R.id.address);
625 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400628 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629
630 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400631 mCancelButton.setOnClickListener(this);
632
Leon Scroggins504433a2011-01-13 12:26:52 -0500633 mFolder = (FolderSpinner) findViewById(R.id.folder);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500634 mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
Leon Scroggins504433a2011-01-13 12:26:52 -0500635 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400636
637 mDefaultView = findViewById(R.id.default_view);
638 mFolderSelector = findViewById(R.id.folder_selector);
639
Leon Scroggins162f8352010-10-18 15:02:44 -0400640 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
641 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400642 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500643 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
644 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400645
646 mAddNewFolder = findViewById(R.id.add_new_folder);
647 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400648 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400649
Leon Scroggins74dbe012010-10-15 10:54:27 -0400650 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
651 mCrumbs.setUseBackButton(true);
652 mCrumbs.setController(this);
Leon Scroggins905250c2010-12-17 15:25:33 -0500653 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_bookmark_widget_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400654 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800655 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400656
Leon Scroggins III052ce662010-09-13 14:44:16 -0400657 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400658 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400659 View empty = findViewById(R.id.empty);
660 mListView.setEmptyView(empty);
661 mListView.setAdapter(mAdapter);
662 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400663 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500664
665 if (!window.getDecorView().isInTouchMode()) {
666 mButton.requestFocus();
667 }
668
669 String[] accountInfo = getAccountNameAndType(this);
670 if (accountInfo == null) {
671 onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT);
672 } else {
673 Bundle args = new Bundle();
674 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]);
675 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]);
676 getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this);
677 }
678
679 }
680
681 // Called once we have determined which folder is the root folder
682 private void onRootFolderFound(long root) {
683 mRootFolder = root;
684 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
685 mCurrentFolder = mRootFolder;
686 }
687 String name = getString(R.string.bookmarks);
688 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
689 new Folder(name, mRootFolder));
690 // To better match the other folders.
691 mTopLevelLabel.setCompoundDrawablePadding(6);
692 if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl)) {
693 onCurrentFolderFound();
694 } else {
695 // User is attempting to bookmark a site, rather than deliberately
696 // editing a bookmark. Rather than let them create a duplicate
697 // bookmark, see if the bookmark already exists.
698 getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this);
699 }
700 }
701
702 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400703 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500704 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400705 // Find all the folders
706 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500707 // Since we're not in the root folder, change the selection to other
708 // folder now. The text will get changed once we select the correct
709 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500710 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500711 } else {
712 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500713 if (!mEditingFolder) {
714 // Initially the "Bookmarks" folder should be showing, rather than
715 // the home screen. In the editing folder case, home screen is not
716 // an option, so "Bookmarks" folder is already at the top.
717 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
718 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400719 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400720 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400721 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500722}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500723 /**
724 * Get the account name and type of the currently synced account.
725 * @param context Context to access preferences.
726 * @return null if no account name or type. Otherwise, the result will be
727 * an array of two Strings, the accountName and accountType, respectively.
728 */
729 private String[] getAccountNameAndType(Context context) {
730 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
731 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
732 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
733 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
734 return null;
735 }
736 return new String[] { accountName, accountType };
737 }
738
Leon Scroggins02065b02010-01-04 14:30:13 -0500739 /**
740 * Runnable to save a bookmark, so it can be performed in its own thread.
741 */
742 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400743 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500744 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200745 private Context mContext;
746 public SaveBookmarkRunnable(Context ctx, Message msg) {
747 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500748 mMessage = msg;
749 }
750 public void run() {
751 // Unbundle bookmark data.
752 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400753 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
754 String url = bundle.getString(BrowserContract.Bookmarks.URL);
755 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500756 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400757 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
758 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500759
760 // Save to the bookmarks DB.
761 try {
762 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400763 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
764 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500765 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200766 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500767 }
768 mMessage.arg1 = 1;
769 } catch (IllegalStateException e) {
770 mMessage.arg1 = 0;
771 }
772 mMessage.sendToTarget();
773 }
774 }
775
John Reckc8490812010-11-22 14:15:36 -0800776 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
777 Context mContext;
778 Long mId;
779
780 public UpdateBookmarkTask(Context context, long id) {
781 mContext = context;
782 mId = id;
783 }
784
785 @Override
786 protected Void doInBackground(ContentValues... params) {
787 if (params.length != 1) {
788 throw new IllegalArgumentException("No ContentValues provided!");
789 }
790 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
791 mContext.getContentResolver().update(
792 uri,
793 params[0], null, null);
794 return null;
795 }
796 }
797
Ben Murdoch1794fe22009-09-29 18:14:30 +0100798 private void createHandler() {
799 if (mHandler == null) {
800 mHandler = new Handler() {
801 @Override
802 public void handleMessage(Message msg) {
803 switch (msg.what) {
804 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500805 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100806 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
807 Toast.LENGTH_LONG).show();
808 } else {
809 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
810 Toast.LENGTH_LONG).show();
811 }
812 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400813 case TOUCH_ICON_DOWNLOADED:
814 Bundle b = msg.getData();
815 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400816 AddBookmarkPage.this,
817 b.getString(BrowserContract.Bookmarks.URL),
818 b.getString(BrowserContract.Bookmarks.TITLE),
819 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
820 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400821 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100822 }
823 }
824 };
825 }
826 }
827
The Android Open Source Project0c908882009-03-03 19:32:16 -0800828 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100829 * 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 -0800830 */
831 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100832 createHandler();
833
The Android Open Source Project0c908882009-03-03 19:32:16 -0800834 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100835 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100837
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 boolean emptyTitle = title.length() == 0;
839 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
840 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800841 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800842 if (emptyTitle) {
843 mTitle.setError(r.getText(R.string.bookmark_needs_title));
844 }
845 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400846 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800847 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400848 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100849
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000851 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800852 if (!mEditingFolder) {
853 try {
854 // We allow bookmarks with a javascript: scheme, but these will in most cases
855 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000856
John Reckc8490812010-11-22 14:15:36 -0800857 if (!url.toLowerCase().startsWith("javascript:")) {
858 URI uriObj = new URI(url);
859 String scheme = uriObj.getScheme();
860 if (!Bookmarks.urlHasAcceptableScheme(url)) {
861 // If the scheme was non-null, let the user know that we
862 // can't save their bookmark. If it was null, we'll assume
863 // they meant http when we parse it in the WebAddress class.
864 if (scheme != null) {
865 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
866 return false;
867 }
868 WebAddress address;
869 try {
870 address = new WebAddress(unfilteredUrl);
871 } catch (ParseException e) {
872 throw new URISyntaxException("", "");
873 }
874 if (address.getHost().length() == 0) {
875 throw new URISyntaxException("", "");
876 }
877 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000878 }
Ben Murdochde353622009-10-12 10:29:00 +0100879 }
John Reckc8490812010-11-22 14:15:36 -0800880 } catch (URISyntaxException e) {
881 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
882 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800883 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800884 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100885
Leon Scroggins88d08032010-10-21 15:17:10 -0400886 if (mSaveToHomeScreen) {
887 mEditingExisting = false;
888 }
889
890 boolean urlUnmodified = url.equals(mOriginalUrl);
891
Ben Murdoch1794fe22009-09-29 18:14:30 +0100892 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800893 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
894 ContentValues values = new ContentValues();
895 values.put(BrowserContract.Bookmarks.TITLE, title);
896 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
897 if (!mEditingFolder) {
898 values.put(BrowserContract.Bookmarks.URL, url);
899 if (!urlUnmodified) {
900 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
901 }
902 }
903 if (values.size() > 0) {
904 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
905 }
906 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100907 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400908 Bitmap thumbnail;
909 Bitmap favicon;
910 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400911 thumbnail = (Bitmap) mMap.getParcelable(
912 BrowserContract.Bookmarks.THUMBNAIL);
913 favicon = (Bitmap) mMap.getParcelable(
914 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400915 } else {
916 thumbnail = null;
917 favicon = null;
918 }
919
Ben Murdoch1794fe22009-09-29 18:14:30 +0100920 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400921 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
922 bundle.putString(BrowserContract.Bookmarks.URL, url);
923 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400924
925 if (mSaveToHomeScreen) {
926 if (mTouchIconUrl != null && urlUnmodified) {
927 Message msg = Message.obtain(mHandler,
928 TOUCH_ICON_DOWNLOADED);
929 msg.setData(bundle);
930 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400931 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400932 icon.execute(mTouchIconUrl);
933 } else {
934 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
935 title, null /*touchIcon*/, favicon));
936 }
937 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400938 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
939 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
940 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400941 // Post a message to write to the DB.
942 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
943 msg.setData(bundle);
944 // Start a new thread so as to not slow down the UI
945 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
946 t.start();
947 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100948 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000949 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800950 }
951 return true;
952 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400953
954 /*
955 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
956 */
957 public static class CustomListView extends ListView {
958 private EditText mEditText;
959
960 public void addEditText(EditText editText) {
961 mEditText = editText;
962 }
963
964 public CustomListView(Context context) {
965 super(context);
966 }
967
968 public CustomListView(Context context, AttributeSet attrs) {
969 super(context, attrs);
970 }
971
972 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
973 super(context, attrs, defStyle);
974 }
975
976 @Override
977 public boolean checkInputConnectionProxy(View view) {
978 return view == mEditText;
979 }
980 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800981}