blob: a73625d1b0be8cf3893a6c6b859dfaf804856d5f [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;
Pankaj Garg634bf432015-07-13 09:54:21 -0700106 private View mDeleteButton;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -0800108 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400110 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +0100111 private String mOriginalUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500112 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400113 private View mDefaultView;
114 private View mFolderSelector;
115 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500116 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400117 private boolean mIsFolderNamerShowing;
118 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400120 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500121 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400122 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400123 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400124 private TextView mFakeTitle;
125 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400126 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400127 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400128 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500129 private TextView mTopLevelLabel;
130 private Drawable mHeaderIcon;
Leon Scroggins75630672011-01-13 17:56:15 -0500131 private View mRemoveLink;
132 private View mFakeTitleHolder;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500133 private FolderSpinnerAdapter mFolderAdapter;
John Reck2eec4c32011-05-11 15:55:32 -0700134 private Spinner mAccountSpinner;
135 private ArrayAdapter<BookmarkAccount> mAccountAdapter;
luxiaoldbe4a622013-07-19 17:14:06 +0800136 // add for carrier which requires same title or address can not exist.
137 private long mDuplicateId;
138 private Context mDuplicateContext;
John Reck2eec4c32011-05-11 15:55:32 -0700139
Leon Scroggins III052ce662010-09-13 14:44:16 -0400140 private static class Folder {
141 String Name;
142 long Id;
143 Folder(String name, long id) {
144 Name = name;
145 Id = id;
146 }
147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148
Ben Murdoch1794fe22009-09-29 18:14:30 +0100149 // Message IDs
150 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400151 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500152 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100153
154 private Handler mHandler;
155
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100156 private InputMethodManager getInputMethodManager() {
157 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
158 }
159
Leon Scroggins8baaa632010-12-08 19:46:53 -0500160 private Uri getUriForFolder(long folder) {
John Reck903a0722011-11-09 17:53:10 -0800161 BookmarkAccount account =
162 (BookmarkAccount) mAccountSpinner.getSelectedItem();
163 if (folder == mRootFolder && account != null) {
164 return BookmarksLoader.addAccount(
165 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
166 account.accountType, account.accountName);
167 }
John Reck2eec4c32011-05-11 15:55:32 -0700168 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500169 }
170
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800171 private String getNameFromId(long mCurrentFolder2) {
172 String title = "";
173 Cursor cursor = null;
174 try {
175 cursor = getApplicationContext().getContentResolver().query(
176 BrowserContract.Bookmarks.CONTENT_URI,
177 new String[] {
178 BrowserContract.Bookmarks.TITLE
179 },
180 BrowserContract.Bookmarks._ID + " = ? AND "
181 + BrowserContract.Bookmarks.IS_DELETED + " = ? AND "
182 + BrowserContract.Bookmarks.IS_FOLDER + " = ? ", new String[] {
183 String.valueOf(mCurrentFolder2), 0 + "", 1 + ""
184 }, null);
185 if (cursor != null && cursor.getCount() != 0) {
186 while (cursor.moveToNext()) {
187 title = cursor.getString(0);
188 }
189 }
190 } finally {
191 if (cursor != null) {
192 cursor.close();
193 }
194 }
195 return title;
196 }
197
Leon Scroggins III052ce662010-09-13 14:44:16 -0400198 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700199 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400200 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400201 Folder folderData = (Folder) data;
202 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400203 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700204 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400205 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500206 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400207 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400208 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400209 completeOrCancelFolderNaming(true);
210 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500211 setShowBookmarkIcon(level == 1);
212 }
213
214 /**
215 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
216 * @param show True if the icon should visible, false otherwise.
217 */
218 private void setShowBookmarkIcon(boolean show) {
219 Drawable drawable = show ? mHeaderIcon: null;
220 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400221 }
222
Leon Scroggins74dbe012010-10-15 10:54:27 -0400223 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400224 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
225 if (v == mFolderNamer) {
226 if (v.getText().length() > 0) {
227 if (actionId == EditorInfo.IME_NULL) {
228 // Only want to do this once.
229 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400230 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400231 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400232 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 }
Michael Kolb31829b92010-10-01 11:50:21 -0700234 // Steal the key press; otherwise a newline will be added
Vivek Sekhar5e631472014-03-31 23:59:10 -0700235 // return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400237 return false;
238 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800239
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400240 private void switchToDefaultView(boolean changedFolder) {
241 mFolderSelector.setVisibility(View.GONE);
242 mDefaultView.setVisibility(View.VISIBLE);
243 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500244 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400245 if (changedFolder) {
246 Object data = mCrumbs.getTopData();
247 if (data != null) {
248 Folder folder = (Folder) data;
249 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500250 if (mCurrentFolder == mRootFolder) {
251 // The Spinner changed to show "Other folder ..." Change
252 // it back to "Bookmarks", which is position 0 if we are
253 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500254 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500255 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700256 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500257 }
258 }
259 } else {
260 // The user canceled selecting a folder. Revert back to the earlier
261 // selection.
262 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500263 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500264 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500265 if (mCurrentFolder == mRootFolder) {
266 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
267 } else {
268 Object data = mCrumbs.getTopData();
269 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700270 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500271 // folder selector will say "Other folder..." Change it
272 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700273 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500274 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700275 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500276 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500277 setupTopCrumb();
278 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500279 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
280
281 }
282 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400283 }
284 }
285 }
286
Leon Scroggins III052ce662010-09-13 14:44:16 -0400287 @Override
288 public void onClick(View v) {
289 if (v == mButton) {
290 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400291 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400292 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400293 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700294 } else {
295 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400296 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400297 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700298 }
luxiaoldbe4a622013-07-19 17:14:06 +0800299 } else {
300 // add for carrier which requires same title or address can not
301 // exist.
302 if (mSaveToHomeScreen) {
303 if (save()) {
304 return;
305 }
306 } else {
307 onSaveWithConfirm();
308 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400309 }
310 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400311 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400312 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400313 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
314 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700315 } else {
316 finish();
317 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700318 } else if (v == mDeleteButton || v == mRemoveLink) {
319 onDeleteWithConfirm();
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500320 } else if (v == mFolderCancel) {
321 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400322 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400323 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400324 mFolderNamer.setText(R.string.new_folder);
325 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700326 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400327 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400328 InputMethodManager imm = getInputMethodManager();
329 // Set the InputMethodManager to focus on the ListView so that it
330 // can transfer the focus to mFolderNamer.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800331 //imm.focusIn(mListView);
332 Object[] params = {mListView};
333 Class[] type = new Class[] {View.class};
334 ReflectHelper.invokeMethod(imm, "focusIn", type, params);
Leon Scroggins162f8352010-10-18 15:02:44 -0400335 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400337 }
338
Leon Scroggins504433a2011-01-13 12:26:52 -0500339 // FolderSpinner.OnSetSelectionListener
340
Leon Scroggins88d08032010-10-21 15:17:10 -0400341 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500342 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500343 int intId = (int) id;
344 switch (intId) {
345 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400346 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400347 mSaveToHomeScreen = false;
348 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500349 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400350 // Create a short cut to the home screen
351 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400352 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500353 case FolderSpinnerAdapter.OTHER_FOLDER:
Leon Scroggins88d08032010-10-21 15:17:10 -0400354 switchToFolderSelector();
355 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500356 case FolderSpinnerAdapter.RECENT_FOLDER:
357 mCurrentFolder = mFolderAdapter.recentFolderId();
358 mSaveToHomeScreen = false;
359 // In case the user decides to select OTHER_FOLDER
360 // and choose a different one, so that we will start from
361 // the correct place.
362 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500363 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500364 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400365 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500366 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400367 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500368 }
369
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500370 /**
371 * Finish naming a folder, and close the IME
372 * @param cancel If true, the new folder is not created. If false, the new
373 * folder is created and the user is taken inside it.
374 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400375 private void completeOrCancelFolderNaming(boolean cancel) {
376 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700377 String name = mFolderNamer.getText().toString();
378 long id = addFolderToCurrent(mFolderNamer.getText().toString());
379 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700380 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400381 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400382 mAddNewFolder.setVisibility(View.VISIBLE);
383 mAddSeparator.setVisibility(View.VISIBLE);
384 getInputMethodManager().hideSoftInputFromWindow(
Leon Scroggins162f8352010-10-18 15:02:44 -0400385 mListView.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700386 }
387
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700388 private long addFolderToCurrent(String name) {
389 // Add the folder to the database
390 ContentValues values = new ContentValues();
391 values.put(BrowserContract.Bookmarks.TITLE,
392 name);
393 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400394 long currentFolder;
395 Object data = mCrumbs.getTopData();
396 if (data != null) {
397 currentFolder = ((Folder) data).Id;
398 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400399 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400400 }
401 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700402 Uri uri = getContentResolver().insert(
403 BrowserContract.Bookmarks.CONTENT_URI, values);
404 if (uri != null) {
405 return ContentUris.parseId(uri);
406 } else {
407 return -1;
408 }
409 }
410
Leon Scroggins III052ce662010-09-13 14:44:16 -0400411 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500412 // Set the list to the top in case it is scrolled.
413 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400414 mDefaultView.setVisibility(View.GONE);
415 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400416 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500417 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400418 mAddNewFolder.setVisibility(View.VISIBLE);
419 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700420 getInputMethodManager().hideSoftInputFromWindow(
421 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400422 }
423
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700424 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700425 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400426 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400427 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700428 }
429 }
430
John Reck2eec4c32011-05-11 15:55:32 -0700431 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
432 new LoaderCallbacks<EditBookmarkInfo>() {
433
434 @Override
435 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
436 // Don't care
437 }
438
439 @Override
440 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
441 EditBookmarkInfo info) {
442 boolean setAccount = false;
443 if (info.id != -1) {
444 mEditingExisting = true;
445 showRemoveButton();
446 mFakeTitle.setText(R.string.edit_bookmark);
447 mTitle.setText(info.title);
448 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
449 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
450 setAccount = true;
451 setAccount(info.accountName, info.accountType);
452 mCurrentFolder = info.parentId;
453 onCurrentFolderFound();
454 }
John Reck37894a92011-05-13 12:47:53 -0700455 // TODO: Detect if lastUsedId is a subfolder of info.id in the
456 // editing folder case. For now, just don't show the last used
457 // folder at all to prevent any chance of the user adding a parent
458 // folder to a child folder
459 if (info.lastUsedId != -1 && info.lastUsedId != info.id
460 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700461 if (setAccount && info.lastUsedId != mRootFolder
462 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
463 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
464 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
465 } else if (!setAccount) {
466 setAccount = true;
467 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
468 if (info.lastUsedId != mRootFolder) {
469 mFolderAdapter.addRecentFolder(info.lastUsedId,
470 info.lastUsedTitle);
471 }
472 }
473 }
474 if (!setAccount) {
475 mAccountSpinner.setSelection(0);
476 }
477 }
478
479 @Override
480 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
481 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
482 }
483 };
484
485 void setAccount(String accountName, String accountType) {
486 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
487 BookmarkAccount account = mAccountAdapter.getItem(i);
488 if (TextUtils.equals(account.accountName, accountName)
489 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700490 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800491 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700492 return;
493 }
494 }
495 }
496
Leon Scroggins III052ce662010-09-13 14:44:16 -0400497 @Override
498 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
499 String[] projection;
500 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700501 case LOADER_ID_ACCOUNTS:
502 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400503 case LOADER_ID_FOLDER_CONTENTS:
504 projection = new String[] {
505 BrowserContract.Bookmarks._ID,
506 BrowserContract.Bookmarks.TITLE,
507 BrowserContract.Bookmarks.IS_FOLDER
508 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500509 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700510 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500511 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700512 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
513 whereArgs = new String[] { Long.toString(mMap.getLong(
514 BrowserContract.Bookmarks._ID)) };
515 }
516 long currentFolder;
517 Object data = mCrumbs.getTopData();
518 if (data != null) {
519 currentFolder = ((Folder) data).Id;
520 } else {
521 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500522 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400523 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700524 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400525 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500526 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700527 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500528 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400529 default:
530 throw new AssertionError("Asking for nonexistant loader!");
531 }
532 }
533
534 @Override
535 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
536 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700537 case LOADER_ID_ACCOUNTS:
538 mAccountAdapter.clear();
539 while (cursor.moveToNext()) {
540 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500541 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700542
543 if (cursor.getCount() < 2) {
544 View accountView = findViewById(R.id.row_account);
545 if (accountView != null) {
546 accountView.setVisibility(View.GONE);
547 }
548 }
549
John Reck2eec4c32011-05-11 15:55:32 -0700550 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
551 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
552 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500553 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400554 case LOADER_ID_FOLDER_CONTENTS:
555 mAdapter.changeCursor(cursor);
556 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400557 }
558 }
559
Dianne Hackborn39772c82010-12-16 00:43:54 -0800560 public void onLoaderReset(Loader<Cursor> loader) {
561 switch (loader.getId()) {
562 case LOADER_ID_FOLDER_CONTENTS:
563 mAdapter.changeCursor(null);
564 break;
565 }
566 }
567
Leon Scroggins02081942010-11-01 17:52:42 -0400568 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500569 * Move cursor to the position that has folderToFind as its "_id".
570 * @param cursor Cursor containing folders in the bookmarks database
571 * @param folderToFind "_id" of the folder to move to.
572 * @param idIndex Index in cursor of "_id"
573 * @throws AssertionError if cursor is empty or there is no row with folderToFind
574 * as its "_id".
575 */
576 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
577 throws AssertionError {
578 if (!cursor.moveToFirst()) {
579 throw new AssertionError("No folders in the database!");
580 }
581 long folder;
582 do {
583 folder = cursor.getLong(idIndex);
584 } while (folder != folderToFind && cursor.moveToNext());
585 if (cursor.isAfterLast()) {
586 throw new AssertionError("Folder(id=" + folderToFind
587 + ") holding this bookmark does not exist!");
588 }
589 }
590
Leon Scroggins III052ce662010-09-13 14:44:16 -0400591 @Override
592 public void onItemClick(AdapterView<?> parent, View view, int position,
593 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400594 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400596 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400597 }
598
Leon Scroggins162f8352010-10-18 15:02:44 -0400599 private void setShowFolderNamer(boolean show) {
600 if (show != mIsFolderNamerShowing) {
601 mIsFolderNamerShowing = show;
602 if (show) {
603 // Set the selection to the folder namer so it will be in
604 // view.
605 mListView.addFooterView(mFolderNamerHolder);
606 } else {
607 mListView.removeFooterView(mFolderNamerHolder);
608 }
609 // Refresh the list.
610 mListView.setAdapter(mAdapter);
611 if (show) {
612 mListView.setSelection(mListView.getCount() - 1);
613 }
614 }
615 }
616
Leon Scroggins III052ce662010-09-13 14:44:16 -0400617 /**
618 * Shows a list of names of folders.
619 */
620 private class FolderAdapter extends CursorAdapter {
621 public FolderAdapter(Context context) {
622 super(context, null);
623 }
624
625 @Override
626 public void bindView(View view, Context context, Cursor cursor) {
627 ((TextView) view.findViewById(android.R.id.text1)).setText(
628 cursor.getString(cursor.getColumnIndexOrThrow(
629 BrowserContract.Bookmarks.TITLE)));
630 }
631
632 @Override
633 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700634 View view = LayoutInflater.from(context).inflate(
635 R.layout.folder_list_item, null);
636 view.setBackgroundDrawable(context.getResources().
637 getDrawable(android.R.drawable.list_selector_background));
638 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400639 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400640
641 @Override
642 public boolean isEmpty() {
643 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400644 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400645 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400646 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647
Leon Scrogginsc1129902010-12-08 15:28:33 -0500648 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800649 protected void onCreate(Bundle icicle) {
650 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500651 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100652
653 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100654
Leon Scroggins III052ce662010-09-13 14:44:16 -0400655 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100656
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400657 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700658
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 String title = null;
660 String url = null;
Pankaj Garg21dad562015-07-02 17:17:24 -0700661 mTouchIconUrl = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100662
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400663 mFakeTitle = (TextView) findViewById(R.id.fake_title);
Pankaj Garg634bf432015-07-13 09:54:21 -0700664
665 mDeleteButton = findViewById(R.id.delete);
666 mDeleteButton.setOnClickListener(this);
667
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800669 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800671 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 mMap = b;
673 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400674 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800675 if (mEditingFolder) {
676 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500677 } else {
678 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800679 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400680 } else {
681 int gravity = mMap.getInt("gravity", -1);
682 if (gravity != -1) {
683 WindowManager.LayoutParams l = window.getAttributes();
684 l.gravity = gravity;
685 window.setAttributes(l);
686 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800687 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400688 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
689 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
690 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700691 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800692
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800693 // Check if title is not empty to prevent NPE
694 if (!TextUtils.isEmpty(title)) {
695 if (title.length() > MAX_TITLE_LENGTH) {
696 title = title.substring(0, MAX_TITLE_LENGTH);
697 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800698 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700699
Leon Scroggins25230d72010-09-28 20:09:25 -0400700 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800701
702 mTitle = (EditText) findViewById(R.id.title);
703 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700704 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100705
Leon Scroggins III052ce662010-09-13 14:44:16 -0400706 mAddress = (EditText) findViewById(R.id.address);
707 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800708 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709
The Android Open Source Project0c908882009-03-03 19:32:16 -0800710 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400711 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800712
713 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400714 mCancelButton.setOnClickListener(this);
715
Leon Scroggins504433a2011-01-13 12:26:52 -0500716 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700717 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500718 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500719 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400720
721 mDefaultView = findViewById(R.id.default_view);
722 mFolderSelector = findViewById(R.id.folder_selector);
723
Leon Scroggins162f8352010-10-18 15:02:44 -0400724 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
725 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400726 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800727
728 // add for carrier test about warning limit of edit text
729 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
730 BrowserUtils.FILENAME_MAX_LENGTH);
731
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500732 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
733 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400734
735 mAddNewFolder = findViewById(R.id.add_new_folder);
736 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400737 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400738
Leon Scroggins74dbe012010-10-15 10:54:27 -0400739 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
Pankaj Garg634bf432015-07-13 09:54:21 -0700740 //mCrumbs.setUseBackButton(true);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400741 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800742 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400743 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800744 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400745
Leon Scroggins III052ce662010-09-13 14:44:16 -0400746 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400747 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400748 View empty = findViewById(R.id.empty);
749 mListView.setEmptyView(empty);
750 mListView.setAdapter(mAdapter);
751 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400752 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500753
John Reck2eec4c32011-05-11 15:55:32 -0700754 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
755 android.R.layout.simple_spinner_item);
756 mAccountAdapter.setDropDownViewResource(
757 android.R.layout.simple_spinner_dropdown_item);
758 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
759 mAccountSpinner.setAdapter(mAccountAdapter);
760 mAccountSpinner.setOnItemSelectedListener(this);
Sagar Dhawan7e9d2472015-07-20 10:39:40 -0700761 mFolder.setSelectionIgnoringSelectionChange(1); // Select Bookmarks by default
John Reck2eec4c32011-05-11 15:55:32 -0700762
Leon Scroggins75630672011-01-13 17:56:15 -0500763 mFakeTitleHolder = findViewById(R.id.title_holder);
764
Leon Scroggins504433a2011-01-13 12:26:52 -0500765 if (!window.getDecorView().isInTouchMode()) {
766 mButton.requestFocus();
767 }
768
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800769 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
770 mFolder.setSelectionIgnoringSelectionChange(2);
771 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
772 }
773
John Reck2eec4c32011-05-11 15:55:32 -0700774 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500775 }
776
Leon Scroggins75630672011-01-13 17:56:15 -0500777 private void showRemoveButton() {
Pankaj Garg634bf432015-07-13 09:54:21 -0700778 mDeleteButton.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500779 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
780 mRemoveLink = findViewById(R.id.remove);
781 mRemoveLink.setVisibility(View.VISIBLE);
782 mRemoveLink.setOnClickListener(this);
783 }
784
Leon Scroggins504433a2011-01-13 12:26:52 -0500785 // Called once we have determined which folder is the root folder
786 private void onRootFolderFound(long root) {
787 mRootFolder = root;
Pankaj Garg63396942015-03-06 10:05:25 -0800788 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500789 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700790 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500791 }
792
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500793 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700794 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500795 String name = getString(R.string.bookmarks);
796 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
797 new Folder(name, mRootFolder));
798 // To better match the other folders.
799 mTopLevelLabel.setCompoundDrawablePadding(6);
800 }
801
Leon Scroggins504433a2011-01-13 12:26:52 -0500802 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400803 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500804 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500805 // Since we're not in the root folder, change the selection to other
806 // folder now. The text will get changed once we select the correct
807 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500808 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500809 } else {
810 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500811 if (!mEditingFolder) {
812 // Initially the "Bookmarks" folder should be showing, rather than
813 // the home screen. In the editing folder case, home screen is not
814 // an option, so "Bookmarks" folder is already at the top.
815 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
816 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400817 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400818 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700819 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500820 }
821
Leon Scroggins02065b02010-01-04 14:30:13 -0500822 /**
823 * Runnable to save a bookmark, so it can be performed in its own thread.
824 */
825 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400826 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500827 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200828 private Context mContext;
829 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100830 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500831 mMessage = msg;
832 }
833 public void run() {
834 // Unbundle bookmark data.
835 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400836 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
837 String url = bundle.getString(BrowserContract.Bookmarks.URL);
838 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500839 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700840 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400841 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500842
843 // Save to the bookmarks DB.
844 try {
845 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400846 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700847 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500848 if (touchIconUrl != null) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700849 new DownloadTouchIcon(mContext, cr, url).execute(touchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500850 }
851 mMessage.arg1 = 1;
852 } catch (IllegalStateException e) {
853 mMessage.arg1 = 0;
854 }
855 mMessage.sendToTarget();
856 }
857 }
858
John Reckc8490812010-11-22 14:15:36 -0800859 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
860 Context mContext;
861 Long mId;
862
863 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100864 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800865 mId = id;
866 }
867
868 @Override
869 protected Void doInBackground(ContentValues... params) {
870 if (params.length != 1) {
871 throw new IllegalArgumentException("No ContentValues provided!");
872 }
873 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
874 mContext.getContentResolver().update(
875 uri,
876 params[0], null, null);
877 return null;
878 }
879 }
880
Ben Murdoch1794fe22009-09-29 18:14:30 +0100881 private void createHandler() {
882 if (mHandler == null) {
883 mHandler = new Handler() {
884 @Override
885 public void handleMessage(Message msg) {
886 switch (msg.what) {
887 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500888 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100889 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
890 Toast.LENGTH_LONG).show();
891 } else {
892 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
893 Toast.LENGTH_LONG).show();
894 }
895 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400896 case TOUCH_ICON_DOWNLOADED:
897 Bundle b = msg.getData();
898 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400899 AddBookmarkPage.this,
900 b.getString(BrowserContract.Bookmarks.URL),
901 b.getString(BrowserContract.Bookmarks.TITLE),
902 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
903 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400904 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500905 case BOOKMARK_DELETED:
906 finish();
907 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100908 }
909 }
910 };
911 }
912 }
913
luxiaoldbe4a622013-07-19 17:14:06 +0800914 static void deleteDuplicateBookmark(final Context context, final long id) {
915 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
916 context.getContentResolver().delete(uri, null, null);
917 }
918
Pankaj Garg634bf432015-07-13 09:54:21 -0700919 private void onDeleteWithConfirm() {
920 final String title = mTitle.getText().toString().trim();
921 final String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
922 final String url = unfilteredUrl.trim();
923 new AlertDialog.Builder(this)
924 .setIconAttribute(android.R.attr.alertDialogIcon)
925 .setMessage(getString(R.string.delete_bookmark_warning, title))
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 ContentResolver cr = getContentResolver();
930 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
931 BookmarksLoader.PROJECTION,
932 "title = ? OR url = ?",
933 new String[] {
934 title, url
935 },
936 null);
937
938 if (cursor == null) {
939 finish();
940 return;
941 }
942
943 try {
944 if (cursor.moveToFirst()) {
945 do {
946 long index = cursor.getLong(
947 cursor.getColumnIndex(BrowserContract.Bookmarks._ID));
948 cr.delete(ContentUris.withAppendedId(
949 BrowserContract.Bookmarks.CONTENT_URI, index),
950 null, null);
951 } while (cursor.moveToNext());
952 }
953 } catch (IllegalStateException e) {
954 e.printStackTrace();
955 } finally {
956 if (cursor != null)
957 cursor.close();
958 }
959 finish();
960 }
961 })
962 .show();
963 }
964
luxiaoldbe4a622013-07-19 17:14:06 +0800965 private void onSaveWithConfirm() {
966 String title = mTitle.getText().toString().trim();
967 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
968 String url = unfilteredUrl.trim();
969 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
970 int duplicateCount;
971 final ContentResolver cr = getContentResolver();
972
973 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
974 BookmarksLoader.PROJECTION,
975 "( title = ? OR url = ? ) AND parent = ?",
976 new String[] {
977 title, url, Long.toString(mCurrentFolder)
978 },
979 null);
980
981 if (cursor == null) {
982 save();
983 return;
984 }
985
986 duplicateCount = cursor.getCount();
987 if (duplicateCount <= 0) {
988 cursor.close();
989 save();
990 return;
991 } else {
992 try {
993 while (cursor.moveToNext()) {
994 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
995 mDuplicateContext = AddBookmarkPage.this;
996 }
997 } catch (IllegalStateException e) {
998 e.printStackTrace();
999 } finally {
1000 if (cursor != null)
1001 cursor.close();
1002 }
1003 }
1004
1005 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
1006 save();
1007 return;
1008 }
1009
1010 new AlertDialog.Builder(this)
1011 .setTitle(getString(R.string.save_to_bookmarks_title))
1012 .setMessage(getString(R.string.overwrite_bookmark_msg))
1013 .setNegativeButton(android.R.string.cancel, null)
1014 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1015 public void onClick(DialogInterface dialog, int which) {
1016 if (mDuplicateContext == null) {
1017 return;
1018 }
1019 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
1020 save();
1021 }
1022 })
1023 .show();
1024 }
1025
The Android Open Source Project0c908882009-03-03 19:32:16 -08001026 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +01001027 * 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 -08001028 */
1029 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +01001030 createHandler();
1031
The Android Open Source Project0c908882009-03-03 19:32:16 -08001032 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +08001033 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +01001034
The Android Open Source Project0c908882009-03-03 19:32:16 -08001035 boolean emptyTitle = title.length() == 0;
1036 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
1037 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -08001038 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001039 if (emptyTitle) {
1040 mTitle.setError(r.getText(R.string.bookmark_needs_title));
1041 }
1042 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -04001043 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001044 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -04001045 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001046 }
Ben Murdochca12cfa2009-11-17 13:57:44 +00001047 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -08001048 if (!mEditingFolder) {
1049 try {
1050 // We allow bookmarks with a javascript: scheme, but these will in most cases
1051 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +00001052
John Reckc8490812010-11-22 14:15:36 -08001053 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -07001054 String encodedUrl = URLEncoder.encode(url, "UTF-8");
1055 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -08001056 String scheme = uriObj.getScheme();
1057 if (!Bookmarks.urlHasAcceptableScheme(url)) {
1058 // If the scheme was non-null, let the user know that we
1059 // can't save their bookmark. If it was null, we'll assume
1060 // they meant http when we parse it in the WebAddress class.
1061 if (scheme != null) {
1062 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
1063 return false;
1064 }
1065 WebAddress address;
1066 try {
1067 address = new WebAddress(unfilteredUrl);
1068 } catch (ParseException e) {
1069 throw new URISyntaxException("", "");
1070 }
1071 if (address.getHost().length() == 0) {
1072 throw new URISyntaxException("", "");
1073 }
1074 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001075 }
Ben Murdochde353622009-10-12 10:29:00 +01001076 }
John Reckc8490812010-11-22 14:15:36 -08001077 } catch (URISyntaxException e) {
1078 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1079 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -07001080 } catch (UnsupportedEncodingException e) {
1081 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1082 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001083 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001084 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001085
Leon Scroggins88d08032010-10-21 15:17:10 -04001086 if (mSaveToHomeScreen) {
1087 mEditingExisting = false;
1088 }
1089
1090 boolean urlUnmodified = url.equals(mOriginalUrl);
1091
Ben Murdoch1794fe22009-09-29 18:14:30 +01001092 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001093 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1094 ContentValues values = new ContentValues();
1095 values.put(BrowserContract.Bookmarks.TITLE, title);
1096 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1097 if (!mEditingFolder) {
1098 values.put(BrowserContract.Bookmarks.URL, url);
1099 if (!urlUnmodified) {
1100 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1101 }
1102 }
1103 if (values.size() > 0) {
1104 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1105 }
1106 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001107 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001108 Bitmap thumbnail;
1109 Bitmap favicon;
1110 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001111 thumbnail = (Bitmap) mMap.getParcelable(
1112 BrowserContract.Bookmarks.THUMBNAIL);
1113 favicon = (Bitmap) mMap.getParcelable(
1114 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001115 } else {
1116 thumbnail = null;
1117 favicon = null;
1118 }
1119
Ben Murdoch1794fe22009-09-29 18:14:30 +01001120 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001121 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1122 bundle.putString(BrowserContract.Bookmarks.URL, url);
1123 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001124
1125 if (mSaveToHomeScreen) {
1126 if (mTouchIconUrl != null && urlUnmodified) {
1127 Message msg = Message.obtain(mHandler,
1128 TOUCH_ICON_DOWNLOADED);
1129 msg.setData(bundle);
1130 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001131 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001132 icon.execute(mTouchIconUrl);
1133 } else {
1134 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1135 title, null /*touchIcon*/, favicon));
1136 }
1137 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001138 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1139 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Pankaj Garg21dad562015-07-02 17:17:24 -07001140 if (mTouchIconUrl != null) {
1141 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
1142 }
Leon Scroggins88d08032010-10-21 15:17:10 -04001143 // Post a message to write to the DB.
1144 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1145 msg.setData(bundle);
1146 // Start a new thread so as to not slow down the UI
1147 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1148 t.start();
1149 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001150 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001151 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001152 }
luxiaoldbe4a622013-07-19 17:14:06 +08001153 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001154 return true;
1155 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001156
John Reck2eec4c32011-05-11 15:55:32 -07001157 @Override
1158 public void onItemSelected(AdapterView<?> parent, View view, int position,
1159 long id) {
1160 if (mAccountSpinner == parent) {
1161 long root = mAccountAdapter.getItem(position).rootFolderId;
1162 if (root != mRootFolder) {
1163 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001164 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001165 }
1166 }
1167 }
1168
1169 @Override
1170 public void onNothingSelected(AdapterView<?> parent) {
1171 // Don't care
1172 }
1173
Leon Scroggins162f8352010-10-18 15:02:44 -04001174 /*
1175 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1176 */
1177 public static class CustomListView extends ListView {
1178 private EditText mEditText;
1179
1180 public void addEditText(EditText editText) {
1181 mEditText = editText;
1182 }
1183
1184 public CustomListView(Context context) {
1185 super(context);
1186 }
1187
1188 public CustomListView(Context context, AttributeSet attrs) {
1189 super(context, attrs);
1190 }
1191
1192 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1193 super(context, attrs, defStyle);
1194 }
1195
1196 @Override
1197 public boolean checkInputConnectionProxy(View view) {
1198 return view == mEditText;
1199 }
1200 }
John Reck2eec4c32011-05-11 15:55:32 -07001201
John Reck3bf3cfb2011-10-17 18:00:05 -07001202 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001203
1204 static final String[] PROJECTION = new String[] {
1205 Accounts.ACCOUNT_NAME,
1206 Accounts.ACCOUNT_TYPE,
1207 Accounts.ROOT_ID,
1208 };
1209
1210 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1211 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1212 static final int COLUMN_INDEX_ROOT_ID = 2;
1213
1214 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001215 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001216 }
1217
1218 }
1219
John Reck9b8cd1e2011-05-25 18:14:01 -07001220 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001221
1222 private String mLabel;
1223 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001224 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001225
1226 public BookmarkAccount(Context context, Cursor cursor) {
1227 accountName = cursor.getString(
1228 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1229 accountType = cursor.getString(
1230 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1231 rootFolderId = cursor.getLong(
1232 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1233 mLabel = accountName;
1234 if (TextUtils.isEmpty(mLabel)) {
1235 mLabel = context.getString(R.string.local_bookmarks);
1236 }
1237 }
1238
1239 @Override
1240 public String toString() {
1241 return mLabel;
1242 }
1243 }
1244
1245 static class EditBookmarkInfo {
1246 long id = -1;
1247 long parentId = -1;
1248 String parentTitle;
1249 String title;
1250 String accountName;
1251 String accountType;
1252
1253 long lastUsedId = -1;
1254 String lastUsedTitle;
1255 String lastUsedAccountName;
1256 String lastUsedAccountType;
1257 }
1258
1259 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1260
1261 private Context mContext;
1262 private Bundle mMap;
1263
1264 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1265 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001266 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001267 mMap = bundle;
1268 }
1269
1270 @Override
1271 public EditBookmarkInfo loadInBackground() {
1272 final ContentResolver cr = mContext.getContentResolver();
1273 EditBookmarkInfo info = new EditBookmarkInfo();
1274 Cursor c = null;
1275
1276 try {
1277 // First, let's lookup the bookmark (check for dupes, get needed info)
1278 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1279 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1280 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1281 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1282 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1283 new String[] { BrowserContract.Bookmarks._ID},
1284 BrowserContract.Bookmarks.URL + "=?",
1285 new String[] { url }, null);
1286 if (c.getCount() == 1 && c.moveToFirst()) {
1287 info.id = c.getLong(0);
1288 }
1289 c.close();
1290 }
1291 if (info.id != -1) {
1292 c = cr.query(ContentUris.withAppendedId(
1293 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1294 new String[] {
1295 BrowserContract.Bookmarks.PARENT,
1296 BrowserContract.Bookmarks.ACCOUNT_NAME,
1297 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1298 BrowserContract.Bookmarks.TITLE},
1299 null, null, null);
1300 if (c.moveToFirst()) {
1301 info.parentId = c.getLong(0);
1302 info.accountName = c.getString(1);
1303 info.accountType = c.getString(2);
1304 info.title = c.getString(3);
1305 }
1306 c.close();
1307 c = cr.query(ContentUris.withAppendedId(
1308 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1309 new String[] {
1310 BrowserContract.Bookmarks.TITLE,},
1311 null, null, null);
1312 if (c.moveToFirst()) {
1313 info.parentTitle = c.getString(0);
1314 }
1315 c.close();
1316 }
1317
1318 // Figure out the last used folder/account
1319 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1320 new String[] {
1321 BrowserContract.Bookmarks.PARENT,
1322 }, null, null,
1323 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1324 if (c.moveToFirst()) {
1325 long parent = c.getLong(0);
1326 c.close();
1327 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1328 new String[] {
1329 BrowserContract.Bookmarks.TITLE,
1330 BrowserContract.Bookmarks.ACCOUNT_NAME,
1331 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1332 BrowserContract.Bookmarks._ID + "=?", new String[] {
1333 Long.toString(parent)}, null);
1334 if (c.moveToFirst()) {
1335 info.lastUsedId = parent;
1336 info.lastUsedTitle = c.getString(0);
1337 info.lastUsedAccountName = c.getString(1);
1338 info.lastUsedAccountType = c.getString(2);
1339 }
1340 c.close();
1341 }
1342 } finally {
1343 if (c != null) {
1344 c.close();
1345 }
1346 }
1347
1348 return info;
1349 }
1350
1351 @Override
1352 protected void onStartLoading() {
1353 forceLoad();
1354 }
1355
1356 }
1357
The Android Open Source Project0c908882009-03-03 19:32:16 -08001358}