blob: eb242c9101188785667a0c7679cd8b0a989adc96 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
19import android.app.Activity;
luxiaoldbe4a622013-07-19 17:14:06 +080020import android.app.AlertDialog;
Leon Scroggins III052ce662010-09-13 14:44:16 -040021import android.app.LoaderManager;
John Reck2eec4c32011-05-11 15:55:32 -070022import android.app.LoaderManager.LoaderCallbacks;
23import android.content.AsyncTaskLoader;
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;
luxiaoldbe4a622013-07-19 17:14:06 +080029import android.content.DialogInterface;
Leon Scroggins III052ce662010-09-13 14:44:16 -040030import android.content.Loader;
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;
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.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040042import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.view.KeyEvent;
44import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040046import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040048import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
51import android.widget.AdapterView;
John Reck2eec4c32011-05-11 15:55:32 -070052import android.widget.AdapterView.OnItemSelectedListener;
53import android.widget.ArrayAdapter;
Leon Scroggins III052ce662010-09-13 14:44:16 -040054import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040056import android.widget.ListView;
John Reck2eec4c32011-05-11 15:55:32 -070057import android.widget.Spinner;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.widget.TextView;
59import android.widget.Toast;
60
Bijan Amirzada41242f22014-03-21 12:12:18 -070061import com.android.browser.BrowserUtils;
62import com.android.browser.R;
63import com.android.browser.addbookmark.FolderSpinner;
64import com.android.browser.addbookmark.FolderSpinnerAdapter;
65import com.android.browser.platformsupport.BrowserContract;
66import com.android.browser.platformsupport.WebAddress;
67import com.android.browser.platformsupport.BrowserContract.Accounts;
68import com.android.browser.reflect.ReflectHelper;
luxiaoldbe4a622013-07-19 17:14:06 +080069
Cary Clarkf2407c62009-09-04 12:25:10 -040070import java.net.URI;
Vivek Sekharf95fb382014-07-23 11:13:44 -070071import java.net.URLEncoder;
Cary Clarkf2407c62009-09-04 12:25:10 -040072import java.net.URISyntaxException;
Vivek Sekharf95fb382014-07-23 11:13:44 -070073import java.io.UnsupportedEncodingException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040074
75public class AddBookmarkPage extends Activity
76 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040077 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
John Reck2eec4c32011-05-11 15:55:32 -070078 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener,
79 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080080
Michael Kolb370a4f32010-10-06 10:45:32 -070081 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040082 public static final String TOUCH_ICON_URL = "touch_icon_url";
83 // Place on an edited bookmark to remove the saved thumbnail
84 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
85 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050086 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070087
John Reckc8490812010-11-22 14:15:36 -080088 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
89 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
90
kaiyiz3d7e4d52013-09-17 17:56:27 +080091 private static final int MAX_CRUMBS_SHOWN = 1;
Leon Scroggins74dbe012010-10-15 10:54:27 -040092
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 private final String LOGTAG = "Bookmarks";
94
Leon Scroggins III052ce662010-09-13 14:44:16 -040095 // IDs for the CursorLoaders that are used.
John Reck2eec4c32011-05-11 15:55:32 -070096 private final int LOADER_ID_ACCOUNTS = 0;
97 private final int LOADER_ID_FOLDER_CONTENTS = 1;
98 private final int LOADER_ID_EDIT_INFO = 2;
Leon Scroggins III052ce662010-09-13 14:44:16 -040099
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700100 final static int MAX_TITLE_LENGTH = 80;
101
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102 private EditText mTitle;
103 private EditText mAddress;
104 private TextView mButton;
105 private View mCancelButton;
106 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -0800107 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400109 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +0100110 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500111 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400112 private View mDefaultView;
113 private View mFolderSelector;
114 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500115 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400116 private boolean mIsFolderNamerShowing;
117 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400118 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400119 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500120 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400121 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400122 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400123 private TextView mFakeTitle;
124 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400125 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400126 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400127 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500128 private TextView mTopLevelLabel;
129 private Drawable mHeaderIcon;
Leon Scroggins75630672011-01-13 17:56:15 -0500130 private View mRemoveLink;
131 private View mFakeTitleHolder;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500132 private FolderSpinnerAdapter mFolderAdapter;
John Reck2eec4c32011-05-11 15:55:32 -0700133 private Spinner mAccountSpinner;
134 private ArrayAdapter<BookmarkAccount> mAccountAdapter;
luxiaoldbe4a622013-07-19 17:14:06 +0800135 // add for carrier which requires same title or address can not exist.
136 private long mDuplicateId;
137 private Context mDuplicateContext;
John Reck2eec4c32011-05-11 15:55:32 -0700138
Leon Scroggins III052ce662010-09-13 14:44:16 -0400139 private static class Folder {
140 String Name;
141 long Id;
142 Folder(String name, long id) {
143 Name = name;
144 Id = id;
145 }
146 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147
Ben Murdoch1794fe22009-09-29 18:14:30 +0100148 // Message IDs
149 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400150 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500151 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100152
153 private Handler mHandler;
154
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100155 private InputMethodManager getInputMethodManager() {
156 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
157 }
158
Leon Scroggins8baaa632010-12-08 19:46:53 -0500159 private Uri getUriForFolder(long folder) {
John Reck903a0722011-11-09 17:53:10 -0800160 BookmarkAccount account =
161 (BookmarkAccount) mAccountSpinner.getSelectedItem();
162 if (folder == mRootFolder && account != null) {
163 return BookmarksLoader.addAccount(
164 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
165 account.accountType, account.accountName);
166 }
John Reck2eec4c32011-05-11 15:55:32 -0700167 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500168 }
169
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800170 private String getNameFromId(long mCurrentFolder2) {
171 String title = "";
172 Cursor cursor = null;
173 try {
174 cursor = getApplicationContext().getContentResolver().query(
175 BrowserContract.Bookmarks.CONTENT_URI,
176 new String[] {
177 BrowserContract.Bookmarks.TITLE
178 },
179 BrowserContract.Bookmarks._ID + " = ? AND "
180 + BrowserContract.Bookmarks.IS_DELETED + " = ? AND "
181 + BrowserContract.Bookmarks.IS_FOLDER + " = ? ", new String[] {
182 String.valueOf(mCurrentFolder2), 0 + "", 1 + ""
183 }, null);
184 if (cursor != null && cursor.getCount() != 0) {
185 while (cursor.moveToNext()) {
186 title = cursor.getString(0);
187 }
188 }
189 } finally {
190 if (cursor != null) {
191 cursor.close();
192 }
193 }
194 return title;
195 }
196
Leon Scroggins III052ce662010-09-13 14:44:16 -0400197 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700198 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400199 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400200 Folder folderData = (Folder) data;
201 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400202 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700203 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400204 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500205 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400206 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400207 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400208 completeOrCancelFolderNaming(true);
209 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500210 setShowBookmarkIcon(level == 1);
211 }
212
213 /**
214 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
215 * @param show True if the icon should visible, false otherwise.
216 */
217 private void setShowBookmarkIcon(boolean show) {
218 Drawable drawable = show ? mHeaderIcon: null;
219 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400220 }
221
Leon Scroggins74dbe012010-10-15 10:54:27 -0400222 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400223 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
224 if (v == mFolderNamer) {
225 if (v.getText().length() > 0) {
226 if (actionId == EditorInfo.IME_NULL) {
227 // Only want to do this once.
228 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400229 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400230 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400231 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 }
Michael Kolb31829b92010-10-01 11:50:21 -0700233 // Steal the key press; otherwise a newline will be added
Vivek Sekhar5e631472014-03-31 23:59:10 -0700234 // return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400236 return false;
237 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400239 private void switchToDefaultView(boolean changedFolder) {
240 mFolderSelector.setVisibility(View.GONE);
241 mDefaultView.setVisibility(View.VISIBLE);
242 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500243 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400244 if (changedFolder) {
245 Object data = mCrumbs.getTopData();
246 if (data != null) {
247 Folder folder = (Folder) data;
248 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500249 if (mCurrentFolder == mRootFolder) {
250 // The Spinner changed to show "Other folder ..." Change
251 // it back to "Bookmarks", which is position 0 if we are
252 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500253 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500254 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700255 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500256 }
257 }
258 } else {
259 // The user canceled selecting a folder. Revert back to the earlier
260 // selection.
261 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500262 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500263 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500264 if (mCurrentFolder == mRootFolder) {
265 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
266 } else {
267 Object data = mCrumbs.getTopData();
268 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700269 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500270 // folder selector will say "Other folder..." Change it
271 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700272 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500273 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700274 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500275 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500276 setupTopCrumb();
277 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500278 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
279
280 }
281 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400282 }
283 }
284 }
285
Leon Scroggins III052ce662010-09-13 14:44:16 -0400286 @Override
287 public void onClick(View v) {
288 if (v == mButton) {
289 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400290 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400291 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400292 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700293 } else {
294 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400295 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400296 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700297 }
luxiaoldbe4a622013-07-19 17:14:06 +0800298 } else {
299 // add for carrier which requires same title or address can not
300 // exist.
301 if (mSaveToHomeScreen) {
302 if (save()) {
303 return;
304 }
305 } else {
306 onSaveWithConfirm();
307 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400308 }
309 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400310 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400311 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400312 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
313 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700314 } else {
315 finish();
316 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500317 } else if (v == mFolderCancel) {
318 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400319 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400320 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400321 mFolderNamer.setText(R.string.new_folder);
322 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700323 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400324 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400325 InputMethodManager imm = getInputMethodManager();
326 // Set the InputMethodManager to focus on the ListView so that it
327 // can transfer the focus to mFolderNamer.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800328 //imm.focusIn(mListView);
329 Object[] params = {mListView};
330 Class[] type = new Class[] {View.class};
331 ReflectHelper.invokeMethod(imm, "focusIn", type, params);
Leon Scroggins162f8352010-10-18 15:02:44 -0400332 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500333 } else if (v == mRemoveLink) {
334 if (!mEditingExisting) {
335 throw new AssertionError("Remove button should not be shown for"
336 + " new bookmarks");
337 }
338 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
339 createHandler();
340 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
341 BookmarkUtils.displayRemoveBookmarkDialog(id,
342 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800343 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400344 }
345
Leon Scroggins504433a2011-01-13 12:26:52 -0500346 // FolderSpinner.OnSetSelectionListener
347
Leon Scroggins88d08032010-10-21 15:17:10 -0400348 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500349 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500350 int intId = (int) id;
351 switch (intId) {
352 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400353 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400354 mSaveToHomeScreen = false;
355 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500356 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400357 // Create a short cut to the home screen
358 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400359 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500360 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400361 switchToFolderSelector();
362 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500363 case FolderSpinnerAdapter.RECENT_FOLDER:
364 mCurrentFolder = mFolderAdapter.recentFolderId();
365 mSaveToHomeScreen = false;
366 // In case the user decides to select OTHER_FOLDER
367 // and choose a different one, so that we will start from
368 // the correct place.
369 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500370 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500371 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400372 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500373 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400374 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500375 }
376
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500377 /**
378 * Finish naming a folder, and close the IME
379 * @param cancel If true, the new folder is not created. If false, the new
380 * folder is created and the user is taken inside it.
381 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400382 private void completeOrCancelFolderNaming(boolean cancel) {
383 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700384 String name = mFolderNamer.getText().toString();
385 long id = addFolderToCurrent(mFolderNamer.getText().toString());
386 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700387 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400388 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400389 mAddNewFolder.setVisibility(View.VISIBLE);
390 mAddSeparator.setVisibility(View.VISIBLE);
391 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400392 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700393 }
394
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700395 private long addFolderToCurrent(String name) {
396 // Add the folder to the database
397 ContentValues values = new ContentValues();
398 values.put(BrowserContract.Bookmarks.TITLE,
399 name);
400 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400401 long currentFolder;
402 Object data = mCrumbs.getTopData();
403 if (data != null) {
404 currentFolder = ((Folder) data).Id;
405 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400406 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400407 }
408 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700409 Uri uri = getContentResolver().insert(
410 BrowserContract.Bookmarks.CONTENT_URI, values);
411 if (uri != null) {
412 return ContentUris.parseId(uri);
413 } else {
414 return -1;
415 }
416 }
417
Leon Scroggins III052ce662010-09-13 14:44:16 -0400418 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500419 // Set the list to the top in case it is scrolled.
420 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400421 mDefaultView.setVisibility(View.GONE);
422 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400423 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500424 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400425 mAddNewFolder.setVisibility(View.VISIBLE);
426 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700427 getInputMethodManager().hideSoftInputFromWindow(
428 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400429 }
430
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700431 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700432 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400433 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400434 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700435 }
436 }
437
John Reck2eec4c32011-05-11 15:55:32 -0700438 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
439 new LoaderCallbacks<EditBookmarkInfo>() {
440
441 @Override
442 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
443 // Don't care
444 }
445
446 @Override
447 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
448 EditBookmarkInfo info) {
449 boolean setAccount = false;
450 if (info.id != -1) {
451 mEditingExisting = true;
452 showRemoveButton();
453 mFakeTitle.setText(R.string.edit_bookmark);
454 mTitle.setText(info.title);
455 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
456 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
457 setAccount = true;
458 setAccount(info.accountName, info.accountType);
459 mCurrentFolder = info.parentId;
460 onCurrentFolderFound();
461 }
John Reck37894a92011-05-13 12:47:53 -0700462 // TODO: Detect if lastUsedId is a subfolder of info.id in the
463 // editing folder case. For now, just don't show the last used
464 // folder at all to prevent any chance of the user adding a parent
465 // folder to a child folder
466 if (info.lastUsedId != -1 && info.lastUsedId != info.id
467 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700468 if (setAccount && info.lastUsedId != mRootFolder
469 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
470 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
471 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
472 } else if (!setAccount) {
473 setAccount = true;
474 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
475 if (info.lastUsedId != mRootFolder) {
476 mFolderAdapter.addRecentFolder(info.lastUsedId,
477 info.lastUsedTitle);
478 }
479 }
480 }
481 if (!setAccount) {
482 mAccountSpinner.setSelection(0);
483 }
484 }
485
486 @Override
487 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
488 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
489 }
490 };
491
492 void setAccount(String accountName, String accountType) {
493 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
494 BookmarkAccount account = mAccountAdapter.getItem(i);
495 if (TextUtils.equals(account.accountName, accountName)
496 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700497 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800498 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700499 return;
500 }
501 }
502 }
503
Leon Scroggins III052ce662010-09-13 14:44:16 -0400504 @Override
505 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
506 String[] projection;
507 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700508 case LOADER_ID_ACCOUNTS:
509 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400510 case LOADER_ID_FOLDER_CONTENTS:
511 projection = new String[] {
512 BrowserContract.Bookmarks._ID,
513 BrowserContract.Bookmarks.TITLE,
514 BrowserContract.Bookmarks.IS_FOLDER
515 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500516 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700517 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500518 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700519 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
520 whereArgs = new String[] { Long.toString(mMap.getLong(
521 BrowserContract.Bookmarks._ID)) };
522 }
523 long currentFolder;
524 Object data = mCrumbs.getTopData();
525 if (data != null) {
526 currentFolder = ((Folder) data).Id;
527 } else {
528 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500529 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400530 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700531 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400532 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500533 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700534 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500535 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400536 default:
537 throw new AssertionError("Asking for nonexistant loader!");
538 }
539 }
540
541 @Override
542 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
543 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700544 case LOADER_ID_ACCOUNTS:
545 mAccountAdapter.clear();
546 while (cursor.moveToNext()) {
547 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500548 }
John Reck2eec4c32011-05-11 15:55:32 -0700549 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
550 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
551 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500552 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400553 case LOADER_ID_FOLDER_CONTENTS:
554 mAdapter.changeCursor(cursor);
555 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400556 }
557 }
558
Dianne Hackborn39772c82010-12-16 00:43:54 -0800559 public void onLoaderReset(Loader<Cursor> loader) {
560 switch (loader.getId()) {
561 case LOADER_ID_FOLDER_CONTENTS:
562 mAdapter.changeCursor(null);
563 break;
564 }
565 }
566
Leon Scroggins02081942010-11-01 17:52:42 -0400567 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500568 * Move cursor to the position that has folderToFind as its "_id".
569 * @param cursor Cursor containing folders in the bookmarks database
570 * @param folderToFind "_id" of the folder to move to.
571 * @param idIndex Index in cursor of "_id"
572 * @throws AssertionError if cursor is empty or there is no row with folderToFind
573 * as its "_id".
574 */
575 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
576 throws AssertionError {
577 if (!cursor.moveToFirst()) {
578 throw new AssertionError("No folders in the database!");
579 }
580 long folder;
581 do {
582 folder = cursor.getLong(idIndex);
583 } while (folder != folderToFind && cursor.moveToNext());
584 if (cursor.isAfterLast()) {
585 throw new AssertionError("Folder(id=" + folderToFind
586 + ") holding this bookmark does not exist!");
587 }
588 }
589
Leon Scroggins III052ce662010-09-13 14:44:16 -0400590 @Override
591 public void onItemClick(AdapterView<?> parent, View view, int position,
592 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400593 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400594 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400595 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400596 }
597
Leon Scroggins162f8352010-10-18 15:02:44 -0400598 private void setShowFolderNamer(boolean show) {
599 if (show != mIsFolderNamerShowing) {
600 mIsFolderNamerShowing = show;
601 if (show) {
602 // Set the selection to the folder namer so it will be in
603 // view.
604 mListView.addFooterView(mFolderNamerHolder);
605 } else {
606 mListView.removeFooterView(mFolderNamerHolder);
607 }
608 // Refresh the list.
609 mListView.setAdapter(mAdapter);
610 if (show) {
611 mListView.setSelection(mListView.getCount() - 1);
612 }
613 }
614 }
615
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 /**
617 * Shows a list of names of folders.
618 */
619 private class FolderAdapter extends CursorAdapter {
620 public FolderAdapter(Context context) {
621 super(context, null);
622 }
623
624 @Override
625 public void bindView(View view, Context context, Cursor cursor) {
626 ((TextView) view.findViewById(android.R.id.text1)).setText(
627 cursor.getString(cursor.getColumnIndexOrThrow(
628 BrowserContract.Bookmarks.TITLE)));
629 }
630
631 @Override
632 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700633 View view = LayoutInflater.from(context).inflate(
634 R.layout.folder_list_item, null);
635 view.setBackgroundDrawable(context.getResources().
636 getDrawable(android.R.drawable.list_selector_background));
637 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400638 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400639
640 @Override
641 public boolean isEmpty() {
642 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400643 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400644 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400645 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800646
Leon Scrogginsc1129902010-12-08 15:28:33 -0500647 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 protected void onCreate(Bundle icicle) {
649 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500650 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100651
652 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100653
Leon Scroggins III052ce662010-09-13 14:44:16 -0400654 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100655
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400656 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700657
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 String title = null;
659 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100660
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400661 mFakeTitle = (TextView) findViewById(R.id.fake_title);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800663 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800665 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 mMap = b;
667 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400668 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800669 if (mEditingFolder) {
670 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500671 } else {
672 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800673 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400674 } else {
675 int gravity = mMap.getInt("gravity", -1);
676 if (gravity != -1) {
677 WindowManager.LayoutParams l = window.getAttributes();
678 l.gravity = gravity;
679 window.setAttributes(l);
680 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400682 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
683 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
684 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700685 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800686
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800687 // Check if title is not empty to prevent NPE
688 if (!TextUtils.isEmpty(title)) {
689 if (title.length() > MAX_TITLE_LENGTH) {
690 title = title.substring(0, MAX_TITLE_LENGTH);
691 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800692 }
Leon Scroggins25230d72010-09-28 20:09:25 -0400693 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694
695 mTitle = (EditText) findViewById(R.id.title);
696 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700697 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100698
Leon Scroggins III052ce662010-09-13 14:44:16 -0400699 mAddress = (EditText) findViewById(R.id.address);
700 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800701 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800702
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400704 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705
706 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400707 mCancelButton.setOnClickListener(this);
708
Leon Scroggins504433a2011-01-13 12:26:52 -0500709 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700710 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500711 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500712 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400713
714 mDefaultView = findViewById(R.id.default_view);
715 mFolderSelector = findViewById(R.id.folder_selector);
716
Leon Scroggins162f8352010-10-18 15:02:44 -0400717 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
718 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400719 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800720
721 // add for carrier test about warning limit of edit text
722 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
723 BrowserUtils.FILENAME_MAX_LENGTH);
724
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500725 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
726 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400727
728 mAddNewFolder = findViewById(R.id.add_new_folder);
729 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400730 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400731
Leon Scroggins74dbe012010-10-15 10:54:27 -0400732 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
733 mCrumbs.setUseBackButton(true);
734 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800735 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400736 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800737 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400738
Leon Scroggins III052ce662010-09-13 14:44:16 -0400739 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400740 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400741 View empty = findViewById(R.id.empty);
742 mListView.setEmptyView(empty);
743 mListView.setAdapter(mAdapter);
744 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400745 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500746
John Reck2eec4c32011-05-11 15:55:32 -0700747 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
748 android.R.layout.simple_spinner_item);
749 mAccountAdapter.setDropDownViewResource(
750 android.R.layout.simple_spinner_dropdown_item);
751 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
752 mAccountSpinner.setAdapter(mAccountAdapter);
753 mAccountSpinner.setOnItemSelectedListener(this);
754
755
Leon Scroggins75630672011-01-13 17:56:15 -0500756 mFakeTitleHolder = findViewById(R.id.title_holder);
757
Leon Scroggins504433a2011-01-13 12:26:52 -0500758 if (!window.getDecorView().isInTouchMode()) {
759 mButton.requestFocus();
760 }
761
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800762 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
763 mFolder.setSelectionIgnoringSelectionChange(2);
764 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
765 }
766
John Reck2eec4c32011-05-11 15:55:32 -0700767 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500768 }
769
Leon Scroggins75630672011-01-13 17:56:15 -0500770 private void showRemoveButton() {
771 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
772 mRemoveLink = findViewById(R.id.remove);
773 mRemoveLink.setVisibility(View.VISIBLE);
774 mRemoveLink.setOnClickListener(this);
775 }
776
Leon Scroggins504433a2011-01-13 12:26:52 -0500777 // Called once we have determined which folder is the root folder
778 private void onRootFolderFound(long root) {
779 mRootFolder = root;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500780 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700781 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500782 }
783
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500784 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700785 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500786 String name = getString(R.string.bookmarks);
787 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
788 new Folder(name, mRootFolder));
789 // To better match the other folders.
790 mTopLevelLabel.setCompoundDrawablePadding(6);
791 }
792
Leon Scroggins504433a2011-01-13 12:26:52 -0500793 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400794 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500795 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500796 // Since we're not in the root folder, change the selection to other
797 // folder now. The text will get changed once we select the correct
798 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500799 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500800 } else {
801 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500802 if (!mEditingFolder) {
803 // Initially the "Bookmarks" folder should be showing, rather than
804 // the home screen. In the editing folder case, home screen is not
805 // an option, so "Bookmarks" folder is already at the top.
806 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
807 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400808 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400809 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700810 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500811 }
812
Leon Scroggins02065b02010-01-04 14:30:13 -0500813 /**
814 * Runnable to save a bookmark, so it can be performed in its own thread.
815 */
816 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400817 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500818 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200819 private Context mContext;
820 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100821 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500822 mMessage = msg;
823 }
824 public void run() {
825 // Unbundle bookmark data.
826 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400827 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
828 String url = bundle.getString(BrowserContract.Bookmarks.URL);
829 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500830 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400831 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
832 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500833
834 // Save to the bookmarks DB.
835 try {
836 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400837 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700838 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500839 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200840 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500841 }
842 mMessage.arg1 = 1;
843 } catch (IllegalStateException e) {
844 mMessage.arg1 = 0;
845 }
846 mMessage.sendToTarget();
847 }
848 }
849
John Reckc8490812010-11-22 14:15:36 -0800850 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
851 Context mContext;
852 Long mId;
853
854 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100855 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800856 mId = id;
857 }
858
859 @Override
860 protected Void doInBackground(ContentValues... params) {
861 if (params.length != 1) {
862 throw new IllegalArgumentException("No ContentValues provided!");
863 }
864 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
865 mContext.getContentResolver().update(
866 uri,
867 params[0], null, null);
868 return null;
869 }
870 }
871
Ben Murdoch1794fe22009-09-29 18:14:30 +0100872 private void createHandler() {
873 if (mHandler == null) {
874 mHandler = new Handler() {
875 @Override
876 public void handleMessage(Message msg) {
877 switch (msg.what) {
878 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500879 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100880 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
881 Toast.LENGTH_LONG).show();
882 } else {
883 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
884 Toast.LENGTH_LONG).show();
885 }
886 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400887 case TOUCH_ICON_DOWNLOADED:
888 Bundle b = msg.getData();
889 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400890 AddBookmarkPage.this,
891 b.getString(BrowserContract.Bookmarks.URL),
892 b.getString(BrowserContract.Bookmarks.TITLE),
893 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
894 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400895 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500896 case BOOKMARK_DELETED:
897 finish();
898 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100899 }
900 }
901 };
902 }
903 }
904
luxiaoldbe4a622013-07-19 17:14:06 +0800905 static void deleteDuplicateBookmark(final Context context, final long id) {
906 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
907 context.getContentResolver().delete(uri, null, null);
908 }
909
910 private void onSaveWithConfirm() {
911 String title = mTitle.getText().toString().trim();
912 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
913 String url = unfilteredUrl.trim();
914 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
915 int duplicateCount;
916 final ContentResolver cr = getContentResolver();
917
918 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
919 BookmarksLoader.PROJECTION,
920 "( title = ? OR url = ? ) AND parent = ?",
921 new String[] {
922 title, url, Long.toString(mCurrentFolder)
923 },
924 null);
925
926 if (cursor == null) {
927 save();
928 return;
929 }
930
931 duplicateCount = cursor.getCount();
932 if (duplicateCount <= 0) {
933 cursor.close();
934 save();
935 return;
936 } else {
937 try {
938 while (cursor.moveToNext()) {
939 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
940 mDuplicateContext = AddBookmarkPage.this;
941 }
942 } catch (IllegalStateException e) {
943 e.printStackTrace();
944 } finally {
945 if (cursor != null)
946 cursor.close();
947 }
948 }
949
950 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
951 save();
952 return;
953 }
954
955 new AlertDialog.Builder(this)
956 .setTitle(getString(R.string.save_to_bookmarks_title))
957 .setMessage(getString(R.string.overwrite_bookmark_msg))
958 .setNegativeButton(android.R.string.cancel, null)
959 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
960 public void onClick(DialogInterface dialog, int which) {
961 if (mDuplicateContext == null) {
962 return;
963 }
964 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
965 save();
966 }
967 })
968 .show();
969 }
970
The Android Open Source Project0c908882009-03-03 19:32:16 -0800971 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100972 * 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 -0800973 */
974 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100975 createHandler();
976
The Android Open Source Project0c908882009-03-03 19:32:16 -0800977 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +0800978 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100979
The Android Open Source Project0c908882009-03-03 19:32:16 -0800980 boolean emptyTitle = title.length() == 0;
981 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
982 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800983 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800984 if (emptyTitle) {
985 mTitle.setError(r.getText(R.string.bookmark_needs_title));
986 }
987 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400988 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800989 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400990 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800991 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000992 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800993 if (!mEditingFolder) {
994 try {
995 // We allow bookmarks with a javascript: scheme, but these will in most cases
996 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000997
John Reckc8490812010-11-22 14:15:36 -0800998 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -0700999 String encodedUrl = URLEncoder.encode(url, "UTF-8");
1000 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -08001001 String scheme = uriObj.getScheme();
1002 if (!Bookmarks.urlHasAcceptableScheme(url)) {
1003 // If the scheme was non-null, let the user know that we
1004 // can't save their bookmark. If it was null, we'll assume
1005 // they meant http when we parse it in the WebAddress class.
1006 if (scheme != null) {
1007 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
1008 return false;
1009 }
1010 WebAddress address;
1011 try {
1012 address = new WebAddress(unfilteredUrl);
1013 } catch (ParseException e) {
1014 throw new URISyntaxException("", "");
1015 }
1016 if (address.getHost().length() == 0) {
1017 throw new URISyntaxException("", "");
1018 }
1019 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001020 }
Ben Murdochde353622009-10-12 10:29:00 +01001021 }
John Reckc8490812010-11-22 14:15:36 -08001022 } catch (URISyntaxException e) {
1023 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1024 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -07001025 } catch (UnsupportedEncodingException e) {
1026 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1027 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001028 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001029 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001030
Leon Scroggins88d08032010-10-21 15:17:10 -04001031 if (mSaveToHomeScreen) {
1032 mEditingExisting = false;
1033 }
1034
1035 boolean urlUnmodified = url.equals(mOriginalUrl);
1036
Ben Murdoch1794fe22009-09-29 18:14:30 +01001037 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001038 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1039 ContentValues values = new ContentValues();
1040 values.put(BrowserContract.Bookmarks.TITLE, title);
1041 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1042 if (!mEditingFolder) {
1043 values.put(BrowserContract.Bookmarks.URL, url);
1044 if (!urlUnmodified) {
1045 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1046 }
1047 }
1048 if (values.size() > 0) {
1049 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1050 }
1051 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001052 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001053 Bitmap thumbnail;
1054 Bitmap favicon;
1055 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001056 thumbnail = (Bitmap) mMap.getParcelable(
1057 BrowserContract.Bookmarks.THUMBNAIL);
1058 favicon = (Bitmap) mMap.getParcelable(
1059 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001060 } else {
1061 thumbnail = null;
1062 favicon = null;
1063 }
1064
Ben Murdoch1794fe22009-09-29 18:14:30 +01001065 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001066 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1067 bundle.putString(BrowserContract.Bookmarks.URL, url);
1068 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001069
1070 if (mSaveToHomeScreen) {
1071 if (mTouchIconUrl != null && urlUnmodified) {
1072 Message msg = Message.obtain(mHandler,
1073 TOUCH_ICON_DOWNLOADED);
1074 msg.setData(bundle);
1075 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001076 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001077 icon.execute(mTouchIconUrl);
1078 } else {
1079 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1080 title, null /*touchIcon*/, favicon));
1081 }
1082 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001083 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1084 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
1085 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001086 // Post a message to write to the DB.
1087 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1088 msg.setData(bundle);
1089 // Start a new thread so as to not slow down the UI
1090 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1091 t.start();
1092 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001093 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001094 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001095 }
luxiaoldbe4a622013-07-19 17:14:06 +08001096 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001097 return true;
1098 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001099
John Reck2eec4c32011-05-11 15:55:32 -07001100 @Override
1101 public void onItemSelected(AdapterView<?> parent, View view, int position,
1102 long id) {
1103 if (mAccountSpinner == parent) {
1104 long root = mAccountAdapter.getItem(position).rootFolderId;
1105 if (root != mRootFolder) {
1106 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001107 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001108 }
1109 }
1110 }
1111
1112 @Override
1113 public void onNothingSelected(AdapterView<?> parent) {
1114 // Don't care
1115 }
1116
Leon Scroggins162f8352010-10-18 15:02:44 -04001117 /*
1118 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1119 */
1120 public static class CustomListView extends ListView {
1121 private EditText mEditText;
1122
1123 public void addEditText(EditText editText) {
1124 mEditText = editText;
1125 }
1126
1127 public CustomListView(Context context) {
1128 super(context);
1129 }
1130
1131 public CustomListView(Context context, AttributeSet attrs) {
1132 super(context, attrs);
1133 }
1134
1135 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1136 super(context, attrs, defStyle);
1137 }
1138
1139 @Override
1140 public boolean checkInputConnectionProxy(View view) {
1141 return view == mEditText;
1142 }
1143 }
John Reck2eec4c32011-05-11 15:55:32 -07001144
John Reck3bf3cfb2011-10-17 18:00:05 -07001145 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001146
1147 static final String[] PROJECTION = new String[] {
1148 Accounts.ACCOUNT_NAME,
1149 Accounts.ACCOUNT_TYPE,
1150 Accounts.ROOT_ID,
1151 };
1152
1153 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1154 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1155 static final int COLUMN_INDEX_ROOT_ID = 2;
1156
1157 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001158 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001159 }
1160
1161 }
1162
John Reck9b8cd1e2011-05-25 18:14:01 -07001163 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001164
1165 private String mLabel;
1166 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001167 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001168
1169 public BookmarkAccount(Context context, Cursor cursor) {
1170 accountName = cursor.getString(
1171 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1172 accountType = cursor.getString(
1173 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1174 rootFolderId = cursor.getLong(
1175 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1176 mLabel = accountName;
1177 if (TextUtils.isEmpty(mLabel)) {
1178 mLabel = context.getString(R.string.local_bookmarks);
1179 }
1180 }
1181
1182 @Override
1183 public String toString() {
1184 return mLabel;
1185 }
1186 }
1187
1188 static class EditBookmarkInfo {
1189 long id = -1;
1190 long parentId = -1;
1191 String parentTitle;
1192 String title;
1193 String accountName;
1194 String accountType;
1195
1196 long lastUsedId = -1;
1197 String lastUsedTitle;
1198 String lastUsedAccountName;
1199 String lastUsedAccountType;
1200 }
1201
1202 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1203
1204 private Context mContext;
1205 private Bundle mMap;
1206
1207 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1208 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001209 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001210 mMap = bundle;
1211 }
1212
1213 @Override
1214 public EditBookmarkInfo loadInBackground() {
1215 final ContentResolver cr = mContext.getContentResolver();
1216 EditBookmarkInfo info = new EditBookmarkInfo();
1217 Cursor c = null;
1218
1219 try {
1220 // First, let's lookup the bookmark (check for dupes, get needed info)
1221 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1222 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1223 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1224 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1225 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1226 new String[] { BrowserContract.Bookmarks._ID},
1227 BrowserContract.Bookmarks.URL + "=?",
1228 new String[] { url }, null);
1229 if (c.getCount() == 1 && c.moveToFirst()) {
1230 info.id = c.getLong(0);
1231 }
1232 c.close();
1233 }
1234 if (info.id != -1) {
1235 c = cr.query(ContentUris.withAppendedId(
1236 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1237 new String[] {
1238 BrowserContract.Bookmarks.PARENT,
1239 BrowserContract.Bookmarks.ACCOUNT_NAME,
1240 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1241 BrowserContract.Bookmarks.TITLE},
1242 null, null, null);
1243 if (c.moveToFirst()) {
1244 info.parentId = c.getLong(0);
1245 info.accountName = c.getString(1);
1246 info.accountType = c.getString(2);
1247 info.title = c.getString(3);
1248 }
1249 c.close();
1250 c = cr.query(ContentUris.withAppendedId(
1251 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1252 new String[] {
1253 BrowserContract.Bookmarks.TITLE,},
1254 null, null, null);
1255 if (c.moveToFirst()) {
1256 info.parentTitle = c.getString(0);
1257 }
1258 c.close();
1259 }
1260
1261 // Figure out the last used folder/account
1262 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1263 new String[] {
1264 BrowserContract.Bookmarks.PARENT,
1265 }, null, null,
1266 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1267 if (c.moveToFirst()) {
1268 long parent = c.getLong(0);
1269 c.close();
1270 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1271 new String[] {
1272 BrowserContract.Bookmarks.TITLE,
1273 BrowserContract.Bookmarks.ACCOUNT_NAME,
1274 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1275 BrowserContract.Bookmarks._ID + "=?", new String[] {
1276 Long.toString(parent)}, null);
1277 if (c.moveToFirst()) {
1278 info.lastUsedId = parent;
1279 info.lastUsedTitle = c.getString(0);
1280 info.lastUsedAccountName = c.getString(1);
1281 info.lastUsedAccountType = c.getString(2);
1282 }
1283 c.close();
1284 }
1285 } finally {
1286 if (c != null) {
1287 c.close();
1288 }
1289 }
1290
1291 return info;
1292 }
1293
1294 @Override
1295 protected void onStartLoading() {
1296 forceLoad();
1297 }
1298
1299 }
1300
The Android Open Source Project0c908882009-03-03 19:32:16 -08001301}