blob: 884cdc4da5ab71cb15d33cc30dd7482f4b155fae [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;
Pankaj Garg63396942015-03-06 10:05:25 -0800780 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500781 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700782 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500783 }
784
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500785 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700786 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500787 String name = getString(R.string.bookmarks);
788 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
789 new Folder(name, mRootFolder));
790 // To better match the other folders.
791 mTopLevelLabel.setCompoundDrawablePadding(6);
792 }
793
Leon Scroggins504433a2011-01-13 12:26:52 -0500794 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400795 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500796 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500797 // Since we're not in the root folder, change the selection to other
798 // folder now. The text will get changed once we select the correct
799 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500800 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500801 } else {
802 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500803 if (!mEditingFolder) {
804 // Initially the "Bookmarks" folder should be showing, rather than
805 // the home screen. In the editing folder case, home screen is not
806 // an option, so "Bookmarks" folder is already at the top.
807 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
808 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400809 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400810 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700811 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500812 }
813
Leon Scroggins02065b02010-01-04 14:30:13 -0500814 /**
815 * Runnable to save a bookmark, so it can be performed in its own thread.
816 */
817 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400818 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500819 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200820 private Context mContext;
821 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100822 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500823 mMessage = msg;
824 }
825 public void run() {
826 // Unbundle bookmark data.
827 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400828 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
829 String url = bundle.getString(BrowserContract.Bookmarks.URL);
830 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500831 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700832 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400833 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500834
835 // Save to the bookmarks DB.
836 try {
837 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400838 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700839 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500840 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200841 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500842 }
843 mMessage.arg1 = 1;
844 } catch (IllegalStateException e) {
845 mMessage.arg1 = 0;
846 }
847 mMessage.sendToTarget();
848 }
849 }
850
John Reckc8490812010-11-22 14:15:36 -0800851 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
852 Context mContext;
853 Long mId;
854
855 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100856 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800857 mId = id;
858 }
859
860 @Override
861 protected Void doInBackground(ContentValues... params) {
862 if (params.length != 1) {
863 throw new IllegalArgumentException("No ContentValues provided!");
864 }
865 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
866 mContext.getContentResolver().update(
867 uri,
868 params[0], null, null);
869 return null;
870 }
871 }
872
Ben Murdoch1794fe22009-09-29 18:14:30 +0100873 private void createHandler() {
874 if (mHandler == null) {
875 mHandler = new Handler() {
876 @Override
877 public void handleMessage(Message msg) {
878 switch (msg.what) {
879 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500880 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100881 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
882 Toast.LENGTH_LONG).show();
883 } else {
884 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
885 Toast.LENGTH_LONG).show();
886 }
887 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400888 case TOUCH_ICON_DOWNLOADED:
889 Bundle b = msg.getData();
890 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400891 AddBookmarkPage.this,
892 b.getString(BrowserContract.Bookmarks.URL),
893 b.getString(BrowserContract.Bookmarks.TITLE),
894 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
895 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400896 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500897 case BOOKMARK_DELETED:
898 finish();
899 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100900 }
901 }
902 };
903 }
904 }
905
luxiaoldbe4a622013-07-19 17:14:06 +0800906 static void deleteDuplicateBookmark(final Context context, final long id) {
907 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
908 context.getContentResolver().delete(uri, null, null);
909 }
910
911 private void onSaveWithConfirm() {
912 String title = mTitle.getText().toString().trim();
913 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
914 String url = unfilteredUrl.trim();
915 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
916 int duplicateCount;
917 final ContentResolver cr = getContentResolver();
918
919 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
920 BookmarksLoader.PROJECTION,
921 "( title = ? OR url = ? ) AND parent = ?",
922 new String[] {
923 title, url, Long.toString(mCurrentFolder)
924 },
925 null);
926
927 if (cursor == null) {
928 save();
929 return;
930 }
931
932 duplicateCount = cursor.getCount();
933 if (duplicateCount <= 0) {
934 cursor.close();
935 save();
936 return;
937 } else {
938 try {
939 while (cursor.moveToNext()) {
940 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
941 mDuplicateContext = AddBookmarkPage.this;
942 }
943 } catch (IllegalStateException e) {
944 e.printStackTrace();
945 } finally {
946 if (cursor != null)
947 cursor.close();
948 }
949 }
950
951 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
952 save();
953 return;
954 }
955
956 new AlertDialog.Builder(this)
957 .setTitle(getString(R.string.save_to_bookmarks_title))
958 .setMessage(getString(R.string.overwrite_bookmark_msg))
959 .setNegativeButton(android.R.string.cancel, null)
960 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
961 public void onClick(DialogInterface dialog, int which) {
962 if (mDuplicateContext == null) {
963 return;
964 }
965 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
966 save();
967 }
968 })
969 .show();
970 }
971
The Android Open Source Project0c908882009-03-03 19:32:16 -0800972 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100973 * 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 -0800974 */
975 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100976 createHandler();
977
The Android Open Source Project0c908882009-03-03 19:32:16 -0800978 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +0800979 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100980
The Android Open Source Project0c908882009-03-03 19:32:16 -0800981 boolean emptyTitle = title.length() == 0;
982 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
983 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800984 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800985 if (emptyTitle) {
986 mTitle.setError(r.getText(R.string.bookmark_needs_title));
987 }
988 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400989 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800990 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400991 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800992 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000993 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800994 if (!mEditingFolder) {
995 try {
996 // We allow bookmarks with a javascript: scheme, but these will in most cases
997 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000998
John Reckc8490812010-11-22 14:15:36 -0800999 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -07001000 String encodedUrl = URLEncoder.encode(url, "UTF-8");
1001 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -08001002 String scheme = uriObj.getScheme();
1003 if (!Bookmarks.urlHasAcceptableScheme(url)) {
1004 // If the scheme was non-null, let the user know that we
1005 // can't save their bookmark. If it was null, we'll assume
1006 // they meant http when we parse it in the WebAddress class.
1007 if (scheme != null) {
1008 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
1009 return false;
1010 }
1011 WebAddress address;
1012 try {
1013 address = new WebAddress(unfilteredUrl);
1014 } catch (ParseException e) {
1015 throw new URISyntaxException("", "");
1016 }
1017 if (address.getHost().length() == 0) {
1018 throw new URISyntaxException("", "");
1019 }
1020 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001021 }
Ben Murdochde353622009-10-12 10:29:00 +01001022 }
John Reckc8490812010-11-22 14:15:36 -08001023 } catch (URISyntaxException e) {
1024 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1025 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -07001026 } catch (UnsupportedEncodingException e) {
1027 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1028 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001029 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001030 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001031
Leon Scroggins88d08032010-10-21 15:17:10 -04001032 if (mSaveToHomeScreen) {
1033 mEditingExisting = false;
1034 }
1035
1036 boolean urlUnmodified = url.equals(mOriginalUrl);
1037
Ben Murdoch1794fe22009-09-29 18:14:30 +01001038 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001039 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1040 ContentValues values = new ContentValues();
1041 values.put(BrowserContract.Bookmarks.TITLE, title);
1042 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1043 if (!mEditingFolder) {
1044 values.put(BrowserContract.Bookmarks.URL, url);
1045 if (!urlUnmodified) {
1046 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1047 }
1048 }
1049 if (values.size() > 0) {
1050 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1051 }
1052 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001053 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001054 Bitmap thumbnail;
1055 Bitmap favicon;
1056 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001057 thumbnail = (Bitmap) mMap.getParcelable(
1058 BrowserContract.Bookmarks.THUMBNAIL);
1059 favicon = (Bitmap) mMap.getParcelable(
1060 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001061 } else {
1062 thumbnail = null;
1063 favicon = null;
1064 }
1065
Ben Murdoch1794fe22009-09-29 18:14:30 +01001066 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001067 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1068 bundle.putString(BrowserContract.Bookmarks.URL, url);
1069 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001070
1071 if (mSaveToHomeScreen) {
1072 if (mTouchIconUrl != null && urlUnmodified) {
1073 Message msg = Message.obtain(mHandler,
1074 TOUCH_ICON_DOWNLOADED);
1075 msg.setData(bundle);
1076 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001077 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001078 icon.execute(mTouchIconUrl);
1079 } else {
1080 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1081 title, null /*touchIcon*/, favicon));
1082 }
1083 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001084 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1085 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
1086 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001087 // Post a message to write to the DB.
1088 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1089 msg.setData(bundle);
1090 // Start a new thread so as to not slow down the UI
1091 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1092 t.start();
1093 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001094 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001095 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001096 }
luxiaoldbe4a622013-07-19 17:14:06 +08001097 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001098 return true;
1099 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001100
John Reck2eec4c32011-05-11 15:55:32 -07001101 @Override
1102 public void onItemSelected(AdapterView<?> parent, View view, int position,
1103 long id) {
1104 if (mAccountSpinner == parent) {
1105 long root = mAccountAdapter.getItem(position).rootFolderId;
1106 if (root != mRootFolder) {
1107 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001108 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001109 }
1110 }
1111 }
1112
1113 @Override
1114 public void onNothingSelected(AdapterView<?> parent) {
1115 // Don't care
1116 }
1117
Leon Scroggins162f8352010-10-18 15:02:44 -04001118 /*
1119 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1120 */
1121 public static class CustomListView extends ListView {
1122 private EditText mEditText;
1123
1124 public void addEditText(EditText editText) {
1125 mEditText = editText;
1126 }
1127
1128 public CustomListView(Context context) {
1129 super(context);
1130 }
1131
1132 public CustomListView(Context context, AttributeSet attrs) {
1133 super(context, attrs);
1134 }
1135
1136 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1137 super(context, attrs, defStyle);
1138 }
1139
1140 @Override
1141 public boolean checkInputConnectionProxy(View view) {
1142 return view == mEditText;
1143 }
1144 }
John Reck2eec4c32011-05-11 15:55:32 -07001145
John Reck3bf3cfb2011-10-17 18:00:05 -07001146 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001147
1148 static final String[] PROJECTION = new String[] {
1149 Accounts.ACCOUNT_NAME,
1150 Accounts.ACCOUNT_TYPE,
1151 Accounts.ROOT_ID,
1152 };
1153
1154 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1155 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1156 static final int COLUMN_INDEX_ROOT_ID = 2;
1157
1158 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001159 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001160 }
1161
1162 }
1163
John Reck9b8cd1e2011-05-25 18:14:01 -07001164 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001165
1166 private String mLabel;
1167 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001168 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001169
1170 public BookmarkAccount(Context context, Cursor cursor) {
1171 accountName = cursor.getString(
1172 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1173 accountType = cursor.getString(
1174 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1175 rootFolderId = cursor.getLong(
1176 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1177 mLabel = accountName;
1178 if (TextUtils.isEmpty(mLabel)) {
1179 mLabel = context.getString(R.string.local_bookmarks);
1180 }
1181 }
1182
1183 @Override
1184 public String toString() {
1185 return mLabel;
1186 }
1187 }
1188
1189 static class EditBookmarkInfo {
1190 long id = -1;
1191 long parentId = -1;
1192 String parentTitle;
1193 String title;
1194 String accountName;
1195 String accountType;
1196
1197 long lastUsedId = -1;
1198 String lastUsedTitle;
1199 String lastUsedAccountName;
1200 String lastUsedAccountType;
1201 }
1202
1203 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1204
1205 private Context mContext;
1206 private Bundle mMap;
1207
1208 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1209 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001210 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001211 mMap = bundle;
1212 }
1213
1214 @Override
1215 public EditBookmarkInfo loadInBackground() {
1216 final ContentResolver cr = mContext.getContentResolver();
1217 EditBookmarkInfo info = new EditBookmarkInfo();
1218 Cursor c = null;
1219
1220 try {
1221 // First, let's lookup the bookmark (check for dupes, get needed info)
1222 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1223 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1224 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1225 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1226 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1227 new String[] { BrowserContract.Bookmarks._ID},
1228 BrowserContract.Bookmarks.URL + "=?",
1229 new String[] { url }, null);
1230 if (c.getCount() == 1 && c.moveToFirst()) {
1231 info.id = c.getLong(0);
1232 }
1233 c.close();
1234 }
1235 if (info.id != -1) {
1236 c = cr.query(ContentUris.withAppendedId(
1237 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1238 new String[] {
1239 BrowserContract.Bookmarks.PARENT,
1240 BrowserContract.Bookmarks.ACCOUNT_NAME,
1241 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1242 BrowserContract.Bookmarks.TITLE},
1243 null, null, null);
1244 if (c.moveToFirst()) {
1245 info.parentId = c.getLong(0);
1246 info.accountName = c.getString(1);
1247 info.accountType = c.getString(2);
1248 info.title = c.getString(3);
1249 }
1250 c.close();
1251 c = cr.query(ContentUris.withAppendedId(
1252 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1253 new String[] {
1254 BrowserContract.Bookmarks.TITLE,},
1255 null, null, null);
1256 if (c.moveToFirst()) {
1257 info.parentTitle = c.getString(0);
1258 }
1259 c.close();
1260 }
1261
1262 // Figure out the last used folder/account
1263 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1264 new String[] {
1265 BrowserContract.Bookmarks.PARENT,
1266 }, null, null,
1267 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1268 if (c.moveToFirst()) {
1269 long parent = c.getLong(0);
1270 c.close();
1271 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1272 new String[] {
1273 BrowserContract.Bookmarks.TITLE,
1274 BrowserContract.Bookmarks.ACCOUNT_NAME,
1275 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1276 BrowserContract.Bookmarks._ID + "=?", new String[] {
1277 Long.toString(parent)}, null);
1278 if (c.moveToFirst()) {
1279 info.lastUsedId = parent;
1280 info.lastUsedTitle = c.getString(0);
1281 info.lastUsedAccountName = c.getString(1);
1282 info.lastUsedAccountType = c.getString(2);
1283 }
1284 c.close();
1285 }
1286 } finally {
1287 if (c != null) {
1288 c.close();
1289 }
1290 }
1291
1292 return info;
1293 }
1294
1295 @Override
1296 protected void onStartLoading() {
1297 forceLoad();
1298 }
1299
1300 }
1301
The Android Open Source Project0c908882009-03-03 19:32:16 -08001302}