blob: a93a51888a0fd477b0e0a879ed92fe4fd4856e35 [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 Scroggins75630672011-01-13 17:56:15 -0500118 private View mRemoveLink;
119 private View mFakeTitleHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400120 private static class Folder {
121 String Name;
122 long Id;
123 Folder(String name, long id) {
124 Name = name;
125 Id = id;
126 }
127 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128
Ben Murdoch1794fe22009-09-29 18:14:30 +0100129 // Message IDs
130 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400131 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500132 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100133
134 private Handler mHandler;
135
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100136 private InputMethodManager getInputMethodManager() {
137 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
138 }
139
Leon Scroggins8baaa632010-12-08 19:46:53 -0500140 private Uri getUriForFolder(long folder) {
141 Uri uri;
142 if (folder == mRootFolder) {
143 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
144 } else {
145 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
146 }
147 String[] accountInfo = getAccountNameAndType(this);
148 if (accountInfo != null) {
149 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
150 }
151 return uri;
152 }
153
Leon Scroggins III052ce662010-09-13 14:44:16 -0400154 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400155 public void onTop(int level, Object data) {
156 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400157 Folder folderData = (Folder) data;
158 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400159 LoaderManager manager = getLoaderManager();
160 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
161 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500162 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400163 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400164 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400165 completeOrCancelFolderNaming(true);
166 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500167 setShowBookmarkIcon(level == 1);
168 }
169
170 /**
171 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
172 * @param show True if the icon should visible, false otherwise.
173 */
174 private void setShowBookmarkIcon(boolean show) {
175 Drawable drawable = show ? mHeaderIcon: null;
176 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400177 }
178
Leon Scroggins74dbe012010-10-15 10:54:27 -0400179 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400180 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
181 if (v == mFolderNamer) {
182 if (v.getText().length() > 0) {
183 if (actionId == EditorInfo.IME_NULL) {
184 // Only want to do this once.
185 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400186 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400187 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400188 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
Michael Kolb31829b92010-10-01 11:50:21 -0700190 // Steal the key press; otherwise a newline will be added
191 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400193 return false;
194 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800195
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400196 private void switchToDefaultView(boolean changedFolder) {
197 mFolderSelector.setVisibility(View.GONE);
198 mDefaultView.setVisibility(View.VISIBLE);
199 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500200 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400201 if (changedFolder) {
202 Object data = mCrumbs.getTopData();
203 if (data != null) {
204 Folder folder = (Folder) data;
205 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500206 if (mCurrentFolder == mRootFolder) {
207 // The Spinner changed to show "Other folder ..." Change
208 // it back to "Bookmarks", which is position 0 if we are
209 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500210 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500211 } else {
212 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
213 }
214 }
215 } else {
216 // The user canceled selecting a folder. Revert back to the earlier
217 // selection.
218 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500219 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500220 } else {
Leon Scroggins504433a2011-01-13 12:26:52 -0500221 // FIXME: Need to find the actual folder.
222 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400223 }
224 }
225 }
226
Leon Scroggins III052ce662010-09-13 14:44:16 -0400227 @Override
228 public void onClick(View v) {
229 if (v == mButton) {
230 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400231 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400232 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400233 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700234 } else {
235 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400236 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400237 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700238 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400239 } else if (save()) {
240 finish();
241 }
242 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400243 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400244 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400245 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
246 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700247 } else {
248 finish();
249 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500250 } else if (v == mFolderCancel) {
251 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400252 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400253 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400254 mFolderNamer.setText(R.string.new_folder);
255 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700256 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400257 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400258 InputMethodManager imm = getInputMethodManager();
259 // Set the InputMethodManager to focus on the ListView so that it
260 // can transfer the focus to mFolderNamer.
261 imm.focusIn(mListView);
262 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500263 } else if (v == mRemoveLink) {
264 if (!mEditingExisting) {
265 throw new AssertionError("Remove button should not be shown for"
266 + " new bookmarks");
267 }
268 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
269 createHandler();
270 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
271 BookmarkUtils.displayRemoveBookmarkDialog(id,
272 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400274 }
275
Leon Scroggins504433a2011-01-13 12:26:52 -0500276 // FolderSpinner.OnSetSelectionListener
277
Leon Scroggins88d08032010-10-21 15:17:10 -0400278 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500279 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500280 int intId = (int) id;
281 switch (intId) {
282 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400283 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400284 mSaveToHomeScreen = false;
285 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500286 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400287 // Create a short cut to the home screen
288 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400289 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500290 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400291 switchToFolderSelector();
292 break;
293 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500294 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400295 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500296 }
297
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500298 /**
299 * Finish naming a folder, and close the IME
300 * @param cancel If true, the new folder is not created. If false, the new
301 * folder is created and the user is taken inside it.
302 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400303 private void completeOrCancelFolderNaming(boolean cancel) {
304 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700305 String name = mFolderNamer.getText().toString();
306 long id = addFolderToCurrent(mFolderNamer.getText().toString());
307 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700308 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400309 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400310 mAddNewFolder.setVisibility(View.VISIBLE);
311 mAddSeparator.setVisibility(View.VISIBLE);
312 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400313 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700314 }
315
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700316 private long addFolderToCurrent(String name) {
317 // Add the folder to the database
318 ContentValues values = new ContentValues();
319 values.put(BrowserContract.Bookmarks.TITLE,
320 name);
321 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500322 String[] accountInfo = getAccountNameAndType(this);
323 if (accountInfo != null) {
324 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
325 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700326 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400327 long currentFolder;
328 Object data = mCrumbs.getTopData();
329 if (data != null) {
330 currentFolder = ((Folder) data).Id;
331 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400332 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400333 }
334 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700335 Uri uri = getContentResolver().insert(
336 BrowserContract.Bookmarks.CONTENT_URI, values);
337 if (uri != null) {
338 return ContentUris.parseId(uri);
339 } else {
340 return -1;
341 }
342 }
343
Leon Scroggins III052ce662010-09-13 14:44:16 -0400344 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500345 // Set the list to the top in case it is scrolled.
346 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400347 mDefaultView.setVisibility(View.GONE);
348 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400349 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500350 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400351 mAddNewFolder.setVisibility(View.VISIBLE);
352 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400353 }
354
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700355 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700356 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400357 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400358 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700359 }
360 }
361
Leon Scroggins III052ce662010-09-13 14:44:16 -0400362 @Override
363 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
364 String[] projection;
365 switch (id) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500366 case LOADER_ID_CHECK_FOR_DUPE:
367 projection = new String[] {
368 BrowserContract.Bookmarks._ID,
369 BrowserContract.Bookmarks.PARENT,
370 BrowserContract.Bookmarks.TITLE
371 };
372 return new CursorLoader(this,
373 BookmarkUtils.getBookmarksUri(this),
374 projection,
375 BrowserContract.Bookmarks.URL + " = ?",
376 new String[] { mOriginalUrl },
377 null);
378 case LOADER_ID_FIND_ROOT:
379 String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME);
380 String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE);
381
382 projection = new String[] { BrowserContract.Bookmarks._ID };
383 String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
384 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
385 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?";
386 String[] selArgs = new String[] {
387 BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
388 name,
389 type
390 };
391 return new CursorLoader(this,
392 BrowserContract.Bookmarks.CONTENT_URI,
393 projection,
394 selection,
395 selArgs,
396 null);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400397 case LOADER_ID_ALL_FOLDERS:
398 projection = new String[] {
399 BrowserContract.Bookmarks._ID,
400 BrowserContract.Bookmarks.PARENT,
401 BrowserContract.Bookmarks.TITLE,
402 BrowserContract.Bookmarks.IS_FOLDER
403 };
404 return new CursorLoader(this,
405 BrowserContract.Bookmarks.CONTENT_URI,
406 projection,
407 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
408 null,
409 null);
410 case LOADER_ID_FOLDER_CONTENTS:
411 projection = new String[] {
412 BrowserContract.Bookmarks._ID,
413 BrowserContract.Bookmarks.TITLE,
414 BrowserContract.Bookmarks.IS_FOLDER
415 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500416 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
417 if (mEditingFolder) {
418 where += " AND " + BrowserContract.Bookmarks._ID + " != "
419 + mMap.getLong(BrowserContract.Bookmarks._ID);
420 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400421 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500422 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400423 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500424 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400425 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500426 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400427 default:
428 throw new AssertionError("Asking for nonexistant loader!");
429 }
430 }
431
432 @Override
433 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
434 switch (loader.getId()) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500435 case LOADER_ID_CHECK_FOR_DUPE:
436 if (cursor != null && cursor.moveToFirst()) {
437 // Site is bookmarked.
438 mEditingExisting = true;
Leon Scroggins75630672011-01-13 17:56:15 -0500439 showRemoveButton();
Leon Scroggins504433a2011-01-13 12:26:52 -0500440 mFakeTitle.setText(R.string.edit_bookmark);
441 int index = cursor.getColumnIndexOrThrow(
442 BrowserContract.Bookmarks.PARENT);
443 mCurrentFolder = cursor.getLong(index);
444 index = cursor.getColumnIndexOrThrow(
445 BrowserContract.Bookmarks.TITLE);
446 String title = cursor.getString(index);
447 mTitle.setText(title);
448 index = cursor.getColumnIndexOrThrow(
449 BrowserContract.Bookmarks._ID);
450 long id = cursor.getLong(index);
451 mMap.putLong(BrowserContract.Bookmarks._ID, id);
452 }
453 onCurrentFolderFound();
454 getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE);
455 break;
456 case LOADER_ID_FIND_ROOT:
457 long root;
458 if (cursor != null && cursor.moveToFirst()) {
459 root = cursor.getLong(0);
460 } else {
461 root = BrowserProvider2.FIXED_ID_ROOT;
462 }
463 onRootFolderFound(root);
464 getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT);
465 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400466 case LOADER_ID_FOLDER_CONTENTS:
467 mAdapter.changeCursor(cursor);
468 break;
469 case LOADER_ID_ALL_FOLDERS:
470 long parent = mCurrentFolder;
471 int idIndex = cursor.getColumnIndexOrThrow(
472 BrowserContract.Bookmarks._ID);
473 int titleIndex = cursor.getColumnIndexOrThrow(
474 BrowserContract.Bookmarks.TITLE);
475 int parentIndex = cursor.getColumnIndexOrThrow(
476 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500477 // If the user is editing anything inside the "Other Bookmarks"
478 // folder, we need to stop searching up when we reach its parent.
479 // Find the root folder
480 moveCursorToFolder(cursor, mRootFolder, idIndex);
481 // omniparent is the folder which contains root, and therefore
482 // also the parent of the "Other Bookmarks" folder.
483 long omniparent = cursor.getLong(parentIndex);
484 Stack<Folder> folderStack = new Stack<Folder>();
485 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400486 // First, find the folder corresponding to the current
487 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500488 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400489 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400490 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500491 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400492 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400493 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400494 parent = cursor.getLong(parentIndex);
495 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400496 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500497 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400498 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400499 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800500 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400501 break;
502 default:
503 break;
504 }
505 }
506
Dianne Hackborn39772c82010-12-16 00:43:54 -0800507 public void onLoaderReset(Loader<Cursor> loader) {
508 switch (loader.getId()) {
509 case LOADER_ID_FOLDER_CONTENTS:
510 mAdapter.changeCursor(null);
511 break;
512 }
513 }
514
Leon Scroggins02081942010-11-01 17:52:42 -0400515 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500516 * Move cursor to the position that has folderToFind as its "_id".
517 * @param cursor Cursor containing folders in the bookmarks database
518 * @param folderToFind "_id" of the folder to move to.
519 * @param idIndex Index in cursor of "_id"
520 * @throws AssertionError if cursor is empty or there is no row with folderToFind
521 * as its "_id".
522 */
523 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
524 throws AssertionError {
525 if (!cursor.moveToFirst()) {
526 throw new AssertionError("No folders in the database!");
527 }
528 long folder;
529 do {
530 folder = cursor.getLong(idIndex);
531 } while (folder != folderToFind && cursor.moveToNext());
532 if (cursor.isAfterLast()) {
533 throw new AssertionError("Folder(id=" + folderToFind
534 + ") holding this bookmark does not exist!");
535 }
536 }
537
Leon Scroggins III052ce662010-09-13 14:44:16 -0400538 @Override
539 public void onItemClick(AdapterView<?> parent, View view, int position,
540 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400541 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400542 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400543 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400544 }
545
Leon Scroggins162f8352010-10-18 15:02:44 -0400546 private void setShowFolderNamer(boolean show) {
547 if (show != mIsFolderNamerShowing) {
548 mIsFolderNamerShowing = show;
549 if (show) {
550 // Set the selection to the folder namer so it will be in
551 // view.
552 mListView.addFooterView(mFolderNamerHolder);
553 } else {
554 mListView.removeFooterView(mFolderNamerHolder);
555 }
556 // Refresh the list.
557 mListView.setAdapter(mAdapter);
558 if (show) {
559 mListView.setSelection(mListView.getCount() - 1);
560 }
561 }
562 }
563
Leon Scroggins III052ce662010-09-13 14:44:16 -0400564 /**
565 * Shows a list of names of folders.
566 */
567 private class FolderAdapter extends CursorAdapter {
568 public FolderAdapter(Context context) {
569 super(context, null);
570 }
571
572 @Override
573 public void bindView(View view, Context context, Cursor cursor) {
574 ((TextView) view.findViewById(android.R.id.text1)).setText(
575 cursor.getString(cursor.getColumnIndexOrThrow(
576 BrowserContract.Bookmarks.TITLE)));
577 }
578
579 @Override
580 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700581 View view = LayoutInflater.from(context).inflate(
582 R.layout.folder_list_item, null);
583 view.setBackgroundDrawable(context.getResources().
584 getDrawable(android.R.drawable.list_selector_background));
585 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400586 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400587
588 @Override
589 public boolean isEmpty() {
590 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400591 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400592 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400593 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800594
Leon Scrogginsc1129902010-12-08 15:28:33 -0500595 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800596 protected void onCreate(Bundle icicle) {
597 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500598 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100599
600 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100601
Leon Scroggins III052ce662010-09-13 14:44:16 -0400602 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100603
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400604 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700605
The Android Open Source Project0c908882009-03-03 19:32:16 -0800606 String title = null;
607 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100608
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400609 mFakeTitle = (TextView) findViewById(R.id.fake_title);
610
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800612 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800614 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 mMap = b;
616 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400617 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800618 if (mEditingFolder) {
619 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500620 } else {
621 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800622 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400623 } else {
624 int gravity = mMap.getInt("gravity", -1);
625 if (gravity != -1) {
626 WindowManager.LayoutParams l = window.getAttributes();
627 l.gravity = gravity;
628 window.setAttributes(l);
629 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800630 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400631 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
632 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
633 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700634 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400635 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800636
637 mTitle = (EditText) findViewById(R.id.title);
638 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100639
Leon Scroggins III052ce662010-09-13 14:44:16 -0400640 mAddress = (EditText) findViewById(R.id.address);
641 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800642
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400644 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645
646 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400647 mCancelButton.setOnClickListener(this);
648
Leon Scroggins504433a2011-01-13 12:26:52 -0500649 mFolder = (FolderSpinner) findViewById(R.id.folder);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500650 mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
Leon Scroggins504433a2011-01-13 12:26:52 -0500651 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400652
653 mDefaultView = findViewById(R.id.default_view);
654 mFolderSelector = findViewById(R.id.folder_selector);
655
Leon Scroggins162f8352010-10-18 15:02:44 -0400656 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
657 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400658 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500659 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
660 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400661
662 mAddNewFolder = findViewById(R.id.add_new_folder);
663 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400664 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400665
Leon Scroggins74dbe012010-10-15 10:54:27 -0400666 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
667 mCrumbs.setUseBackButton(true);
668 mCrumbs.setController(this);
Leon Scroggins905250c2010-12-17 15:25:33 -0500669 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_bookmark_widget_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400670 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800671 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400672
Leon Scroggins III052ce662010-09-13 14:44:16 -0400673 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400674 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400675 View empty = findViewById(R.id.empty);
676 mListView.setEmptyView(empty);
677 mListView.setAdapter(mAdapter);
678 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400679 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500680
Leon Scroggins75630672011-01-13 17:56:15 -0500681 mFakeTitleHolder = findViewById(R.id.title_holder);
682
Leon Scroggins504433a2011-01-13 12:26:52 -0500683 if (!window.getDecorView().isInTouchMode()) {
684 mButton.requestFocus();
685 }
686
687 String[] accountInfo = getAccountNameAndType(this);
688 if (accountInfo == null) {
689 onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT);
690 } else {
691 Bundle args = new Bundle();
692 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]);
693 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]);
694 getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this);
695 }
696
697 }
698
Leon Scroggins75630672011-01-13 17:56:15 -0500699 private void showRemoveButton() {
700 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
701 mRemoveLink = findViewById(R.id.remove);
702 mRemoveLink.setVisibility(View.VISIBLE);
703 mRemoveLink.setOnClickListener(this);
704 }
705
Leon Scroggins504433a2011-01-13 12:26:52 -0500706 // Called once we have determined which folder is the root folder
707 private void onRootFolderFound(long root) {
708 mRootFolder = root;
709 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
710 mCurrentFolder = mRootFolder;
711 }
712 String name = getString(R.string.bookmarks);
713 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
714 new Folder(name, mRootFolder));
715 // To better match the other folders.
716 mTopLevelLabel.setCompoundDrawablePadding(6);
717 if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl)) {
718 onCurrentFolderFound();
719 } else {
720 // User is attempting to bookmark a site, rather than deliberately
721 // editing a bookmark. Rather than let them create a duplicate
722 // bookmark, see if the bookmark already exists.
723 getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this);
724 }
725 }
726
727 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400728 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500729 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400730 // Find all the folders
731 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500732 // Since we're not in the root folder, change the selection to other
733 // folder now. The text will get changed once we select the correct
734 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500735 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500736 } else {
737 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500738 if (!mEditingFolder) {
739 // Initially the "Bookmarks" folder should be showing, rather than
740 // the home screen. In the editing folder case, home screen is not
741 // an option, so "Bookmarks" folder is already at the top.
742 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
743 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400744 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400745 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400746 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500747}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500748 /**
749 * Get the account name and type of the currently synced account.
750 * @param context Context to access preferences.
751 * @return null if no account name or type. Otherwise, the result will be
752 * an array of two Strings, the accountName and accountType, respectively.
753 */
754 private String[] getAccountNameAndType(Context context) {
755 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
756 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
757 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
758 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
759 return null;
760 }
761 return new String[] { accountName, accountType };
762 }
763
Leon Scroggins02065b02010-01-04 14:30:13 -0500764 /**
765 * Runnable to save a bookmark, so it can be performed in its own thread.
766 */
767 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400768 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500769 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200770 private Context mContext;
771 public SaveBookmarkRunnable(Context ctx, Message msg) {
772 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500773 mMessage = msg;
774 }
775 public void run() {
776 // Unbundle bookmark data.
777 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400778 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
779 String url = bundle.getString(BrowserContract.Bookmarks.URL);
780 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500781 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400782 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
783 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500784
785 // Save to the bookmarks DB.
786 try {
787 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400788 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
789 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500790 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200791 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500792 }
793 mMessage.arg1 = 1;
794 } catch (IllegalStateException e) {
795 mMessage.arg1 = 0;
796 }
797 mMessage.sendToTarget();
798 }
799 }
800
John Reckc8490812010-11-22 14:15:36 -0800801 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
802 Context mContext;
803 Long mId;
804
805 public UpdateBookmarkTask(Context context, long id) {
806 mContext = context;
807 mId = id;
808 }
809
810 @Override
811 protected Void doInBackground(ContentValues... params) {
812 if (params.length != 1) {
813 throw new IllegalArgumentException("No ContentValues provided!");
814 }
815 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
816 mContext.getContentResolver().update(
817 uri,
818 params[0], null, null);
819 return null;
820 }
821 }
822
Ben Murdoch1794fe22009-09-29 18:14:30 +0100823 private void createHandler() {
824 if (mHandler == null) {
825 mHandler = new Handler() {
826 @Override
827 public void handleMessage(Message msg) {
828 switch (msg.what) {
829 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500830 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100831 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
832 Toast.LENGTH_LONG).show();
833 } else {
834 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
835 Toast.LENGTH_LONG).show();
836 }
837 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400838 case TOUCH_ICON_DOWNLOADED:
839 Bundle b = msg.getData();
840 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400841 AddBookmarkPage.this,
842 b.getString(BrowserContract.Bookmarks.URL),
843 b.getString(BrowserContract.Bookmarks.TITLE),
844 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
845 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400846 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500847 case BOOKMARK_DELETED:
848 finish();
849 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100850 }
851 }
852 };
853 }
854 }
855
The Android Open Source Project0c908882009-03-03 19:32:16 -0800856 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100857 * 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 -0800858 */
859 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100860 createHandler();
861
The Android Open Source Project0c908882009-03-03 19:32:16 -0800862 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100863 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100865
The Android Open Source Project0c908882009-03-03 19:32:16 -0800866 boolean emptyTitle = title.length() == 0;
867 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
868 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800869 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800870 if (emptyTitle) {
871 mTitle.setError(r.getText(R.string.bookmark_needs_title));
872 }
873 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400874 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800875 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400876 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100877
The Android Open Source Project0c908882009-03-03 19:32:16 -0800878 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000879 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800880 if (!mEditingFolder) {
881 try {
882 // We allow bookmarks with a javascript: scheme, but these will in most cases
883 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000884
John Reckc8490812010-11-22 14:15:36 -0800885 if (!url.toLowerCase().startsWith("javascript:")) {
886 URI uriObj = new URI(url);
887 String scheme = uriObj.getScheme();
888 if (!Bookmarks.urlHasAcceptableScheme(url)) {
889 // If the scheme was non-null, let the user know that we
890 // can't save their bookmark. If it was null, we'll assume
891 // they meant http when we parse it in the WebAddress class.
892 if (scheme != null) {
893 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
894 return false;
895 }
896 WebAddress address;
897 try {
898 address = new WebAddress(unfilteredUrl);
899 } catch (ParseException e) {
900 throw new URISyntaxException("", "");
901 }
902 if (address.getHost().length() == 0) {
903 throw new URISyntaxException("", "");
904 }
905 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000906 }
Ben Murdochde353622009-10-12 10:29:00 +0100907 }
John Reckc8490812010-11-22 14:15:36 -0800908 } catch (URISyntaxException e) {
909 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
910 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800911 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800912 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100913
Leon Scroggins88d08032010-10-21 15:17:10 -0400914 if (mSaveToHomeScreen) {
915 mEditingExisting = false;
916 }
917
918 boolean urlUnmodified = url.equals(mOriginalUrl);
919
Ben Murdoch1794fe22009-09-29 18:14:30 +0100920 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800921 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
922 ContentValues values = new ContentValues();
923 values.put(BrowserContract.Bookmarks.TITLE, title);
924 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
925 if (!mEditingFolder) {
926 values.put(BrowserContract.Bookmarks.URL, url);
927 if (!urlUnmodified) {
928 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
929 }
930 }
931 if (values.size() > 0) {
932 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
933 }
934 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100935 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400936 Bitmap thumbnail;
937 Bitmap favicon;
938 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400939 thumbnail = (Bitmap) mMap.getParcelable(
940 BrowserContract.Bookmarks.THUMBNAIL);
941 favicon = (Bitmap) mMap.getParcelable(
942 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400943 } else {
944 thumbnail = null;
945 favicon = null;
946 }
947
Ben Murdoch1794fe22009-09-29 18:14:30 +0100948 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400949 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
950 bundle.putString(BrowserContract.Bookmarks.URL, url);
951 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400952
953 if (mSaveToHomeScreen) {
954 if (mTouchIconUrl != null && urlUnmodified) {
955 Message msg = Message.obtain(mHandler,
956 TOUCH_ICON_DOWNLOADED);
957 msg.setData(bundle);
958 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400959 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400960 icon.execute(mTouchIconUrl);
961 } else {
962 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
963 title, null /*touchIcon*/, favicon));
964 }
965 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400966 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
967 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
968 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400969 // Post a message to write to the DB.
970 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
971 msg.setData(bundle);
972 // Start a new thread so as to not slow down the UI
973 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
974 t.start();
975 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100976 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000977 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800978 }
979 return true;
980 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400981
982 /*
983 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
984 */
985 public static class CustomListView extends ListView {
986 private EditText mEditText;
987
988 public void addEditText(EditText editText) {
989 mEditText = editText;
990 }
991
992 public CustomListView(Context context) {
993 super(context);
994 }
995
996 public CustomListView(Context context, AttributeSet attrs) {
997 super(context, attrs);
998 }
999
1000 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1001 super(context, attrs, defStyle);
1002 }
1003
1004 @Override
1005 public boolean checkInputConnectionProxy(View view) {
1006 return view == mEditText;
1007 }
1008 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001009}