blob: 5e389ea317195b39df26586488db880a79d5ad2d [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 Scroggins0e3a7b82011-01-11 19:08:03 -0500101 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400102 private boolean mIsFolderNamerShowing;
103 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400104 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400105 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400106 private long mCurrentFolder = 0;
107 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400108 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400109 private TextView mFakeTitle;
110 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400111 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400112 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400113 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500114 private TextView mTopLevelLabel;
115 private Drawable mHeaderIcon;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500116 // We manually change the spinner's selection if the edited bookmark is not
117 // in the root folder. This makes sure our listener ignores this change.
118 private boolean mIgnoreSelectionChange;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 private static class Folder {
120 String Name;
121 long Id;
122 Folder(String name, long id) {
123 Name = name;
124 Id = id;
125 }
126 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
Ben Murdoch1794fe22009-09-29 18:14:30 +0100128 // Message IDs
129 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400130 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100131
132 private Handler mHandler;
133
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100134 private InputMethodManager getInputMethodManager() {
135 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
136 }
137
Leon Scroggins8baaa632010-12-08 19:46:53 -0500138 private Uri getUriForFolder(long folder) {
139 Uri uri;
140 if (folder == mRootFolder) {
141 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
142 } else {
143 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
144 }
145 String[] accountInfo = getAccountNameAndType(this);
146 if (accountInfo != null) {
147 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
148 }
149 return uri;
150 }
151
Leon Scroggins III052ce662010-09-13 14:44:16 -0400152 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400153 public void onTop(int level, Object data) {
154 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400155 Folder folderData = (Folder) data;
156 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400157 LoaderManager manager = getLoaderManager();
158 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
159 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500160 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400161 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400162 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400163 completeOrCancelFolderNaming(true);
164 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500165 setShowBookmarkIcon(level == 1);
166 }
167
168 /**
169 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
170 * @param show True if the icon should visible, false otherwise.
171 */
172 private void setShowBookmarkIcon(boolean show) {
173 Drawable drawable = show ? mHeaderIcon: null;
174 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400175 }
176
Leon Scroggins74dbe012010-10-15 10:54:27 -0400177 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400178 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
179 if (v == mFolderNamer) {
180 if (v.getText().length() > 0) {
181 if (actionId == EditorInfo.IME_NULL) {
182 // Only want to do this once.
183 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400184 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400186 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 }
Michael Kolb31829b92010-10-01 11:50:21 -0700188 // Steal the key press; otherwise a newline will be added
189 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400191 return false;
192 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400194 private void switchToDefaultView(boolean changedFolder) {
195 mFolderSelector.setVisibility(View.GONE);
196 mDefaultView.setVisibility(View.VISIBLE);
197 mCrumbHolder.setVisibility(View.GONE);
198 mFakeTitle.setVisibility(View.VISIBLE);
199 if (changedFolder) {
200 Object data = mCrumbs.getTopData();
201 if (data != null) {
202 Folder folder = (Folder) data;
203 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500204 if (mCurrentFolder == mRootFolder) {
205 // The Spinner changed to show "Other folder ..." Change
206 // it back to "Bookmarks", which is position 0 if we are
207 // editing a folder, 1 otherwise.
208 mFolder.setSelection(mEditingFolder ? 0 : 1);
209 } else {
210 ((TextView) mFolder.getSelectedView()).setText(folder.Name);
211 }
212 }
213 } else {
214 // The user canceled selecting a folder. Revert back to the earlier
215 // selection.
216 if (mSaveToHomeScreen) {
217 mFolder.setSelection(0);
218 } else {
219 mFolder.setSelection(mEditingFolder ? 0 : 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400220 }
221 }
222 }
223
Leon Scroggins III052ce662010-09-13 14:44:16 -0400224 @Override
225 public void onClick(View v) {
226 if (v == mButton) {
227 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400228 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400229 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400230 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700231 } else {
232 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400233 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400234 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700235 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400236 } else if (save()) {
237 finish();
238 }
239 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400240 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400241 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400242 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
243 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700244 } else {
245 finish();
246 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500247 } else if (v == mFolderCancel) {
248 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400249 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400250 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400251 mFolderNamer.setText(R.string.new_folder);
252 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700253 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400254 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400255 InputMethodManager imm = getInputMethodManager();
256 // Set the InputMethodManager to focus on the ListView so that it
257 // can transfer the focus to mFolderNamer.
258 imm.focusIn(mListView);
259 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800260 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400261 }
262
Leon Scroggins88d08032010-10-21 15:17:10 -0400263 @Override
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500264 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
265 if (mIgnoreSelectionChange) {
266 mIgnoreSelectionChange = false;
267 return;
268 }
269 // In response to the spinner changing.
270 int intId = (int) id;
271 switch (intId) {
272 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400273 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400274 mSaveToHomeScreen = false;
275 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500276 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400277 // Create a short cut to the home screen
278 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400279 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500280 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400281 switchToFolderSelector();
282 break;
283 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500284 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400285 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500286 }
287
288 @Override
289 public void onNothingSelected(AdapterView<?> parent) {
Leon Scroggins88d08032010-10-21 15:17:10 -0400290 }
291
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500292 /**
293 * Finish naming a folder, and close the IME
294 * @param cancel If true, the new folder is not created. If false, the new
295 * folder is created and the user is taken inside it.
296 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400297 private void completeOrCancelFolderNaming(boolean cancel) {
298 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700299 String name = mFolderNamer.getText().toString();
300 long id = addFolderToCurrent(mFolderNamer.getText().toString());
301 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700302 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400303 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400304 mAddNewFolder.setVisibility(View.VISIBLE);
305 mAddSeparator.setVisibility(View.VISIBLE);
306 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400307 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700308 }
309
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700310 private long addFolderToCurrent(String name) {
311 // Add the folder to the database
312 ContentValues values = new ContentValues();
313 values.put(BrowserContract.Bookmarks.TITLE,
314 name);
315 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500316 String[] accountInfo = getAccountNameAndType(this);
317 if (accountInfo != null) {
318 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
319 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700320 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400321 long currentFolder;
322 Object data = mCrumbs.getTopData();
323 if (data != null) {
324 currentFolder = ((Folder) data).Id;
325 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400326 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400327 }
328 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700329 Uri uri = getContentResolver().insert(
330 BrowserContract.Bookmarks.CONTENT_URI, values);
331 if (uri != null) {
332 return ContentUris.parseId(uri);
333 } else {
334 return -1;
335 }
336 }
337
Leon Scroggins III052ce662010-09-13 14:44:16 -0400338 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500339 // Set the list to the top in case it is scrolled.
340 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400341 mDefaultView.setVisibility(View.GONE);
342 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400343 mCrumbHolder.setVisibility(View.VISIBLE);
344 mFakeTitle.setVisibility(View.GONE);
345 mAddNewFolder.setVisibility(View.VISIBLE);
346 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400347 }
348
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700349 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700350 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400351 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400352 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700353 }
354 }
355
Leon Scroggins III052ce662010-09-13 14:44:16 -0400356 @Override
357 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
358 String[] projection;
359 switch (id) {
360 case LOADER_ID_ALL_FOLDERS:
361 projection = new String[] {
362 BrowserContract.Bookmarks._ID,
363 BrowserContract.Bookmarks.PARENT,
364 BrowserContract.Bookmarks.TITLE,
365 BrowserContract.Bookmarks.IS_FOLDER
366 };
367 return new CursorLoader(this,
368 BrowserContract.Bookmarks.CONTENT_URI,
369 projection,
370 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
371 null,
372 null);
373 case LOADER_ID_FOLDER_CONTENTS:
374 projection = new String[] {
375 BrowserContract.Bookmarks._ID,
376 BrowserContract.Bookmarks.TITLE,
377 BrowserContract.Bookmarks.IS_FOLDER
378 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500379 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
380 if (mEditingFolder) {
381 where += " AND " + BrowserContract.Bookmarks._ID + " != "
382 + mMap.getLong(BrowserContract.Bookmarks._ID);
383 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400384 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500385 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400386 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500387 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400388 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500389 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400390 default:
391 throw new AssertionError("Asking for nonexistant loader!");
392 }
393 }
394
395 @Override
396 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
397 switch (loader.getId()) {
398 case LOADER_ID_FOLDER_CONTENTS:
399 mAdapter.changeCursor(cursor);
400 break;
401 case LOADER_ID_ALL_FOLDERS:
402 long parent = mCurrentFolder;
403 int idIndex = cursor.getColumnIndexOrThrow(
404 BrowserContract.Bookmarks._ID);
405 int titleIndex = cursor.getColumnIndexOrThrow(
406 BrowserContract.Bookmarks.TITLE);
407 int parentIndex = cursor.getColumnIndexOrThrow(
408 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500409 // If the user is editing anything inside the "Other Bookmarks"
410 // folder, we need to stop searching up when we reach its parent.
411 // Find the root folder
412 moveCursorToFolder(cursor, mRootFolder, idIndex);
413 // omniparent is the folder which contains root, and therefore
414 // also the parent of the "Other Bookmarks" folder.
415 long omniparent = cursor.getLong(parentIndex);
416 Stack<Folder> folderStack = new Stack<Folder>();
417 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400418 // First, find the folder corresponding to the current
419 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500420 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400421 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400422 if (parent == mCurrentFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500423 ((TextView) mFolder.getSelectedView()).setText(name);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400424 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400425 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400426 parent = cursor.getLong(parentIndex);
427 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400428 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500429 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400430 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400431 }
Dianne Hackborn71e76c72010-12-20 11:44:09 -0800432 getLoaderManager().destroyLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400433 break;
434 default:
435 break;
436 }
437 }
438
Dianne Hackborn39772c82010-12-16 00:43:54 -0800439 public void onLoaderReset(Loader<Cursor> loader) {
440 switch (loader.getId()) {
441 case LOADER_ID_FOLDER_CONTENTS:
442 mAdapter.changeCursor(null);
443 break;
444 }
445 }
446
Leon Scroggins02081942010-11-01 17:52:42 -0400447 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500448 * Move cursor to the position that has folderToFind as its "_id".
449 * @param cursor Cursor containing folders in the bookmarks database
450 * @param folderToFind "_id" of the folder to move to.
451 * @param idIndex Index in cursor of "_id"
452 * @throws AssertionError if cursor is empty or there is no row with folderToFind
453 * as its "_id".
454 */
455 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
456 throws AssertionError {
457 if (!cursor.moveToFirst()) {
458 throw new AssertionError("No folders in the database!");
459 }
460 long folder;
461 do {
462 folder = cursor.getLong(idIndex);
463 } while (folder != folderToFind && cursor.moveToNext());
464 if (cursor.isAfterLast()) {
465 throw new AssertionError("Folder(id=" + folderToFind
466 + ") holding this bookmark does not exist!");
467 }
468 }
469
Leon Scroggins III052ce662010-09-13 14:44:16 -0400470 @Override
471 public void onItemClick(AdapterView<?> parent, View view, int position,
472 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400473 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400474 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400475 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400476 }
477
Leon Scroggins162f8352010-10-18 15:02:44 -0400478 private void setShowFolderNamer(boolean show) {
479 if (show != mIsFolderNamerShowing) {
480 mIsFolderNamerShowing = show;
481 if (show) {
482 // Set the selection to the folder namer so it will be in
483 // view.
484 mListView.addFooterView(mFolderNamerHolder);
485 } else {
486 mListView.removeFooterView(mFolderNamerHolder);
487 }
488 // Refresh the list.
489 mListView.setAdapter(mAdapter);
490 if (show) {
491 mListView.setSelection(mListView.getCount() - 1);
492 }
493 }
494 }
495
Leon Scroggins III052ce662010-09-13 14:44:16 -0400496 /**
497 * Shows a list of names of folders.
498 */
499 private class FolderAdapter extends CursorAdapter {
500 public FolderAdapter(Context context) {
501 super(context, null);
502 }
503
504 @Override
505 public void bindView(View view, Context context, Cursor cursor) {
506 ((TextView) view.findViewById(android.R.id.text1)).setText(
507 cursor.getString(cursor.getColumnIndexOrThrow(
508 BrowserContract.Bookmarks.TITLE)));
509 }
510
511 @Override
512 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700513 View view = LayoutInflater.from(context).inflate(
514 R.layout.folder_list_item, null);
515 view.setBackgroundDrawable(context.getResources().
516 getDrawable(android.R.drawable.list_selector_background));
517 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400518 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400519
520 @Override
521 public boolean isEmpty() {
522 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400523 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400524 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400525 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526
Leon Scrogginsc1129902010-12-08 15:28:33 -0500527 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 protected void onCreate(Bundle icicle) {
529 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500530 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100531
532 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100533
Leon Scroggins III052ce662010-09-13 14:44:16 -0400534 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100535
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400536 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700537
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 String title = null;
539 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100540
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400541 mFakeTitle = (TextView) findViewById(R.id.fake_title);
542
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800544 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800546 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547 mMap = b;
548 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400549 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800550 if (mEditingFolder) {
551 findViewById(R.id.row_address).setVisibility(View.GONE);
552 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400553 } else {
554 int gravity = mMap.getInt("gravity", -1);
555 if (gravity != -1) {
556 WindowManager.LayoutParams l = window.getAttributes();
557 l.gravity = gravity;
558 window.setAttributes(l);
559 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400561 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
562 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
563 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700564 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400565 }
Leon Scroggins02081942010-11-01 17:52:42 -0400566 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700567 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400568 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 }
570
571 mTitle = (EditText) findViewById(R.id.title);
572 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100573
Leon Scroggins III052ce662010-09-13 14:44:16 -0400574 mAddress = (EditText) findViewById(R.id.address);
575 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800576
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400578 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579
580 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400581 mCancelButton.setOnClickListener(this);
582
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500583 mFolder = (Spinner) findViewById(R.id.folder);
584 mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
585 if (!mEditingFolder) {
586 // Initially the "Bookmarks" folder should be showing, rather than
587 // the home screen. In the editing folder case, home screen is not
588 // an option, so "Bookmarks" folder is already at the top.
589 mFolder.setSelection(FolderSpinnerAdapter.ROOT_FOLDER);
590 }
591 mFolder.setOnItemSelectedListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400592
593 mDefaultView = findViewById(R.id.default_view);
594 mFolderSelector = findViewById(R.id.folder_selector);
595
Leon Scroggins162f8352010-10-18 15:02:44 -0400596 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
597 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400598 mFolderNamer.setOnEditorActionListener(this);
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500599 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
600 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400601
602 mAddNewFolder = findViewById(R.id.add_new_folder);
603 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400604 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400605
Leon Scroggins74dbe012010-10-15 10:54:27 -0400606 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
607 mCrumbs.setUseBackButton(true);
608 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400609 String name = getString(R.string.bookmarks);
Leon Scroggins905250c2010-12-17 15:25:33 -0500610 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
611 // To better match the other folders.
612 mTopLevelLabel.setCompoundDrawablePadding(6);
613 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_bookmark_widget_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400614 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800615 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616
Leon Scroggins III052ce662010-09-13 14:44:16 -0400617 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400618 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400619 View empty = findViewById(R.id.empty);
620 mListView.setEmptyView(empty);
621 mListView.setAdapter(mAdapter);
622 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400623 mListView.addEditText(mFolderNamer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400624 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500625 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400626 // Find all the folders
627 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500628 // Since we're not in the root folder, change the selection to other
629 // folder now. The text will get changed once we select the correct
630 // folder.
631 mIgnoreSelectionChange = true;
632 mFolder.setSelection(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500633 } else {
634 setShowBookmarkIcon(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400635 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400636 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400637 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
638
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700639
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400640 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800641 mButton.requestFocus();
642 }
643 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100644
Leon Scroggins8baaa632010-12-08 19:46:53 -0500645 /**
646 * Get the account name and type of the currently synced account.
647 * @param context Context to access preferences.
648 * @return null if no account name or type. Otherwise, the result will be
649 * an array of two Strings, the accountName and accountType, respectively.
650 */
651 private String[] getAccountNameAndType(Context context) {
652 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
653 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
654 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
655 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
656 return null;
657 }
658 return new String[] { accountName, accountType };
659 }
660
Leon Scroggins25230d72010-09-28 20:09:25 -0400661 // FIXME: Use a CursorLoader
662 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500663 String[] accountInfo = getAccountNameAndType(context);
664 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500665 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400666 }
667 Cursor cursor = null;
668 try {
669 cursor = context.getContentResolver().query(
670 BrowserContract.Bookmarks.CONTENT_URI,
671 new String[] { BrowserContract.Bookmarks._ID },
672 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
673 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
674 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
675 new String[] {
676 BrowserContract.ChromeSyncColumns
677 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500678 accountInfo[0],
679 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400680 null);
681 if (cursor != null && cursor.moveToFirst()) {
682 return cursor.getLong(0);
683 }
684 } finally {
685 if (cursor != null) cursor.close();
686 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500687 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400688 }
689
Leon Scroggins02065b02010-01-04 14:30:13 -0500690 /**
691 * Runnable to save a bookmark, so it can be performed in its own thread.
692 */
693 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400694 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500695 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200696 private Context mContext;
697 public SaveBookmarkRunnable(Context ctx, Message msg) {
698 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500699 mMessage = msg;
700 }
701 public void run() {
702 // Unbundle bookmark data.
703 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400704 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
705 String url = bundle.getString(BrowserContract.Bookmarks.URL);
706 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500707 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400708 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
709 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500710
711 // Save to the bookmarks DB.
712 try {
713 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400714 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
715 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500716 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200717 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500718 }
719 mMessage.arg1 = 1;
720 } catch (IllegalStateException e) {
721 mMessage.arg1 = 0;
722 }
723 mMessage.sendToTarget();
724 }
725 }
726
John Reckc8490812010-11-22 14:15:36 -0800727 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
728 Context mContext;
729 Long mId;
730
731 public UpdateBookmarkTask(Context context, long id) {
732 mContext = context;
733 mId = id;
734 }
735
736 @Override
737 protected Void doInBackground(ContentValues... params) {
738 if (params.length != 1) {
739 throw new IllegalArgumentException("No ContentValues provided!");
740 }
741 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
742 mContext.getContentResolver().update(
743 uri,
744 params[0], null, null);
745 return null;
746 }
747 }
748
Ben Murdoch1794fe22009-09-29 18:14:30 +0100749 private void createHandler() {
750 if (mHandler == null) {
751 mHandler = new Handler() {
752 @Override
753 public void handleMessage(Message msg) {
754 switch (msg.what) {
755 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500756 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100757 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
758 Toast.LENGTH_LONG).show();
759 } else {
760 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
761 Toast.LENGTH_LONG).show();
762 }
763 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400764 case TOUCH_ICON_DOWNLOADED:
765 Bundle b = msg.getData();
766 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400767 AddBookmarkPage.this,
768 b.getString(BrowserContract.Bookmarks.URL),
769 b.getString(BrowserContract.Bookmarks.TITLE),
770 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
771 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400772 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100773 }
774 }
775 };
776 }
777 }
778
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100780 * 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 -0800781 */
782 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100783 createHandler();
784
The Android Open Source Project0c908882009-03-03 19:32:16 -0800785 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100786 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100788
The Android Open Source Project0c908882009-03-03 19:32:16 -0800789 boolean emptyTitle = title.length() == 0;
790 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
791 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800792 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800793 if (emptyTitle) {
794 mTitle.setError(r.getText(R.string.bookmark_needs_title));
795 }
796 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400797 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800798 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400799 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100800
The Android Open Source Project0c908882009-03-03 19:32:16 -0800801 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000802 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800803 if (!mEditingFolder) {
804 try {
805 // We allow bookmarks with a javascript: scheme, but these will in most cases
806 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000807
John Reckc8490812010-11-22 14:15:36 -0800808 if (!url.toLowerCase().startsWith("javascript:")) {
809 URI uriObj = new URI(url);
810 String scheme = uriObj.getScheme();
811 if (!Bookmarks.urlHasAcceptableScheme(url)) {
812 // If the scheme was non-null, let the user know that we
813 // can't save their bookmark. If it was null, we'll assume
814 // they meant http when we parse it in the WebAddress class.
815 if (scheme != null) {
816 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
817 return false;
818 }
819 WebAddress address;
820 try {
821 address = new WebAddress(unfilteredUrl);
822 } catch (ParseException e) {
823 throw new URISyntaxException("", "");
824 }
825 if (address.getHost().length() == 0) {
826 throw new URISyntaxException("", "");
827 }
828 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000829 }
Ben Murdochde353622009-10-12 10:29:00 +0100830 }
John Reckc8490812010-11-22 14:15:36 -0800831 } catch (URISyntaxException e) {
832 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
833 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800834 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800835 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100836
Leon Scroggins88d08032010-10-21 15:17:10 -0400837 if (mSaveToHomeScreen) {
838 mEditingExisting = false;
839 }
840
841 boolean urlUnmodified = url.equals(mOriginalUrl);
842
Ben Murdoch1794fe22009-09-29 18:14:30 +0100843 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800844 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
845 ContentValues values = new ContentValues();
846 values.put(BrowserContract.Bookmarks.TITLE, title);
847 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
848 if (!mEditingFolder) {
849 values.put(BrowserContract.Bookmarks.URL, url);
850 if (!urlUnmodified) {
851 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
852 }
853 }
854 if (values.size() > 0) {
855 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
856 }
857 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100858 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400859 Bitmap thumbnail;
860 Bitmap favicon;
861 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400862 thumbnail = (Bitmap) mMap.getParcelable(
863 BrowserContract.Bookmarks.THUMBNAIL);
864 favicon = (Bitmap) mMap.getParcelable(
865 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400866 } else {
867 thumbnail = null;
868 favicon = null;
869 }
870
Ben Murdoch1794fe22009-09-29 18:14:30 +0100871 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400872 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
873 bundle.putString(BrowserContract.Bookmarks.URL, url);
874 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400875
876 if (mSaveToHomeScreen) {
877 if (mTouchIconUrl != null && urlUnmodified) {
878 Message msg = Message.obtain(mHandler,
879 TOUCH_ICON_DOWNLOADED);
880 msg.setData(bundle);
881 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400882 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400883 icon.execute(mTouchIconUrl);
884 } else {
885 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
886 title, null /*touchIcon*/, favicon));
887 }
888 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400889 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
890 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
891 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400892 // Post a message to write to the DB.
893 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
894 msg.setData(bundle);
895 // Start a new thread so as to not slow down the UI
896 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
897 t.start();
898 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100899 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000900 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800901 }
902 return true;
903 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400904
905 /*
906 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
907 */
908 public static class CustomListView extends ListView {
909 private EditText mEditText;
910
911 public void addEditText(EditText editText) {
912 mEditText = editText;
913 }
914
915 public CustomListView(Context context) {
916 super(context);
917 }
918
919 public CustomListView(Context context, AttributeSet attrs) {
920 super(context, attrs);
921 }
922
923 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
924 super(context, attrs, defStyle);
925 }
926
927 @Override
928 public boolean checkInputConnectionProxy(View view) {
929 return view == mEditText;
930 }
931 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800932}