blob: fc3b0a98dc4da74f4ba4d8e1c80375a817ea90f9 [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";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050076 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070077
John Reckc8490812010-11-22 14:15:36 -080078 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
79 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
80
Leon Scroggins74dbe012010-10-15 10:54:27 -040081 private static final int MAX_CRUMBS_SHOWN = 2;
82
The Android Open Source Project0c908882009-03-03 19:32:16 -080083 private final String LOGTAG = "Bookmarks";
84
Leon Scroggins III052ce662010-09-13 14:44:16 -040085 // IDs for the CursorLoaders that are used.
86 private final int LOADER_ID_FOLDER_CONTENTS = 0;
87 private final int LOADER_ID_ALL_FOLDERS = 1;
Leon Scroggins504433a2011-01-13 12:26:52 -050088 private final int LOADER_ID_FIND_ROOT = 2;
89 private final int LOADER_ID_CHECK_FOR_DUPE = 3;
Leon Scroggins III052ce662010-09-13 14:44:16 -040090
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 private EditText mTitle;
92 private EditText mAddress;
93 private TextView mButton;
94 private View mCancelButton;
95 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080096 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040098 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010099 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500100 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400101 private View mDefaultView;
102 private View mFolderSelector;
103 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500104 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400105 private boolean mIsFolderNamerShowing;
106 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400107 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400108 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500109 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400110 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400111 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400112 private TextView mFakeTitle;
113 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400114 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400115 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400116 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500117 private TextView mTopLevelLabel;
118 private Drawable mHeaderIcon;
Leon Scroggins75630672011-01-13 17:56:15 -0500119 private View mRemoveLink;
120 private View mFakeTitleHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400121 private static class Folder {
122 String Name;
123 long Id;
124 Folder(String name, long id) {
125 Name = name;
126 Id = id;
127 }
128 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129
Ben Murdoch1794fe22009-09-29 18:14:30 +0100130 // Message IDs
131 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400132 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500133 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100134
135 private Handler mHandler;
136
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100137 private InputMethodManager getInputMethodManager() {
138 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
139 }
140
Leon Scroggins8baaa632010-12-08 19:46:53 -0500141 private Uri getUriForFolder(long folder) {
142 Uri uri;
143 if (folder == mRootFolder) {
144 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
145 } else {
146 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
147 }
148 String[] accountInfo = getAccountNameAndType(this);
149 if (accountInfo != null) {
150 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
151 }
152 return uri;
153 }
154
Leon Scroggins III052ce662010-09-13 14:44:16 -0400155 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400156 public void onTop(int level, Object data) {
157 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400158 Folder folderData = (Folder) data;
159 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400160 LoaderManager manager = getLoaderManager();
161 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
162 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500163 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400164 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400165 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400166 completeOrCancelFolderNaming(true);
167 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500168 setShowBookmarkIcon(level == 1);
169 }
170
171 /**
172 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
173 * @param show True if the icon should visible, false otherwise.
174 */
175 private void setShowBookmarkIcon(boolean show) {
176 Drawable drawable = show ? mHeaderIcon: null;
177 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400178 }
179
Leon Scroggins74dbe012010-10-15 10:54:27 -0400180 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400181 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
182 if (v == mFolderNamer) {
183 if (v.getText().length() > 0) {
184 if (actionId == EditorInfo.IME_NULL) {
185 // Only want to do this once.
186 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400187 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400188 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400189 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
Michael Kolb31829b92010-10-01 11:50:21 -0700191 // Steal the key press; otherwise a newline will be added
192 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400194 return false;
195 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400197 private void switchToDefaultView(boolean changedFolder) {
198 mFolderSelector.setVisibility(View.GONE);
199 mDefaultView.setVisibility(View.VISIBLE);
200 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500201 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400202 if (changedFolder) {
203 Object data = mCrumbs.getTopData();
204 if (data != null) {
205 Folder folder = (Folder) data;
206 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500207 if (mCurrentFolder == mRootFolder) {
208 // The Spinner changed to show "Other folder ..." Change
209 // it back to "Bookmarks", which is position 0 if we are
210 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500211 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500212 } else {
213 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
214 }
215 }
216 } else {
217 // The user canceled selecting a folder. Revert back to the earlier
218 // selection.
219 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500220 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500221 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500222 if (mCurrentFolder == mRootFolder) {
223 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
224 } else {
225 Object data = mCrumbs.getTopData();
226 if (data != null && ((Folder) data).Id == mCurrentFolder) {
227 // We are showing the correct folder heirarchy. The
228 // folder selector will say "Other folder..." Change it
229 // to say the name of the folder once again.
230 ((TextView) mFolder.getSelectedView()).setText(((Folder) data).Name);
231 } else {
232 // We are not be showing the correct folder heirarchy.
233 // Clear the Crumbs and find the proper folder
234 mCrumbs.clear();
235 setupTopCrumb();
236 LoaderManager manager = getLoaderManager();
237 manager.restartLoader(LOADER_ID_ALL_FOLDERS, null, this);
238 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
239
240 }
241 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400242 }
243 }
244 }
245
Leon Scroggins III052ce662010-09-13 14:44:16 -0400246 @Override
247 public void onClick(View v) {
248 if (v == mButton) {
249 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400250 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400251 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400252 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700253 } else {
254 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400255 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400256 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700257 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400258 } else if (save()) {
259 finish();
260 }
261 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400262 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400263 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400264 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
265 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700266 } else {
267 finish();
268 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500269 } else if (v == mFolderCancel) {
270 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400271 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400272 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400273 mFolderNamer.setText(R.string.new_folder);
274 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700275 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400276 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400277 InputMethodManager imm = getInputMethodManager();
278 // Set the InputMethodManager to focus on the ListView so that it
279 // can transfer the focus to mFolderNamer.
280 imm.focusIn(mListView);
281 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500282 } else if (v == mRemoveLink) {
283 if (!mEditingExisting) {
284 throw new AssertionError("Remove button should not be shown for"
285 + " new bookmarks");
286 }
287 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
288 createHandler();
289 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
290 BookmarkUtils.displayRemoveBookmarkDialog(id,
291 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400293 }
294
Leon Scroggins504433a2011-01-13 12:26:52 -0500295 // FolderSpinner.OnSetSelectionListener
296
Leon Scroggins88d08032010-10-21 15:17:10 -0400297 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500298 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500299 int intId = (int) id;
300 switch (intId) {
301 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400302 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400303 mSaveToHomeScreen = false;
304 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500305 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400306 // Create a short cut to the home screen
307 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400308 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500309 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400310 switchToFolderSelector();
311 break;
312 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500313 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400314 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500315 }
316
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500317 /**
318 * Finish naming a folder, and close the IME
319 * @param cancel If true, the new folder is not created. If false, the new
320 * folder is created and the user is taken inside it.
321 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400322 private void completeOrCancelFolderNaming(boolean cancel) {
323 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700324 String name = mFolderNamer.getText().toString();
325 long id = addFolderToCurrent(mFolderNamer.getText().toString());
326 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700327 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400328 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400329 mAddNewFolder.setVisibility(View.VISIBLE);
330 mAddSeparator.setVisibility(View.VISIBLE);
331 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400332 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700333 }
334
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700335 private long addFolderToCurrent(String name) {
336 // Add the folder to the database
337 ContentValues values = new ContentValues();
338 values.put(BrowserContract.Bookmarks.TITLE,
339 name);
340 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500341 String[] accountInfo = getAccountNameAndType(this);
342 if (accountInfo != null) {
343 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
344 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700345 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400346 long currentFolder;
347 Object data = mCrumbs.getTopData();
348 if (data != null) {
349 currentFolder = ((Folder) data).Id;
350 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400351 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400352 }
353 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700354 Uri uri = getContentResolver().insert(
355 BrowserContract.Bookmarks.CONTENT_URI, values);
356 if (uri != null) {
357 return ContentUris.parseId(uri);
358 } else {
359 return -1;
360 }
361 }
362
Leon Scroggins III052ce662010-09-13 14:44:16 -0400363 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500364 // Set the list to the top in case it is scrolled.
365 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400366 mDefaultView.setVisibility(View.GONE);
367 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400368 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500369 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400370 mAddNewFolder.setVisibility(View.VISIBLE);
371 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400372 }
373
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700374 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700375 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400376 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400377 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700378 }
379 }
380
Leon Scroggins III052ce662010-09-13 14:44:16 -0400381 @Override
382 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
383 String[] projection;
384 switch (id) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500385 case LOADER_ID_CHECK_FOR_DUPE:
386 projection = new String[] {
387 BrowserContract.Bookmarks._ID,
388 BrowserContract.Bookmarks.PARENT,
389 BrowserContract.Bookmarks.TITLE
390 };
391 return new CursorLoader(this,
392 BookmarkUtils.getBookmarksUri(this),
393 projection,
394 BrowserContract.Bookmarks.URL + " = ?",
395 new String[] { mOriginalUrl },
396 null);
397 case LOADER_ID_FIND_ROOT:
398 String name = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME);
399 String type = args.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE);
400
401 projection = new String[] { BrowserContract.Bookmarks._ID };
402 String selection = BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
403 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
404 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?";
405 String[] selArgs = new String[] {
406 BrowserContract.ChromeSyncColumns.FOLDER_NAME_BOOKMARKS_BAR,
407 name,
408 type
409 };
410 return new CursorLoader(this,
411 BrowserContract.Bookmarks.CONTENT_URI,
412 projection,
413 selection,
414 selArgs,
415 null);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400416 case LOADER_ID_ALL_FOLDERS:
417 projection = new String[] {
418 BrowserContract.Bookmarks._ID,
419 BrowserContract.Bookmarks.PARENT,
420 BrowserContract.Bookmarks.TITLE,
421 BrowserContract.Bookmarks.IS_FOLDER
422 };
423 return new CursorLoader(this,
424 BrowserContract.Bookmarks.CONTENT_URI,
425 projection,
426 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
427 null,
428 null);
429 case LOADER_ID_FOLDER_CONTENTS:
430 projection = new String[] {
431 BrowserContract.Bookmarks._ID,
432 BrowserContract.Bookmarks.TITLE,
433 BrowserContract.Bookmarks.IS_FOLDER
434 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500435 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
436 if (mEditingFolder) {
437 where += " AND " + BrowserContract.Bookmarks._ID + " != "
438 + mMap.getLong(BrowserContract.Bookmarks._ID);
439 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400440 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500441 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400442 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500443 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400444 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500445 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400446 default:
447 throw new AssertionError("Asking for nonexistant loader!");
448 }
449 }
450
451 @Override
452 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
453 switch (loader.getId()) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500454 case LOADER_ID_CHECK_FOR_DUPE:
455 if (cursor != null && cursor.moveToFirst()) {
456 // Site is bookmarked.
457 mEditingExisting = true;
Leon Scroggins75630672011-01-13 17:56:15 -0500458 showRemoveButton();
Leon Scroggins504433a2011-01-13 12:26:52 -0500459 mFakeTitle.setText(R.string.edit_bookmark);
460 int index = cursor.getColumnIndexOrThrow(
461 BrowserContract.Bookmarks.PARENT);
462 mCurrentFolder = cursor.getLong(index);
463 index = cursor.getColumnIndexOrThrow(
464 BrowserContract.Bookmarks.TITLE);
465 String title = cursor.getString(index);
466 mTitle.setText(title);
467 index = cursor.getColumnIndexOrThrow(
468 BrowserContract.Bookmarks._ID);
469 long id = cursor.getLong(index);
470 mMap.putLong(BrowserContract.Bookmarks._ID, id);
471 }
472 onCurrentFolderFound();
473 getLoaderManager().destroyLoader(LOADER_ID_CHECK_FOR_DUPE);
474 break;
475 case LOADER_ID_FIND_ROOT:
476 long root;
477 if (cursor != null && cursor.moveToFirst()) {
478 root = cursor.getLong(0);
479 } else {
480 root = BrowserProvider2.FIXED_ID_ROOT;
481 }
482 onRootFolderFound(root);
483 getLoaderManager().destroyLoader(LOADER_ID_FIND_ROOT);
484 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400485 case LOADER_ID_FOLDER_CONTENTS:
486 mAdapter.changeCursor(cursor);
487 break;
488 case LOADER_ID_ALL_FOLDERS:
489 long parent = mCurrentFolder;
490 int idIndex = cursor.getColumnIndexOrThrow(
491 BrowserContract.Bookmarks._ID);
492 int titleIndex = cursor.getColumnIndexOrThrow(
493 BrowserContract.Bookmarks.TITLE);
494 int parentIndex = cursor.getColumnIndexOrThrow(
495 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500496 // If the user is editing anything inside the "Other Bookmarks"
497 // folder, we need to stop searching up when we reach its parent.
498 // Find the root folder
499 moveCursorToFolder(cursor, mRootFolder, idIndex);
500 // omniparent is the folder which contains root, and therefore
501 // also the parent of the "Other Bookmarks" folder.
502 long omniparent = cursor.getLong(parentIndex);
503 Stack<Folder> folderStack = new Stack<Folder>();
504 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400505 // First, find the folder corresponding to the current
506 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500507 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400508 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400509 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500510 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400511 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400512 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400513 parent = cursor.getLong(parentIndex);
514 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400515 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500516 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400517 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400518 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800519 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400520 break;
521 default:
522 break;
523 }
524 }
525
Dianne Hackborn39772c82010-12-16 00:43:54 -0800526 public void onLoaderReset(Loader<Cursor> loader) {
527 switch (loader.getId()) {
528 case LOADER_ID_FOLDER_CONTENTS:
529 mAdapter.changeCursor(null);
530 break;
531 }
532 }
533
Leon Scroggins02081942010-11-01 17:52:42 -0400534 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500535 * Move cursor to the position that has folderToFind as its "_id".
536 * @param cursor Cursor containing folders in the bookmarks database
537 * @param folderToFind "_id" of the folder to move to.
538 * @param idIndex Index in cursor of "_id"
539 * @throws AssertionError if cursor is empty or there is no row with folderToFind
540 * as its "_id".
541 */
542 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
543 throws AssertionError {
544 if (!cursor.moveToFirst()) {
545 throw new AssertionError("No folders in the database!");
546 }
547 long folder;
548 do {
549 folder = cursor.getLong(idIndex);
550 } while (folder != folderToFind && cursor.moveToNext());
551 if (cursor.isAfterLast()) {
552 throw new AssertionError("Folder(id=" + folderToFind
553 + ") holding this bookmark does not exist!");
554 }
555 }
556
Leon Scroggins III052ce662010-09-13 14:44:16 -0400557 @Override
558 public void onItemClick(AdapterView<?> parent, View view, int position,
559 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400560 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400561 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400562 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400563 }
564
Leon Scroggins162f8352010-10-18 15:02:44 -0400565 private void setShowFolderNamer(boolean show) {
566 if (show != mIsFolderNamerShowing) {
567 mIsFolderNamerShowing = show;
568 if (show) {
569 // Set the selection to the folder namer so it will be in
570 // view.
571 mListView.addFooterView(mFolderNamerHolder);
572 } else {
573 mListView.removeFooterView(mFolderNamerHolder);
574 }
575 // Refresh the list.
576 mListView.setAdapter(mAdapter);
577 if (show) {
578 mListView.setSelection(mListView.getCount() - 1);
579 }
580 }
581 }
582
Leon Scroggins III052ce662010-09-13 14:44:16 -0400583 /**
584 * Shows a list of names of folders.
585 */
586 private class FolderAdapter extends CursorAdapter {
587 public FolderAdapter(Context context) {
588 super(context, null);
589 }
590
591 @Override
592 public void bindView(View view, Context context, Cursor cursor) {
593 ((TextView) view.findViewById(android.R.id.text1)).setText(
594 cursor.getString(cursor.getColumnIndexOrThrow(
595 BrowserContract.Bookmarks.TITLE)));
596 }
597
598 @Override
599 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700600 View view = LayoutInflater.from(context).inflate(
601 R.layout.folder_list_item, null);
602 view.setBackgroundDrawable(context.getResources().
603 getDrawable(android.R.drawable.list_selector_background));
604 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400605 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400606
607 @Override
608 public boolean isEmpty() {
609 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400610 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400611 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400612 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613
Leon Scrogginsc1129902010-12-08 15:28:33 -0500614 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 protected void onCreate(Bundle icicle) {
616 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500617 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100618
619 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100620
Leon Scroggins III052ce662010-09-13 14:44:16 -0400621 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100622
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400623 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700624
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 String title = null;
626 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100627
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400628 mFakeTitle = (TextView) findViewById(R.id.fake_title);
629
The Android Open Source Project0c908882009-03-03 19:32:16 -0800630 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800631 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800632 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800633 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634 mMap = b;
635 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400636 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800637 if (mEditingFolder) {
638 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500639 } else {
640 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800641 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400642 } else {
643 int gravity = mMap.getInt("gravity", -1);
644 if (gravity != -1) {
645 WindowManager.LayoutParams l = window.getAttributes();
646 l.gravity = gravity;
647 window.setAttributes(l);
648 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800649 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400650 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
651 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
652 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700653 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400654 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655
656 mTitle = (EditText) findViewById(R.id.title);
657 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100658
Leon Scroggins III052ce662010-09-13 14:44:16 -0400659 mAddress = (EditText) findViewById(R.id.address);
660 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400663 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664
665 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400666 mCancelButton.setOnClickListener(this);
667
Leon Scroggins504433a2011-01-13 12:26:52 -0500668 mFolder = (FolderSpinner) findViewById(R.id.folder);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500669 mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
Leon Scroggins504433a2011-01-13 12:26:52 -0500670 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400671
672 mDefaultView = findViewById(R.id.default_view);
673 mFolderSelector = findViewById(R.id.folder_selector);
674
Leon Scroggins162f8352010-10-18 15:02:44 -0400675 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
676 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400677 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500678 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
679 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400680
681 mAddNewFolder = findViewById(R.id.add_new_folder);
682 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400683 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400684
Leon Scroggins74dbe012010-10-15 10:54:27 -0400685 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
686 mCrumbs.setUseBackButton(true);
687 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800688 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400689 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800690 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400691
Leon Scroggins III052ce662010-09-13 14:44:16 -0400692 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400693 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400694 View empty = findViewById(R.id.empty);
695 mListView.setEmptyView(empty);
696 mListView.setAdapter(mAdapter);
697 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400698 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500699
Leon Scroggins75630672011-01-13 17:56:15 -0500700 mFakeTitleHolder = findViewById(R.id.title_holder);
701
Leon Scroggins504433a2011-01-13 12:26:52 -0500702 if (!window.getDecorView().isInTouchMode()) {
703 mButton.requestFocus();
704 }
705
706 String[] accountInfo = getAccountNameAndType(this);
707 if (accountInfo == null) {
708 onRootFolderFound(BrowserProvider2.FIXED_ID_ROOT);
709 } else {
710 Bundle args = new Bundle();
711 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, accountInfo[0]);
712 args.putString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, accountInfo[1]);
713 getLoaderManager().initLoader(LOADER_ID_FIND_ROOT, args, this);
714 }
715
716 }
717
Leon Scroggins75630672011-01-13 17:56:15 -0500718 private void showRemoveButton() {
719 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
720 mRemoveLink = findViewById(R.id.remove);
721 mRemoveLink.setVisibility(View.VISIBLE);
722 mRemoveLink.setOnClickListener(this);
723 }
724
Leon Scroggins504433a2011-01-13 12:26:52 -0500725 // Called once we have determined which folder is the root folder
726 private void onRootFolderFound(long root) {
727 mRootFolder = root;
728 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
729 mCurrentFolder = mRootFolder;
730 }
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500731 setupTopCrumb();
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500732 if (mEditingExisting || TextUtils.isEmpty(mOriginalUrl)
733 || !(mMap != null && mMap.getBoolean(CHECK_FOR_DUPE))) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500734 onCurrentFolderFound();
735 } else {
736 // User is attempting to bookmark a site, rather than deliberately
737 // editing a bookmark. Rather than let them create a duplicate
738 // bookmark, see if the bookmark already exists.
739 getLoaderManager().initLoader(LOADER_ID_CHECK_FOR_DUPE, null, this);
740 }
741 }
742
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500743 private void setupTopCrumb() {
744 String name = getString(R.string.bookmarks);
745 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
746 new Folder(name, mRootFolder));
747 // To better match the other folders.
748 mTopLevelLabel.setCompoundDrawablePadding(6);
749 }
750
Leon Scroggins504433a2011-01-13 12:26:52 -0500751 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400752 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500753 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400754 // Find all the folders
755 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500756 // Since we're not in the root folder, change the selection to other
757 // folder now. The text will get changed once we select the correct
758 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500759 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500760 } else {
761 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500762 if (!mEditingFolder) {
763 // Initially the "Bookmarks" folder should be showing, rather than
764 // the home screen. In the editing folder case, home screen is not
765 // an option, so "Bookmarks" folder is already at the top.
766 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
767 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400768 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400769 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400770 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500771}
Leon Scroggins8baaa632010-12-08 19:46:53 -0500772 /**
773 * Get the account name and type of the currently synced account.
774 * @param context Context to access preferences.
775 * @return null if no account name or type. Otherwise, the result will be
776 * an array of two Strings, the accountName and accountType, respectively.
777 */
778 private String[] getAccountNameAndType(Context context) {
779 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
780 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
781 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
782 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
783 return null;
784 }
785 return new String[] { accountName, accountType };
786 }
787
Leon Scroggins02065b02010-01-04 14:30:13 -0500788 /**
789 * Runnable to save a bookmark, so it can be performed in its own thread.
790 */
791 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400792 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500793 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200794 private Context mContext;
795 public SaveBookmarkRunnable(Context ctx, Message msg) {
796 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500797 mMessage = msg;
798 }
799 public void run() {
800 // Unbundle bookmark data.
801 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400802 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
803 String url = bundle.getString(BrowserContract.Bookmarks.URL);
804 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500805 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400806 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
807 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500808
809 // Save to the bookmarks DB.
810 try {
811 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400812 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
813 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500814 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200815 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500816 }
817 mMessage.arg1 = 1;
818 } catch (IllegalStateException e) {
819 mMessage.arg1 = 0;
820 }
821 mMessage.sendToTarget();
822 }
823 }
824
John Reckc8490812010-11-22 14:15:36 -0800825 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
826 Context mContext;
827 Long mId;
828
829 public UpdateBookmarkTask(Context context, long id) {
830 mContext = context;
831 mId = id;
832 }
833
834 @Override
835 protected Void doInBackground(ContentValues... params) {
836 if (params.length != 1) {
837 throw new IllegalArgumentException("No ContentValues provided!");
838 }
839 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
840 mContext.getContentResolver().update(
841 uri,
842 params[0], null, null);
843 return null;
844 }
845 }
846
Ben Murdoch1794fe22009-09-29 18:14:30 +0100847 private void createHandler() {
848 if (mHandler == null) {
849 mHandler = new Handler() {
850 @Override
851 public void handleMessage(Message msg) {
852 switch (msg.what) {
853 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500854 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100855 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
856 Toast.LENGTH_LONG).show();
857 } else {
858 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
859 Toast.LENGTH_LONG).show();
860 }
861 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400862 case TOUCH_ICON_DOWNLOADED:
863 Bundle b = msg.getData();
864 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400865 AddBookmarkPage.this,
866 b.getString(BrowserContract.Bookmarks.URL),
867 b.getString(BrowserContract.Bookmarks.TITLE),
868 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
869 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400870 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500871 case BOOKMARK_DELETED:
872 finish();
873 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100874 }
875 }
876 };
877 }
878 }
879
The Android Open Source Project0c908882009-03-03 19:32:16 -0800880 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100881 * 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 -0800882 */
883 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100884 createHandler();
885
The Android Open Source Project0c908882009-03-03 19:32:16 -0800886 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100887 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100889
The Android Open Source Project0c908882009-03-03 19:32:16 -0800890 boolean emptyTitle = title.length() == 0;
891 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
892 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800893 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800894 if (emptyTitle) {
895 mTitle.setError(r.getText(R.string.bookmark_needs_title));
896 }
897 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400898 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800899 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400900 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100901
The Android Open Source Project0c908882009-03-03 19:32:16 -0800902 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000903 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800904 if (!mEditingFolder) {
905 try {
906 // We allow bookmarks with a javascript: scheme, but these will in most cases
907 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000908
John Reckc8490812010-11-22 14:15:36 -0800909 if (!url.toLowerCase().startsWith("javascript:")) {
910 URI uriObj = new URI(url);
911 String scheme = uriObj.getScheme();
912 if (!Bookmarks.urlHasAcceptableScheme(url)) {
913 // If the scheme was non-null, let the user know that we
914 // can't save their bookmark. If it was null, we'll assume
915 // they meant http when we parse it in the WebAddress class.
916 if (scheme != null) {
917 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
918 return false;
919 }
920 WebAddress address;
921 try {
922 address = new WebAddress(unfilteredUrl);
923 } catch (ParseException e) {
924 throw new URISyntaxException("", "");
925 }
926 if (address.getHost().length() == 0) {
927 throw new URISyntaxException("", "");
928 }
929 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000930 }
Ben Murdochde353622009-10-12 10:29:00 +0100931 }
John Reckc8490812010-11-22 14:15:36 -0800932 } catch (URISyntaxException e) {
933 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
934 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800935 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800936 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100937
Leon Scroggins88d08032010-10-21 15:17:10 -0400938 if (mSaveToHomeScreen) {
939 mEditingExisting = false;
940 }
941
942 boolean urlUnmodified = url.equals(mOriginalUrl);
943
Ben Murdoch1794fe22009-09-29 18:14:30 +0100944 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800945 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
946 ContentValues values = new ContentValues();
947 values.put(BrowserContract.Bookmarks.TITLE, title);
948 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
949 if (!mEditingFolder) {
950 values.put(BrowserContract.Bookmarks.URL, url);
951 if (!urlUnmodified) {
952 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
953 }
954 }
955 if (values.size() > 0) {
956 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
957 }
958 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100959 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400960 Bitmap thumbnail;
961 Bitmap favicon;
962 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400963 thumbnail = (Bitmap) mMap.getParcelable(
964 BrowserContract.Bookmarks.THUMBNAIL);
965 favicon = (Bitmap) mMap.getParcelable(
966 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400967 } else {
968 thumbnail = null;
969 favicon = null;
970 }
971
Ben Murdoch1794fe22009-09-29 18:14:30 +0100972 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400973 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
974 bundle.putString(BrowserContract.Bookmarks.URL, url);
975 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400976
977 if (mSaveToHomeScreen) {
978 if (mTouchIconUrl != null && urlUnmodified) {
979 Message msg = Message.obtain(mHandler,
980 TOUCH_ICON_DOWNLOADED);
981 msg.setData(bundle);
982 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400983 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400984 icon.execute(mTouchIconUrl);
985 } else {
986 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
987 title, null /*touchIcon*/, favicon));
988 }
989 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400990 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
991 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
992 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400993 // Post a message to write to the DB.
994 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
995 msg.setData(bundle);
996 // Start a new thread so as to not slow down the UI
997 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
998 t.start();
999 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001000 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001001 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001002 }
1003 return true;
1004 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001005
1006 /*
1007 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1008 */
1009 public static class CustomListView extends ListView {
1010 private EditText mEditText;
1011
1012 public void addEditText(EditText editText) {
1013 mEditText = editText;
1014 }
1015
1016 public CustomListView(Context context) {
1017 super(context);
1018 }
1019
1020 public CustomListView(Context context, AttributeSet attrs) {
1021 super(context, attrs);
1022 }
1023
1024 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1025 super(context, attrs, defStyle);
1026 }
1027
1028 @Override
1029 public boolean checkInputConnectionProxy(View view) {
1030 return view == mEditText;
1031 }
1032 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001033}