blob: f5b677a66897c74d56cb6b5a9d48eb23012882af [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;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040022import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070024import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040025import android.content.ContentValues;
26import android.content.Context;
27import android.content.CursorLoader;
Leon Scroggins III052ce662010-09-13 14:44:16 -040028import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040029import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040031import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010032import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040033import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070035import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.WebAddress;
John Reckc8490812010-11-22 14:15:36 -080037import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010039import android.os.Handler;
40import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040041import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040042import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040043import android.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040044import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040045import android.view.KeyEvent;
46import android.view.LayoutInflater;
Leon Scroggins88d08032010-10-21 15:17:10 -040047import android.view.MenuItem;
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 Scroggins88d08032010-10-21 15:17:10 -040058import android.widget.PopupMenu;
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 Scroggins88d08032010-10-21 15:17:10 -040069 BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener {
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 Scroggins III052ce662010-09-13 14:44:16 -040097 private TextView mFolder;
98 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 Scroggins III052ce662010-09-13 14:44:16 -0400115
116 private static class Folder {
117 String Name;
118 long Id;
119 Folder(String name, long id) {
120 Name = name;
121 Id = id;
122 }
123 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124
Ben Murdoch1794fe22009-09-29 18:14:30 +0100125 // Message IDs
126 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400127 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100128
129 private Handler mHandler;
130
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100131 private InputMethodManager getInputMethodManager() {
132 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
133 }
134
Leon Scroggins8baaa632010-12-08 19:46:53 -0500135 private Uri getUriForFolder(long folder) {
136 Uri uri;
137 if (folder == mRootFolder) {
138 uri = BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER;
139 } else {
140 uri = BrowserContract.Bookmarks.buildFolderUri(folder);
141 }
142 String[] accountInfo = getAccountNameAndType(this);
143 if (accountInfo != null) {
144 uri = BookmarksLoader.addAccount(uri, accountInfo[1], accountInfo[0]);
145 }
146 return uri;
147 }
148
Leon Scroggins III052ce662010-09-13 14:44:16 -0400149 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400150 public void onTop(int level, Object data) {
151 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400152 Folder folderData = (Folder) data;
153 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400154 LoaderManager manager = getLoaderManager();
155 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
156 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500157 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400158 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400159 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400160 completeOrCancelFolderNaming(true);
161 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500162 setShowBookmarkIcon(level == 1);
163 }
164
165 /**
166 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
167 * @param show True if the icon should visible, false otherwise.
168 */
169 private void setShowBookmarkIcon(boolean show) {
170 Drawable drawable = show ? mHeaderIcon: null;
171 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400172 }
173
Leon Scroggins74dbe012010-10-15 10:54:27 -0400174 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400175 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
176 if (v == mFolderNamer) {
177 if (v.getText().length() > 0) {
178 if (actionId == EditorInfo.IME_NULL) {
179 // Only want to do this once.
180 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400181 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400182 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400183 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 }
Michael Kolb31829b92010-10-01 11:50:21 -0700185 // Steal the key press; otherwise a newline will be added
186 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400188 return false;
189 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400191 private void switchToDefaultView(boolean changedFolder) {
192 mFolderSelector.setVisibility(View.GONE);
193 mDefaultView.setVisibility(View.VISIBLE);
194 mCrumbHolder.setVisibility(View.GONE);
195 mFakeTitle.setVisibility(View.VISIBLE);
196 if (changedFolder) {
197 Object data = mCrumbs.getTopData();
198 if (data != null) {
199 Folder folder = (Folder) data;
200 mCurrentFolder = folder.Id;
Leon Scroggins02081942010-11-01 17:52:42 -0400201 int resource = mCurrentFolder == mRootFolder ?
202 R.drawable.ic_menu_bookmarks :
203 com.android.internal.R.drawable.ic_menu_archive;
204 Drawable drawable = getResources().getDrawable(resource);
205 updateFolderLabel(folder.Name, drawable);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400206 }
207 }
208 }
209
Leon Scroggins III052ce662010-09-13 14:44:16 -0400210 @Override
211 public void onClick(View v) {
212 if (v == mButton) {
213 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400214 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400215 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400216 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700217 } else {
218 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400219 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400220 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700221 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400222 } else if (save()) {
223 finish();
224 }
225 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400226 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400227 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400228 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
229 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700230 } else {
231 finish();
232 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400233 } else if (v == mFolder) {
Leon Scroggins26318a12010-11-08 10:08:40 -0500234 PopupMenu popup = new PopupMenu(this, mFolder);
Leon Scroggins88d08032010-10-21 15:17:10 -0400235 popup.getMenuInflater().inflate(R.menu.folder_choice,
236 popup.getMenu());
John Reckc8490812010-11-22 14:15:36 -0800237 if (mEditingFolder) {
238 popup.getMenu().removeItem(R.id.home_screen);
239 }
Leon Scroggins88d08032010-10-21 15:17:10 -0400240 popup.setOnMenuItemClickListener(this);
241 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400242 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400243 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400244 mFolderNamer.setText(R.string.new_folder);
245 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700246 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400247 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400248 InputMethodManager imm = getInputMethodManager();
249 // Set the InputMethodManager to focus on the ListView so that it
250 // can transfer the focus to mFolderNamer.
251 imm.focusIn(mListView);
252 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400254 }
255
Leon Scroggins88d08032010-10-21 15:17:10 -0400256 @Override
257 public boolean onMenuItemClick(MenuItem item) {
258 switch(item.getItemId()) {
259 case R.id.bookmarks:
Leon Scroggins02081942010-11-01 17:52:42 -0400260 mCurrentFolder = mRootFolder;
261 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400262 mSaveToHomeScreen = false;
263 break;
264 case R.id.home_screen:
265 // Create a short cut to the home screen
266 mSaveToHomeScreen = true;
Leon Scroggins02081942010-11-01 17:52:42 -0400267 updateFolderLabel(item.getTitle(), item.getIcon());
Leon Scroggins88d08032010-10-21 15:17:10 -0400268 break;
269 case R.id.other:
270 switchToFolderSelector();
271 break;
272 default:
273 return false;
274 }
275 return true;
276 }
277
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400278 private void completeOrCancelFolderNaming(boolean cancel) {
279 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700280 String name = mFolderNamer.getText().toString();
281 long id = addFolderToCurrent(mFolderNamer.getText().toString());
282 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700283 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400284 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400285 mAddNewFolder.setVisibility(View.VISIBLE);
286 mAddSeparator.setVisibility(View.VISIBLE);
287 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400288 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700289 }
290
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700291 private long addFolderToCurrent(String name) {
292 // Add the folder to the database
293 ContentValues values = new ContentValues();
294 values.put(BrowserContract.Bookmarks.TITLE,
295 name);
296 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500297 String[] accountInfo = getAccountNameAndType(this);
298 if (accountInfo != null) {
299 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountInfo[1]);
300 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountInfo[0]);
John Recke89daa92010-11-05 14:39:39 -0700301 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400302 long currentFolder;
303 Object data = mCrumbs.getTopData();
304 if (data != null) {
305 currentFolder = ((Folder) data).Id;
306 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400307 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400308 }
309 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700310 Uri uri = getContentResolver().insert(
311 BrowserContract.Bookmarks.CONTENT_URI, values);
312 if (uri != null) {
313 return ContentUris.parseId(uri);
314 } else {
315 return -1;
316 }
317 }
318
Leon Scroggins III052ce662010-09-13 14:44:16 -0400319 private void switchToFolderSelector() {
320 mDefaultView.setVisibility(View.GONE);
321 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400322 mCrumbHolder.setVisibility(View.VISIBLE);
323 mFakeTitle.setVisibility(View.GONE);
324 mAddNewFolder.setVisibility(View.VISIBLE);
325 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400326 }
327
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700328 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700329 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400330 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400331 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700332 }
333 }
334
Leon Scroggins III052ce662010-09-13 14:44:16 -0400335 @Override
336 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
337 String[] projection;
338 switch (id) {
339 case LOADER_ID_ALL_FOLDERS:
340 projection = new String[] {
341 BrowserContract.Bookmarks._ID,
342 BrowserContract.Bookmarks.PARENT,
343 BrowserContract.Bookmarks.TITLE,
344 BrowserContract.Bookmarks.IS_FOLDER
345 };
346 return new CursorLoader(this,
347 BrowserContract.Bookmarks.CONTENT_URI,
348 projection,
349 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
350 null,
351 null);
352 case LOADER_ID_FOLDER_CONTENTS:
353 projection = new String[] {
354 BrowserContract.Bookmarks._ID,
355 BrowserContract.Bookmarks.TITLE,
356 BrowserContract.Bookmarks.IS_FOLDER
357 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500358 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
359 if (mEditingFolder) {
360 where += " AND " + BrowserContract.Bookmarks._ID + " != "
361 + mMap.getLong(BrowserContract.Bookmarks._ID);
362 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400363 return new CursorLoader(this,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500364 getUriForFolder(mCurrentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400365 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500366 where,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400367 null,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500368 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400369 default:
370 throw new AssertionError("Asking for nonexistant loader!");
371 }
372 }
373
374 @Override
375 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
376 switch (loader.getId()) {
377 case LOADER_ID_FOLDER_CONTENTS:
378 mAdapter.changeCursor(cursor);
379 break;
380 case LOADER_ID_ALL_FOLDERS:
381 long parent = mCurrentFolder;
382 int idIndex = cursor.getColumnIndexOrThrow(
383 BrowserContract.Bookmarks._ID);
384 int titleIndex = cursor.getColumnIndexOrThrow(
385 BrowserContract.Bookmarks.TITLE);
386 int parentIndex = cursor.getColumnIndexOrThrow(
387 BrowserContract.Bookmarks.PARENT);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500388 // If the user is editing anything inside the "Other Bookmarks"
389 // folder, we need to stop searching up when we reach its parent.
390 // Find the root folder
391 moveCursorToFolder(cursor, mRootFolder, idIndex);
392 // omniparent is the folder which contains root, and therefore
393 // also the parent of the "Other Bookmarks" folder.
394 long omniparent = cursor.getLong(parentIndex);
395 Stack<Folder> folderStack = new Stack<Folder>();
396 while ((parent != mRootFolder) && (parent != 0) && (parent != omniparent)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400397 // First, find the folder corresponding to the current
398 // folder
Leon Scroggins8baaa632010-12-08 19:46:53 -0500399 moveCursorToFolder(cursor, parent, idIndex);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400400 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400401 if (parent == mCurrentFolder) {
Leon Scroggins02081942010-11-01 17:52:42 -0400402 Drawable draw = getResources().getDrawable(
403 com.android.internal.R.drawable.ic_menu_archive);
404 updateFolderLabel(name, draw);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400405 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400406 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400407 parent = cursor.getLong(parentIndex);
408 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400409 while (!folderStack.isEmpty()) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500410 Folder thisFolder = folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400411 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400412 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400413 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400414 break;
415 default:
416 break;
417 }
418 }
419
Dianne Hackborn39772c82010-12-16 00:43:54 -0800420 public void onLoaderReset(Loader<Cursor> loader) {
421 switch (loader.getId()) {
422 case LOADER_ID_FOLDER_CONTENTS:
423 mAdapter.changeCursor(null);
424 break;
425 }
426 }
427
Leon Scroggins02081942010-11-01 17:52:42 -0400428 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500429 * Move cursor to the position that has folderToFind as its "_id".
430 * @param cursor Cursor containing folders in the bookmarks database
431 * @param folderToFind "_id" of the folder to move to.
432 * @param idIndex Index in cursor of "_id"
433 * @throws AssertionError if cursor is empty or there is no row with folderToFind
434 * as its "_id".
435 */
436 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
437 throws AssertionError {
438 if (!cursor.moveToFirst()) {
439 throw new AssertionError("No folders in the database!");
440 }
441 long folder;
442 do {
443 folder = cursor.getLong(idIndex);
444 } while (folder != folderToFind && cursor.moveToNext());
445 if (cursor.isAfterLast()) {
446 throw new AssertionError("Folder(id=" + folderToFind
447 + ") holding this bookmark does not exist!");
448 }
449 }
450
451 /**
Leon Scroggins02081942010-11-01 17:52:42 -0400452 * Update the name and image to show where the bookmark will be added
453 * @param name Name of the location to save (folder name, bookmarks, or home
454 * screen.
455 * @param drawable Image to show corresponding to the save location.
456 */
457 void updateFolderLabel(CharSequence name, Drawable drawable) {
458 mFolder.setText(name);
459 mFolder.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
460 null);
461 }
462
Leon Scroggins III052ce662010-09-13 14:44:16 -0400463 @Override
464 public void onItemClick(AdapterView<?> parent, View view, int position,
465 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400466 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400467 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400468 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400469 }
470
Leon Scroggins162f8352010-10-18 15:02:44 -0400471 private void setShowFolderNamer(boolean show) {
472 if (show != mIsFolderNamerShowing) {
473 mIsFolderNamerShowing = show;
474 if (show) {
475 // Set the selection to the folder namer so it will be in
476 // view.
477 mListView.addFooterView(mFolderNamerHolder);
478 } else {
479 mListView.removeFooterView(mFolderNamerHolder);
480 }
481 // Refresh the list.
482 mListView.setAdapter(mAdapter);
483 if (show) {
484 mListView.setSelection(mListView.getCount() - 1);
485 }
486 }
487 }
488
Leon Scroggins III052ce662010-09-13 14:44:16 -0400489 /**
490 * Shows a list of names of folders.
491 */
492 private class FolderAdapter extends CursorAdapter {
493 public FolderAdapter(Context context) {
494 super(context, null);
495 }
496
497 @Override
498 public void bindView(View view, Context context, Cursor cursor) {
499 ((TextView) view.findViewById(android.R.id.text1)).setText(
500 cursor.getString(cursor.getColumnIndexOrThrow(
501 BrowserContract.Bookmarks.TITLE)));
502 }
503
504 @Override
505 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700506 View view = LayoutInflater.from(context).inflate(
507 R.layout.folder_list_item, null);
508 view.setBackgroundDrawable(context.getResources().
509 getDrawable(android.R.drawable.list_selector_background));
510 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400511 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400512
513 @Override
514 public boolean isEmpty() {
515 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400516 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400517 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400518 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519
Leon Scrogginsc1129902010-12-08 15:28:33 -0500520 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 protected void onCreate(Bundle icicle) {
522 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500523 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100524
525 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100526
Leon Scroggins III052ce662010-09-13 14:44:16 -0400527 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100528
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400529 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700530
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 String title = null;
532 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100533
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400534 mFakeTitle = (TextView) findViewById(R.id.fake_title);
535
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800537 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800539 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 mMap = b;
541 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400542 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800543 if (mEditingFolder) {
544 findViewById(R.id.row_address).setVisibility(View.GONE);
545 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400546 } else {
547 int gravity = mMap.getInt("gravity", -1);
548 if (gravity != -1) {
549 WindowManager.LayoutParams l = window.getAttributes();
550 l.gravity = gravity;
551 window.setAttributes(l);
552 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800553 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400554 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
555 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
556 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700557 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400558 }
Leon Scroggins02081942010-11-01 17:52:42 -0400559 mRootFolder = getBookmarksBarId(this);
Michael Kolb370a4f32010-10-06 10:45:32 -0700560 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins02081942010-11-01 17:52:42 -0400561 mCurrentFolder = mRootFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800562 }
563
564 mTitle = (EditText) findViewById(R.id.title);
565 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100566
Leon Scroggins III052ce662010-09-13 14:44:16 -0400567 mAddress = (EditText) findViewById(R.id.address);
568 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400571 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572
573 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400574 mCancelButton.setOnClickListener(this);
575
576 mFolder = (TextView) findViewById(R.id.folder);
577 mFolder.setOnClickListener(this);
578
579 mDefaultView = findViewById(R.id.default_view);
580 mFolderSelector = findViewById(R.id.folder_selector);
581
Leon Scroggins162f8352010-10-18 15:02:44 -0400582 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
583 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400584 mFolderNamer.setOnEditorActionListener(this);
585
586 mAddNewFolder = findViewById(R.id.add_new_folder);
587 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400588 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400589
Leon Scroggins74dbe012010-10-15 10:54:27 -0400590 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
591 mCrumbs.setUseBackButton(true);
592 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400593 String name = getString(R.string.bookmarks);
Leon Scroggins905250c2010-12-17 15:25:33 -0500594 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false, new Folder(name, mRootFolder));
595 // To better match the other folders.
596 mTopLevelLabel.setCompoundDrawablePadding(6);
597 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_bookmark_widget_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400598 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800599 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400600
Leon Scroggins III052ce662010-09-13 14:44:16 -0400601 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400602 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400603 View empty = findViewById(R.id.empty);
604 mListView.setEmptyView(empty);
605 mListView.setAdapter(mAdapter);
606 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400607 mListView.addEditText(mFolderNamer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400608 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500609 if (mCurrentFolder != mRootFolder) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400610 // Find all the folders
611 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
Leon Scroggins905250c2010-12-17 15:25:33 -0500612 } else {
613 setShowBookmarkIcon(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400614 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400615 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
617
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700618
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400619 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 mButton.requestFocus();
621 }
622 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100623
Leon Scroggins8baaa632010-12-08 19:46:53 -0500624 /**
625 * Get the account name and type of the currently synced account.
626 * @param context Context to access preferences.
627 * @return null if no account name or type. Otherwise, the result will be
628 * an array of two Strings, the accountName and accountType, respectively.
629 */
630 private String[] getAccountNameAndType(Context context) {
631 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
632 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
633 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
634 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
635 return null;
636 }
637 return new String[] { accountName, accountType };
638 }
639
Leon Scroggins25230d72010-09-28 20:09:25 -0400640 // FIXME: Use a CursorLoader
641 private long getBookmarksBarId(Context context) {
Leon Scroggins8baaa632010-12-08 19:46:53 -0500642 String[] accountInfo = getAccountNameAndType(context);
643 if (accountInfo == null) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500644 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400645 }
646 Cursor cursor = null;
647 try {
648 cursor = context.getContentResolver().query(
649 BrowserContract.Bookmarks.CONTENT_URI,
650 new String[] { BrowserContract.Bookmarks._ID },
651 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
652 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
653 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
654 new String[] {
655 BrowserContract.ChromeSyncColumns
656 .FOLDER_NAME_BOOKMARKS_BAR,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500657 accountInfo[0],
658 accountInfo[1] },
Leon Scroggins25230d72010-09-28 20:09:25 -0400659 null);
660 if (cursor != null && cursor.moveToFirst()) {
661 return cursor.getLong(0);
662 }
663 } finally {
664 if (cursor != null) cursor.close();
665 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500666 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400667 }
668
Leon Scroggins02065b02010-01-04 14:30:13 -0500669 /**
670 * Runnable to save a bookmark, so it can be performed in its own thread.
671 */
672 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400673 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500674 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200675 private Context mContext;
676 public SaveBookmarkRunnable(Context ctx, Message msg) {
677 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500678 mMessage = msg;
679 }
680 public void run() {
681 // Unbundle bookmark data.
682 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400683 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
684 String url = bundle.getString(BrowserContract.Bookmarks.URL);
685 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500686 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400687 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
688 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500689
690 // Save to the bookmarks DB.
691 try {
692 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400693 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
694 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500695 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200696 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500697 }
698 mMessage.arg1 = 1;
699 } catch (IllegalStateException e) {
700 mMessage.arg1 = 0;
701 }
702 mMessage.sendToTarget();
703 }
704 }
705
John Reckc8490812010-11-22 14:15:36 -0800706 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
707 Context mContext;
708 Long mId;
709
710 public UpdateBookmarkTask(Context context, long id) {
711 mContext = context;
712 mId = id;
713 }
714
715 @Override
716 protected Void doInBackground(ContentValues... params) {
717 if (params.length != 1) {
718 throw new IllegalArgumentException("No ContentValues provided!");
719 }
720 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
721 mContext.getContentResolver().update(
722 uri,
723 params[0], null, null);
724 return null;
725 }
726 }
727
Ben Murdoch1794fe22009-09-29 18:14:30 +0100728 private void createHandler() {
729 if (mHandler == null) {
730 mHandler = new Handler() {
731 @Override
732 public void handleMessage(Message msg) {
733 switch (msg.what) {
734 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500735 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100736 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
737 Toast.LENGTH_LONG).show();
738 } else {
739 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
740 Toast.LENGTH_LONG).show();
741 }
742 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400743 case TOUCH_ICON_DOWNLOADED:
744 Bundle b = msg.getData();
745 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400746 AddBookmarkPage.this,
747 b.getString(BrowserContract.Bookmarks.URL),
748 b.getString(BrowserContract.Bookmarks.TITLE),
749 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
750 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400751 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100752 }
753 }
754 };
755 }
756 }
757
The Android Open Source Project0c908882009-03-03 19:32:16 -0800758 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100759 * 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 -0800760 */
761 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100762 createHandler();
763
The Android Open Source Project0c908882009-03-03 19:32:16 -0800764 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100765 String unfilteredUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100767
The Android Open Source Project0c908882009-03-03 19:32:16 -0800768 boolean emptyTitle = title.length() == 0;
769 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
770 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800771 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800772 if (emptyTitle) {
773 mTitle.setError(r.getText(R.string.bookmark_needs_title));
774 }
775 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400776 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800777 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400778 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100779
The Android Open Source Project0c908882009-03-03 19:32:16 -0800780 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000781 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800782 if (!mEditingFolder) {
783 try {
784 // We allow bookmarks with a javascript: scheme, but these will in most cases
785 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000786
John Reckc8490812010-11-22 14:15:36 -0800787 if (!url.toLowerCase().startsWith("javascript:")) {
788 URI uriObj = new URI(url);
789 String scheme = uriObj.getScheme();
790 if (!Bookmarks.urlHasAcceptableScheme(url)) {
791 // If the scheme was non-null, let the user know that we
792 // can't save their bookmark. If it was null, we'll assume
793 // they meant http when we parse it in the WebAddress class.
794 if (scheme != null) {
795 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
796 return false;
797 }
798 WebAddress address;
799 try {
800 address = new WebAddress(unfilteredUrl);
801 } catch (ParseException e) {
802 throw new URISyntaxException("", "");
803 }
804 if (address.getHost().length() == 0) {
805 throw new URISyntaxException("", "");
806 }
807 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000808 }
Ben Murdochde353622009-10-12 10:29:00 +0100809 }
John Reckc8490812010-11-22 14:15:36 -0800810 } catch (URISyntaxException e) {
811 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
812 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800813 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800814 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100815
Leon Scroggins88d08032010-10-21 15:17:10 -0400816 if (mSaveToHomeScreen) {
817 mEditingExisting = false;
818 }
819
820 boolean urlUnmodified = url.equals(mOriginalUrl);
821
Ben Murdoch1794fe22009-09-29 18:14:30 +0100822 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800823 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
824 ContentValues values = new ContentValues();
825 values.put(BrowserContract.Bookmarks.TITLE, title);
826 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
827 if (!mEditingFolder) {
828 values.put(BrowserContract.Bookmarks.URL, url);
829 if (!urlUnmodified) {
830 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
831 }
832 }
833 if (values.size() > 0) {
834 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
835 }
836 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100837 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400838 Bitmap thumbnail;
839 Bitmap favicon;
840 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400841 thumbnail = (Bitmap) mMap.getParcelable(
842 BrowserContract.Bookmarks.THUMBNAIL);
843 favicon = (Bitmap) mMap.getParcelable(
844 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400845 } else {
846 thumbnail = null;
847 favicon = null;
848 }
849
Ben Murdoch1794fe22009-09-29 18:14:30 +0100850 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400851 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
852 bundle.putString(BrowserContract.Bookmarks.URL, url);
853 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400854
855 if (mSaveToHomeScreen) {
856 if (mTouchIconUrl != null && urlUnmodified) {
857 Message msg = Message.obtain(mHandler,
858 TOUCH_ICON_DOWNLOADED);
859 msg.setData(bundle);
860 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400861 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400862 icon.execute(mTouchIconUrl);
863 } else {
864 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
865 title, null /*touchIcon*/, favicon));
866 }
867 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400868 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
869 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
870 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400871 // Post a message to write to the DB.
872 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
873 msg.setData(bundle);
874 // Start a new thread so as to not slow down the UI
875 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
876 t.start();
877 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100878 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000879 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800880 }
881 return true;
882 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400883
884 /*
885 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
886 */
887 public static class CustomListView extends ListView {
888 private EditText mEditText;
889
890 public void addEditText(EditText editText) {
891 mEditText = editText;
892 }
893
894 public CustomListView(Context context) {
895 super(context);
896 }
897
898 public CustomListView(Context context, AttributeSet attrs) {
899 super(context, attrs);
900 }
901
902 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
903 super(context, attrs, defStyle);
904 }
905
906 @Override
907 public boolean checkInputConnectionProxy(View view) {
908 return view == mEditText;
909 }
910 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800911}