blob: 9d8c73cb7f414797b042d85fb1616436253f721d [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 Scrogginsdd13bad2011-01-06 20:25:54 -050020import com.android.browser.addbookmark.FolderSpinnerAdapter;
Leon Scroggins25230d72010-09-28 20:09:25 -040021
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040023import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070025import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040026import android.content.ContentValues;
27import android.content.Context;
28import android.content.CursorLoader;
Leon Scroggins III052ce662010-09-13 14:44:16 -040029import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040030import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040032import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010033import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040034import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070036import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.net.WebAddress;
John Reckc8490812010-11-22 14:15:36 -080038import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010040import android.os.Handler;
41import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040042import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040044import android.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040045import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040046import android.view.KeyEvent;
47import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040051import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040052import android.view.inputmethod.EditorInfo;
53import android.view.inputmethod.InputMethodManager;
54import android.widget.AdapterView;
55import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040057import android.widget.ListView;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -050058import android.widget.Spinner;
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 Scrogginsdd13bad2011-01-06 20:25:54 -050069 BreadCrumbView.Controller, AdapterView.OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080070
Michael Kolb370a4f32010-10-06 10:45:32 -070071 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040072 public static final String TOUCH_ICON_URL = "touch_icon_url";
73 // Place on an edited bookmark to remove the saved thumbnail
74 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
75 public static final String USER_AGENT = "user_agent";
Michael Kolb370a4f32010-10-06 10:45:32 -070076
John Reckc8490812010-11-22 14:15:36 -080077 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
78 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
79
Leon Scroggins74dbe012010-10-15 10:54:27 -040080 private static final int MAX_CRUMBS_SHOWN = 2;
81
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 private final String LOGTAG = "Bookmarks";
83
Leon Scroggins III052ce662010-09-13 14:44:16 -040084 // IDs for the CursorLoaders that are used.
85 private final int LOADER_ID_FOLDER_CONTENTS = 0;
86 private final int LOADER_ID_ALL_FOLDERS = 1;
87
The Android Open Source Project0c908882009-03-03 19:32:16 -080088 private EditText mTitle;
89 private EditText mAddress;
90 private TextView mButton;
91 private View mCancelButton;
92 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -080093 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -080094 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040095 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010096 private String mOriginalUrl;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -050097 private Spinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -040098 private View mDefaultView;
99 private View mFolderSelector;
100 private EditText mFolderNamer;
Leon Scroggins162f8352010-10-18 15:02:44 -0400101 private boolean mIsFolderNamerShowing;
102 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400103 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400104 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400105 private long mCurrentFolder = 0;
106 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400107 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400108 private TextView mFakeTitle;
109 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400110 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400111 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400112 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500113 private TextView mTopLevelLabel;
114 private Drawable mHeaderIcon;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500115 // We manually change the spinner's selection if the edited bookmark is not
116 // in the root folder. This makes sure our listener ignores this change.
117 private boolean mIgnoreSelectionChange;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400118 private static class Folder {
119 String Name;
120 long Id;
121 Folder(String name, long id) {
122 Name = name;
123 Id = id;
124 }
125 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126
Ben Murdoch1794fe22009-09-29 18:14:30 +0100127 // Message IDs
128 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400129 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100130
131 private Handler mHandler;
132
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100133 private InputMethodManager getInputMethodManager() {
134 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
135 }
136
Leon Scroggins8baaa632010-12-08 19:46:53 -0500137 private Uri getUriForFolder(long folder) {
138 Uri uri;
139 if (folder == mRootFolder) {
140 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
141 } else {
142 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
143 }
144 String[] accountInfo = getAccountNameAndType(this);
145 if (accountInfo != null) {
146 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
147 }
148 return uri;
149 }
150
Leon Scroggins III052ce662010-09-13 14:44:16 -0400151 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400152 public void onTop(int level, Object data) {
153 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400154 Folder folderData = (Folder) data;
155 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400156 LoaderManager manager = getLoaderManager();
157 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
158 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500159 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400160 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400161 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400162 completeOrCancelFolderNaming(true);
163 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500164 setShowBookmarkIcon(level == 1);
165 }
166
167 /**
168 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
169 * @param show True if the icon should visible, false otherwise.
170 */
171 private void setShowBookmarkIcon(boolean show) {
172 Drawable drawable = show ? mHeaderIcon: null;
173 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400174 }
175
Leon Scroggins74dbe012010-10-15 10:54:27 -0400176 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400177 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
178 if (v == mFolderNamer) {
179 if (v.getText().length() > 0) {
180 if (actionId == EditorInfo.IME_NULL) {
181 // Only want to do this once.
182 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400183 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
Michael Kolb31829b92010-10-01 11:50:21 -0700187 // Steal the key press; otherwise a newline will be added
188 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400190 return false;
191 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400193 private void switchToDefaultView(boolean changedFolder) {
194 mFolderSelector.setVisibility(View.GONE);
195 mDefaultView.setVisibility(View.VISIBLE);
196 mCrumbHolder.setVisibility(View.GONE);
197 mFakeTitle.setVisibility(View.VISIBLE);
198 if (changedFolder) {
199 Object data = mCrumbs.getTopData();
200 if (data != null) {
201 Folder folder = (Folder) data;
202 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500203 if (mCurrentFolder == mRootFolder) {
204 // The Spinner changed to show "Other folder ..." Change
205 // it back to "Bookmarks", which is position 0 if we are
206 // editing a folder, 1 otherwise.
207 mFolder.setSelection(mEditingFolder ? 0 : 1);
208 } else {
209 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
210 }
211 }
212 } else {
213 // The user canceled selecting a folder. Revert back to the earlier
214 // selection.
215 if (mSaveToHomeScreen) {
216 mFolder.setSelection(0);
217 } else {
218 mFolder.setSelection(mEditingFolder ? 0 : 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400219 }
220 }
221 }
222
Leon Scroggins III052ce662010-09-13 14:44:16 -0400223 @Override
224 public void onClick(View v) {
225 if (v == mButton) {
226 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400227 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400228 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400229 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700230 } else {
231 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400232 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400233 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700234 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400235 } else if (save()) {
236 finish();
237 }
238 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400239 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400240 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400241 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
242 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700243 } else {
244 finish();
245 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400246 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400247 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400248 mFolderNamer.setText(R.string.new_folder);
249 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700250 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400251 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400252 InputMethodManager imm = getInputMethodManager();
253 // Set the InputMethodManager to focus on the ListView so that it
254 // can transfer the focus to mFolderNamer.
255 imm.focusIn(mListView);
256 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400258 }
259
Leon Scroggins88d08032010-10-21 15:17:10 -0400260 @Override
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500261 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
262 if (mIgnoreSelectionChange) {
263 mIgnoreSelectionChange = false;
264 return;
265 }
266 // In response to the spinner changing.
267 int intId = (int) id;
268 switch (intId) {
269 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400270 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400271 mSaveToHomeScreen = false;
272 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500273 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400274 // Create a short cut to the home screen
275 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400276 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500277 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400278 switchToFolderSelector();
279 break;
280 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500281 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400282 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500283 }
284
285 @Override
286 public void onNothingSelected(AdapterView<?> parent) {
Leon Scroggins88d08032010-10-21 15:17:10 -0400287 }
288
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400289 private void completeOrCancelFolderNaming(boolean cancel) {
290 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700291 String name = mFolderNamer.getText().toString();
292 long id = addFolderToCurrent(mFolderNamer.getText().toString());
293 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700294 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400295 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400296 mAddNewFolder.setVisibility(View.VISIBLE);
297 mAddSeparator.setVisibility(View.VISIBLE);
298 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400299 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700300 }
301
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700302 private long addFolderToCurrent(String name) {
303 // Add the folder to the database
304 ContentValues values = new ContentValues();
305 values.put(BrowserContract.Bookmarks.TITLE,
306 name);
307 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500308 String[] accountInfo = getAccountNameAndType(this);
309 if (accountInfo != null) {
310 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
311 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700312 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400313 long currentFolder;
314 Object data = mCrumbs.getTopData();
315 if (data != null) {
316 currentFolder = ((Folder) data).Id;
317 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400318 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400319 }
320 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700321 Uri uri = getContentResolver().insert(
322 BrowserContract.Bookmarks.CONTENT_URI, values);
323 if (uri != null) {
324 return ContentUris.parseId(uri);
325 } else {
326 return -1;
327 }
328 }
329
Leon Scroggins III052ce662010-09-13 14:44:16 -0400330 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500331 // Set the list to the top in case it is scrolled.
332 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400333 mDefaultView.setVisibility(View.GONE);
334 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400335 mCrumbHolder.setVisibility(View.VISIBLE);
336 mFakeTitle.setVisibility(View.GONE);
337 mAddNewFolder.setVisibility(View.VISIBLE);
338 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400339 }
340
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700341 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700342 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400343 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400344 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700345 }
346 }
347
Leon Scroggins III052ce662010-09-13 14:44:16 -0400348 @Override
349 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
350 String[] projection;
351 switch (id) {
352 case LOADER_ID_ALL_FOLDERS:
353 projection = new String[] {
354 BrowserContract.Bookmarks._ID,
355 BrowserContract.Bookmarks.PARENT,
356 BrowserContract.Bookmarks.TITLE,
357 BrowserContract.Bookmarks.IS_FOLDER
358 };
359 return new CursorLoader(this,
360 BrowserContract.Bookmarks.CONTENT_URI,
361 projection,
362 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
363 null,
364 null);
365 case LOADER_ID_FOLDER_CONTENTS:
366 projection = new String[] {
367 BrowserContract.Bookmarks._ID,
368 BrowserContract.Bookmarks.TITLE,
369 BrowserContract.Bookmarks.IS_FOLDER
370 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500371 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
372 if (mEditingFolder) {
373 where += " AND " + BrowserContract.Bookmarks._ID + " != "
374 + mMap.getLong(BrowserContract.Bookmarks._ID);
375 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400376 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500377 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400378 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500379 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400380 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500381 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400382 default:
383 throw new AssertionError("Asking for nonexistant loader!");
384 }
385 }
386
387 @Override
388 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
389 switch (loader.getId()) {
390 case LOADER_ID_FOLDER_CONTENTS:
391 mAdapter.changeCursor(cursor);
392 break;
393 case LOADER_ID_ALL_FOLDERS:
394 long parent = mCurrentFolder;
395 int idIndex = cursor.getColumnIndexOrThrow(
396 BrowserContract.Bookmarks._ID);
397 int titleIndex = cursor.getColumnIndexOrThrow(
398 BrowserContract.Bookmarks.TITLE);
399 int parentIndex = cursor.getColumnIndexOrThrow(
400 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500401 // If the user is editing anything inside the "Other Bookmarks"
402 // folder, we need to stop searching up when we reach its parent.
403 // Find the root folder
404 moveCursorToFolder(cursor, mRootFolder, idIndex);
405 // omniparent is the folder which contains root, and therefore
406 // also the parent of the "Other Bookmarks" folder.
407 long omniparent = cursor.getLong(parentIndex);
408 Stack<Folder> folderStack = new Stack<Folder>();
409 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400410 // First, find the folder corresponding to the current
411 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500412 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400413 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400414 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500415 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400416 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400417 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400418 parent = cursor.getLong(parentIndex);
419 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400420 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500421 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400422 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400423 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800424 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400425 break;
426 default:
427 break;
428 }
429 }
430
Dianne Hackborn39772c82010-12-16 00:43:54 -0800431 public void onLoaderReset(Loader<Cursor> loader) {
432 switch (loader.getId()) {
433 case LOADER_ID_FOLDER_CONTENTS:
434 mAdapter.changeCursor(null);
435 break;
436 }
437 }
438
Leon Scroggins02081942010-11-01 17:52:42 -0400439 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500440 * Move cursor to the position that has folderToFind as its "_id".
441 * @param cursor Cursor containing folders in the bookmarks database
442 * @param folderToFind "_id" of the folder to move to.
443 * @param idIndex Index in cursor of "_id"
444 * @throws AssertionError if cursor is empty or there is no row with folderToFind
445 * as its "_id".
446 */
447 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
448 throws AssertionError {
449 if (!cursor.moveToFirst()) {
450 throw new AssertionError("No folders in the database!");
451 }
452 long folder;
453 do {
454 folder = cursor.getLong(idIndex);
455 } while (folder != folderToFind && cursor.moveToNext());
456 if (cursor.isAfterLast()) {
457 throw new AssertionError("Folder(id=" + folderToFind
458 + ") holding this bookmark does not exist!");
459 }
460 }
461
Leon Scroggins III052ce662010-09-13 14:44:16 -0400462 @Override
463 public void onItemClick(AdapterView<?> parent, View view, int position,
464 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400465 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400466 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400467 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400468 }
469
Leon Scroggins162f8352010-10-18 15:02:44 -0400470 private void setShowFolderNamer(boolean show) {
471 if (show != mIsFolderNamerShowing) {
472 mIsFolderNamerShowing = show;
473 if (show) {
474 // Set the selection to the folder namer so it will be in
475 // view.
476 mListView.addFooterView(mFolderNamerHolder);
477 } else {
478 mListView.removeFooterView(mFolderNamerHolder);
479 }
480 // Refresh the list.
481 mListView.setAdapter(mAdapter);
482 if (show) {
483 mListView.setSelection(mListView.getCount() - 1);
484 }
485 }
486 }
487
Leon Scroggins III052ce662010-09-13 14:44:16 -0400488 /**
489 * Shows a list of names of folders.
490 */
491 private class FolderAdapter extends CursorAdapter {
492 public FolderAdapter(Context context) {
493 super(context, null);
494 }
495
496 @Override
497 public void bindView(View view, Context context, Cursor cursor) {
498 ((TextView) view.findViewById(android.R.id.text1)).setText(
499 cursor.getString(cursor.getColumnIndexOrThrow(
500 BrowserContract.Bookmarks.TITLE)));
501 }
502
503 @Override
504 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700505 View view = LayoutInflater.from(context).inflate(
506 R.layout.folder_list_item, null);
507 view.setBackgroundDrawable(context.getResources().
508 getDrawable(android.R.drawable.list_selector_background));
509 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400510 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400511
512 @Override
513 public boolean isEmpty() {
514 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400515 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400516 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400517 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518
Leon Scrogginsc1129902010-12-08 15:28:33 -0500519 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 protected void onCreate(Bundle icicle) {
521 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500522 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100523
524 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100525
Leon Scroggins III052ce662010-09-13 14:44:16 -0400526 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100527
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400528 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700529
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 String title = null;
531 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100532
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400533 mFakeTitle = (TextView) findViewById(R.id.fake_title);
534
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800536 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800538 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 mMap = b;
540 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400541 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800542 if (mEditingFolder) {
543 findViewById(R.id.row_address).setVisibility(View.GONE);
544 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400545 } else {
546 int gravity = mMap.getInt("gravity", -1);
547 if (gravity != -1) {
548 WindowManager.LayoutParams l = window.getAttributes();
549 l.gravity = gravity;
550 window.setAttributes(l);
551 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400553 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
554 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
555 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700556 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400557 }
Leon Scroggins02081942010-11-01 17:52:42 -0400558 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700559 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400560 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 }
562
563 mTitle = (EditText) findViewById(R.id.title);
564 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100565
Leon Scroggins III052ce662010-09-13 14:44:16 -0400566 mAddress = (EditText) findViewById(R.id.address);
567 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800568
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400570 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571
572 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400573 mCancelButton.setOnClickListener(this);
574
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500575 mFolder = (Spinner) findViewById(R.id.folder);
576 mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
577 if (!mEditingFolder) {
578 // Initially the "Bookmarks" folder should be showing, rather than
579 // the home screen. In the editing folder case, home screen is not
580 // an option, so "Bookmarks" folder is already at the top.
581 mFolder.setSelection(FolderSpinnerAdapter.ROOT_FOLDER);
582 }
583 mFolder.setOnItemSelectedListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400584
585 mDefaultView = findViewById(R.id.default_view);
586 mFolderSelector = findViewById(R.id.folder_selector);
587
Leon Scroggins162f8352010-10-18 15:02:44 -0400588 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
589 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400590 mFolderNamer.setOnEditorActionListener(this);
591
592 mAddNewFolder = findViewById(R.id.add_new_folder);
593 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400594 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595
Leon Scroggins74dbe012010-10-15 10:54:27 -0400596 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
597 mCrumbs.setUseBackButton(true);
598 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400599 String name = getString(R.string.bookmarks);
Leon Scroggins905250c2010-12-17 15:25:33 -0500600 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
601 // To better match the other folders.
602 mTopLevelLabel.setCompoundDrawablePadding(6);
603 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_bookmark_widget_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400604 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800605 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400606
Leon Scroggins III052ce662010-09-13 14:44:16 -0400607 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400608 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400609 View empty = findViewById(R.id.empty);
610 mListView.setEmptyView(empty);
611 mListView.setAdapter(mAdapter);
612 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400613 mListView.addEditText(mFolderNamer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400614 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500615 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 // Find all the folders
617 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500618 // Since we're not in the root folder, change the selection to other
619 // folder now. The text will get changed once we select the correct
620 // folder.
621 mIgnoreSelectionChange = true;
622 mFolder.setSelection(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500623 } else {
624 setShowBookmarkIcon(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400625 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400626 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400627 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
628
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700629
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400630 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 mButton.requestFocus();
632 }
633 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100634
Leon Scroggins8baaa632010-12-08 19:46:53 -0500635 /**
636 * Get the account name and type of the currently synced account.
637 * @param context Context to access preferences.
638 * @return null if no account name or type. Otherwise, the result will be
639 * an array of two Strings, the accountName and accountType, respectively.
640 */
641 private String[] getAccountNameAndType(Context context) {
642 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
643 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
644 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
645 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
646 return null;
647 }
648 return new String[] { accountName, accountType };
649 }
650
Leon Scroggins25230d72010-09-28 20:09:25 -0400651 // FIXME: Use a CursorLoader
652 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500653 String[] accountInfo = getAccountNameAndType(context);
654 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500655 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400656 }
657 Cursor cursor = null;
658 try {
659 cursor = context.getContentResolver().query(
660 BrowserContract.Bookmarks.CONTENT_URI,
661 new String[] { BrowserContract.Bookmarks._ID },
662 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
663 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
664 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
665 new String[] {
666 BrowserContract.ChromeSyncColumns
667 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500668 accountInfo[0],
669 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400670 null);
671 if (cursor != null && cursor.moveToFirst()) {
672 return cursor.getLong(0);
673 }
674 } finally {
675 if (cursor != null) cursor.close();
676 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500677 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400678 }
679
Leon Scroggins02065b02010-01-04 14:30:13 -0500680 /**
681 * Runnable to save a bookmark, so it can be performed in its own thread.
682 */
683 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400684 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500685 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200686 private Context mContext;
687 public SaveBookmarkRunnable(Context ctx, Message msg) {
688 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500689 mMessage = msg;
690 }
691 public void run() {
692 // Unbundle bookmark data.
693 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400694 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
695 String url = bundle.getString(BrowserContract.Bookmarks.URL);
696 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500697 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400698 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
699 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500700
701 // Save to the bookmarks DB.
702 try {
703 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400704 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
705 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500706 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200707 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500708 }
709 mMessage.arg1 = 1;
710 } catch (IllegalStateException e) {
711 mMessage.arg1 = 0;
712 }
713 mMessage.sendToTarget();
714 }
715 }
716
John Reckc8490812010-11-22 14:15:36 -0800717 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
718 Context mContext;
719 Long mId;
720
721 public UpdateBookmarkTask(Context context, long id) {
722 mContext = context;
723 mId = id;
724 }
725
726 @Override
727 protected Void doInBackground(ContentValues... params) {
728 if (params.length != 1) {
729 throw new IllegalArgumentException("No ContentValues provided!");
730 }
731 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
732 mContext.getContentResolver().update(
733 uri,
734 params[0], null, null);
735 return null;
736 }
737 }
738
Ben Murdoch1794fe22009-09-29 18:14:30 +0100739 private void createHandler() {
740 if (mHandler == null) {
741 mHandler = new Handler() {
742 @Override
743 public void handleMessage(Message msg) {
744 switch (msg.what) {
745 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500746 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100747 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
748 Toast.LENGTH_LONG).show();
749 } else {
750 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
751 Toast.LENGTH_LONG).show();
752 }
753 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400754 case TOUCH_ICON_DOWNLOADED:
755 Bundle b = msg.getData();
756 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400757 AddBookmarkPage.this,
758 b.getString(BrowserContract.Bookmarks.URL),
759 b.getString(BrowserContract.Bookmarks.TITLE),
760 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
761 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400762 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100763 }
764 }
765 };
766 }
767 }
768
The Android Open Source Project0c908882009-03-03 19:32:16 -0800769 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100770 * 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 -0800771 */
772 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100773 createHandler();
774
The Android Open Source Project0c908882009-03-03 19:32:16 -0800775 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100776 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100778
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779 boolean emptyTitle = title.length() == 0;
780 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
781 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800782 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800783 if (emptyTitle) {
784 mTitle.setError(r.getText(R.string.bookmark_needs_title));
785 }
786 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400787 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800788 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400789 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100790
The Android Open Source Project0c908882009-03-03 19:32:16 -0800791 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000792 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800793 if (!mEditingFolder) {
794 try {
795 // We allow bookmarks with a javascript: scheme, but these will in most cases
796 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000797
John Reckc8490812010-11-22 14:15:36 -0800798 if (!url.toLowerCase().startsWith("javascript:")) {
799 URI uriObj = new URI(url);
800 String scheme = uriObj.getScheme();
801 if (!Bookmarks.urlHasAcceptableScheme(url)) {
802 // If the scheme was non-null, let the user know that we
803 // can't save their bookmark. If it was null, we'll assume
804 // they meant http when we parse it in the WebAddress class.
805 if (scheme != null) {
806 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
807 return false;
808 }
809 WebAddress address;
810 try {
811 address = new WebAddress(unfilteredUrl);
812 } catch (ParseException e) {
813 throw new URISyntaxException("", "");
814 }
815 if (address.getHost().length() == 0) {
816 throw new URISyntaxException("", "");
817 }
818 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000819 }
Ben Murdochde353622009-10-12 10:29:00 +0100820 }
John Reckc8490812010-11-22 14:15:36 -0800821 } catch (URISyntaxException e) {
822 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
823 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800824 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800825 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100826
Leon Scroggins88d08032010-10-21 15:17:10 -0400827 if (mSaveToHomeScreen) {
828 mEditingExisting = false;
829 }
830
831 boolean urlUnmodified = url.equals(mOriginalUrl);
832
Ben Murdoch1794fe22009-09-29 18:14:30 +0100833 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800834 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
835 ContentValues values = new ContentValues();
836 values.put(BrowserContract.Bookmarks.TITLE, title);
837 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
838 if (!mEditingFolder) {
839 values.put(BrowserContract.Bookmarks.URL, url);
840 if (!urlUnmodified) {
841 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
842 }
843 }
844 if (values.size() > 0) {
845 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
846 }
847 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100848 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400849 Bitmap thumbnail;
850 Bitmap favicon;
851 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400852 thumbnail = (Bitmap) mMap.getParcelable(
853 BrowserContract.Bookmarks.THUMBNAIL);
854 favicon = (Bitmap) mMap.getParcelable(
855 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400856 } else {
857 thumbnail = null;
858 favicon = null;
859 }
860
Ben Murdoch1794fe22009-09-29 18:14:30 +0100861 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400862 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
863 bundle.putString(BrowserContract.Bookmarks.URL, url);
864 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400865
866 if (mSaveToHomeScreen) {
867 if (mTouchIconUrl != null && urlUnmodified) {
868 Message msg = Message.obtain(mHandler,
869 TOUCH_ICON_DOWNLOADED);
870 msg.setData(bundle);
871 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400872 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400873 icon.execute(mTouchIconUrl);
874 } else {
875 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
876 title, null /*touchIcon*/, favicon));
877 }
878 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400879 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
880 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
881 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400882 // Post a message to write to the DB.
883 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
884 msg.setData(bundle);
885 // Start a new thread so as to not slow down the UI
886 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
887 t.start();
888 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100889 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000890 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800891 }
892 return true;
893 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400894
895 /*
896 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
897 */
898 public static class CustomListView extends ListView {
899 private EditText mEditText;
900
901 public void addEditText(EditText editText) {
902 mEditText = editText;
903 }
904
905 public CustomListView(Context context) {
906 super(context);
907 }
908
909 public CustomListView(Context context, AttributeSet attrs) {
910 super(context, attrs);
911 }
912
913 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
914 super(context, attrs, defStyle);
915 }
916
917 @Override
918 public boolean checkInputConnectionProxy(View view) {
919 return view == mEditText;
920 }
921 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800922}