blob: 25cde080fe98ecedae2d3e73611eb1e5f33a6058 [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;
jrizzoli38a81f62015-10-25 18:22:38 +010056import android.widget.ImageButton;
Leon Scroggins III052ce662010-09-13 14:44:16 -040057import android.widget.ListView;
John Reck2eec4c32011-05-11 15:55:32 -070058import android.widget.Spinner;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059import android.widget.TextView;
60import android.widget.Toast;
61
Bijan Amirzada41242f22014-03-21 12:12:18 -070062import com.android.browser.BrowserUtils;
63import com.android.browser.R;
64import com.android.browser.addbookmark.FolderSpinner;
65import com.android.browser.addbookmark.FolderSpinnerAdapter;
66import com.android.browser.platformsupport.BrowserContract;
67import com.android.browser.platformsupport.WebAddress;
68import com.android.browser.platformsupport.BrowserContract.Accounts;
69import com.android.browser.reflect.ReflectHelper;
luxiaoldbe4a622013-07-19 17:14:06 +080070
Sagar Dhawanef06f562015-09-07 15:23:22 +020071import org.codeaurora.swe.util.SWEUrlUtils;
72
Cary Clarkf2407c62009-09-04 12:25:10 -040073import java.net.URI;
Vivek Sekharf95fb382014-07-23 11:13:44 -070074import java.net.URLEncoder;
Cary Clarkf2407c62009-09-04 12:25:10 -040075import java.net.URISyntaxException;
Vivek Sekharf95fb382014-07-23 11:13:44 -070076import java.io.UnsupportedEncodingException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040077
78public class AddBookmarkPage extends Activity
79 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040080 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
John Reck2eec4c32011-05-11 15:55:32 -070081 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener,
82 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080083
Michael Kolb370a4f32010-10-06 10:45:32 -070084 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040085 public static final String TOUCH_ICON_URL = "touch_icon_url";
86 // Place on an edited bookmark to remove the saved thumbnail
87 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
88 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050089 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070090
John Reckc8490812010-11-22 14:15:36 -080091 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
92 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
93
kaiyiz3d7e4d52013-09-17 17:56:27 +080094 private static final int MAX_CRUMBS_SHOWN = 1;
Leon Scroggins74dbe012010-10-15 10:54:27 -040095
The Android Open Source Project0c908882009-03-03 19:32:16 -080096 private final String LOGTAG = "Bookmarks";
97
Leon Scroggins III052ce662010-09-13 14:44:16 -040098 // IDs for the CursorLoaders that are used.
John Reck2eec4c32011-05-11 15:55:32 -070099 private final int LOADER_ID_ACCOUNTS = 0;
100 private final int LOADER_ID_FOLDER_CONTENTS = 1;
101 private final int LOADER_ID_EDIT_INFO = 2;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400102
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700103 final static int MAX_TITLE_LENGTH = 80;
104
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105 private EditText mTitle;
106 private EditText mAddress;
jrizzoli38a81f62015-10-25 18:22:38 +0100107 private ImageButton mButton;
108 private ImageButton mCancelButton;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -0800110 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400112 private String mTouchIconUrl;
Sagar Dhawanef06f562015-09-07 15:23:22 +0200113 private String mUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500114 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400115 private View mDefaultView;
116 private View mFolderSelector;
117 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500118 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400119 private boolean mIsFolderNamerShowing;
120 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400121 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400122 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500123 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400124 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400125 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400126 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400127 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400128 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400129 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500130 private TextView mTopLevelLabel;
131 private Drawable mHeaderIcon;
jrizzoli38a81f62015-10-25 18:22:38 +0100132 private ImageButton mRemoveLink;
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 Scroggins52f7daa2010-10-18 16:57:46 -0400244 if (changedFolder) {
245 Object data = mCrumbs.getTopData();
246 if (data != null) {
247 Folder folder = (Folder) data;
248 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500249 if (mCurrentFolder == mRootFolder) {
250 // The Spinner changed to show "Other folder ..." Change
251 // it back to "Bookmarks", which is position 0 if we are
252 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500253 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500254 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700255 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500256 }
257 }
258 } else {
259 // The user canceled selecting a folder. Revert back to the earlier
260 // selection.
261 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500262 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500263 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500264 if (mCurrentFolder == mRootFolder) {
265 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
266 } else {
267 Object data = mCrumbs.getTopData();
268 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700269 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500270 // folder selector will say "Other folder..." Change it
271 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700272 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500273 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700274 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500275 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500276 setupTopCrumb();
277 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500278 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
279
280 }
281 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400282 }
283 }
284 }
285
Leon Scroggins III052ce662010-09-13 14:44:16 -0400286 @Override
287 public void onClick(View v) {
288 if (v == mButton) {
289 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400290 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400291 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400292 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700293 } else {
294 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400295 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400296 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700297 }
luxiaoldbe4a622013-07-19 17:14:06 +0800298 } else {
299 // add for carrier which requires same title or address can not
300 // exist.
301 if (mSaveToHomeScreen) {
302 if (save()) {
303 return;
304 }
305 } else {
306 onSaveWithConfirm();
307 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400308 }
309 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400310 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400311 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400312 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
313 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700314 } else {
315 finish();
316 }
jrizzoli38a81f62015-10-25 18:22:38 +0100317 } else if (v == mRemoveLink) {
Pankaj Garg634bf432015-07-13 09:54:21 -0700318 onDeleteWithConfirm();
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500319 } else if (v == mFolderCancel) {
320 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400321 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400322 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400323 mFolderNamer.setText(R.string.new_folder);
324 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700325 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400326 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400327 InputMethodManager imm = getInputMethodManager();
328 // Set the InputMethodManager to focus on the ListView so that it
329 // can transfer the focus to mFolderNamer.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800330 //imm.focusIn(mListView);
331 Object[] params = {mListView};
332 Class[] type = new Class[] {View.class};
333 ReflectHelper.invokeMethod(imm, "focusIn", type, params);
Leon Scroggins162f8352010-10-18 15:02:44 -0400334 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800335 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400336 }
337
Leon Scroggins504433a2011-01-13 12:26:52 -0500338 // FolderSpinner.OnSetSelectionListener
339
Leon Scroggins88d08032010-10-21 15:17:10 -0400340 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500341 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500342 int intId = (int) id;
343 switch (intId) {
344 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400345 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400346 mSaveToHomeScreen = false;
347 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500348 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400349 // Create a short cut to the home screen
350 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400351 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500352 case FolderSpinnerAdapter.OTHER_FOLDER:
Pankaj Garg52d36492015-09-07 15:09:40 +0200353 setupTopCrumb();
354 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins88d08032010-10-21 15:17:10 -0400355 switchToFolderSelector();
356 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500357 case FolderSpinnerAdapter.RECENT_FOLDER:
358 mCurrentFolder = mFolderAdapter.recentFolderId();
359 mSaveToHomeScreen = false;
360 // In case the user decides to select OTHER_FOLDER
361 // and choose a different one, so that we will start from
362 // the correct place.
363 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500364 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500365 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400366 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500367 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400368 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500369 }
370
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500371 /**
372 * Finish naming a folder, and close the IME
373 * @param cancel If true, the new folder is not created. If false, the new
374 * folder is created and the user is taken inside it.
375 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400376 private void completeOrCancelFolderNaming(boolean cancel) {
377 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700378 String name = mFolderNamer.getText().toString();
379 long id = addFolderToCurrent(mFolderNamer.getText().toString());
380 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700381 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400382 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400383 mAddNewFolder.setVisibility(View.VISIBLE);
384 mAddSeparator.setVisibility(View.VISIBLE);
385 getInputMethodManager().hideSoftInputFromWindow(
Pankaj Garg52d36492015-09-07 15:09:40 +0200386 mFolderNamer.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700387 }
388
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700389 private long addFolderToCurrent(String name) {
390 // Add the folder to the database
391 ContentValues values = new ContentValues();
392 values.put(BrowserContract.Bookmarks.TITLE,
393 name);
394 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400395 long currentFolder;
396 Object data = mCrumbs.getTopData();
397 if (data != null) {
398 currentFolder = ((Folder) data).Id;
399 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400400 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400401 }
402 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700403 Uri uri = getContentResolver().insert(
404 BrowserContract.Bookmarks.CONTENT_URI, values);
405 if (uri != null) {
406 return ContentUris.parseId(uri);
407 } else {
408 return -1;
409 }
410 }
411
Leon Scroggins III052ce662010-09-13 14:44:16 -0400412 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500413 // Set the list to the top in case it is scrolled.
414 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400415 mDefaultView.setVisibility(View.GONE);
416 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400417 mCrumbHolder.setVisibility(View.VISIBLE);
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;
John Reck2eec4c32011-05-11 15:55:32 -0700445 mTitle.setText(info.title);
446 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
447 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
448 setAccount = true;
449 setAccount(info.accountName, info.accountType);
450 mCurrentFolder = info.parentId;
451 onCurrentFolderFound();
452 }
John Reck37894a92011-05-13 12:47:53 -0700453 // TODO: Detect if lastUsedId is a subfolder of info.id in the
454 // editing folder case. For now, just don't show the last used
455 // folder at all to prevent any chance of the user adding a parent
456 // folder to a child folder
457 if (info.lastUsedId != -1 && info.lastUsedId != info.id
458 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700459 if (setAccount && info.lastUsedId != mRootFolder
460 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
461 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
462 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
463 } else if (!setAccount) {
464 setAccount = true;
465 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
466 if (info.lastUsedId != mRootFolder) {
467 mFolderAdapter.addRecentFolder(info.lastUsedId,
468 info.lastUsedTitle);
469 }
470 }
471 }
472 if (!setAccount) {
473 mAccountSpinner.setSelection(0);
474 }
475 }
476
477 @Override
478 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
479 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
480 }
481 };
482
483 void setAccount(String accountName, String accountType) {
484 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
485 BookmarkAccount account = mAccountAdapter.getItem(i);
486 if (TextUtils.equals(account.accountName, accountName)
487 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700488 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800489 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700490 return;
491 }
492 }
493 }
494
Leon Scroggins III052ce662010-09-13 14:44:16 -0400495 @Override
496 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
497 String[] projection;
498 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700499 case LOADER_ID_ACCOUNTS:
500 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400501 case LOADER_ID_FOLDER_CONTENTS:
502 projection = new String[] {
503 BrowserContract.Bookmarks._ID,
504 BrowserContract.Bookmarks.TITLE,
505 BrowserContract.Bookmarks.IS_FOLDER
506 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500507 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700508 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500509 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700510 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
511 whereArgs = new String[] { Long.toString(mMap.getLong(
512 BrowserContract.Bookmarks._ID)) };
513 }
514 long currentFolder;
515 Object data = mCrumbs.getTopData();
516 if (data != null) {
517 currentFolder = ((Folder) data).Id;
518 } else {
519 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500520 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400521 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700522 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400523 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500524 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700525 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500526 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400527 default:
528 throw new AssertionError("Asking for nonexistant loader!");
529 }
530 }
531
532 @Override
533 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
534 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700535 case LOADER_ID_ACCOUNTS:
536 mAccountAdapter.clear();
537 while (cursor.moveToNext()) {
538 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500539 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700540
541 if (cursor.getCount() < 2) {
542 View accountView = findViewById(R.id.row_account);
543 if (accountView != null) {
544 accountView.setVisibility(View.GONE);
545 }
546 }
547
John Reck2eec4c32011-05-11 15:55:32 -0700548 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
549 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
550 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500551 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400552 case LOADER_ID_FOLDER_CONTENTS:
553 mAdapter.changeCursor(cursor);
554 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400555 }
556 }
557
Dianne Hackborn39772c82010-12-16 00:43:54 -0800558 public void onLoaderReset(Loader<Cursor> loader) {
559 switch (loader.getId()) {
560 case LOADER_ID_FOLDER_CONTENTS:
561 mAdapter.changeCursor(null);
562 break;
563 }
564 }
565
Leon Scroggins02081942010-11-01 17:52:42 -0400566 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500567 * Move cursor to the position that has folderToFind as its "_id".
568 * @param cursor Cursor containing folders in the bookmarks database
569 * @param folderToFind "_id" of the folder to move to.
570 * @param idIndex Index in cursor of "_id"
571 * @throws AssertionError if cursor is empty or there is no row with folderToFind
572 * as its "_id".
573 */
574 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
575 throws AssertionError {
576 if (!cursor.moveToFirst()) {
577 throw new AssertionError("No folders in the database!");
578 }
579 long folder;
580 do {
581 folder = cursor.getLong(idIndex);
582 } while (folder != folderToFind && cursor.moveToNext());
583 if (cursor.isAfterLast()) {
584 throw new AssertionError("Folder(id=" + folderToFind
585 + ") holding this bookmark does not exist!");
586 }
587 }
588
Leon Scroggins III052ce662010-09-13 14:44:16 -0400589 @Override
590 public void onItemClick(AdapterView<?> parent, View view, int position,
591 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400592 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400593 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400594 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 }
596
Leon Scroggins162f8352010-10-18 15:02:44 -0400597 private void setShowFolderNamer(boolean show) {
598 if (show != mIsFolderNamerShowing) {
599 mIsFolderNamerShowing = show;
600 if (show) {
601 // Set the selection to the folder namer so it will be in
602 // view.
603 mListView.addFooterView(mFolderNamerHolder);
604 } else {
605 mListView.removeFooterView(mFolderNamerHolder);
606 }
607 // Refresh the list.
608 mListView.setAdapter(mAdapter);
609 if (show) {
610 mListView.setSelection(mListView.getCount() - 1);
611 }
612 }
613 }
614
Leon Scroggins III052ce662010-09-13 14:44:16 -0400615 /**
616 * Shows a list of names of folders.
617 */
618 private class FolderAdapter extends CursorAdapter {
619 public FolderAdapter(Context context) {
620 super(context, null);
621 }
622
623 @Override
624 public void bindView(View view, Context context, Cursor cursor) {
625 ((TextView) view.findViewById(android.R.id.text1)).setText(
626 cursor.getString(cursor.getColumnIndexOrThrow(
627 BrowserContract.Bookmarks.TITLE)));
628 }
629
630 @Override
631 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700632 View view = LayoutInflater.from(context).inflate(
633 R.layout.folder_list_item, null);
634 view.setBackgroundDrawable(context.getResources().
635 getDrawable(android.R.drawable.list_selector_background));
636 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400637 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400638
639 @Override
640 public boolean isEmpty() {
641 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400642 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400643 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400644 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645
Leon Scrogginsc1129902010-12-08 15:28:33 -0500646 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 protected void onCreate(Bundle icicle) {
648 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500649 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100650
651 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100652
Leon Scroggins III052ce662010-09-13 14:44:16 -0400653 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100654
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400655 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700656
The Android Open Source Project0c908882009-03-03 19:32:16 -0800657 String title = null;
658 String url = null;
Pankaj Garg21dad562015-07-02 17:17:24 -0700659 mTouchIconUrl = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100660
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800662 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
Sagar Dhawanef06f562015-09-07 15:23:22 +0200663 boolean existing = mMap.getBoolean(CHECK_FOR_DUPE, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800665 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 mMap = b;
667 mEditingExisting = true;
John Reckc8490812010-11-22 14:15:36 -0800668 if (mEditingFolder) {
669 findViewById(R.id.row_address).setVisibility(View.GONE);
670 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400671 } else {
672 int gravity = mMap.getInt("gravity", -1);
673 if (gravity != -1) {
674 WindowManager.LayoutParams l = window.getAttributes();
675 l.gravity = gravity;
676 window.setAttributes(l);
677 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400679 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
Sagar Dhawanef06f562015-09-07 15:23:22 +0200680 url = mUrl = UrlUtils.fixUpUrl(mMap.getString(BrowserContract.Bookmarks.URL));
Leon Scrogginsbc922852010-10-22 12:15:27 -0400681 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700682 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800683
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800684 // Check if title is not empty to prevent NPE
685 if (!TextUtils.isEmpty(title)) {
686 if (title.length() > MAX_TITLE_LENGTH) {
687 title = title.substring(0, MAX_TITLE_LENGTH);
688 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800689 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700690
Leon Scroggins25230d72010-09-28 20:09:25 -0400691 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800692
693 mTitle = (EditText) findViewById(R.id.title);
694 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700695 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100696
Leon Scroggins III052ce662010-09-13 14:44:16 -0400697 mAddress = (EditText) findViewById(R.id.address);
698 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800699 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800700
jrizzoli38a81f62015-10-25 18:22:38 +0100701 mButton = (ImageButton) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400702 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703
jrizzoli38a81f62015-10-25 18:22:38 +0100704 mCancelButton = (ImageButton) findViewById(R.id.book_cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400705 mCancelButton.setOnClickListener(this);
706
Leon Scroggins504433a2011-01-13 12:26:52 -0500707 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700708 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500709 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500710 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400711
712 mDefaultView = findViewById(R.id.default_view);
713 mFolderSelector = findViewById(R.id.folder_selector);
714
Leon Scroggins162f8352010-10-18 15:02:44 -0400715 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
716 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400717 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800718
719 // add for carrier test about warning limit of edit text
720 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
721 BrowserUtils.FILENAME_MAX_LENGTH);
722
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500723 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
724 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400725
726 mAddNewFolder = findViewById(R.id.add_new_folder);
727 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400728 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400729
Leon Scroggins74dbe012010-10-15 10:54:27 -0400730 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
Pankaj Garg634bf432015-07-13 09:54:21 -0700731 //mCrumbs.setUseBackButton(true);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400732 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800733 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400734 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800735 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400736
Leon Scroggins III052ce662010-09-13 14:44:16 -0400737 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400738 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400739 View empty = findViewById(R.id.empty);
740 mListView.setEmptyView(empty);
741 mListView.setAdapter(mAdapter);
742 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400743 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500744
John Reck2eec4c32011-05-11 15:55:32 -0700745 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
746 android.R.layout.simple_spinner_item);
747 mAccountAdapter.setDropDownViewResource(
748 android.R.layout.simple_spinner_dropdown_item);
749 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
750 mAccountSpinner.setAdapter(mAccountAdapter);
751 mAccountSpinner.setOnItemSelectedListener(this);
Sagar Dhawan7e9d2472015-07-20 10:39:40 -0700752 mFolder.setSelectionIgnoringSelectionChange(1); // Select Bookmarks by default
John Reck2eec4c32011-05-11 15:55:32 -0700753
Leon Scroggins504433a2011-01-13 12:26:52 -0500754 if (!window.getDecorView().isInTouchMode()) {
755 mButton.requestFocus();
756 }
757
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800758 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
759 mFolder.setSelectionIgnoringSelectionChange(2);
760 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
761 }
762
John Reck2eec4c32011-05-11 15:55:32 -0700763 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500764 }
765
766 // Called once we have determined which folder is the root folder
767 private void onRootFolderFound(long root) {
768 mRootFolder = root;
Pankaj Garg63396942015-03-06 10:05:25 -0800769 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500770 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700771 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500772 }
773
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500774 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700775 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500776 String name = getString(R.string.bookmarks);
777 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
778 new Folder(name, mRootFolder));
779 // To better match the other folders.
780 mTopLevelLabel.setCompoundDrawablePadding(6);
781 }
782
Leon Scroggins504433a2011-01-13 12:26:52 -0500783 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400784 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500785 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500786 // Since we're not in the root folder, change the selection to other
787 // folder now. The text will get changed once we select the correct
788 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500789 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500790 } else {
791 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500792 if (!mEditingFolder) {
793 // Initially the "Bookmarks" folder should be showing, rather than
794 // the home screen. In the editing folder case, home screen is not
795 // an option, so "Bookmarks" folder is already at the top.
796 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
797 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400798 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400799 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700800 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500801 }
802
Leon Scroggins02065b02010-01-04 14:30:13 -0500803 /**
804 * Runnable to save a bookmark, so it can be performed in its own thread.
805 */
806 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400807 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500808 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200809 private Context mContext;
810 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100811 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500812 mMessage = msg;
813 }
814 public void run() {
815 // Unbundle bookmark data.
816 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400817 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
818 String url = bundle.getString(BrowserContract.Bookmarks.URL);
819 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500820 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700821 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400822 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500823
824 // Save to the bookmarks DB.
825 try {
826 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400827 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700828 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500829 if (touchIconUrl != null) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700830 new DownloadTouchIcon(mContext, cr, url).execute(touchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500831 }
832 mMessage.arg1 = 1;
833 } catch (IllegalStateException e) {
834 mMessage.arg1 = 0;
835 }
836 mMessage.sendToTarget();
837 }
838 }
839
John Reckc8490812010-11-22 14:15:36 -0800840 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
841 Context mContext;
842 Long mId;
843
844 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100845 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800846 mId = id;
847 }
848
849 @Override
850 protected Void doInBackground(ContentValues... params) {
851 if (params.length != 1) {
852 throw new IllegalArgumentException("No ContentValues provided!");
853 }
854 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
855 mContext.getContentResolver().update(
856 uri,
857 params[0], null, null);
858 return null;
859 }
860 }
861
Ben Murdoch1794fe22009-09-29 18:14:30 +0100862 private void createHandler() {
863 if (mHandler == null) {
864 mHandler = new Handler() {
865 @Override
866 public void handleMessage(Message msg) {
867 switch (msg.what) {
868 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500869 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100870 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
871 Toast.LENGTH_LONG).show();
872 } else {
873 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
874 Toast.LENGTH_LONG).show();
875 }
876 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400877 case TOUCH_ICON_DOWNLOADED:
878 Bundle b = msg.getData();
879 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400880 AddBookmarkPage.this,
881 b.getString(BrowserContract.Bookmarks.URL),
882 b.getString(BrowserContract.Bookmarks.TITLE),
883 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
884 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400885 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500886 case BOOKMARK_DELETED:
887 finish();
888 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100889 }
890 }
891 };
892 }
893 }
894
luxiaoldbe4a622013-07-19 17:14:06 +0800895 static void deleteDuplicateBookmark(final Context context, final long id) {
896 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
897 context.getContentResolver().delete(uri, null, null);
898 }
899
Pankaj Garg634bf432015-07-13 09:54:21 -0700900 private void onDeleteWithConfirm() {
901 final String title = mTitle.getText().toString().trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +0200902 final String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
Pankaj Garg634bf432015-07-13 09:54:21 -0700903 final String url = unfilteredUrl.trim();
904 new AlertDialog.Builder(this)
905 .setIconAttribute(android.R.attr.alertDialogIcon)
906 .setMessage(getString(R.string.delete_bookmark_warning, title))
907 .setNegativeButton(android.R.string.cancel, null)
908 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
909 public void onClick(DialogInterface dialog, int which) {
910 ContentResolver cr = getContentResolver();
911 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
912 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +0200913 "url = ?",
Pankaj Garg634bf432015-07-13 09:54:21 -0700914 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +0200915 url
Pankaj Garg634bf432015-07-13 09:54:21 -0700916 },
917 null);
918
919 if (cursor == null) {
920 finish();
921 return;
922 }
923
924 try {
925 if (cursor.moveToFirst()) {
926 do {
927 long index = cursor.getLong(
928 cursor.getColumnIndex(BrowserContract.Bookmarks._ID));
929 cr.delete(ContentUris.withAppendedId(
930 BrowserContract.Bookmarks.CONTENT_URI, index),
931 null, null);
932 } while (cursor.moveToNext());
933 }
934 } catch (IllegalStateException e) {
935 e.printStackTrace();
936 } finally {
937 if (cursor != null)
938 cursor.close();
939 }
940 finish();
941 }
942 })
943 .show();
944 }
945
luxiaoldbe4a622013-07-19 17:14:06 +0800946 private void onSaveWithConfirm() {
Sagar Dhawanef06f562015-09-07 15:23:22 +0200947 String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
luxiaoldbe4a622013-07-19 17:14:06 +0800948 String url = unfilteredUrl.trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +0200949 Long id = (mMap == null) ?
950 -1 :
951 mMap.getLong(BrowserContract.Bookmarks._ID);
luxiaoldbe4a622013-07-19 17:14:06 +0800952 int duplicateCount;
953 final ContentResolver cr = getContentResolver();
954
955 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
956 BookmarksLoader.PROJECTION,
Sagar Dhawan05f51cc2015-10-14 10:57:44 -0700957 "(url = ? OR title = ?) AND parent = ?",
luxiaoldbe4a622013-07-19 17:14:06 +0800958 new String[] {
Sagar Dhawan05f51cc2015-10-14 10:57:44 -0700959 url, mTitle.getText().toString().trim(), Long.toString(mCurrentFolder)
luxiaoldbe4a622013-07-19 17:14:06 +0800960 },
961 null);
962
963 if (cursor == null) {
964 save();
965 return;
966 }
967
968 duplicateCount = cursor.getCount();
969 if (duplicateCount <= 0) {
970 cursor.close();
971 save();
972 return;
973 } else {
974 try {
975 while (cursor.moveToNext()) {
976 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
977 mDuplicateContext = AddBookmarkPage.this;
978 }
979 } catch (IllegalStateException e) {
980 e.printStackTrace();
981 } finally {
982 if (cursor != null)
983 cursor.close();
984 }
985 }
986
987 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
988 save();
989 return;
990 }
991
992 new AlertDialog.Builder(this)
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700993 .setTitle(getString(R.string.save_to_bookmarks))
luxiaoldbe4a622013-07-19 17:14:06 +0800994 .setMessage(getString(R.string.overwrite_bookmark_msg))
995 .setNegativeButton(android.R.string.cancel, null)
996 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
997 public void onClick(DialogInterface dialog, int which) {
998 if (mDuplicateContext == null) {
999 return;
1000 }
1001 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
1002 save();
1003 }
1004 })
1005 .show();
1006 }
1007
The Android Open Source Project0c908882009-03-03 19:32:16 -08001008 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +01001009 * 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 -08001010 */
1011 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +01001012 createHandler();
1013
The Android Open Source Project0c908882009-03-03 19:32:16 -08001014 String title = mTitle.getText().toString().trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001015 String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +01001016
The Android Open Source Project0c908882009-03-03 19:32:16 -08001017 boolean emptyTitle = title.length() == 0;
1018 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
1019 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -08001020 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001021 if (emptyTitle) {
1022 mTitle.setError(r.getText(R.string.bookmark_needs_title));
1023 }
1024 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -04001025 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001026 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -04001027 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001028 }
Ben Murdochca12cfa2009-11-17 13:57:44 +00001029 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -08001030 if (!mEditingFolder) {
1031 try {
1032 // We allow bookmarks with a javascript: scheme, but these will in most cases
1033 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +00001034
John Reckc8490812010-11-22 14:15:36 -08001035 if (!url.toLowerCase().startsWith("javascript:")) {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001036 URI uriObj = new URI(url);
John Reckc8490812010-11-22 14:15:36 -08001037 String scheme = uriObj.getScheme();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001038 if (scheme == null) { // SWE will allow bookmarking any scheme
John Reckc8490812010-11-22 14:15:36 -08001039 WebAddress address;
1040 try {
1041 address = new WebAddress(unfilteredUrl);
1042 } catch (ParseException e) {
1043 throw new URISyntaxException("", "");
1044 }
1045 if (address.getHost().length() == 0) {
1046 throw new URISyntaxException("", "");
1047 }
1048 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001049 }
Ben Murdochde353622009-10-12 10:29:00 +01001050 }
John Reckc8490812010-11-22 14:15:36 -08001051 } catch (URISyntaxException e) {
1052 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1053 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001054 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001055 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001056
Leon Scroggins88d08032010-10-21 15:17:10 -04001057 if (mSaveToHomeScreen) {
1058 mEditingExisting = false;
1059 }
1060
Sagar Dhawanef06f562015-09-07 15:23:22 +02001061 boolean urlUnmodified = url.equals(mUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001062
Ben Murdoch1794fe22009-09-29 18:14:30 +01001063 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001064 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1065 ContentValues values = new ContentValues();
1066 values.put(BrowserContract.Bookmarks.TITLE, title);
1067 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1068 if (!mEditingFolder) {
1069 values.put(BrowserContract.Bookmarks.URL, url);
1070 if (!urlUnmodified) {
1071 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1072 }
1073 }
1074 if (values.size() > 0) {
1075 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1076 }
1077 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001078 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001079 Bitmap thumbnail;
1080 Bitmap favicon;
1081 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001082 thumbnail = (Bitmap) mMap.getParcelable(
1083 BrowserContract.Bookmarks.THUMBNAIL);
1084 favicon = (Bitmap) mMap.getParcelable(
1085 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001086 } else {
1087 thumbnail = null;
1088 favicon = null;
1089 }
1090
Ben Murdoch1794fe22009-09-29 18:14:30 +01001091 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001092 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1093 bundle.putString(BrowserContract.Bookmarks.URL, url);
1094 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001095
1096 if (mSaveToHomeScreen) {
1097 if (mTouchIconUrl != null && urlUnmodified) {
1098 Message msg = Message.obtain(mHandler,
1099 TOUCH_ICON_DOWNLOADED);
1100 msg.setData(bundle);
1101 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001102 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001103 icon.execute(mTouchIconUrl);
1104 } else {
1105 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1106 title, null /*touchIcon*/, favicon));
1107 }
1108 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001109 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1110 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Pankaj Garg21dad562015-07-02 17:17:24 -07001111 if (mTouchIconUrl != null) {
1112 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
1113 }
Leon Scroggins88d08032010-10-21 15:17:10 -04001114 // Post a message to write to the DB.
1115 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1116 msg.setData(bundle);
1117 // Start a new thread so as to not slow down the UI
1118 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1119 t.start();
1120 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001121 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001122 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001123 }
luxiaoldbe4a622013-07-19 17:14:06 +08001124 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001125 return true;
1126 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001127
John Reck2eec4c32011-05-11 15:55:32 -07001128 @Override
1129 public void onItemSelected(AdapterView<?> parent, View view, int position,
1130 long id) {
1131 if (mAccountSpinner == parent) {
1132 long root = mAccountAdapter.getItem(position).rootFolderId;
1133 if (root != mRootFolder) {
1134 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001135 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001136 }
1137 }
1138 }
1139
1140 @Override
1141 public void onNothingSelected(AdapterView<?> parent) {
1142 // Don't care
1143 }
1144
Leon Scroggins162f8352010-10-18 15:02:44 -04001145 /*
1146 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1147 */
1148 public static class CustomListView extends ListView {
1149 private EditText mEditText;
1150
1151 public void addEditText(EditText editText) {
1152 mEditText = editText;
1153 }
1154
1155 public CustomListView(Context context) {
1156 super(context);
1157 }
1158
1159 public CustomListView(Context context, AttributeSet attrs) {
1160 super(context, attrs);
1161 }
1162
1163 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1164 super(context, attrs, defStyle);
1165 }
1166
1167 @Override
1168 public boolean checkInputConnectionProxy(View view) {
1169 return view == mEditText;
1170 }
1171 }
John Reck2eec4c32011-05-11 15:55:32 -07001172
John Reck3bf3cfb2011-10-17 18:00:05 -07001173 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001174
1175 static final String[] PROJECTION = new String[] {
1176 Accounts.ACCOUNT_NAME,
1177 Accounts.ACCOUNT_TYPE,
1178 Accounts.ROOT_ID,
1179 };
1180
1181 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1182 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1183 static final int COLUMN_INDEX_ROOT_ID = 2;
1184
1185 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001186 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001187 }
1188
1189 }
1190
John Reck9b8cd1e2011-05-25 18:14:01 -07001191 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001192
1193 private String mLabel;
1194 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001195 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001196
1197 public BookmarkAccount(Context context, Cursor cursor) {
1198 accountName = cursor.getString(
1199 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1200 accountType = cursor.getString(
1201 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1202 rootFolderId = cursor.getLong(
1203 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1204 mLabel = accountName;
1205 if (TextUtils.isEmpty(mLabel)) {
1206 mLabel = context.getString(R.string.local_bookmarks);
1207 }
1208 }
1209
1210 @Override
1211 public String toString() {
1212 return mLabel;
1213 }
1214 }
1215
1216 static class EditBookmarkInfo {
1217 long id = -1;
1218 long parentId = -1;
1219 String parentTitle;
1220 String title;
1221 String accountName;
1222 String accountType;
1223
1224 long lastUsedId = -1;
1225 String lastUsedTitle;
1226 String lastUsedAccountName;
1227 String lastUsedAccountType;
1228 }
1229
1230 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1231
1232 private Context mContext;
1233 private Bundle mMap;
1234
1235 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1236 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001237 mContext = context.getApplicationContext();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001238 mMap = (bundle==null) ? new Bundle() : bundle;
John Reck2eec4c32011-05-11 15:55:32 -07001239 }
1240
1241 @Override
1242 public EditBookmarkInfo loadInBackground() {
1243 final ContentResolver cr = mContext.getContentResolver();
1244 EditBookmarkInfo info = new EditBookmarkInfo();
1245 Cursor c = null;
1246
1247 try {
1248 // First, let's lookup the bookmark (check for dupes, get needed info)
1249 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1250 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1251 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1252 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1253 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1254 new String[] { BrowserContract.Bookmarks._ID},
1255 BrowserContract.Bookmarks.URL + "=?",
1256 new String[] { url }, null);
1257 if (c.getCount() == 1 && c.moveToFirst()) {
1258 info.id = c.getLong(0);
1259 }
1260 c.close();
1261 }
1262 if (info.id != -1) {
1263 c = cr.query(ContentUris.withAppendedId(
1264 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1265 new String[] {
1266 BrowserContract.Bookmarks.PARENT,
1267 BrowserContract.Bookmarks.ACCOUNT_NAME,
1268 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1269 BrowserContract.Bookmarks.TITLE},
1270 null, null, null);
1271 if (c.moveToFirst()) {
1272 info.parentId = c.getLong(0);
1273 info.accountName = c.getString(1);
1274 info.accountType = c.getString(2);
1275 info.title = c.getString(3);
1276 }
1277 c.close();
1278 c = cr.query(ContentUris.withAppendedId(
1279 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1280 new String[] {
1281 BrowserContract.Bookmarks.TITLE,},
1282 null, null, null);
1283 if (c.moveToFirst()) {
1284 info.parentTitle = c.getString(0);
1285 }
1286 c.close();
1287 }
1288
1289 // Figure out the last used folder/account
1290 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1291 new String[] {
1292 BrowserContract.Bookmarks.PARENT,
1293 }, null, null,
1294 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1295 if (c.moveToFirst()) {
1296 long parent = c.getLong(0);
1297 c.close();
1298 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1299 new String[] {
1300 BrowserContract.Bookmarks.TITLE,
1301 BrowserContract.Bookmarks.ACCOUNT_NAME,
1302 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1303 BrowserContract.Bookmarks._ID + "=?", new String[] {
1304 Long.toString(parent)}, null);
1305 if (c.moveToFirst()) {
1306 info.lastUsedId = parent;
1307 info.lastUsedTitle = c.getString(0);
1308 info.lastUsedAccountName = c.getString(1);
1309 info.lastUsedAccountType = c.getString(2);
1310 }
1311 c.close();
1312 }
1313 } finally {
1314 if (c != null) {
1315 c.close();
1316 }
1317 }
1318
1319 return info;
1320 }
1321
1322 @Override
1323 protected void onStartLoading() {
1324 forceLoad();
1325 }
1326
1327 }
1328
The Android Open Source Project0c908882009-03-03 19:32:16 -08001329}