blob: 00b892ab59fae9cad5d54158ff00bfe8b5057d96 [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:
Pankaj Garg52d36492015-09-07 15:09:40 +0200354 setupTopCrumb();
355 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins88d08032010-10-21 15:17:10 -0400356 switchToFolderSelector();
357 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500358 case FolderSpinnerAdapter.RECENT_FOLDER:
359 mCurrentFolder = mFolderAdapter.recentFolderId();
360 mSaveToHomeScreen = false;
361 // In case the user decides to select OTHER_FOLDER
362 // and choose a different one, so that we will start from
363 // the correct place.
364 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500365 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500366 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400367 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500368 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400369 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500370 }
371
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500372 /**
373 * Finish naming a folder, and close the IME
374 * @param cancel If true, the new folder is not created. If false, the new
375 * folder is created and the user is taken inside it.
376 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400377 private void completeOrCancelFolderNaming(boolean cancel) {
378 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700379 String name = mFolderNamer.getText().toString();
380 long id = addFolderToCurrent(mFolderNamer.getText().toString());
381 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700382 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400383 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400384 mAddNewFolder.setVisibility(View.VISIBLE);
385 mAddSeparator.setVisibility(View.VISIBLE);
386 getInputMethodManager().hideSoftInputFromWindow(
Pankaj Garg52d36492015-09-07 15:09:40 +0200387 mFolderNamer.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700388 }
389
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700390 private long addFolderToCurrent(String name) {
391 // Add the folder to the database
392 ContentValues values = new ContentValues();
393 values.put(BrowserContract.Bookmarks.TITLE,
394 name);
395 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400396 long currentFolder;
397 Object data = mCrumbs.getTopData();
398 if (data != null) {
399 currentFolder = ((Folder) data).Id;
400 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400401 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400402 }
403 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700404 Uri uri = getContentResolver().insert(
405 BrowserContract.Bookmarks.CONTENT_URI, values);
406 if (uri != null) {
407 return ContentUris.parseId(uri);
408 } else {
409 return -1;
410 }
411 }
412
Leon Scroggins III052ce662010-09-13 14:44:16 -0400413 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500414 // Set the list to the top in case it is scrolled.
415 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400416 mDefaultView.setVisibility(View.GONE);
417 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400418 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500419 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400420 mAddNewFolder.setVisibility(View.VISIBLE);
421 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700422 getInputMethodManager().hideSoftInputFromWindow(
423 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400424 }
425
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700426 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700427 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400428 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400429 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700430 }
431 }
432
John Reck2eec4c32011-05-11 15:55:32 -0700433 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
434 new LoaderCallbacks<EditBookmarkInfo>() {
435
436 @Override
437 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
438 // Don't care
439 }
440
441 @Override
442 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
443 EditBookmarkInfo info) {
444 boolean setAccount = false;
445 if (info.id != -1) {
446 mEditingExisting = true;
447 showRemoveButton();
448 mFakeTitle.setText(R.string.edit_bookmark);
449 mTitle.setText(info.title);
450 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
451 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
452 setAccount = true;
453 setAccount(info.accountName, info.accountType);
454 mCurrentFolder = info.parentId;
455 onCurrentFolderFound();
456 }
John Reck37894a92011-05-13 12:47:53 -0700457 // TODO: Detect if lastUsedId is a subfolder of info.id in the
458 // editing folder case. For now, just don't show the last used
459 // folder at all to prevent any chance of the user adding a parent
460 // folder to a child folder
461 if (info.lastUsedId != -1 && info.lastUsedId != info.id
462 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700463 if (setAccount && info.lastUsedId != mRootFolder
464 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
465 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
466 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
467 } else if (!setAccount) {
468 setAccount = true;
469 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
470 if (info.lastUsedId != mRootFolder) {
471 mFolderAdapter.addRecentFolder(info.lastUsedId,
472 info.lastUsedTitle);
473 }
474 }
475 }
476 if (!setAccount) {
477 mAccountSpinner.setSelection(0);
478 }
479 }
480
481 @Override
482 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
483 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
484 }
485 };
486
487 void setAccount(String accountName, String accountType) {
488 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
489 BookmarkAccount account = mAccountAdapter.getItem(i);
490 if (TextUtils.equals(account.accountName, accountName)
491 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700492 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800493 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700494 return;
495 }
496 }
497 }
498
Leon Scroggins III052ce662010-09-13 14:44:16 -0400499 @Override
500 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
501 String[] projection;
502 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700503 case LOADER_ID_ACCOUNTS:
504 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400505 case LOADER_ID_FOLDER_CONTENTS:
506 projection = new String[] {
507 BrowserContract.Bookmarks._ID,
508 BrowserContract.Bookmarks.TITLE,
509 BrowserContract.Bookmarks.IS_FOLDER
510 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500511 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700512 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500513 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700514 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
515 whereArgs = new String[] { Long.toString(mMap.getLong(
516 BrowserContract.Bookmarks._ID)) };
517 }
518 long currentFolder;
519 Object data = mCrumbs.getTopData();
520 if (data != null) {
521 currentFolder = ((Folder) data).Id;
522 } else {
523 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500524 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400525 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700526 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400527 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500528 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700529 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500530 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400531 default:
532 throw new AssertionError("Asking for nonexistant loader!");
533 }
534 }
535
536 @Override
537 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
538 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700539 case LOADER_ID_ACCOUNTS:
540 mAccountAdapter.clear();
541 while (cursor.moveToNext()) {
542 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500543 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700544
545 if (cursor.getCount() < 2) {
546 View accountView = findViewById(R.id.row_account);
547 if (accountView != null) {
548 accountView.setVisibility(View.GONE);
549 }
550 }
551
John Reck2eec4c32011-05-11 15:55:32 -0700552 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
553 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
554 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500555 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400556 case LOADER_ID_FOLDER_CONTENTS:
557 mAdapter.changeCursor(cursor);
558 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400559 }
560 }
561
Dianne Hackborn39772c82010-12-16 00:43:54 -0800562 public void onLoaderReset(Loader<Cursor> loader) {
563 switch (loader.getId()) {
564 case LOADER_ID_FOLDER_CONTENTS:
565 mAdapter.changeCursor(null);
566 break;
567 }
568 }
569
Leon Scroggins02081942010-11-01 17:52:42 -0400570 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500571 * Move cursor to the position that has folderToFind as its "_id".
572 * @param cursor Cursor containing folders in the bookmarks database
573 * @param folderToFind "_id" of the folder to move to.
574 * @param idIndex Index in cursor of "_id"
575 * @throws AssertionError if cursor is empty or there is no row with folderToFind
576 * as its "_id".
577 */
578 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
579 throws AssertionError {
580 if (!cursor.moveToFirst()) {
581 throw new AssertionError("No folders in the database!");
582 }
583 long folder;
584 do {
585 folder = cursor.getLong(idIndex);
586 } while (folder != folderToFind && cursor.moveToNext());
587 if (cursor.isAfterLast()) {
588 throw new AssertionError("Folder(id=" + folderToFind
589 + ") holding this bookmark does not exist!");
590 }
591 }
592
Leon Scroggins III052ce662010-09-13 14:44:16 -0400593 @Override
594 public void onItemClick(AdapterView<?> parent, View view, int position,
595 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400596 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400597 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400598 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400599 }
600
Leon Scroggins162f8352010-10-18 15:02:44 -0400601 private void setShowFolderNamer(boolean show) {
602 if (show != mIsFolderNamerShowing) {
603 mIsFolderNamerShowing = show;
604 if (show) {
605 // Set the selection to the folder namer so it will be in
606 // view.
607 mListView.addFooterView(mFolderNamerHolder);
608 } else {
609 mListView.removeFooterView(mFolderNamerHolder);
610 }
611 // Refresh the list.
612 mListView.setAdapter(mAdapter);
613 if (show) {
614 mListView.setSelection(mListView.getCount() - 1);
615 }
616 }
617 }
618
Leon Scroggins III052ce662010-09-13 14:44:16 -0400619 /**
620 * Shows a list of names of folders.
621 */
622 private class FolderAdapter extends CursorAdapter {
623 public FolderAdapter(Context context) {
624 super(context, null);
625 }
626
627 @Override
628 public void bindView(View view, Context context, Cursor cursor) {
629 ((TextView) view.findViewById(android.R.id.text1)).setText(
630 cursor.getString(cursor.getColumnIndexOrThrow(
631 BrowserContract.Bookmarks.TITLE)));
632 }
633
634 @Override
635 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700636 View view = LayoutInflater.from(context).inflate(
637 R.layout.folder_list_item, null);
638 view.setBackgroundDrawable(context.getResources().
639 getDrawable(android.R.drawable.list_selector_background));
640 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400641 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400642
643 @Override
644 public boolean isEmpty() {
645 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400646 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400647 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400648 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800649
Leon Scrogginsc1129902010-12-08 15:28:33 -0500650 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 protected void onCreate(Bundle icicle) {
652 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500653 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100654
655 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100656
Leon Scroggins III052ce662010-09-13 14:44:16 -0400657 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100658
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400659 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700660
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 String title = null;
662 String url = null;
Pankaj Garg21dad562015-07-02 17:17:24 -0700663 mTouchIconUrl = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100664
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400665 mFakeTitle = (TextView) findViewById(R.id.fake_title);
Pankaj Garg634bf432015-07-13 09:54:21 -0700666
667 mDeleteButton = findViewById(R.id.delete);
668 mDeleteButton.setOnClickListener(this);
669
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800671 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800673 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 mMap = b;
675 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400676 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800677 if (mEditingFolder) {
678 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500679 } else {
680 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800681 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400682 } else {
683 int gravity = mMap.getInt("gravity", -1);
684 if (gravity != -1) {
685 WindowManager.LayoutParams l = window.getAttributes();
686 l.gravity = gravity;
687 window.setAttributes(l);
688 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800689 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400690 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
691 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
692 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700693 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800694
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800695 // Check if title is not empty to prevent NPE
696 if (!TextUtils.isEmpty(title)) {
697 if (title.length() > MAX_TITLE_LENGTH) {
698 title = title.substring(0, MAX_TITLE_LENGTH);
699 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800700 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700701
Leon Scroggins25230d72010-09-28 20:09:25 -0400702 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703
704 mTitle = (EditText) findViewById(R.id.title);
705 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700706 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100707
Leon Scroggins III052ce662010-09-13 14:44:16 -0400708 mAddress = (EditText) findViewById(R.id.address);
709 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800710 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800711
The Android Open Source Project0c908882009-03-03 19:32:16 -0800712 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400713 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800714
715 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400716 mCancelButton.setOnClickListener(this);
717
Leon Scroggins504433a2011-01-13 12:26:52 -0500718 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700719 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500720 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500721 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400722
723 mDefaultView = findViewById(R.id.default_view);
724 mFolderSelector = findViewById(R.id.folder_selector);
725
Leon Scroggins162f8352010-10-18 15:02:44 -0400726 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
727 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400728 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800729
730 // add for carrier test about warning limit of edit text
731 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
732 BrowserUtils.FILENAME_MAX_LENGTH);
733
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500734 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
735 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400736
737 mAddNewFolder = findViewById(R.id.add_new_folder);
738 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400739 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400740
Leon Scroggins74dbe012010-10-15 10:54:27 -0400741 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
Pankaj Garg634bf432015-07-13 09:54:21 -0700742 //mCrumbs.setUseBackButton(true);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400743 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800744 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400745 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800746 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400747
Leon Scroggins III052ce662010-09-13 14:44:16 -0400748 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400749 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400750 View empty = findViewById(R.id.empty);
751 mListView.setEmptyView(empty);
752 mListView.setAdapter(mAdapter);
753 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400754 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500755
John Reck2eec4c32011-05-11 15:55:32 -0700756 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
757 android.R.layout.simple_spinner_item);
758 mAccountAdapter.setDropDownViewResource(
759 android.R.layout.simple_spinner_dropdown_item);
760 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
761 mAccountSpinner.setAdapter(mAccountAdapter);
762 mAccountSpinner.setOnItemSelectedListener(this);
Sagar Dhawan7e9d2472015-07-20 10:39:40 -0700763 mFolder.setSelectionIgnoringSelectionChange(1); // Select Bookmarks by default
John Reck2eec4c32011-05-11 15:55:32 -0700764
Leon Scroggins75630672011-01-13 17:56:15 -0500765 mFakeTitleHolder = findViewById(R.id.title_holder);
766
Leon Scroggins504433a2011-01-13 12:26:52 -0500767 if (!window.getDecorView().isInTouchMode()) {
768 mButton.requestFocus();
769 }
770
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800771 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
772 mFolder.setSelectionIgnoringSelectionChange(2);
773 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
774 }
775
John Reck2eec4c32011-05-11 15:55:32 -0700776 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500777 }
778
Leon Scroggins75630672011-01-13 17:56:15 -0500779 private void showRemoveButton() {
Pankaj Garg634bf432015-07-13 09:54:21 -0700780 mDeleteButton.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500781 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
782 mRemoveLink = findViewById(R.id.remove);
783 mRemoveLink.setVisibility(View.VISIBLE);
784 mRemoveLink.setOnClickListener(this);
785 }
786
Leon Scroggins504433a2011-01-13 12:26:52 -0500787 // Called once we have determined which folder is the root folder
788 private void onRootFolderFound(long root) {
789 mRootFolder = root;
Pankaj Garg63396942015-03-06 10:05:25 -0800790 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500791 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700792 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500793 }
794
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500795 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700796 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500797 String name = getString(R.string.bookmarks);
798 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
799 new Folder(name, mRootFolder));
800 // To better match the other folders.
801 mTopLevelLabel.setCompoundDrawablePadding(6);
802 }
803
Leon Scroggins504433a2011-01-13 12:26:52 -0500804 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400805 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500806 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500807 // Since we're not in the root folder, change the selection to other
808 // folder now. The text will get changed once we select the correct
809 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500810 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500811 } else {
812 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500813 if (!mEditingFolder) {
814 // Initially the "Bookmarks" folder should be showing, rather than
815 // the home screen. In the editing folder case, home screen is not
816 // an option, so "Bookmarks" folder is already at the top.
817 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
818 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400819 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400820 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700821 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500822 }
823
Leon Scroggins02065b02010-01-04 14:30:13 -0500824 /**
825 * Runnable to save a bookmark, so it can be performed in its own thread.
826 */
827 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400828 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500829 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200830 private Context mContext;
831 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100832 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500833 mMessage = msg;
834 }
835 public void run() {
836 // Unbundle bookmark data.
837 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400838 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
839 String url = bundle.getString(BrowserContract.Bookmarks.URL);
840 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500841 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700842 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400843 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500844
845 // Save to the bookmarks DB.
846 try {
847 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400848 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700849 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500850 if (touchIconUrl != null) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700851 new DownloadTouchIcon(mContext, cr, url).execute(touchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500852 }
853 mMessage.arg1 = 1;
854 } catch (IllegalStateException e) {
855 mMessage.arg1 = 0;
856 }
857 mMessage.sendToTarget();
858 }
859 }
860
John Reckc8490812010-11-22 14:15:36 -0800861 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
862 Context mContext;
863 Long mId;
864
865 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100866 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800867 mId = id;
868 }
869
870 @Override
871 protected Void doInBackground(ContentValues... params) {
872 if (params.length != 1) {
873 throw new IllegalArgumentException("No ContentValues provided!");
874 }
875 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
876 mContext.getContentResolver().update(
877 uri,
878 params[0], null, null);
879 return null;
880 }
881 }
882
Ben Murdoch1794fe22009-09-29 18:14:30 +0100883 private void createHandler() {
884 if (mHandler == null) {
885 mHandler = new Handler() {
886 @Override
887 public void handleMessage(Message msg) {
888 switch (msg.what) {
889 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500890 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100891 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
892 Toast.LENGTH_LONG).show();
893 } else {
894 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
895 Toast.LENGTH_LONG).show();
896 }
897 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400898 case TOUCH_ICON_DOWNLOADED:
899 Bundle b = msg.getData();
900 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400901 AddBookmarkPage.this,
902 b.getString(BrowserContract.Bookmarks.URL),
903 b.getString(BrowserContract.Bookmarks.TITLE),
904 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
905 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400906 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500907 case BOOKMARK_DELETED:
908 finish();
909 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100910 }
911 }
912 };
913 }
914 }
915
luxiaoldbe4a622013-07-19 17:14:06 +0800916 static void deleteDuplicateBookmark(final Context context, final long id) {
917 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
918 context.getContentResolver().delete(uri, null, null);
919 }
920
Pankaj Garg634bf432015-07-13 09:54:21 -0700921 private void onDeleteWithConfirm() {
922 final String title = mTitle.getText().toString().trim();
923 final String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
924 final String url = unfilteredUrl.trim();
925 new AlertDialog.Builder(this)
926 .setIconAttribute(android.R.attr.alertDialogIcon)
927 .setMessage(getString(R.string.delete_bookmark_warning, title))
928 .setNegativeButton(android.R.string.cancel, null)
929 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
930 public void onClick(DialogInterface dialog, int which) {
931 ContentResolver cr = getContentResolver();
932 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
933 BookmarksLoader.PROJECTION,
934 "title = ? OR url = ?",
935 new String[] {
936 title, url
937 },
938 null);
939
940 if (cursor == null) {
941 finish();
942 return;
943 }
944
945 try {
946 if (cursor.moveToFirst()) {
947 do {
948 long index = cursor.getLong(
949 cursor.getColumnIndex(BrowserContract.Bookmarks._ID));
950 cr.delete(ContentUris.withAppendedId(
951 BrowserContract.Bookmarks.CONTENT_URI, index),
952 null, null);
953 } while (cursor.moveToNext());
954 }
955 } catch (IllegalStateException e) {
956 e.printStackTrace();
957 } finally {
958 if (cursor != null)
959 cursor.close();
960 }
961 finish();
962 }
963 })
964 .show();
965 }
966
luxiaoldbe4a622013-07-19 17:14:06 +0800967 private void onSaveWithConfirm() {
968 String title = mTitle.getText().toString().trim();
969 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
970 String url = unfilteredUrl.trim();
971 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
972 int duplicateCount;
973 final ContentResolver cr = getContentResolver();
974
975 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
976 BookmarksLoader.PROJECTION,
977 "( title = ? OR url = ? ) AND parent = ?",
978 new String[] {
979 title, url, Long.toString(mCurrentFolder)
980 },
981 null);
982
983 if (cursor == null) {
984 save();
985 return;
986 }
987
988 duplicateCount = cursor.getCount();
989 if (duplicateCount <= 0) {
990 cursor.close();
991 save();
992 return;
993 } else {
994 try {
995 while (cursor.moveToNext()) {
996 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
997 mDuplicateContext = AddBookmarkPage.this;
998 }
999 } catch (IllegalStateException e) {
1000 e.printStackTrace();
1001 } finally {
1002 if (cursor != null)
1003 cursor.close();
1004 }
1005 }
1006
1007 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
1008 save();
1009 return;
1010 }
1011
1012 new AlertDialog.Builder(this)
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07001013 .setTitle(getString(R.string.save_to_bookmarks))
luxiaoldbe4a622013-07-19 17:14:06 +08001014 .setMessage(getString(R.string.overwrite_bookmark_msg))
1015 .setNegativeButton(android.R.string.cancel, null)
1016 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1017 public void onClick(DialogInterface dialog, int which) {
1018 if (mDuplicateContext == null) {
1019 return;
1020 }
1021 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
1022 save();
1023 }
1024 })
1025 .show();
1026 }
1027
The Android Open Source Project0c908882009-03-03 19:32:16 -08001028 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +01001029 * 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 -08001030 */
1031 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +01001032 createHandler();
1033
The Android Open Source Project0c908882009-03-03 19:32:16 -08001034 String title = mTitle.getText().toString().trim();
luxiaoldbe4a622013-07-19 17:14:06 +08001035 String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +01001036
The Android Open Source Project0c908882009-03-03 19:32:16 -08001037 boolean emptyTitle = title.length() == 0;
1038 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
1039 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -08001040 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001041 if (emptyTitle) {
1042 mTitle.setError(r.getText(R.string.bookmark_needs_title));
1043 }
1044 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -04001045 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001046 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -04001047 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001048 }
Ben Murdochca12cfa2009-11-17 13:57:44 +00001049 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -08001050 if (!mEditingFolder) {
1051 try {
1052 // We allow bookmarks with a javascript: scheme, but these will in most cases
1053 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +00001054
John Reckc8490812010-11-22 14:15:36 -08001055 if (!url.toLowerCase().startsWith("javascript:")) {
Vivek Sekharf95fb382014-07-23 11:13:44 -07001056 String encodedUrl = URLEncoder.encode(url, "UTF-8");
1057 URI uriObj = new URI(encodedUrl);
John Reckc8490812010-11-22 14:15:36 -08001058 String scheme = uriObj.getScheme();
1059 if (!Bookmarks.urlHasAcceptableScheme(url)) {
1060 // If the scheme was non-null, let the user know that we
1061 // can't save their bookmark. If it was null, we'll assume
1062 // they meant http when we parse it in the WebAddress class.
1063 if (scheme != null) {
1064 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
1065 return false;
1066 }
1067 WebAddress address;
1068 try {
1069 address = new WebAddress(unfilteredUrl);
1070 } catch (ParseException e) {
1071 throw new URISyntaxException("", "");
1072 }
1073 if (address.getHost().length() == 0) {
1074 throw new URISyntaxException("", "");
1075 }
1076 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001077 }
Ben Murdochde353622009-10-12 10:29:00 +01001078 }
John Reckc8490812010-11-22 14:15:36 -08001079 } catch (URISyntaxException e) {
1080 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1081 return false;
Vivek Sekharf95fb382014-07-23 11:13:44 -07001082 } catch (UnsupportedEncodingException e) {
1083 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1084 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001085 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001086 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001087
Leon Scroggins88d08032010-10-21 15:17:10 -04001088 if (mSaveToHomeScreen) {
1089 mEditingExisting = false;
1090 }
1091
1092 boolean urlUnmodified = url.equals(mOriginalUrl);
1093
Ben Murdoch1794fe22009-09-29 18:14:30 +01001094 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001095 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1096 ContentValues values = new ContentValues();
1097 values.put(BrowserContract.Bookmarks.TITLE, title);
1098 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1099 if (!mEditingFolder) {
1100 values.put(BrowserContract.Bookmarks.URL, url);
1101 if (!urlUnmodified) {
1102 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1103 }
1104 }
1105 if (values.size() > 0) {
1106 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1107 }
1108 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001109 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001110 Bitmap thumbnail;
1111 Bitmap favicon;
1112 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001113 thumbnail = (Bitmap) mMap.getParcelable(
1114 BrowserContract.Bookmarks.THUMBNAIL);
1115 favicon = (Bitmap) mMap.getParcelable(
1116 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001117 } else {
1118 thumbnail = null;
1119 favicon = null;
1120 }
1121
Ben Murdoch1794fe22009-09-29 18:14:30 +01001122 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001123 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1124 bundle.putString(BrowserContract.Bookmarks.URL, url);
1125 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001126
1127 if (mSaveToHomeScreen) {
1128 if (mTouchIconUrl != null && urlUnmodified) {
1129 Message msg = Message.obtain(mHandler,
1130 TOUCH_ICON_DOWNLOADED);
1131 msg.setData(bundle);
1132 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001133 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001134 icon.execute(mTouchIconUrl);
1135 } else {
1136 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1137 title, null /*touchIcon*/, favicon));
1138 }
1139 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001140 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1141 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Pankaj Garg21dad562015-07-02 17:17:24 -07001142 if (mTouchIconUrl != null) {
1143 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
1144 }
Leon Scroggins88d08032010-10-21 15:17:10 -04001145 // Post a message to write to the DB.
1146 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1147 msg.setData(bundle);
1148 // Start a new thread so as to not slow down the UI
1149 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1150 t.start();
1151 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001152 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001153 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001154 }
luxiaoldbe4a622013-07-19 17:14:06 +08001155 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001156 return true;
1157 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001158
John Reck2eec4c32011-05-11 15:55:32 -07001159 @Override
1160 public void onItemSelected(AdapterView<?> parent, View view, int position,
1161 long id) {
1162 if (mAccountSpinner == parent) {
1163 long root = mAccountAdapter.getItem(position).rootFolderId;
1164 if (root != mRootFolder) {
1165 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001166 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001167 }
1168 }
1169 }
1170
1171 @Override
1172 public void onNothingSelected(AdapterView<?> parent) {
1173 // Don't care
1174 }
1175
Leon Scroggins162f8352010-10-18 15:02:44 -04001176 /*
1177 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1178 */
1179 public static class CustomListView extends ListView {
1180 private EditText mEditText;
1181
1182 public void addEditText(EditText editText) {
1183 mEditText = editText;
1184 }
1185
1186 public CustomListView(Context context) {
1187 super(context);
1188 }
1189
1190 public CustomListView(Context context, AttributeSet attrs) {
1191 super(context, attrs);
1192 }
1193
1194 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1195 super(context, attrs, defStyle);
1196 }
1197
1198 @Override
1199 public boolean checkInputConnectionProxy(View view) {
1200 return view == mEditText;
1201 }
1202 }
John Reck2eec4c32011-05-11 15:55:32 -07001203
John Reck3bf3cfb2011-10-17 18:00:05 -07001204 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001205
1206 static final String[] PROJECTION = new String[] {
1207 Accounts.ACCOUNT_NAME,
1208 Accounts.ACCOUNT_TYPE,
1209 Accounts.ROOT_ID,
1210 };
1211
1212 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1213 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1214 static final int COLUMN_INDEX_ROOT_ID = 2;
1215
1216 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001217 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001218 }
1219
1220 }
1221
John Reck9b8cd1e2011-05-25 18:14:01 -07001222 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001223
1224 private String mLabel;
1225 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001226 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001227
1228 public BookmarkAccount(Context context, Cursor cursor) {
1229 accountName = cursor.getString(
1230 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1231 accountType = cursor.getString(
1232 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1233 rootFolderId = cursor.getLong(
1234 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1235 mLabel = accountName;
1236 if (TextUtils.isEmpty(mLabel)) {
1237 mLabel = context.getString(R.string.local_bookmarks);
1238 }
1239 }
1240
1241 @Override
1242 public String toString() {
1243 return mLabel;
1244 }
1245 }
1246
1247 static class EditBookmarkInfo {
1248 long id = -1;
1249 long parentId = -1;
1250 String parentTitle;
1251 String title;
1252 String accountName;
1253 String accountType;
1254
1255 long lastUsedId = -1;
1256 String lastUsedTitle;
1257 String lastUsedAccountName;
1258 String lastUsedAccountType;
1259 }
1260
1261 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1262
1263 private Context mContext;
1264 private Bundle mMap;
1265
1266 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1267 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001268 mContext = context.getApplicationContext();
John Reck2eec4c32011-05-11 15:55:32 -07001269 mMap = bundle;
1270 }
1271
1272 @Override
1273 public EditBookmarkInfo loadInBackground() {
1274 final ContentResolver cr = mContext.getContentResolver();
1275 EditBookmarkInfo info = new EditBookmarkInfo();
1276 Cursor c = null;
1277
1278 try {
1279 // First, let's lookup the bookmark (check for dupes, get needed info)
1280 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1281 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1282 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1283 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1284 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1285 new String[] { BrowserContract.Bookmarks._ID},
1286 BrowserContract.Bookmarks.URL + "=?",
1287 new String[] { url }, null);
1288 if (c.getCount() == 1 && c.moveToFirst()) {
1289 info.id = c.getLong(0);
1290 }
1291 c.close();
1292 }
1293 if (info.id != -1) {
1294 c = cr.query(ContentUris.withAppendedId(
1295 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1296 new String[] {
1297 BrowserContract.Bookmarks.PARENT,
1298 BrowserContract.Bookmarks.ACCOUNT_NAME,
1299 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1300 BrowserContract.Bookmarks.TITLE},
1301 null, null, null);
1302 if (c.moveToFirst()) {
1303 info.parentId = c.getLong(0);
1304 info.accountName = c.getString(1);
1305 info.accountType = c.getString(2);
1306 info.title = c.getString(3);
1307 }
1308 c.close();
1309 c = cr.query(ContentUris.withAppendedId(
1310 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1311 new String[] {
1312 BrowserContract.Bookmarks.TITLE,},
1313 null, null, null);
1314 if (c.moveToFirst()) {
1315 info.parentTitle = c.getString(0);
1316 }
1317 c.close();
1318 }
1319
1320 // Figure out the last used folder/account
1321 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1322 new String[] {
1323 BrowserContract.Bookmarks.PARENT,
1324 }, null, null,
1325 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1326 if (c.moveToFirst()) {
1327 long parent = c.getLong(0);
1328 c.close();
1329 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1330 new String[] {
1331 BrowserContract.Bookmarks.TITLE,
1332 BrowserContract.Bookmarks.ACCOUNT_NAME,
1333 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1334 BrowserContract.Bookmarks._ID + "=?", new String[] {
1335 Long.toString(parent)}, null);
1336 if (c.moveToFirst()) {
1337 info.lastUsedId = parent;
1338 info.lastUsedTitle = c.getString(0);
1339 info.lastUsedAccountName = c.getString(1);
1340 info.lastUsedAccountType = c.getString(2);
1341 }
1342 c.close();
1343 }
1344 } finally {
1345 if (c != null) {
1346 c.close();
1347 }
1348 }
1349
1350 return info;
1351 }
1352
1353 @Override
1354 protected void onStartLoading() {
1355 forceLoad();
1356 }
1357
1358 }
1359
The Android Open Source Project0c908882009-03-03 19:32:16 -08001360}