blob: ee080ca85b798201fb0c02b5a3d5d84e6a52ccee [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 Scrogginsb3ae8802011-01-18 11:55:27 -0500108 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400109 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 Scrogginsb3ae8802011-01-18 11:55:27 -0500221 if (mCurrentFolder == mRootFolder) {
222 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
223 } else {
224 Object data = mCrumbs.getTopData();
225 if (data != null && ((Folder) data).Id == mCurrentFolder) {
226 // We are showing the correct folder heirarchy. The
227 // folder selector will say "Other folder..." Change it
228 // to say the name of the folder once again.
229 ((TextView) mFolder.getSelectedView()).setText(((Folder) data).Name);
230 } else {
231 // We are not be showing the correct folder heirarchy.
232 // Clear the Crumbs and find the proper folder
233 mCrumbs.clear();
234 setupTopCrumb();
235 LoaderManager manager = getLoaderManager();
236 manager.restartLoader(LOADER_ID_ALL_FOLDERS, null, this);
237 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
238
239 }
240 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400241 }
242 }
243 }
244
Leon Scroggins III052ce662010-09-13 14:44:16 -0400245 @Override
246 public void onClick(View v) {
247 if (v == mButton) {
248 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400249 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400250 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400251 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700252 } else {
253 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400254 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400255 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700256 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400257 } else if (save()) {
258 finish();
259 }
260 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400261 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400262 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400263 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
264 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700265 } else {
266 finish();
267 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500268 } else if (v == mFolderCancel) {
269 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400270 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400271 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400272 mFolderNamer.setText(R.string.new_folder);
273 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700274 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400275 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400276 InputMethodManager imm = getInputMethodManager();
277 // Set the InputMethodManager to focus on the ListView so that it
278 // can transfer the focus to mFolderNamer.
279 imm.focusIn(mListView);
280 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500281 } else if (v == mRemoveLink) {
282 if (!mEditingExisting) {
283 throw new AssertionError("Remove button should not be shown for"
284 + " new bookmarks");
285 }
286 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
287 createHandler();
288 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
289 BookmarkUtils.displayRemoveBookmarkDialog(id,
290 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800291 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400292 }
293
Leon Scroggins504433a2011-01-13 12:26:52 -0500294 // FolderSpinner.OnSetSelectionListener
295
Leon Scroggins88d08032010-10-21 15:17:10 -0400296 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500297 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500298 int intId = (int) id;
299 switch (intId) {
300 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400301 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400302 mSaveToHomeScreen = false;
303 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500304 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400305 // Create a short cut to the home screen
306 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400307 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500308 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400309 switchToFolderSelector();
310 break;
311 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500312 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400313 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500314 }
315
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500316 /**
317 * Finish naming a folder, and close the IME
318 * @param cancel If true, the new folder is not created. If false, the new
319 * folder is created and the user is taken inside it.
320 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400321 private void completeOrCancelFolderNaming(boolean cancel) {
322 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700323 String name = mFolderNamer.getText().toString();
324 long id = addFolderToCurrent(mFolderNamer.getText().toString());
325 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700326 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400327 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400328 mAddNewFolder.setVisibility(View.VISIBLE);
329 mAddSeparator.setVisibility(View.VISIBLE);
330 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400331 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700332 }
333
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700334 private long addFolderToCurrent(String name) {
335 // Add the folder to the database
336 ContentValues values = new ContentValues();
337 values.put(BrowserContract.Bookmarks.TITLE,
338 name);
339 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500340 String[] accountInfo = getAccountNameAndType(this);
341 if (accountInfo != null) {
342 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
343 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700344 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400345 long currentFolder;
346 Object data = mCrumbs.getTopData();
347 if (data != null) {
348 currentFolder = ((Folder) data).Id;
349 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400350 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400351 }
352 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700353 Uri uri = getContentResolver().insert(
354 BrowserContract.Bookmarks.CONTENT_URI, values);
355 if (uri != null) {
356 return ContentUris.parseId(uri);
357 } else {
358 return -1;
359 }
360 }
361
Leon Scroggins III052ce662010-09-13 14:44:16 -0400362 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500363 // Set the list to the top in case it is scrolled.
364 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400365 mDefaultView.setVisibility(View.GONE);
366 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400367 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500368 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400369 mAddNewFolder.setVisibility(View.VISIBLE);
370 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400371 }
372
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700373 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700374 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400375 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400376 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700377 }
378 }
379
Leon Scroggins III052ce662010-09-13 14:44:16 -0400380 @Override
381 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
382 String[] projection;
383 switch (id) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500384 case LOADER_ID_CHECK_FOR_DUPE:
385 projection = new String[] {
386 BrowserContract.Bookmarks._ID,
387 BrowserContract.Bookmarks.PARENT,
388 BrowserContract.Bookmarks.TITLE
389 };
390 return new CursorLoader(this,
391 BookmarkUtils.getBookmarksUri(this),
392 projection,
393 BrowserContract.Bookmarks.URL + " = ?",
394 new String[] { mOriginalUrl },
395 null);
396 case LOADER_ID_FIND_ROOT:
397 String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME);
398 String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE);
399
400 projection = new String[] { BrowserContract.Bookmarks._ID };
401 String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
402 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
403 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?";
404 String[] selArgs = new String[] {
405 BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
406 name,
407 type
408 };
409 return new CursorLoader(this,
410 BrowserContract.Bookmarks.CONTENT_URI,
411 projection,
412 selection,
413 selArgs,
414 null);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400415 case LOADER_ID_ALL_FOLDERS:
416 projection = new String[] {
417 BrowserContract.Bookmarks._ID,
418 BrowserContract.Bookmarks.PARENT,
419 BrowserContract.Bookmarks.TITLE,
420 BrowserContract.Bookmarks.IS_FOLDER
421 };
422 return new CursorLoader(this,
423 BrowserContract.Bookmarks.CONTENT_URI,
424 projection,
425 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
426 null,
427 null);
428 case LOADER_ID_FOLDER_CONTENTS:
429 projection = new String[] {
430 BrowserContract.Bookmarks._ID,
431 BrowserContract.Bookmarks.TITLE,
432 BrowserContract.Bookmarks.IS_FOLDER
433 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500434 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
435 if (mEditingFolder) {
436 where += " AND " + BrowserContract.Bookmarks._ID + " != "
437 + mMap.getLong(BrowserContract.Bookmarks._ID);
438 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400439 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500440 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400441 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500442 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400443 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500444 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400445 default:
446 throw new AssertionError("Asking for nonexistant loader!");
447 }
448 }
449
450 @Override
451 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
452 switch (loader.getId()) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500453 case LOADER_ID_CHECK_FOR_DUPE:
454 if (cursor != null && cursor.moveToFirst()) {
455 // Site is bookmarked.
456 mEditingExisting = true;
Leon Scroggins75630672011-01-13 17:56:15 -0500457 showRemoveButton();
Leon Scroggins504433a2011-01-13 12:26:52 -0500458 mFakeTitle.setText(R.string.edit_bookmark);
459 int index = cursor.getColumnIndexOrThrow(
460 BrowserContract.Bookmarks.PARENT);
461 mCurrentFolder = cursor.getLong(index);
462 index = cursor.getColumnIndexOrThrow(
463 BrowserContract.Bookmarks.TITLE);
464 String title = cursor.getString(index);
465 mTitle.setText(title);
466 index = cursor.getColumnIndexOrThrow(
467 BrowserContract.Bookmarks._ID);
468 long id = cursor.getLong(index);
469 mMap.putLong(BrowserContract.Bookmarks._ID, id);
470 }
471 onCurrentFolderFound();
472 getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE);
473 break;
474 case LOADER_ID_FIND_ROOT:
475 long root;
476 if (cursor != null && cursor.moveToFirst()) {
477 root = cursor.getLong(0);
478 } else {
479 root = BrowserProvider2.FIXED_ID_ROOT;
480 }
481 onRootFolderFound(root);
482 getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT);
483 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400484 case LOADER_ID_FOLDER_CONTENTS:
485 mAdapter.changeCursor(cursor);
486 break;
487 case LOADER_ID_ALL_FOLDERS:
488 long parent = mCurrentFolder;
489 int idIndex = cursor.getColumnIndexOrThrow(
490 BrowserContract.Bookmarks._ID);
491 int titleIndex = cursor.getColumnIndexOrThrow(
492 BrowserContract.Bookmarks.TITLE);
493 int parentIndex = cursor.getColumnIndexOrThrow(
494 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500495 // If the user is editing anything inside the "Other Bookmarks"
496 // folder, we need to stop searching up when we reach its parent.
497 // Find the root folder
498 moveCursorToFolder(cursor, mRootFolder, idIndex);
499 // omniparent is the folder which contains root, and therefore
500 // also the parent of the "Other Bookmarks" folder.
501 long omniparent = cursor.getLong(parentIndex);
502 Stack<Folder> folderStack = new Stack<Folder>();
503 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400504 // First, find the folder corresponding to the current
505 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500506 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400507 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400508 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500509 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400510 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400511 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400512 parent = cursor.getLong(parentIndex);
513 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400514 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500515 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400516 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400517 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800518 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400519 break;
520 default:
521 break;
522 }
523 }
524
Dianne Hackborn39772c82010-12-16 00:43:54 -0800525 public void onLoaderReset(Loader<Cursor> loader) {
526 switch (loader.getId()) {
527 case LOADER_ID_FOLDER_CONTENTS:
528 mAdapter.changeCursor(null);
529 break;
530 }
531 }
532
Leon Scroggins02081942010-11-01 17:52:42 -0400533 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500534 * Move cursor to the position that has folderToFind as its "_id".
535 * @param cursor Cursor containing folders in the bookmarks database
536 * @param folderToFind "_id" of the folder to move to.
537 * @param idIndex Index in cursor of "_id"
538 * @throws AssertionError if cursor is empty or there is no row with folderToFind
539 * as its "_id".
540 */
541 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
542 throws AssertionError {
543 if (!cursor.moveToFirst()) {
544 throw new AssertionError("No folders in the database!");
545 }
546 long folder;
547 do {
548 folder = cursor.getLong(idIndex);
549 } while (folder != folderToFind && cursor.moveToNext());
550 if (cursor.isAfterLast()) {
551 throw new AssertionError("Folder(id=" + folderToFind
552 + ") holding this bookmark does not exist!");
553 }
554 }
555
Leon Scroggins III052ce662010-09-13 14:44:16 -0400556 @Override
557 public void onItemClick(AdapterView<?> parent, View view, int position,
558 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400559 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400560 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400561 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400562 }
563
Leon Scroggins162f8352010-10-18 15:02:44 -0400564 private void setShowFolderNamer(boolean show) {
565 if (show != mIsFolderNamerShowing) {
566 mIsFolderNamerShowing = show;
567 if (show) {
568 // Set the selection to the folder namer so it will be in
569 // view.
570 mListView.addFooterView(mFolderNamerHolder);
571 } else {
572 mListView.removeFooterView(mFolderNamerHolder);
573 }
574 // Refresh the list.
575 mListView.setAdapter(mAdapter);
576 if (show) {
577 mListView.setSelection(mListView.getCount() - 1);
578 }
579 }
580 }
581
Leon Scroggins III052ce662010-09-13 14:44:16 -0400582 /**
583 * Shows a list of names of folders.
584 */
585 private class FolderAdapter extends CursorAdapter {
586 public FolderAdapter(Context context) {
587 super(context, null);
588 }
589
590 @Override
591 public void bindView(View view, Context context, Cursor cursor) {
592 ((TextView) view.findViewById(android.R.id.text1)).setText(
593 cursor.getString(cursor.getColumnIndexOrThrow(
594 BrowserContract.Bookmarks.TITLE)));
595 }
596
597 @Override
598 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700599 View view = LayoutInflater.from(context).inflate(
600 R.layout.folder_list_item, null);
601 view.setBackgroundDrawable(context.getResources().
602 getDrawable(android.R.drawable.list_selector_background));
603 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400604 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400605
606 @Override
607 public boolean isEmpty() {
608 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400609 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400610 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400611 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612
Leon Scrogginsc1129902010-12-08 15:28:33 -0500613 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 protected void onCreate(Bundle icicle) {
615 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500616 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100617
618 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100619
Leon Scroggins III052ce662010-09-13 14:44:16 -0400620 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100621
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400622 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700623
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 String title = null;
625 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100626
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400627 mFakeTitle = (TextView) findViewById(R.id.fake_title);
628
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800630 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800632 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 mMap = b;
634 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400635 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800636 if (mEditingFolder) {
637 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500638 } else {
639 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800640 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400641 } else {
642 int gravity = mMap.getInt("gravity", -1);
643 if (gravity != -1) {
644 WindowManager.LayoutParams l = window.getAttributes();
645 l.gravity = gravity;
646 window.setAttributes(l);
647 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400649 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
650 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
651 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700652 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400653 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800654
655 mTitle = (EditText) findViewById(R.id.title);
656 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100657
Leon Scroggins III052ce662010-09-13 14:44:16 -0400658 mAddress = (EditText) findViewById(R.id.address);
659 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800660
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400662 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663
664 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400665 mCancelButton.setOnClickListener(this);
666
Leon Scroggins504433a2011-01-13 12:26:52 -0500667 mFolder = (FolderSpinner) findViewById(R.id.folder);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500668 mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
Leon Scroggins504433a2011-01-13 12:26:52 -0500669 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400670
671 mDefaultView = findViewById(R.id.default_view);
672 mFolderSelector = findViewById(R.id.folder_selector);
673
Leon Scroggins162f8352010-10-18 15:02:44 -0400674 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
675 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400676 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500677 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
678 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400679
680 mAddNewFolder = findViewById(R.id.add_new_folder);
681 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400682 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400683
Leon Scroggins74dbe012010-10-15 10:54:27 -0400684 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
685 mCrumbs.setUseBackButton(true);
686 mCrumbs.setController(this);
Leon Scroggins905250c2010-12-17 15:25:33 -0500687 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_bookmark_widget_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400688 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800689 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400690
Leon Scroggins III052ce662010-09-13 14:44:16 -0400691 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400692 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400693 View empty = findViewById(R.id.empty);
694 mListView.setEmptyView(empty);
695 mListView.setAdapter(mAdapter);
696 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400697 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500698
Leon Scroggins75630672011-01-13 17:56:15 -0500699 mFakeTitleHolder = findViewById(R.id.title_holder);
700
Leon Scroggins504433a2011-01-13 12:26:52 -0500701 if (!window.getDecorView().isInTouchMode()) {
702 mButton.requestFocus();
703 }
704
705 String[] accountInfo = getAccountNameAndType(this);
706 if (accountInfo == null) {
707 onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT);
708 } else {
709 Bundle args = new Bundle();
710 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]);
711 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]);
712 getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this);
713 }
714
715 }
716
Leon Scroggins75630672011-01-13 17:56:15 -0500717 private void showRemoveButton() {
718 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
719 mRemoveLink = findViewById(R.id.remove);
720 mRemoveLink.setVisibility(View.VISIBLE);
721 mRemoveLink.setOnClickListener(this);
722 }
723
Leon Scroggins504433a2011-01-13 12:26:52 -0500724 // Called once we have determined which folder is the root folder
725 private void onRootFolderFound(long root) {
726 mRootFolder = root;
727 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
728 mCurrentFolder = mRootFolder;
729 }
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500730 setupTopCrumb();
Leon Scroggins504433a2011-01-13 12:26:52 -0500731 if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl)) {
732 onCurrentFolderFound();
733 } else {
734 // User is attempting to bookmark a site, rather than deliberately
735 // editing a bookmark. Rather than let them create a duplicate
736 // bookmark, see if the bookmark already exists.
737 getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this);
738 }
739 }
740
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500741 private void setupTopCrumb() {
742 String name = getString(R.string.bookmarks);
743 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
744 new Folder(name, mRootFolder));
745 // To better match the other folders.
746 mTopLevelLabel.setCompoundDrawablePadding(6);
747 }
748
Leon Scroggins504433a2011-01-13 12:26:52 -0500749 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400750 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500751 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400752 // Find all the folders
753 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500754 // Since we're not in the root folder, change the selection to other
755 // folder now. The text will get changed once we select the correct
756 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500757 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500758 } else {
759 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500760 if (!mEditingFolder) {
761 // Initially the "Bookmarks" folder should be showing, rather than
762 // the home screen. In the editing folder case, home screen is not
763 // an option, so "Bookmarks" folder is already at the top.
764 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
765 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400766 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400767 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400768 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500769}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500770 /**
771 * Get the account name and type of the currently synced account.
772 * @param context Context to access preferences.
773 * @return null if no account name or type. Otherwise, the result will be
774 * an array of two Strings, the accountName and accountType, respectively.
775 */
776 private String[] getAccountNameAndType(Context context) {
777 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
778 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
779 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
780 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
781 return null;
782 }
783 return new String[] { accountName, accountType };
784 }
785
Leon Scroggins02065b02010-01-04 14:30:13 -0500786 /**
787 * Runnable to save a bookmark, so it can be performed in its own thread.
788 */
789 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400790 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500791 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200792 private Context mContext;
793 public SaveBookmarkRunnable(Context ctx, Message msg) {
794 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500795 mMessage = msg;
796 }
797 public void run() {
798 // Unbundle bookmark data.
799 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400800 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
801 String url = bundle.getString(BrowserContract.Bookmarks.URL);
802 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500803 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400804 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
805 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500806
807 // Save to the bookmarks DB.
808 try {
809 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400810 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
811 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500812 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200813 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500814 }
815 mMessage.arg1 = 1;
816 } catch (IllegalStateException e) {
817 mMessage.arg1 = 0;
818 }
819 mMessage.sendToTarget();
820 }
821 }
822
John Reckc8490812010-11-22 14:15:36 -0800823 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
824 Context mContext;
825 Long mId;
826
827 public UpdateBookmarkTask(Context context, long id) {
828 mContext = context;
829 mId = id;
830 }
831
832 @Override
833 protected Void doInBackground(ContentValues... params) {
834 if (params.length != 1) {
835 throw new IllegalArgumentException("No ContentValues provided!");
836 }
837 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
838 mContext.getContentResolver().update(
839 uri,
840 params[0], null, null);
841 return null;
842 }
843 }
844
Ben Murdoch1794fe22009-09-29 18:14:30 +0100845 private void createHandler() {
846 if (mHandler == null) {
847 mHandler = new Handler() {
848 @Override
849 public void handleMessage(Message msg) {
850 switch (msg.what) {
851 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500852 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100853 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
854 Toast.LENGTH_LONG).show();
855 } else {
856 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
857 Toast.LENGTH_LONG).show();
858 }
859 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400860 case TOUCH_ICON_DOWNLOADED:
861 Bundle b = msg.getData();
862 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400863 AddBookmarkPage.this,
864 b.getString(BrowserContract.Bookmarks.URL),
865 b.getString(BrowserContract.Bookmarks.TITLE),
866 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
867 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400868 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500869 case BOOKMARK_DELETED:
870 finish();
871 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100872 }
873 }
874 };
875 }
876 }
877
The Android Open Source Project0c908882009-03-03 19:32:16 -0800878 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100879 * 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 -0800880 */
881 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100882 createHandler();
883
The Android Open Source Project0c908882009-03-03 19:32:16 -0800884 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100885 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100887
The Android Open Source Project0c908882009-03-03 19:32:16 -0800888 boolean emptyTitle = title.length() == 0;
889 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
890 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800891 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800892 if (emptyTitle) {
893 mTitle.setError(r.getText(R.string.bookmark_needs_title));
894 }
895 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400896 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800897 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400898 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100899
The Android Open Source Project0c908882009-03-03 19:32:16 -0800900 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000901 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800902 if (!mEditingFolder) {
903 try {
904 // We allow bookmarks with a javascript: scheme, but these will in most cases
905 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000906
John Reckc8490812010-11-22 14:15:36 -0800907 if (!url.toLowerCase().startsWith("javascript:")) {
908 URI uriObj = new URI(url);
909 String scheme = uriObj.getScheme();
910 if (!Bookmarks.urlHasAcceptableScheme(url)) {
911 // If the scheme was non-null, let the user know that we
912 // can't save their bookmark. If it was null, we'll assume
913 // they meant http when we parse it in the WebAddress class.
914 if (scheme != null) {
915 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
916 return false;
917 }
918 WebAddress address;
919 try {
920 address = new WebAddress(unfilteredUrl);
921 } catch (ParseException e) {
922 throw new URISyntaxException("", "");
923 }
924 if (address.getHost().length() == 0) {
925 throw new URISyntaxException("", "");
926 }
927 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000928 }
Ben Murdochde353622009-10-12 10:29:00 +0100929 }
John Reckc8490812010-11-22 14:15:36 -0800930 } catch (URISyntaxException e) {
931 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
932 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800933 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800934 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100935
Leon Scroggins88d08032010-10-21 15:17:10 -0400936 if (mSaveToHomeScreen) {
937 mEditingExisting = false;
938 }
939
940 boolean urlUnmodified = url.equals(mOriginalUrl);
941
Ben Murdoch1794fe22009-09-29 18:14:30 +0100942 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800943 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
944 ContentValues values = new ContentValues();
945 values.put(BrowserContract.Bookmarks.TITLE, title);
946 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
947 if (!mEditingFolder) {
948 values.put(BrowserContract.Bookmarks.URL, url);
949 if (!urlUnmodified) {
950 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
951 }
952 }
953 if (values.size() > 0) {
954 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
955 }
956 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100957 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400958 Bitmap thumbnail;
959 Bitmap favicon;
960 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400961 thumbnail = (Bitmap) mMap.getParcelable(
962 BrowserContract.Bookmarks.THUMBNAIL);
963 favicon = (Bitmap) mMap.getParcelable(
964 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400965 } else {
966 thumbnail = null;
967 favicon = null;
968 }
969
Ben Murdoch1794fe22009-09-29 18:14:30 +0100970 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400971 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
972 bundle.putString(BrowserContract.Bookmarks.URL, url);
973 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400974
975 if (mSaveToHomeScreen) {
976 if (mTouchIconUrl != null && urlUnmodified) {
977 Message msg = Message.obtain(mHandler,
978 TOUCH_ICON_DOWNLOADED);
979 msg.setData(bundle);
980 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400981 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400982 icon.execute(mTouchIconUrl);
983 } else {
984 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
985 title, null /*touchIcon*/, favicon));
986 }
987 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400988 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
989 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
990 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400991 // Post a message to write to the DB.
992 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
993 msg.setData(bundle);
994 // Start a new thread so as to not slow down the UI
995 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
996 t.start();
997 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100998 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000999 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001000 }
1001 return true;
1002 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001003
1004 /*
1005 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1006 */
1007 public static class CustomListView extends ListView {
1008 private EditText mEditText;
1009
1010 public void addEditText(EditText editText) {
1011 mEditText = editText;
1012 }
1013
1014 public CustomListView(Context context) {
1015 super(context);
1016 }
1017
1018 public CustomListView(Context context, AttributeSet attrs) {
1019 super(context, attrs);
1020 }
1021
1022 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1023 super(context, attrs, defStyle);
1024 }
1025
1026 @Override
1027 public boolean checkInputConnectionProxy(View view) {
1028 return view == mEditText;
1029 }
1030 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001031}