blob: 814d93b1cffd1e0575e08979de26cf223afb976f [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
Leon Scroggins III052ce662010-09-13 14:44:16 -0400170 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700171 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400172 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400173 Folder folderData = (Folder) data;
174 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400175 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700176 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400177 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500178 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400179 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400180 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400181 completeOrCancelFolderNaming(true);
182 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500183 setShowBookmarkIcon(level == 1);
184 }
185
186 /**
187 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
188 * @param show True if the icon should visible, false otherwise.
189 */
190 private void setShowBookmarkIcon(boolean show) {
191 Drawable drawable = show ? mHeaderIcon: null;
192 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400193 }
194
Leon Scroggins74dbe012010-10-15 10:54:27 -0400195 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400196 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
197 if (v == mFolderNamer) {
198 if (v.getText().length() > 0) {
199 if (actionId == EditorInfo.IME_NULL) {
200 // Only want to do this once.
201 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400202 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400203 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400204 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205 }
Michael Kolb31829b92010-10-01 11:50:21 -0700206 // Steal the key press; otherwise a newline will be added
Vivek Sekhar5e631472014-03-31 23:59:10 -0700207 // return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400209 return false;
210 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400212 private void switchToDefaultView(boolean changedFolder) {
213 mFolderSelector.setVisibility(View.GONE);
214 mDefaultView.setVisibility(View.VISIBLE);
215 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500216 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400217 if (changedFolder) {
218 Object data = mCrumbs.getTopData();
219 if (data != null) {
220 Folder folder = (Folder) data;
221 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500222 if (mCurrentFolder == mRootFolder) {
223 // The Spinner changed to show "Other folder ..." Change
224 // it back to "Bookmarks", which is position 0 if we are
225 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500226 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500227 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700228 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500229 }
230 }
231 } else {
232 // The user canceled selecting a folder. Revert back to the earlier
233 // selection.
234 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500235 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500236 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500237 if (mCurrentFolder == mRootFolder) {
238 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
239 } else {
240 Object data = mCrumbs.getTopData();
241 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700242 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500243 // folder selector will say "Other folder..." Change it
244 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700245 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500246 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700247 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500248 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500249 setupTopCrumb();
250 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500251 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
252
253 }
254 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400255 }
256 }
257 }
258
Leon Scroggins III052ce662010-09-13 14:44:16 -0400259 @Override
260 public void onClick(View v) {
261 if (v == mButton) {
262 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400263 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400264 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400265 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700266 } else {
267 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400268 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400269 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700270 }
luxiaoldbe4a622013-07-19 17:14:06 +0800271 } else {
272 // add for carrier which requires same title or address can not
273 // exist.
274 if (mSaveToHomeScreen) {
275 if (save()) {
276 return;
277 }
278 } else {
279 onSaveWithConfirm();
280 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400281 }
282 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400283 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400284 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400285 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
286 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700287 } else {
288 finish();
289 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500290 } else if (v == mFolderCancel) {
291 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400292 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400293 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400294 mFolderNamer.setText(R.string.new_folder);
295 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700296 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400297 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400298 InputMethodManager imm = getInputMethodManager();
299 // Set the InputMethodManager to focus on the ListView so that it
300 // can transfer the focus to mFolderNamer.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800301 //imm.focusIn(mListView);
302 Object[] params = {mListView};
303 Class[] type = new Class[] {View.class};
304 ReflectHelper.invokeMethod(imm, "focusIn", type, params);
Leon Scroggins162f8352010-10-18 15:02:44 -0400305 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500306 } else if (v == mRemoveLink) {
307 if (!mEditingExisting) {
308 throw new AssertionError("Remove button should not be shown for"
309 + " new bookmarks");
310 }
311 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
312 createHandler();
313 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
314 BookmarkUtils.displayRemoveBookmarkDialog(id,
315 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800316 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400317 }
318
Leon Scroggins504433a2011-01-13 12:26:52 -0500319 // FolderSpinner.OnSetSelectionListener
320
Leon Scroggins88d08032010-10-21 15:17:10 -0400321 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500322 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500323 int intId = (int) id;
324 switch (intId) {
325 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400326 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400327 mSaveToHomeScreen = false;
328 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500329 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400330 // Create a short cut to the home screen
331 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400332 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500333 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400334 switchToFolderSelector();
335 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500336 case FolderSpinnerAdapter.RECENT_FOLDER:
337 mCurrentFolder = mFolderAdapter.recentFolderId();
338 mSaveToHomeScreen = false;
339 // In case the user decides to select OTHER_FOLDER
340 // and choose a different one, so that we will start from
341 // the correct place.
342 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500343 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500344 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400345 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500346 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400347 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500348 }
349
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500350 /**
351 * Finish naming a folder, and close the IME
352 * @param cancel If true, the new folder is not created. If false, the new
353 * folder is created and the user is taken inside it.
354 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400355 private void completeOrCancelFolderNaming(boolean cancel) {
356 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700357 String name = mFolderNamer.getText().toString();
358 long id = addFolderToCurrent(mFolderNamer.getText().toString());
359 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700360 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400361 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400362 mAddNewFolder.setVisibility(View.VISIBLE);
363 mAddSeparator.setVisibility(View.VISIBLE);
364 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400365 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700366 }
367
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700368 private long addFolderToCurrent(String name) {
369 // Add the folder to the database
370 ContentValues values = new ContentValues();
371 values.put(BrowserContract.Bookmarks.TITLE,
372 name);
373 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400374 long currentFolder;
375 Object data = mCrumbs.getTopData();
376 if (data != null) {
377 currentFolder = ((Folder) data).Id;
378 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400379 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400380 }
381 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700382 Uri uri = getContentResolver().insert(
383 BrowserContract.Bookmarks.CONTENT_URI, values);
384 if (uri != null) {
385 return ContentUris.parseId(uri);
386 } else {
387 return -1;
388 }
389 }
390
Leon Scroggins III052ce662010-09-13 14:44:16 -0400391 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500392 // Set the list to the top in case it is scrolled.
393 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400394 mDefaultView.setVisibility(View.GONE);
395 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400396 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500397 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400398 mAddNewFolder.setVisibility(View.VISIBLE);
399 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700400 getInputMethodManager().hideSoftInputFromWindow(
401 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400402 }
403
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700404 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700405 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400406 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400407 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700408 }
409 }
410
John Reck2eec4c32011-05-11 15:55:32 -0700411 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
412 new LoaderCallbacks<EditBookmarkInfo>() {
413
414 @Override
415 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
416 // Don't care
417 }
418
419 @Override
420 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
421 EditBookmarkInfo info) {
422 boolean setAccount = false;
423 if (info.id != -1) {
424 mEditingExisting = true;
425 showRemoveButton();
426 mFakeTitle.setText(R.string.edit_bookmark);
427 mTitle.setText(info.title);
428 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
429 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
430 setAccount = true;
431 setAccount(info.accountName, info.accountType);
432 mCurrentFolder = info.parentId;
433 onCurrentFolderFound();
434 }
John Reck37894a92011-05-13 12:47:53 -0700435 // TODO: Detect if lastUsedId is a subfolder of info.id in the
436 // editing folder case. For now, just don't show the last used
437 // folder at all to prevent any chance of the user adding a parent
438 // folder to a child folder
439 if (info.lastUsedId != -1 && info.lastUsedId != info.id
440 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700441 if (setAccount && info.lastUsedId != mRootFolder
442 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
443 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
444 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
445 } else if (!setAccount) {
446 setAccount = true;
447 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
448 if (info.lastUsedId != mRootFolder) {
449 mFolderAdapter.addRecentFolder(info.lastUsedId,
450 info.lastUsedTitle);
451 }
452 }
453 }
454 if (!setAccount) {
455 mAccountSpinner.setSelection(0);
456 }
457 }
458
459 @Override
460 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
461 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
462 }
463 };
464
465 void setAccount(String accountName, String accountType) {
466 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
467 BookmarkAccount account = mAccountAdapter.getItem(i);
468 if (TextUtils.equals(account.accountName, accountName)
469 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700470 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800471 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700472 return;
473 }
474 }
475 }
476
Leon Scroggins III052ce662010-09-13 14:44:16 -0400477 @Override
478 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
479 String[] projection;
480 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700481 case LOADER_ID_ACCOUNTS:
482 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400483 case LOADER_ID_FOLDER_CONTENTS:
484 projection = new String[] {
485 BrowserContract.Bookmarks._ID,
486 BrowserContract.Bookmarks.TITLE,
487 BrowserContract.Bookmarks.IS_FOLDER
488 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500489 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700490 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500491 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700492 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
493 whereArgs = new String[] { Long.toString(mMap.getLong(
494 BrowserContract.Bookmarks._ID)) };
495 }
496 long currentFolder;
497 Object data = mCrumbs.getTopData();
498 if (data != null) {
499 currentFolder = ((Folder) data).Id;
500 } else {
501 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500502 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400503 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700504 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400505 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500506 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700507 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500508 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400509 default:
510 throw new AssertionError("Asking for nonexistant loader!");
511 }
512 }
513
514 @Override
515 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
516 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700517 case LOADER_ID_ACCOUNTS:
518 mAccountAdapter.clear();
519 while (cursor.moveToNext()) {
520 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500521 }
John Reck2eec4c32011-05-11 15:55:32 -0700522 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
523 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
524 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500525 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400526 case LOADER_ID_FOLDER_CONTENTS:
527 mAdapter.changeCursor(cursor);
528 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400529 }
530 }
531
Dianne Hackborn39772c82010-12-16 00:43:54 -0800532 public void onLoaderReset(Loader<Cursor> loader) {
533 switch (loader.getId()) {
534 case LOADER_ID_FOLDER_CONTENTS:
535 mAdapter.changeCursor(null);
536 break;
537 }
538 }
539
Leon Scroggins02081942010-11-01 17:52:42 -0400540 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500541 * Move cursor to the position that has folderToFind as its "_id".
542 * @param cursor Cursor containing folders in the bookmarks database
543 * @param folderToFind "_id" of the folder to move to.
544 * @param idIndex Index in cursor of "_id"
545 * @throws AssertionError if cursor is empty or there is no row with folderToFind
546 * as its "_id".
547 */
548 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
549 throws AssertionError {
550 if (!cursor.moveToFirst()) {
551 throw new AssertionError("No folders in the database!");
552 }
553 long folder;
554 do {
555 folder = cursor.getLong(idIndex);
556 } while (folder != folderToFind && cursor.moveToNext());
557 if (cursor.isAfterLast()) {
558 throw new AssertionError("Folder(id=" + folderToFind
559 + ") holding this bookmark does not exist!");
560 }
561 }
562
Leon Scroggins III052ce662010-09-13 14:44:16 -0400563 @Override
564 public void onItemClick(AdapterView<?> parent, View view, int position,
565 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400566 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400567 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400568 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400569 }
570
Leon Scroggins162f8352010-10-18 15:02:44 -0400571 private void setShowFolderNamer(boolean show) {
572 if (show != mIsFolderNamerShowing) {
573 mIsFolderNamerShowing = show;
574 if (show) {
575 // Set the selection to the folder namer so it will be in
576 // view.
577 mListView.addFooterView(mFolderNamerHolder);
578 } else {
579 mListView.removeFooterView(mFolderNamerHolder);
580 }
581 // Refresh the list.
582 mListView.setAdapter(mAdapter);
583 if (show) {
584 mListView.setSelection(mListView.getCount() - 1);
585 }
586 }
587 }
588
Leon Scroggins III052ce662010-09-13 14:44:16 -0400589 /**
590 * Shows a list of names of folders.
591 */
592 private class FolderAdapter extends CursorAdapter {
593 public FolderAdapter(Context context) {
594 super(context, null);
595 }
596
597 @Override
598 public void bindView(View view, Context context, Cursor cursor) {
599 ((TextView) view.findViewById(android.R.id.text1)).setText(
600 cursor.getString(cursor.getColumnIndexOrThrow(
601 BrowserContract.Bookmarks.TITLE)));
602 }
603
604 @Override
605 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700606 View view = LayoutInflater.from(context).inflate(
607 R.layout.folder_list_item, null);
608 view.setBackgroundDrawable(context.getResources().
609 getDrawable(android.R.drawable.list_selector_background));
610 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400611 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400612
613 @Override
614 public boolean isEmpty() {
615 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400616 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400617 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400618 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619
Leon Scrogginsc1129902010-12-08 15:28:33 -0500620 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 protected void onCreate(Bundle icicle) {
622 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500623 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100624
625 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100626
Leon Scroggins III052ce662010-09-13 14:44:16 -0400627 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100628
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400629 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700630
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 String title = null;
632 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100633
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400634 mFakeTitle = (TextView) findViewById(R.id.fake_title);
635
The Android Open Source Project0c908882009-03-03 19:32:16 -0800636 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800637 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800639 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 mMap = b;
641 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400642 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800643 if (mEditingFolder) {
644 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500645 } else {
646 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800647 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400648 } else {
649 int gravity = mMap.getInt("gravity", -1);
650 if (gravity != -1) {
651 WindowManager.LayoutParams l = window.getAttributes();
652 l.gravity = gravity;
653 window.setAttributes(l);
654 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400656 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
657 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
658 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700659 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400660 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661
662 mTitle = (EditText) findViewById(R.id.title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700663
664 if (title.length() > MAX_TITLE_LENGTH) {
665 title = title.substring(0, MAX_TITLE_LENGTH);
666 }
667
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700669 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100670
Leon Scroggins III052ce662010-09-13 14:44:16 -0400671 mAddress = (EditText) findViewById(R.id.address);
672 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800673 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400676 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677
678 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400679 mCancelButton.setOnClickListener(this);
680
Leon Scroggins504433a2011-01-13 12:26:52 -0500681 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700682 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500683 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500684 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400685
686 mDefaultView = findViewById(R.id.default_view);
687 mFolderSelector = findViewById(R.id.folder_selector);
688
Leon Scroggins162f8352010-10-18 15:02:44 -0400689 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
690 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400691 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800692
693 // add for carrier test about warning limit of edit text
694 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
695 BrowserUtils.FILENAME_MAX_LENGTH);
696
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500697 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
698 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400699
700 mAddNewFolder = findViewById(R.id.add_new_folder);
701 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400702 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400703
Leon Scroggins74dbe012010-10-15 10:54:27 -0400704 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
705 mCrumbs.setUseBackButton(true);
706 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800707 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400708 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800709 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400710
Leon Scroggins III052ce662010-09-13 14:44:16 -0400711 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400712 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400713 View empty = findViewById(R.id.empty);
714 mListView.setEmptyView(empty);
715 mListView.setAdapter(mAdapter);
716 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400717 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500718
John Reck2eec4c32011-05-11 15:55:32 -0700719 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
720 android.R.layout.simple_spinner_item);
721 mAccountAdapter.setDropDownViewResource(
722 android.R.layout.simple_spinner_dropdown_item);
723 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
724 mAccountSpinner.setAdapter(mAccountAdapter);
725 mAccountSpinner.setOnItemSelectedListener(this);
726
727
Leon Scroggins75630672011-01-13 17:56:15 -0500728 mFakeTitleHolder = findViewById(R.id.title_holder);
729
Leon Scroggins504433a2011-01-13 12:26:52 -0500730 if (!window.getDecorView().isInTouchMode()) {
731 mButton.requestFocus();
732 }
733
John Reck2eec4c32011-05-11 15:55:32 -0700734 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500735 }
736
Leon Scroggins75630672011-01-13 17:56:15 -0500737 private void showRemoveButton() {
738 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
739 mRemoveLink = findViewById(R.id.remove);
740 mRemoveLink.setVisibility(View.VISIBLE);
741 mRemoveLink.setOnClickListener(this);
742 }
743
Leon Scroggins504433a2011-01-13 12:26:52 -0500744 // Called once we have determined which folder is the root folder
745 private void onRootFolderFound(long root) {
746 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700747 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500748 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700749 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500750 }
751
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500752 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700753 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500754 String name = getString(R.string.bookmarks);
755 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
756 new Folder(name, mRootFolder));
757 // To better match the other folders.
758 mTopLevelLabel.setCompoundDrawablePadding(6);
759 }
760
Leon Scroggins504433a2011-01-13 12:26:52 -0500761 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400762 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500763 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500764 // Since we're not in the root folder, change the selection to other
765 // folder now. The text will get changed once we select the correct
766 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500767 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500768 } else {
769 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500770 if (!mEditingFolder) {
771 // Initially the "Bookmarks" folder should be showing, rather than
772 // the home screen. In the editing folder case, home screen is not
773 // an option, so "Bookmarks" folder is already at the top.
774 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
775 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400776 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400777 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700778 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500779 }
780
Leon Scroggins02065b02010-01-04 14:30:13 -0500781 /**
782 * Runnable to save a bookmark, so it can be performed in its own thread.
783 */
784 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400785 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500786 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200787 private Context mContext;
788 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100789 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500790 mMessage = msg;
791 }
792 public void run() {
793 // Unbundle bookmark data.
794 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400795 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
796 String url = bundle.getString(BrowserContract.Bookmarks.URL);
797 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500798 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400799 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
800 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500801
802 // Save to the bookmarks DB.
803 try {
804 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400805 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700806 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500807 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200808 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500809 }
810 mMessage.arg1 = 1;
811 } catch (IllegalStateException e) {
812 mMessage.arg1 = 0;
813 }
814 mMessage.sendToTarget();
815 }
816 }
817
John Reckc8490812010-11-22 14:15:36 -0800818 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
819 Context mContext;
820 Long mId;
821
822 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100823 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800824 mId = id;
825 }
826
827 @Override
828 protected Void doInBackground(ContentValues... params) {
829 if (params.length != 1) {
830 throw new IllegalArgumentException("No ContentValues provided!");
831 }
832 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
833 mContext.getContentResolver().update(
834 uri,
835 params[0], null, null);
836 return null;
837 }
838 }
839
Ben Murdoch1794fe22009-09-29 18:14:30 +0100840 private void createHandler() {
841 if (mHandler == null) {
842 mHandler = new Handler() {
843 @Override
844 public void handleMessage(Message msg) {
845 switch (msg.what) {
846 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500847 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100848 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
849 Toast.LENGTH_LONG).show();
850 } else {
851 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
852 Toast.LENGTH_LONG).show();
853 }
854 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400855 case TOUCH_ICON_DOWNLOADED:
856 Bundle b = msg.getData();
857 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400858 AddBookmarkPage.this,
859 b.getString(BrowserContract.Bookmarks.URL),
860 b.getString(BrowserContract.Bookmarks.TITLE),
861 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
862 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400863 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500864 case BOOKMARK_DELETED:
865 finish();
866 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100867 }
868 }
869 };
870 }
871 }
872
luxiaoldbe4a622013-07-19 17:14:06 +0800873 static void deleteDuplicateBookmark(final Context context, final long id) {
874 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
875 context.getContentResolver().delete(uri, null, null);
876 }
877
878 private void onSaveWithConfirm() {
879 String title = mTitle.getText().toString().trim();
880 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
881 String url = unfilteredUrl.trim();
882 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
883 int duplicateCount;
884 final ContentResolver cr = getContentResolver();
885
886 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
887 BookmarksLoader.PROJECTION,
888 "( title = ? OR url = ? ) AND parent = ?",
889 new String[] {
890 title, url, Long.toString(mCurrentFolder)
891 },
892 null);
893
894 if (cursor == null) {
895 save();
896 return;
897 }
898
899 duplicateCount = cursor.getCount();
900 if (duplicateCount <= 0) {
901 cursor.close();
902 save();
903 return;
904 } else {
905 try {
906 while (cursor.moveToNext()) {
907 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
908 mDuplicateContext = AddBookmarkPage.this;
909 }
910 } catch (IllegalStateException e) {
911 e.printStackTrace();
912 } finally {
913 if (cursor != null)
914 cursor.close();
915 }
916 }
917
918 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
919 save();
920 return;
921 }
922
923 new AlertDialog.Builder(this)
924 .setTitle(getString(R.string.save_to_bookmarks_title))
925 .setMessage(getString(R.string.overwrite_bookmark_msg))
926 .setNegativeButton(android.R.string.cancel, null)
927 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
928 public void onClick(DialogInterface dialog, int which) {
929 if (mDuplicateContext == null) {
930 return;
931 }
932 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
933 save();
934 }
935 })
936 .show();
937 }
938
The Android Open Source Project0c908882009-03-03 19:32:16 -0800939 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100940 * 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 -0800941 */
942 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100943 createHandler();
944
The Android Open Source Project0c908882009-03-03 19:32:16 -0800945 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +0800946 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100947
The Android Open Source Project0c908882009-03-03 19:32:16 -0800948 boolean emptyTitle = title.length() == 0;
949 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
950 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800951 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800952 if (emptyTitle) {
953 mTitle.setError(r.getText(R.string.bookmark_needs_title));
954 }
955 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400956 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800957 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400958 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800959 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000960 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800961 if (!mEditingFolder) {
962 try {
963 // We allow bookmarks with a javascript: scheme, but these will in most cases
964 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000965
John Reckc8490812010-11-22 14:15:36 -0800966 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -0700967 String encodedUrl = URLEncoder.encode(url, "UTF-8");
968 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -0800969 String scheme = uriObj.getScheme();
970 if (!Bookmarks.urlHasAcceptableScheme(url)) {
971 // If the scheme was non-null, let the user know that we
972 // can't save their bookmark. If it was null, we'll assume
973 // they meant http when we parse it in the WebAddress class.
974 if (scheme != null) {
975 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
976 return false;
977 }
978 WebAddress address;
979 try {
980 address = new WebAddress(unfilteredUrl);
981 } catch (ParseException e) {
982 throw new URISyntaxException("", "");
983 }
984 if (address.getHost().length() == 0) {
985 throw new URISyntaxException("", "");
986 }
987 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000988 }
Ben Murdochde353622009-10-12 10:29:00 +0100989 }
John Reckc8490812010-11-22 14:15:36 -0800990 } catch (URISyntaxException e) {
991 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
992 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -0700993 } catch (UnsupportedEncodingException e) {
994 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
995 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800996 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800997 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100998
Leon Scroggins88d08032010-10-21 15:17:10 -0400999 if (mSaveToHomeScreen) {
1000 mEditingExisting = false;
1001 }
1002
1003 boolean urlUnmodified = url.equals(mOriginalUrl);
1004
Ben Murdoch1794fe22009-09-29 18:14:30 +01001005 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001006 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1007 ContentValues values = new ContentValues();
1008 values.put(BrowserContract.Bookmarks.TITLE, title);
1009 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1010 if (!mEditingFolder) {
1011 values.put(BrowserContract.Bookmarks.URL, url);
1012 if (!urlUnmodified) {
1013 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1014 }
1015 }
1016 if (values.size() > 0) {
1017 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1018 }
1019 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001020 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001021 Bitmap thumbnail;
1022 Bitmap favicon;
1023 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001024 thumbnail = (Bitmap) mMap.getParcelable(
1025 BrowserContract.Bookmarks.THUMBNAIL);
1026 favicon = (Bitmap) mMap.getParcelable(
1027 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001028 } else {
1029 thumbnail = null;
1030 favicon = null;
1031 }
1032
Ben Murdoch1794fe22009-09-29 18:14:30 +01001033 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001034 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1035 bundle.putString(BrowserContract.Bookmarks.URL, url);
1036 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001037
1038 if (mSaveToHomeScreen) {
1039 if (mTouchIconUrl != null && urlUnmodified) {
1040 Message msg = Message.obtain(mHandler,
1041 TOUCH_ICON_DOWNLOADED);
1042 msg.setData(bundle);
1043 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001044 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001045 icon.execute(mTouchIconUrl);
1046 } else {
1047 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1048 title, null /*touchIcon*/, favicon));
1049 }
1050 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001051 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1052 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
1053 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001054 // Post a message to write to the DB.
1055 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1056 msg.setData(bundle);
1057 // Start a new thread so as to not slow down the UI
1058 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1059 t.start();
1060 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001061 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001062 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001063 }
luxiaoldbe4a622013-07-19 17:14:06 +08001064 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001065 return true;
1066 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001067
John Reck2eec4c32011-05-11 15:55:32 -07001068 @Override
1069 public void onItemSelected(AdapterView<?> parent, View view, int position,
1070 long id) {
1071 if (mAccountSpinner == parent) {
1072 long root = mAccountAdapter.getItem(position).rootFolderId;
1073 if (root != mRootFolder) {
1074 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001075 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001076 }
1077 }
1078 }
1079
1080 @Override
1081 public void onNothingSelected(AdapterView<?> parent) {
1082 // Don't care
1083 }
1084
Leon Scroggins162f8352010-10-18 15:02:44 -04001085 /*
1086 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1087 */
1088 public static class CustomListView extends ListView {
1089 private EditText mEditText;
1090
1091 public void addEditText(EditText editText) {
1092 mEditText = editText;
1093 }
1094
1095 public CustomListView(Context context) {
1096 super(context);
1097 }
1098
1099 public CustomListView(Context context, AttributeSet attrs) {
1100 super(context, attrs);
1101 }
1102
1103 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1104 super(context, attrs, defStyle);
1105 }
1106
1107 @Override
1108 public boolean checkInputConnectionProxy(View view) {
1109 return view == mEditText;
1110 }
1111 }
John Reck2eec4c32011-05-11 15:55:32 -07001112
John Reck3bf3cfb2011-10-17 18:00:05 -07001113 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001114
1115 static final String[] PROJECTION = new String[] {
1116 Accounts.ACCOUNT_NAME,
1117 Accounts.ACCOUNT_TYPE,
1118 Accounts.ROOT_ID,
1119 };
1120
1121 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1122 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1123 static final int COLUMN_INDEX_ROOT_ID = 2;
1124
1125 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001126 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001127 }
1128
1129 }
1130
John Reck9b8cd1e2011-05-25 18:14:01 -07001131 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001132
1133 private String mLabel;
1134 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001135 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001136
1137 public BookmarkAccount(Context context, Cursor cursor) {
1138 accountName = cursor.getString(
1139 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1140 accountType = cursor.getString(
1141 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1142 rootFolderId = cursor.getLong(
1143 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1144 mLabel = accountName;
1145 if (TextUtils.isEmpty(mLabel)) {
1146 mLabel = context.getString(R.string.local_bookmarks);
1147 }
1148 }
1149
1150 @Override
1151 public String toString() {
1152 return mLabel;
1153 }
1154 }
1155
1156 static class EditBookmarkInfo {
1157 long id = -1;
1158 long parentId = -1;
1159 String parentTitle;
1160 String title;
1161 String accountName;
1162 String accountType;
1163
1164 long lastUsedId = -1;
1165 String lastUsedTitle;
1166 String lastUsedAccountName;
1167 String lastUsedAccountType;
1168 }
1169
1170 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1171
1172 private Context mContext;
1173 private Bundle mMap;
1174
1175 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1176 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001177 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001178 mMap = bundle;
1179 }
1180
1181 @Override
1182 public EditBookmarkInfo loadInBackground() {
1183 final ContentResolver cr = mContext.getContentResolver();
1184 EditBookmarkInfo info = new EditBookmarkInfo();
1185 Cursor c = null;
1186
1187 try {
1188 // First, let's lookup the bookmark (check for dupes, get needed info)
1189 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1190 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1191 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1192 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1193 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1194 new String[] { BrowserContract.Bookmarks._ID},
1195 BrowserContract.Bookmarks.URL + "=?",
1196 new String[] { url }, null);
1197 if (c.getCount() == 1 && c.moveToFirst()) {
1198 info.id = c.getLong(0);
1199 }
1200 c.close();
1201 }
1202 if (info.id != -1) {
1203 c = cr.query(ContentUris.withAppendedId(
1204 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1205 new String[] {
1206 BrowserContract.Bookmarks.PARENT,
1207 BrowserContract.Bookmarks.ACCOUNT_NAME,
1208 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1209 BrowserContract.Bookmarks.TITLE},
1210 null, null, null);
1211 if (c.moveToFirst()) {
1212 info.parentId = c.getLong(0);
1213 info.accountName = c.getString(1);
1214 info.accountType = c.getString(2);
1215 info.title = c.getString(3);
1216 }
1217 c.close();
1218 c = cr.query(ContentUris.withAppendedId(
1219 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1220 new String[] {
1221 BrowserContract.Bookmarks.TITLE,},
1222 null, null, null);
1223 if (c.moveToFirst()) {
1224 info.parentTitle = c.getString(0);
1225 }
1226 c.close();
1227 }
1228
1229 // Figure out the last used folder/account
1230 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1231 new String[] {
1232 BrowserContract.Bookmarks.PARENT,
1233 }, null, null,
1234 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1235 if (c.moveToFirst()) {
1236 long parent = c.getLong(0);
1237 c.close();
1238 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1239 new String[] {
1240 BrowserContract.Bookmarks.TITLE,
1241 BrowserContract.Bookmarks.ACCOUNT_NAME,
1242 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1243 BrowserContract.Bookmarks._ID + "=?", new String[] {
1244 Long.toString(parent)}, null);
1245 if (c.moveToFirst()) {
1246 info.lastUsedId = parent;
1247 info.lastUsedTitle = c.getString(0);
1248 info.lastUsedAccountName = c.getString(1);
1249 info.lastUsedAccountType = c.getString(2);
1250 }
1251 c.close();
1252 }
1253 } finally {
1254 if (c != null) {
1255 c.close();
1256 }
1257 }
1258
1259 return info;
1260 }
1261
1262 @Override
1263 protected void onStartLoading() {
1264 forceLoad();
1265 }
1266
1267 }
1268
The Android Open Source Project0c908882009-03-03 19:32:16 -08001269}