blob: 57acfcd778ce4cc2d31dbfead88a412d60baf844 [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
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100 private EditText mTitle;
101 private EditText mAddress;
102 private TextView mButton;
103 private View mCancelButton;
104 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -0800105 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400107 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +0100108 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500109 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400110 private View mDefaultView;
111 private View mFolderSelector;
112 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500113 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400114 private boolean mIsFolderNamerShowing;
115 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400116 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400117 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500118 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400120 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400121 private TextView mFakeTitle;
122 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400123 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400124 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400125 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500126 private TextView mTopLevelLabel;
127 private Drawable mHeaderIcon;
Leon Scroggins75630672011-01-13 17:56:15 -0500128 private View mRemoveLink;
129 private View mFakeTitleHolder;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500130 private FolderSpinnerAdapter mFolderAdapter;
John Reck2eec4c32011-05-11 15:55:32 -0700131 private Spinner mAccountSpinner;
132 private ArrayAdapter<BookmarkAccount> mAccountAdapter;
luxiaoldbe4a622013-07-19 17:14:06 +0800133 // add for carrier which requires same title or address can not exist.
134 private long mDuplicateId;
135 private Context mDuplicateContext;
John Reck2eec4c32011-05-11 15:55:32 -0700136
Leon Scroggins III052ce662010-09-13 14:44:16 -0400137 private static class Folder {
138 String Name;
139 long Id;
140 Folder(String name, long id) {
141 Name = name;
142 Id = id;
143 }
144 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145
Ben Murdoch1794fe22009-09-29 18:14:30 +0100146 // Message IDs
147 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400148 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500149 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100150
151 private Handler mHandler;
152
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100153 private InputMethodManager getInputMethodManager() {
154 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
155 }
156
Leon Scroggins8baaa632010-12-08 19:46:53 -0500157 private Uri getUriForFolder(long folder) {
John Reck903a0722011-11-09 17:53:10 -0800158 BookmarkAccount account =
159 (BookmarkAccount) mAccountSpinner.getSelectedItem();
160 if (folder == mRootFolder && account != null) {
161 return BookmarksLoader.addAccount(
162 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
163 account.accountType, account.accountName);
164 }
John Reck2eec4c32011-05-11 15:55:32 -0700165 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500166 }
167
Leon Scroggins III052ce662010-09-13 14:44:16 -0400168 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700169 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400170 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400171 Folder folderData = (Folder) data;
172 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400173 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700174 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400175 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500176 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400177 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400178 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400179 completeOrCancelFolderNaming(true);
180 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500181 setShowBookmarkIcon(level == 1);
182 }
183
184 /**
185 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
186 * @param show True if the icon should visible, false otherwise.
187 */
188 private void setShowBookmarkIcon(boolean show) {
189 Drawable drawable = show ? mHeaderIcon: null;
190 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400191 }
192
Leon Scroggins74dbe012010-10-15 10:54:27 -0400193 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400194 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
195 if (v == mFolderNamer) {
196 if (v.getText().length() > 0) {
197 if (actionId == EditorInfo.IME_NULL) {
198 // Only want to do this once.
199 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400200 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400201 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400202 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203 }
Michael Kolb31829b92010-10-01 11:50:21 -0700204 // Steal the key press; otherwise a newline will be added
Vivek Sekhar5e631472014-03-31 23:59:10 -0700205 // return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400207 return false;
208 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400210 private void switchToDefaultView(boolean changedFolder) {
211 mFolderSelector.setVisibility(View.GONE);
212 mDefaultView.setVisibility(View.VISIBLE);
213 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500214 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400215 if (changedFolder) {
216 Object data = mCrumbs.getTopData();
217 if (data != null) {
218 Folder folder = (Folder) data;
219 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500220 if (mCurrentFolder == mRootFolder) {
221 // The Spinner changed to show "Other folder ..." Change
222 // it back to "Bookmarks", which is position 0 if we are
223 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500224 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500225 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700226 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500227 }
228 }
229 } else {
230 // The user canceled selecting a folder. Revert back to the earlier
231 // selection.
232 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500233 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500234 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500235 if (mCurrentFolder == mRootFolder) {
236 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
237 } else {
238 Object data = mCrumbs.getTopData();
239 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700240 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500241 // folder selector will say "Other folder..." Change it
242 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700243 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500244 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700245 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500246 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500247 setupTopCrumb();
248 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500249 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
250
251 }
252 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400253 }
254 }
255 }
256
Leon Scroggins III052ce662010-09-13 14:44:16 -0400257 @Override
258 public void onClick(View v) {
259 if (v == mButton) {
260 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400261 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400262 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400263 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700264 } else {
265 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400266 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400267 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700268 }
luxiaoldbe4a622013-07-19 17:14:06 +0800269 } else {
270 // add for carrier which requires same title or address can not
271 // exist.
272 if (mSaveToHomeScreen) {
273 if (save()) {
274 return;
275 }
276 } else {
277 onSaveWithConfirm();
278 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400279 }
280 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400281 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400282 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400283 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
284 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700285 } else {
286 finish();
287 }
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500288 } else if (v == mFolderCancel) {
289 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400290 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400291 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400292 mFolderNamer.setText(R.string.new_folder);
293 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700294 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400295 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400296 InputMethodManager imm = getInputMethodManager();
297 // Set the InputMethodManager to focus on the ListView so that it
298 // can transfer the focus to mFolderNamer.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800299 //imm.focusIn(mListView);
300 Object[] params = {mListView};
301 Class[] type = new Class[] {View.class};
302 ReflectHelper.invokeMethod(imm, "focusIn", type, params);
Leon Scroggins162f8352010-10-18 15:02:44 -0400303 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
Leon Scroggins75630672011-01-13 17:56:15 -0500304 } else if (v == mRemoveLink) {
305 if (!mEditingExisting) {
306 throw new AssertionError("Remove button should not be shown for"
307 + " new bookmarks");
308 }
309 long id = mMap.getLong(BrowserContract.Bookmarks._ID);
310 createHandler();
311 Message msg = Message.obtain(mHandler, BOOKMARK_DELETED);
312 BookmarkUtils.displayRemoveBookmarkDialog(id,
313 mTitle.getText().toString(), this, msg);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800314 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400315 }
316
Leon Scroggins504433a2011-01-13 12:26:52 -0500317 // FolderSpinner.OnSetSelectionListener
318
Leon Scroggins88d08032010-10-21 15:17:10 -0400319 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500320 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500321 int intId = (int) id;
322 switch (intId) {
323 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400324 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400325 mSaveToHomeScreen = false;
326 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500327 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400328 // Create a short cut to the home screen
329 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400330 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500331 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400332 switchToFolderSelector();
333 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500334 case FolderSpinnerAdapter.RECENT_FOLDER:
335 mCurrentFolder = mFolderAdapter.recentFolderId();
336 mSaveToHomeScreen = false;
337 // In case the user decides to select OTHER_FOLDER
338 // and choose a different one, so that we will start from
339 // the correct place.
340 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500341 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500342 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400343 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500344 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400345 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500346 }
347
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500348 /**
349 * Finish naming a folder, and close the IME
350 * @param cancel If true, the new folder is not created. If false, the new
351 * folder is created and the user is taken inside it.
352 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400353 private void completeOrCancelFolderNaming(boolean cancel) {
354 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700355 String name = mFolderNamer.getText().toString();
356 long id = addFolderToCurrent(mFolderNamer.getText().toString());
357 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700358 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400359 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400360 mAddNewFolder.setVisibility(View.VISIBLE);
361 mAddSeparator.setVisibility(View.VISIBLE);
362 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400363 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700364 }
365
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700366 private long addFolderToCurrent(String name) {
367 // Add the folder to the database
368 ContentValues values = new ContentValues();
369 values.put(BrowserContract.Bookmarks.TITLE,
370 name);
371 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400372 long currentFolder;
373 Object data = mCrumbs.getTopData();
374 if (data != null) {
375 currentFolder = ((Folder) data).Id;
376 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400377 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400378 }
379 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700380 Uri uri = getContentResolver().insert(
381 BrowserContract.Bookmarks.CONTENT_URI, values);
382 if (uri != null) {
383 return ContentUris.parseId(uri);
384 } else {
385 return -1;
386 }
387 }
388
Leon Scroggins III052ce662010-09-13 14:44:16 -0400389 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500390 // Set the list to the top in case it is scrolled.
391 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400392 mDefaultView.setVisibility(View.GONE);
393 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400394 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500395 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400396 mAddNewFolder.setVisibility(View.VISIBLE);
397 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700398 getInputMethodManager().hideSoftInputFromWindow(
399 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400400 }
401
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700402 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700403 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400404 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400405 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700406 }
407 }
408
John Reck2eec4c32011-05-11 15:55:32 -0700409 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
410 new LoaderCallbacks<EditBookmarkInfo>() {
411
412 @Override
413 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
414 // Don't care
415 }
416
417 @Override
418 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
419 EditBookmarkInfo info) {
420 boolean setAccount = false;
421 if (info.id != -1) {
422 mEditingExisting = true;
423 showRemoveButton();
424 mFakeTitle.setText(R.string.edit_bookmark);
425 mTitle.setText(info.title);
426 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
427 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
428 setAccount = true;
429 setAccount(info.accountName, info.accountType);
430 mCurrentFolder = info.parentId;
431 onCurrentFolderFound();
432 }
John Reck37894a92011-05-13 12:47:53 -0700433 // TODO: Detect if lastUsedId is a subfolder of info.id in the
434 // editing folder case. For now, just don't show the last used
435 // folder at all to prevent any chance of the user adding a parent
436 // folder to a child folder
437 if (info.lastUsedId != -1 && info.lastUsedId != info.id
438 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700439 if (setAccount && info.lastUsedId != mRootFolder
440 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
441 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
442 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
443 } else if (!setAccount) {
444 setAccount = true;
445 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
446 if (info.lastUsedId != mRootFolder) {
447 mFolderAdapter.addRecentFolder(info.lastUsedId,
448 info.lastUsedTitle);
449 }
450 }
451 }
452 if (!setAccount) {
453 mAccountSpinner.setSelection(0);
454 }
455 }
456
457 @Override
458 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
459 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
460 }
461 };
462
463 void setAccount(String accountName, String accountType) {
464 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
465 BookmarkAccount account = mAccountAdapter.getItem(i);
466 if (TextUtils.equals(account.accountName, accountName)
467 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700468 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800469 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700470 return;
471 }
472 }
473 }
474
Leon Scroggins III052ce662010-09-13 14:44:16 -0400475 @Override
476 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
477 String[] projection;
478 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700479 case LOADER_ID_ACCOUNTS:
480 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400481 case LOADER_ID_FOLDER_CONTENTS:
482 projection = new String[] {
483 BrowserContract.Bookmarks._ID,
484 BrowserContract.Bookmarks.TITLE,
485 BrowserContract.Bookmarks.IS_FOLDER
486 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500487 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700488 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500489 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700490 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
491 whereArgs = new String[] { Long.toString(mMap.getLong(
492 BrowserContract.Bookmarks._ID)) };
493 }
494 long currentFolder;
495 Object data = mCrumbs.getTopData();
496 if (data != null) {
497 currentFolder = ((Folder) data).Id;
498 } else {
499 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500500 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400501 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700502 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400503 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500504 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700505 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500506 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400507 default:
508 throw new AssertionError("Asking for nonexistant loader!");
509 }
510 }
511
512 @Override
513 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
514 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700515 case LOADER_ID_ACCOUNTS:
516 mAccountAdapter.clear();
517 while (cursor.moveToNext()) {
518 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500519 }
John Reck2eec4c32011-05-11 15:55:32 -0700520 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
521 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
522 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500523 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400524 case LOADER_ID_FOLDER_CONTENTS:
525 mAdapter.changeCursor(cursor);
526 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400527 }
528 }
529
Dianne Hackborn39772c82010-12-16 00:43:54 -0800530 public void onLoaderReset(Loader<Cursor> loader) {
531 switch (loader.getId()) {
532 case LOADER_ID_FOLDER_CONTENTS:
533 mAdapter.changeCursor(null);
534 break;
535 }
536 }
537
Leon Scroggins02081942010-11-01 17:52:42 -0400538 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500539 * Move cursor to the position that has folderToFind as its "_id".
540 * @param cursor Cursor containing folders in the bookmarks database
541 * @param folderToFind "_id" of the folder to move to.
542 * @param idIndex Index in cursor of "_id"
543 * @throws AssertionError if cursor is empty or there is no row with folderToFind
544 * as its "_id".
545 */
546 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
547 throws AssertionError {
548 if (!cursor.moveToFirst()) {
549 throw new AssertionError("No folders in the database!");
550 }
551 long folder;
552 do {
553 folder = cursor.getLong(idIndex);
554 } while (folder != folderToFind && cursor.moveToNext());
555 if (cursor.isAfterLast()) {
556 throw new AssertionError("Folder(id=" + folderToFind
557 + ") holding this bookmark does not exist!");
558 }
559 }
560
Leon Scroggins III052ce662010-09-13 14:44:16 -0400561 @Override
562 public void onItemClick(AdapterView<?> parent, View view, int position,
563 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400564 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400565 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400566 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400567 }
568
Leon Scroggins162f8352010-10-18 15:02:44 -0400569 private void setShowFolderNamer(boolean show) {
570 if (show != mIsFolderNamerShowing) {
571 mIsFolderNamerShowing = show;
572 if (show) {
573 // Set the selection to the folder namer so it will be in
574 // view.
575 mListView.addFooterView(mFolderNamerHolder);
576 } else {
577 mListView.removeFooterView(mFolderNamerHolder);
578 }
579 // Refresh the list.
580 mListView.setAdapter(mAdapter);
581 if (show) {
582 mListView.setSelection(mListView.getCount() - 1);
583 }
584 }
585 }
586
Leon Scroggins III052ce662010-09-13 14:44:16 -0400587 /**
588 * Shows a list of names of folders.
589 */
590 private class FolderAdapter extends CursorAdapter {
591 public FolderAdapter(Context context) {
592 super(context, null);
593 }
594
595 @Override
596 public void bindView(View view, Context context, Cursor cursor) {
597 ((TextView) view.findViewById(android.R.id.text1)).setText(
598 cursor.getString(cursor.getColumnIndexOrThrow(
599 BrowserContract.Bookmarks.TITLE)));
600 }
601
602 @Override
603 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700604 View view = LayoutInflater.from(context).inflate(
605 R.layout.folder_list_item, null);
606 view.setBackgroundDrawable(context.getResources().
607 getDrawable(android.R.drawable.list_selector_background));
608 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400609 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400610
611 @Override
612 public boolean isEmpty() {
613 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400614 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400615 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800617
Leon Scrogginsc1129902010-12-08 15:28:33 -0500618 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619 protected void onCreate(Bundle icicle) {
620 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500621 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100622
623 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100624
Leon Scroggins III052ce662010-09-13 14:44:16 -0400625 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100626
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400627 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700628
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 String title = null;
630 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100631
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400632 mFakeTitle = (TextView) findViewById(R.id.fake_title);
633
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800635 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800636 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800637 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638 mMap = b;
639 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400640 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800641 if (mEditingFolder) {
642 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500643 } else {
644 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800645 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400646 } else {
647 int gravity = mMap.getInt("gravity", -1);
648 if (gravity != -1) {
649 WindowManager.LayoutParams l = window.getAttributes();
650 l.gravity = gravity;
651 window.setAttributes(l);
652 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400654 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
655 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
656 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700657 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400658 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659
660 mTitle = (EditText) findViewById(R.id.title);
661 mTitle.setText(title);
luxiaoldbe4a622013-07-19 17:14:06 +0800662 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, BrowserUtils.FILENAME_MAX_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100663
Leon Scroggins III052ce662010-09-13 14:44:16 -0400664 mAddress = (EditText) findViewById(R.id.address);
665 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800666 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400669 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670
671 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400672 mCancelButton.setOnClickListener(this);
673
Leon Scroggins504433a2011-01-13 12:26:52 -0500674 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700675 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500676 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500677 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400678
679 mDefaultView = findViewById(R.id.default_view);
680 mFolderSelector = findViewById(R.id.folder_selector);
681
Leon Scroggins162f8352010-10-18 15:02:44 -0400682 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
683 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400684 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800685
686 // add for carrier test about warning limit of edit text
687 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
688 BrowserUtils.FILENAME_MAX_LENGTH);
689
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500690 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
691 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400692
693 mAddNewFolder = findViewById(R.id.add_new_folder);
694 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400695 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400696
Leon Scroggins74dbe012010-10-15 10:54:27 -0400697 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
698 mCrumbs.setUseBackButton(true);
699 mCrumbs.setController(this);
Michael Kolb5a72f182011-01-13 20:35:06 -0800700 mHeaderIcon = getResources().getDrawable(R.drawable.ic_folder_holo_dark);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400701 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800702 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400703
Leon Scroggins III052ce662010-09-13 14:44:16 -0400704 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400705 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400706 View empty = findViewById(R.id.empty);
707 mListView.setEmptyView(empty);
708 mListView.setAdapter(mAdapter);
709 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400710 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500711
John Reck2eec4c32011-05-11 15:55:32 -0700712 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
713 android.R.layout.simple_spinner_item);
714 mAccountAdapter.setDropDownViewResource(
715 android.R.layout.simple_spinner_dropdown_item);
716 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
717 mAccountSpinner.setAdapter(mAccountAdapter);
718 mAccountSpinner.setOnItemSelectedListener(this);
719
720
Leon Scroggins75630672011-01-13 17:56:15 -0500721 mFakeTitleHolder = findViewById(R.id.title_holder);
722
Leon Scroggins504433a2011-01-13 12:26:52 -0500723 if (!window.getDecorView().isInTouchMode()) {
724 mButton.requestFocus();
725 }
726
John Reck2eec4c32011-05-11 15:55:32 -0700727 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500728 }
729
Leon Scroggins75630672011-01-13 17:56:15 -0500730 private void showRemoveButton() {
731 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
732 mRemoveLink = findViewById(R.id.remove);
733 mRemoveLink.setVisibility(View.VISIBLE);
734 mRemoveLink.setOnClickListener(this);
735 }
736
Leon Scroggins504433a2011-01-13 12:26:52 -0500737 // Called once we have determined which folder is the root folder
738 private void onRootFolderFound(long root) {
739 mRootFolder = root;
John Reck2eec4c32011-05-11 15:55:32 -0700740 mCurrentFolder = mRootFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500741 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700742 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500743 }
744
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500745 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700746 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500747 String name = getString(R.string.bookmarks);
748 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
749 new Folder(name, mRootFolder));
750 // To better match the other folders.
751 mTopLevelLabel.setCompoundDrawablePadding(6);
752 }
753
Leon Scroggins504433a2011-01-13 12:26:52 -0500754 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400755 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500756 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500757 // Since we're not in the root folder, change the selection to other
758 // folder now. The text will get changed once we select the correct
759 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500760 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500761 } else {
762 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500763 if (!mEditingFolder) {
764 // Initially the "Bookmarks" folder should be showing, rather than
765 // the home screen. In the editing folder case, home screen is not
766 // an option, so "Bookmarks" folder is already at the top.
767 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
768 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400769 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400770 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700771 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500772 }
773
Leon Scroggins02065b02010-01-04 14:30:13 -0500774 /**
775 * Runnable to save a bookmark, so it can be performed in its own thread.
776 */
777 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400778 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500779 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200780 private Context mContext;
781 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100782 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500783 mMessage = msg;
784 }
785 public void run() {
786 // Unbundle bookmark data.
787 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400788 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
789 String url = bundle.getString(BrowserContract.Bookmarks.URL);
790 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500791 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400792 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
793 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500794
795 // Save to the bookmarks DB.
796 try {
797 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400798 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700799 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500800 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200801 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500802 }
803 mMessage.arg1 = 1;
804 } catch (IllegalStateException e) {
805 mMessage.arg1 = 0;
806 }
807 mMessage.sendToTarget();
808 }
809 }
810
John Reckc8490812010-11-22 14:15:36 -0800811 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
812 Context mContext;
813 Long mId;
814
815 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100816 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800817 mId = id;
818 }
819
820 @Override
821 protected Void doInBackground(ContentValues... params) {
822 if (params.length != 1) {
823 throw new IllegalArgumentException("No ContentValues provided!");
824 }
825 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
826 mContext.getContentResolver().update(
827 uri,
828 params[0], null, null);
829 return null;
830 }
831 }
832
Ben Murdoch1794fe22009-09-29 18:14:30 +0100833 private void createHandler() {
834 if (mHandler == null) {
835 mHandler = new Handler() {
836 @Override
837 public void handleMessage(Message msg) {
838 switch (msg.what) {
839 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500840 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100841 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
842 Toast.LENGTH_LONG).show();
843 } else {
844 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
845 Toast.LENGTH_LONG).show();
846 }
847 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400848 case TOUCH_ICON_DOWNLOADED:
849 Bundle b = msg.getData();
850 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400851 AddBookmarkPage.this,
852 b.getString(BrowserContract.Bookmarks.URL),
853 b.getString(BrowserContract.Bookmarks.TITLE),
854 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
855 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400856 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500857 case BOOKMARK_DELETED:
858 finish();
859 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100860 }
861 }
862 };
863 }
864 }
865
luxiaoldbe4a622013-07-19 17:14:06 +0800866 static void deleteDuplicateBookmark(final Context context, final long id) {
867 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
868 context.getContentResolver().delete(uri, null, null);
869 }
870
871 private void onSaveWithConfirm() {
872 String title = mTitle.getText().toString().trim();
873 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
874 String url = unfilteredUrl.trim();
875 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
876 int duplicateCount;
877 final ContentResolver cr = getContentResolver();
878
879 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
880 BookmarksLoader.PROJECTION,
881 "( title = ? OR url = ? ) AND parent = ?",
882 new String[] {
883 title, url, Long.toString(mCurrentFolder)
884 },
885 null);
886
887 if (cursor == null) {
888 save();
889 return;
890 }
891
892 duplicateCount = cursor.getCount();
893 if (duplicateCount <= 0) {
894 cursor.close();
895 save();
896 return;
897 } else {
898 try {
899 while (cursor.moveToNext()) {
900 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
901 mDuplicateContext = AddBookmarkPage.this;
902 }
903 } catch (IllegalStateException e) {
904 e.printStackTrace();
905 } finally {
906 if (cursor != null)
907 cursor.close();
908 }
909 }
910
911 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
912 save();
913 return;
914 }
915
916 new AlertDialog.Builder(this)
917 .setTitle(getString(R.string.save_to_bookmarks_title))
918 .setMessage(getString(R.string.overwrite_bookmark_msg))
919 .setNegativeButton(android.R.string.cancel, null)
920 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
921 public void onClick(DialogInterface dialog, int which) {
922 if (mDuplicateContext == null) {
923 return;
924 }
925 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
926 save();
927 }
928 })
929 .show();
930 }
931
The Android Open Source Project0c908882009-03-03 19:32:16 -0800932 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100933 * 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 -0800934 */
935 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100936 createHandler();
937
The Android Open Source Project0c908882009-03-03 19:32:16 -0800938 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +0800939 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100940
The Android Open Source Project0c908882009-03-03 19:32:16 -0800941 boolean emptyTitle = title.length() == 0;
942 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
943 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -0800944 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800945 if (emptyTitle) {
946 mTitle.setError(r.getText(R.string.bookmark_needs_title));
947 }
948 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400949 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800950 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400951 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800952 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000953 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -0800954 if (!mEditingFolder) {
955 try {
956 // We allow bookmarks with a javascript: scheme, but these will in most cases
957 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +0000958
John Reckc8490812010-11-22 14:15:36 -0800959 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -0700960 String encodedUrl = URLEncoder.encode(url, "UTF-8");
961 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -0800962 String scheme = uriObj.getScheme();
963 if (!Bookmarks.urlHasAcceptableScheme(url)) {
964 // If the scheme was non-null, let the user know that we
965 // can't save their bookmark. If it was null, we'll assume
966 // they meant http when we parse it in the WebAddress class.
967 if (scheme != null) {
968 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
969 return false;
970 }
971 WebAddress address;
972 try {
973 address = new WebAddress(unfilteredUrl);
974 } catch (ParseException e) {
975 throw new URISyntaxException("", "");
976 }
977 if (address.getHost().length() == 0) {
978 throw new URISyntaxException("", "");
979 }
980 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +0000981 }
Ben Murdochde353622009-10-12 10:29:00 +0100982 }
John Reckc8490812010-11-22 14:15:36 -0800983 } catch (URISyntaxException e) {
984 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
985 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -0700986 } catch (UnsupportedEncodingException e) {
987 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
988 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800989 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800990 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100991
Leon Scroggins88d08032010-10-21 15:17:10 -0400992 if (mSaveToHomeScreen) {
993 mEditingExisting = false;
994 }
995
996 boolean urlUnmodified = url.equals(mOriginalUrl);
997
Ben Murdoch1794fe22009-09-29 18:14:30 +0100998 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -0800999 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1000 ContentValues values = new ContentValues();
1001 values.put(BrowserContract.Bookmarks.TITLE, title);
1002 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1003 if (!mEditingFolder) {
1004 values.put(BrowserContract.Bookmarks.URL, url);
1005 if (!urlUnmodified) {
1006 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1007 }
1008 }
1009 if (values.size() > 0) {
1010 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1011 }
1012 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001013 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001014 Bitmap thumbnail;
1015 Bitmap favicon;
1016 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001017 thumbnail = (Bitmap) mMap.getParcelable(
1018 BrowserContract.Bookmarks.THUMBNAIL);
1019 favicon = (Bitmap) mMap.getParcelable(
1020 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001021 } else {
1022 thumbnail = null;
1023 favicon = null;
1024 }
1025
Ben Murdoch1794fe22009-09-29 18:14:30 +01001026 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001027 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1028 bundle.putString(BrowserContract.Bookmarks.URL, url);
1029 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001030
1031 if (mSaveToHomeScreen) {
1032 if (mTouchIconUrl != null && urlUnmodified) {
1033 Message msg = Message.obtain(mHandler,
1034 TOUCH_ICON_DOWNLOADED);
1035 msg.setData(bundle);
1036 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001037 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001038 icon.execute(mTouchIconUrl);
1039 } else {
1040 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1041 title, null /*touchIcon*/, favicon));
1042 }
1043 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001044 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1045 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
1046 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001047 // Post a message to write to the DB.
1048 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1049 msg.setData(bundle);
1050 // Start a new thread so as to not slow down the UI
1051 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1052 t.start();
1053 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001054 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001055 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001056 }
luxiaoldbe4a622013-07-19 17:14:06 +08001057 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001058 return true;
1059 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001060
John Reck2eec4c32011-05-11 15:55:32 -07001061 @Override
1062 public void onItemSelected(AdapterView<?> parent, View view, int position,
1063 long id) {
1064 if (mAccountSpinner == parent) {
1065 long root = mAccountAdapter.getItem(position).rootFolderId;
1066 if (root != mRootFolder) {
1067 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001068 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001069 }
1070 }
1071 }
1072
1073 @Override
1074 public void onNothingSelected(AdapterView<?> parent) {
1075 // Don't care
1076 }
1077
Leon Scroggins162f8352010-10-18 15:02:44 -04001078 /*
1079 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1080 */
1081 public static class CustomListView extends ListView {
1082 private EditText mEditText;
1083
1084 public void addEditText(EditText editText) {
1085 mEditText = editText;
1086 }
1087
1088 public CustomListView(Context context) {
1089 super(context);
1090 }
1091
1092 public CustomListView(Context context, AttributeSet attrs) {
1093 super(context, attrs);
1094 }
1095
1096 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1097 super(context, attrs, defStyle);
1098 }
1099
1100 @Override
1101 public boolean checkInputConnectionProxy(View view) {
1102 return view == mEditText;
1103 }
1104 }
John Reck2eec4c32011-05-11 15:55:32 -07001105
John Reck3bf3cfb2011-10-17 18:00:05 -07001106 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001107
1108 static final String[] PROJECTION = new String[] {
1109 Accounts.ACCOUNT_NAME,
1110 Accounts.ACCOUNT_TYPE,
1111 Accounts.ROOT_ID,
1112 };
1113
1114 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1115 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1116 static final int COLUMN_INDEX_ROOT_ID = 2;
1117
1118 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001119 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001120 }
1121
1122 }
1123
John Reck9b8cd1e2011-05-25 18:14:01 -07001124 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001125
1126 private String mLabel;
1127 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001128 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001129
1130 public BookmarkAccount(Context context, Cursor cursor) {
1131 accountName = cursor.getString(
1132 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1133 accountType = cursor.getString(
1134 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1135 rootFolderId = cursor.getLong(
1136 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1137 mLabel = accountName;
1138 if (TextUtils.isEmpty(mLabel)) {
1139 mLabel = context.getString(R.string.local_bookmarks);
1140 }
1141 }
1142
1143 @Override
1144 public String toString() {
1145 return mLabel;
1146 }
1147 }
1148
1149 static class EditBookmarkInfo {
1150 long id = -1;
1151 long parentId = -1;
1152 String parentTitle;
1153 String title;
1154 String accountName;
1155 String accountType;
1156
1157 long lastUsedId = -1;
1158 String lastUsedTitle;
1159 String lastUsedAccountName;
1160 String lastUsedAccountType;
1161 }
1162
1163 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1164
1165 private Context mContext;
1166 private Bundle mMap;
1167
1168 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1169 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001170 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001171 mMap = bundle;
1172 }
1173
1174 @Override
1175 public EditBookmarkInfo loadInBackground() {
1176 final ContentResolver cr = mContext.getContentResolver();
1177 EditBookmarkInfo info = new EditBookmarkInfo();
1178 Cursor c = null;
1179
1180 try {
1181 // First, let's lookup the bookmark (check for dupes, get needed info)
1182 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1183 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1184 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1185 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1186 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1187 new String[] { BrowserContract.Bookmarks._ID},
1188 BrowserContract.Bookmarks.URL + "=?",
1189 new String[] { url }, null);
1190 if (c.getCount() == 1 && c.moveToFirst()) {
1191 info.id = c.getLong(0);
1192 }
1193 c.close();
1194 }
1195 if (info.id != -1) {
1196 c = cr.query(ContentUris.withAppendedId(
1197 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1198 new String[] {
1199 BrowserContract.Bookmarks.PARENT,
1200 BrowserContract.Bookmarks.ACCOUNT_NAME,
1201 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1202 BrowserContract.Bookmarks.TITLE},
1203 null, null, null);
1204 if (c.moveToFirst()) {
1205 info.parentId = c.getLong(0);
1206 info.accountName = c.getString(1);
1207 info.accountType = c.getString(2);
1208 info.title = c.getString(3);
1209 }
1210 c.close();
1211 c = cr.query(ContentUris.withAppendedId(
1212 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1213 new String[] {
1214 BrowserContract.Bookmarks.TITLE,},
1215 null, null, null);
1216 if (c.moveToFirst()) {
1217 info.parentTitle = c.getString(0);
1218 }
1219 c.close();
1220 }
1221
1222 // Figure out the last used folder/account
1223 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1224 new String[] {
1225 BrowserContract.Bookmarks.PARENT,
1226 }, null, null,
1227 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1228 if (c.moveToFirst()) {
1229 long parent = c.getLong(0);
1230 c.close();
1231 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1232 new String[] {
1233 BrowserContract.Bookmarks.TITLE,
1234 BrowserContract.Bookmarks.ACCOUNT_NAME,
1235 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1236 BrowserContract.Bookmarks._ID + "=?", new String[] {
1237 Long.toString(parent)}, null);
1238 if (c.moveToFirst()) {
1239 info.lastUsedId = parent;
1240 info.lastUsedTitle = c.getString(0);
1241 info.lastUsedAccountName = c.getString(1);
1242 info.lastUsedAccountType = c.getString(2);
1243 }
1244 c.close();
1245 }
1246 } finally {
1247 if (c != null) {
1248 c.close();
1249 }
1250 }
1251
1252 return info;
1253 }
1254
1255 @Override
1256 protected void onStartLoading() {
1257 forceLoad();
1258 }
1259
1260 }
1261
The Android Open Source Project0c908882009-03-03 19:32:16 -08001262}