blob: a50a7d64d6689faf4cf08c7c8552e0a329c46f6f [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;
Pankaj Garg21dad562015-07-02 17:17:24 -0700660 mTouchIconUrl = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100661
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400662 mFakeTitle = (TextView) findViewById(R.id.fake_title);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800664 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800666 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667 mMap = b;
668 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400669 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800670 if (mEditingFolder) {
671 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500672 } else {
673 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800674 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400675 } else {
676 int gravity = mMap.getInt("gravity", -1);
677 if (gravity != -1) {
678 WindowManager.LayoutParams l = window.getAttributes();
679 l.gravity = gravity;
680 window.setAttributes(l);
681 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800682 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400683 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
684 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
685 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700686 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800687
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800688 // Check if title is not empty to prevent NPE
689 if (!TextUtils.isEmpty(title)) {
690 if (title.length() > MAX_TITLE_LENGTH) {
691 title = title.substring(0, MAX_TITLE_LENGTH);
692 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800693 }
Leon Scroggins25230d72010-09-28 20:09:25 -0400694 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800695
696 mTitle = (EditText) findViewById(R.id.title);
697 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700698 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100699
Leon Scroggins III052ce662010-09-13 14:44:16 -0400700 mAddress = (EditText) findViewById(R.id.address);
701 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800702 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703
The Android Open Source Project0c908882009-03-03 19:32:16 -0800704 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400705 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800706
707 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400708 mCancelButton.setOnClickListener(this);
709
Leon Scroggins504433a2011-01-13 12:26:52 -0500710 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700711 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500712 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500713 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400714
715 mDefaultView = findViewById(R.id.default_view);
716 mFolderSelector = findViewById(R.id.folder_selector);
717
Leon Scroggins162f8352010-10-18 15:02:44 -0400718 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
719 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400720 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800721
722 // add for carrier test about warning limit of edit text
723 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
724 BrowserUtils.FILENAME_MAX_LENGTH);
725
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500726 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
727 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400728
729 mAddNewFolder = findViewById(R.id.add_new_folder);
730 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400731 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400732
Leon Scroggins74dbe012010-10-15 10:54:27 -0400733 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
734 mCrumbs.setUseBackButton(true);
735 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800736 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400737 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800738 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400739
Leon Scroggins III052ce662010-09-13 14:44:16 -0400740 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400741 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400742 View empty = findViewById(R.id.empty);
743 mListView.setEmptyView(empty);
744 mListView.setAdapter(mAdapter);
745 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400746 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500747
John Reck2eec4c32011-05-11 15:55:32 -0700748 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
749 android.R.layout.simple_spinner_item);
750 mAccountAdapter.setDropDownViewResource(
751 android.R.layout.simple_spinner_dropdown_item);
752 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
753 mAccountSpinner.setAdapter(mAccountAdapter);
754 mAccountSpinner.setOnItemSelectedListener(this);
755
756
Leon Scroggins75630672011-01-13 17:56:15 -0500757 mFakeTitleHolder = findViewById(R.id.title_holder);
758
Leon Scroggins504433a2011-01-13 12:26:52 -0500759 if (!window.getDecorView().isInTouchMode()) {
760 mButton.requestFocus();
761 }
762
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800763 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
764 mFolder.setSelectionIgnoringSelectionChange(2);
765 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
766 }
767
John Reck2eec4c32011-05-11 15:55:32 -0700768 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500769 }
770
Leon Scroggins75630672011-01-13 17:56:15 -0500771 private void showRemoveButton() {
772 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
773 mRemoveLink = findViewById(R.id.remove);
774 mRemoveLink.setVisibility(View.VISIBLE);
775 mRemoveLink.setOnClickListener(this);
776 }
777
Leon Scroggins504433a2011-01-13 12:26:52 -0500778 // Called once we have determined which folder is the root folder
779 private void onRootFolderFound(long root) {
780 mRootFolder = root;
Pankaj Garg63396942015-03-06 10:05:25 -0800781 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500782 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700783 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500784 }
785
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500786 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700787 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500788 String name = getString(R.string.bookmarks);
789 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
790 new Folder(name, mRootFolder));
791 // To better match the other folders.
792 mTopLevelLabel.setCompoundDrawablePadding(6);
793 }
794
Leon Scroggins504433a2011-01-13 12:26:52 -0500795 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400796 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500797 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500798 // Since we're not in the root folder, change the selection to other
799 // folder now. The text will get changed once we select the correct
800 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500801 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500802 } else {
803 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500804 if (!mEditingFolder) {
805 // Initially the "Bookmarks" folder should be showing, rather than
806 // the home screen. In the editing folder case, home screen is not
807 // an option, so "Bookmarks" folder is already at the top.
808 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
809 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400810 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400811 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700812 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500813 }
814
Leon Scroggins02065b02010-01-04 14:30:13 -0500815 /**
816 * Runnable to save a bookmark, so it can be performed in its own thread.
817 */
818 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400819 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500820 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200821 private Context mContext;
822 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100823 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500824 mMessage = msg;
825 }
826 public void run() {
827 // Unbundle bookmark data.
828 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400829 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
830 String url = bundle.getString(BrowserContract.Bookmarks.URL);
831 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500832 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700833 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400834 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500835
836 // Save to the bookmarks DB.
837 try {
838 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400839 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700840 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500841 if (touchIconUrl != null) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700842 new DownloadTouchIcon(mContext, cr, url).execute(touchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500843 }
844 mMessage.arg1 = 1;
845 } catch (IllegalStateException e) {
846 mMessage.arg1 = 0;
847 }
848 mMessage.sendToTarget();
849 }
850 }
851
John Reckc8490812010-11-22 14:15:36 -0800852 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
853 Context mContext;
854 Long mId;
855
856 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100857 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800858 mId = id;
859 }
860
861 @Override
862 protected Void doInBackground(ContentValues... params) {
863 if (params.length != 1) {
864 throw new IllegalArgumentException("No ContentValues provided!");
865 }
866 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
867 mContext.getContentResolver().update(
868 uri,
869 params[0], null, null);
870 return null;
871 }
872 }
873
Ben Murdoch1794fe22009-09-29 18:14:30 +0100874 private void createHandler() {
875 if (mHandler == null) {
876 mHandler = new Handler() {
877 @Override
878 public void handleMessage(Message msg) {
879 switch (msg.what) {
880 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500881 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100882 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
883 Toast.LENGTH_LONG).show();
884 } else {
885 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
886 Toast.LENGTH_LONG).show();
887 }
888 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400889 case TOUCH_ICON_DOWNLOADED:
890 Bundle b = msg.getData();
891 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400892 AddBookmarkPage.this,
893 b.getString(BrowserContract.Bookmarks.URL),
894 b.getString(BrowserContract.Bookmarks.TITLE),
895 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
896 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400897 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500898 case BOOKMARK_DELETED:
899 finish();
900 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100901 }
902 }
903 };
904 }
905 }
906
luxiaoldbe4a622013-07-19 17:14:06 +0800907 static void deleteDuplicateBookmark(final Context context, final long id) {
908 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
909 context.getContentResolver().delete(uri, null, null);
910 }
911
912 private void onSaveWithConfirm() {
913 String title = mTitle.getText().toString().trim();
914 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
915 String url = unfilteredUrl.trim();
916 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
917 int duplicateCount;
918 final ContentResolver cr = getContentResolver();
919
920 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
921 BookmarksLoader.PROJECTION,
922 "( title = ? OR url = ? ) AND parent = ?",
923 new String[] {
924 title, url, Long.toString(mCurrentFolder)
925 },
926 null);
927
928 if (cursor == null) {
929 save();
930 return;
931 }
932
933 duplicateCount = cursor.getCount();
934 if (duplicateCount <= 0) {
935 cursor.close();
936 save();
937 return;
938 } else {
939 try {
940 while (cursor.moveToNext()) {
941 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
942 mDuplicateContext = AddBookmarkPage.this;
943 }
944 } catch (IllegalStateException e) {
945 e.printStackTrace();
946 } finally {
947 if (cursor != null)
948 cursor.close();
949 }
950 }
951
952 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
953 save();
954 return;
955 }
956
957 new AlertDialog.Builder(this)
958 .setTitle(getString(R.string.save_to_bookmarks_title))
959 .setMessage(getString(R.string.overwrite_bookmark_msg))
960 .setNegativeButton(android.R.string.cancel, null)
961 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
962 public void onClick(DialogInterface dialog, int which) {
963 if (mDuplicateContext == null) {
964 return;
965 }
966 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
967 save();
968 }
969 })
970 .show();
971 }
972
The Android Open Source Project0c908882009-03-03 19:32:16 -0800973 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100974 * 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 -0800975 */
976 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100977 createHandler();
978
The Android Open Source Project0c908882009-03-03 19:32:16 -0800979 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +0800980 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100981
The Android Open Source Project0c908882009-03-03 19:32:16 -0800982 boolean emptyTitle = title.length() == 0;
983 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
984 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800985 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800986 if (emptyTitle) {
987 mTitle.setError(r.getText(R.string.bookmark_needs_title));
988 }
989 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400990 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800991 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400992 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800993 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000994 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800995 if (!mEditingFolder) {
996 try {
997 // We allow bookmarks with a javascript: scheme, but these will in most cases
998 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000999
John Reckc8490812010-11-22 14:15:36 -08001000 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -07001001 String encodedUrl = URLEncoder.encode(url, "UTF-8");
1002 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -08001003 String scheme = uriObj.getScheme();
1004 if (!Bookmarks.urlHasAcceptableScheme(url)) {
1005 // If the scheme was non-null, let the user know that we
1006 // can't save their bookmark. If it was null, we'll assume
1007 // they meant http when we parse it in the WebAddress class.
1008 if (scheme != null) {
1009 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
1010 return false;
1011 }
1012 WebAddress address;
1013 try {
1014 address = new WebAddress(unfilteredUrl);
1015 } catch (ParseException e) {
1016 throw new URISyntaxException("", "");
1017 }
1018 if (address.getHost().length() == 0) {
1019 throw new URISyntaxException("", "");
1020 }
1021 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001022 }
Ben Murdochde353622009-10-12 10:29:00 +01001023 }
John Reckc8490812010-11-22 14:15:36 -08001024 } catch (URISyntaxException e) {
1025 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1026 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -07001027 } catch (UnsupportedEncodingException e) {
1028 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1029 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001030 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001031 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001032
Leon Scroggins88d08032010-10-21 15:17:10 -04001033 if (mSaveToHomeScreen) {
1034 mEditingExisting = false;
1035 }
1036
1037 boolean urlUnmodified = url.equals(mOriginalUrl);
1038
Ben Murdoch1794fe22009-09-29 18:14:30 +01001039 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001040 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1041 ContentValues values = new ContentValues();
1042 values.put(BrowserContract.Bookmarks.TITLE, title);
1043 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1044 if (!mEditingFolder) {
1045 values.put(BrowserContract.Bookmarks.URL, url);
1046 if (!urlUnmodified) {
1047 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1048 }
1049 }
1050 if (values.size() > 0) {
1051 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1052 }
1053 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001054 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001055 Bitmap thumbnail;
1056 Bitmap favicon;
1057 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001058 thumbnail = (Bitmap) mMap.getParcelable(
1059 BrowserContract.Bookmarks.THUMBNAIL);
1060 favicon = (Bitmap) mMap.getParcelable(
1061 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001062 } else {
1063 thumbnail = null;
1064 favicon = null;
1065 }
1066
Ben Murdoch1794fe22009-09-29 18:14:30 +01001067 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001068 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1069 bundle.putString(BrowserContract.Bookmarks.URL, url);
1070 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001071
1072 if (mSaveToHomeScreen) {
1073 if (mTouchIconUrl != null && urlUnmodified) {
1074 Message msg = Message.obtain(mHandler,
1075 TOUCH_ICON_DOWNLOADED);
1076 msg.setData(bundle);
1077 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001078 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001079 icon.execute(mTouchIconUrl);
1080 } else {
1081 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1082 title, null /*touchIcon*/, favicon));
1083 }
1084 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001085 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1086 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Pankaj Garg21dad562015-07-02 17:17:24 -07001087 if (mTouchIconUrl != null) {
1088 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
1089 }
Leon Scroggins88d08032010-10-21 15:17:10 -04001090 // Post a message to write to the DB.
1091 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1092 msg.setData(bundle);
1093 // Start a new thread so as to not slow down the UI
1094 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1095 t.start();
1096 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001097 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001098 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001099 }
luxiaoldbe4a622013-07-19 17:14:06 +08001100 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001101 return true;
1102 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001103
John Reck2eec4c32011-05-11 15:55:32 -07001104 @Override
1105 public void onItemSelected(AdapterView<?> parent, View view, int position,
1106 long id) {
1107 if (mAccountSpinner == parent) {
1108 long root = mAccountAdapter.getItem(position).rootFolderId;
1109 if (root != mRootFolder) {
1110 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001111 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001112 }
1113 }
1114 }
1115
1116 @Override
1117 public void onNothingSelected(AdapterView<?> parent) {
1118 // Don't care
1119 }
1120
Leon Scroggins162f8352010-10-18 15:02:44 -04001121 /*
1122 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1123 */
1124 public static class CustomListView extends ListView {
1125 private EditText mEditText;
1126
1127 public void addEditText(EditText editText) {
1128 mEditText = editText;
1129 }
1130
1131 public CustomListView(Context context) {
1132 super(context);
1133 }
1134
1135 public CustomListView(Context context, AttributeSet attrs) {
1136 super(context, attrs);
1137 }
1138
1139 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1140 super(context, attrs, defStyle);
1141 }
1142
1143 @Override
1144 public boolean checkInputConnectionProxy(View view) {
1145 return view == mEditText;
1146 }
1147 }
John Reck2eec4c32011-05-11 15:55:32 -07001148
John Reck3bf3cfb2011-10-17 18:00:05 -07001149 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001150
1151 static final String[] PROJECTION = new String[] {
1152 Accounts.ACCOUNT_NAME,
1153 Accounts.ACCOUNT_TYPE,
1154 Accounts.ROOT_ID,
1155 };
1156
1157 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1158 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1159 static final int COLUMN_INDEX_ROOT_ID = 2;
1160
1161 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001162 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001163 }
1164
1165 }
1166
John Reck9b8cd1e2011-05-25 18:14:01 -07001167 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001168
1169 private String mLabel;
1170 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001171 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001172
1173 public BookmarkAccount(Context context, Cursor cursor) {
1174 accountName = cursor.getString(
1175 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1176 accountType = cursor.getString(
1177 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1178 rootFolderId = cursor.getLong(
1179 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1180 mLabel = accountName;
1181 if (TextUtils.isEmpty(mLabel)) {
1182 mLabel = context.getString(R.string.local_bookmarks);
1183 }
1184 }
1185
1186 @Override
1187 public String toString() {
1188 return mLabel;
1189 }
1190 }
1191
1192 static class EditBookmarkInfo {
1193 long id = -1;
1194 long parentId = -1;
1195 String parentTitle;
1196 String title;
1197 String accountName;
1198 String accountType;
1199
1200 long lastUsedId = -1;
1201 String lastUsedTitle;
1202 String lastUsedAccountName;
1203 String lastUsedAccountType;
1204 }
1205
1206 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1207
1208 private Context mContext;
1209 private Bundle mMap;
1210
1211 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1212 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001213 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001214 mMap = bundle;
1215 }
1216
1217 @Override
1218 public EditBookmarkInfo loadInBackground() {
1219 final ContentResolver cr = mContext.getContentResolver();
1220 EditBookmarkInfo info = new EditBookmarkInfo();
1221 Cursor c = null;
1222
1223 try {
1224 // First, let's lookup the bookmark (check for dupes, get needed info)
1225 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1226 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1227 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1228 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1229 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1230 new String[] { BrowserContract.Bookmarks._ID},
1231 BrowserContract.Bookmarks.URL + "=?",
1232 new String[] { url }, null);
1233 if (c.getCount() == 1 && c.moveToFirst()) {
1234 info.id = c.getLong(0);
1235 }
1236 c.close();
1237 }
1238 if (info.id != -1) {
1239 c = cr.query(ContentUris.withAppendedId(
1240 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1241 new String[] {
1242 BrowserContract.Bookmarks.PARENT,
1243 BrowserContract.Bookmarks.ACCOUNT_NAME,
1244 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1245 BrowserContract.Bookmarks.TITLE},
1246 null, null, null);
1247 if (c.moveToFirst()) {
1248 info.parentId = c.getLong(0);
1249 info.accountName = c.getString(1);
1250 info.accountType = c.getString(2);
1251 info.title = c.getString(3);
1252 }
1253 c.close();
1254 c = cr.query(ContentUris.withAppendedId(
1255 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1256 new String[] {
1257 BrowserContract.Bookmarks.TITLE,},
1258 null, null, null);
1259 if (c.moveToFirst()) {
1260 info.parentTitle = c.getString(0);
1261 }
1262 c.close();
1263 }
1264
1265 // Figure out the last used folder/account
1266 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1267 new String[] {
1268 BrowserContract.Bookmarks.PARENT,
1269 }, null, null,
1270 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1271 if (c.moveToFirst()) {
1272 long parent = c.getLong(0);
1273 c.close();
1274 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1275 new String[] {
1276 BrowserContract.Bookmarks.TITLE,
1277 BrowserContract.Bookmarks.ACCOUNT_NAME,
1278 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1279 BrowserContract.Bookmarks._ID + "=?", new String[] {
1280 Long.toString(parent)}, null);
1281 if (c.moveToFirst()) {
1282 info.lastUsedId = parent;
1283 info.lastUsedTitle = c.getString(0);
1284 info.lastUsedAccountName = c.getString(1);
1285 info.lastUsedAccountType = c.getString(2);
1286 }
1287 c.close();
1288 }
1289 } finally {
1290 if (c != null) {
1291 c.close();
1292 }
1293 }
1294
1295 return info;
1296 }
1297
1298 @Override
1299 protected void onStartLoading() {
1300 forceLoad();
1301 }
1302
1303 }
1304
The Android Open Source Project0c908882009-03-03 19:32:16 -08001305}