blob: 2c99b6e11f45ad7a68b00801a602c9530a35c611 [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;
Vivek Sekharb7b51df2016-02-06 20:55:00 +010061import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080062
Bijan Amirzada41242f22014-03-21 12:12:18 -070063import com.android.browser.BrowserUtils;
64import com.android.browser.R;
65import com.android.browser.addbookmark.FolderSpinner;
66import com.android.browser.addbookmark.FolderSpinnerAdapter;
67import com.android.browser.platformsupport.BrowserContract;
68import com.android.browser.platformsupport.WebAddress;
69import com.android.browser.platformsupport.BrowserContract.Accounts;
70import com.android.browser.reflect.ReflectHelper;
luxiaoldbe4a622013-07-19 17:14:06 +080071
Sagar Dhawanef06f562015-09-07 15:23:22 +020072import org.codeaurora.swe.util.SWEUrlUtils;
73
Cary Clarkf2407c62009-09-04 12:25:10 -040074import java.net.URI;
Vivek Sekharf95fb382014-07-23 11:13:44 -070075import java.net.URLEncoder;
Cary Clarkf2407c62009-09-04 12:25:10 -040076import java.net.URISyntaxException;
Vivek Sekharf95fb382014-07-23 11:13:44 -070077import java.io.UnsupportedEncodingException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040078
79public class AddBookmarkPage extends Activity
80 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040081 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
John Reck2eec4c32011-05-11 15:55:32 -070082 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener,
83 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
Michael Kolb370a4f32010-10-06 10:45:32 -070085 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040086 public static final String TOUCH_ICON_URL = "touch_icon_url";
87 // Place on an edited bookmark to remove the saved thumbnail
88 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
89 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050090 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070091
John Reckc8490812010-11-22 14:15:36 -080092 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
93 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
94
kaiyiz3d7e4d52013-09-17 17:56:27 +080095 private static final int MAX_CRUMBS_SHOWN = 1;
Leon Scroggins74dbe012010-10-15 10:54:27 -040096
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 private final String LOGTAG = "Bookmarks";
98
Leon Scroggins III052ce662010-09-13 14:44:16 -040099 // IDs for the CursorLoaders that are used.
John Reck2eec4c32011-05-11 15:55:32 -0700100 private final int LOADER_ID_ACCOUNTS = 0;
101 private final int LOADER_ID_FOLDER_CONTENTS = 1;
102 private final int LOADER_ID_EDIT_INFO = 2;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400103
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700104 final static int MAX_TITLE_LENGTH = 80;
105
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 private EditText mTitle;
107 private EditText mAddress;
jrizzoli38a81f62015-10-25 18:22:38 +0100108 private ImageButton mButton;
109 private ImageButton mCancelButton;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 private boolean mEditingExisting;
John Reckc8490812010-11-22 14:15:36 -0800111 private boolean mEditingFolder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -0400113 private String mTouchIconUrl;
Sagar Dhawanef06f562015-09-07 15:23:22 +0200114 private String mUrl;
Leon Scroggins504433a2011-01-13 12:26:52 -0500115 private FolderSpinner mFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400116 private View mDefaultView;
117 private View mFolderSelector;
118 private EditText mFolderNamer;
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500119 private View mFolderCancel;
Leon Scroggins162f8352010-10-18 15:02:44 -0400120 private boolean mIsFolderNamerShowing;
121 private View mFolderNamerHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400122 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400123 private View mAddSeparator;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500124 private long mCurrentFolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400125 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400126 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400127 private View mCrumbHolder;
Leon Scroggins162f8352010-10-18 15:02:44 -0400128 private CustomListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400129 private boolean mSaveToHomeScreen;
Leon Scroggins02081942010-11-01 17:52:42 -0400130 private long mRootFolder;
Leon Scroggins905250c2010-12-17 15:25:33 -0500131 private TextView mTopLevelLabel;
132 private Drawable mHeaderIcon;
jrizzoli38a81f62015-10-25 18:22:38 +0100133 private ImageButton mRemoveLink;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500134 private FolderSpinnerAdapter mFolderAdapter;
John Reck2eec4c32011-05-11 15:55:32 -0700135 private Spinner mAccountSpinner;
136 private ArrayAdapter<BookmarkAccount> mAccountAdapter;
luxiaoldbe4a622013-07-19 17:14:06 +0800137 // add for carrier which requires same title or address can not exist.
138 private long mDuplicateId;
139 private Context mDuplicateContext;
John Reck2eec4c32011-05-11 15:55:32 -0700140
Leon Scroggins III052ce662010-09-13 14:44:16 -0400141 private static class Folder {
142 String Name;
143 long Id;
144 Folder(String name, long id) {
145 Name = name;
146 Id = id;
147 }
148 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149
Ben Murdoch1794fe22009-09-29 18:14:30 +0100150 // Message IDs
151 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400152 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500153 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100154
155 private Handler mHandler;
156
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100157 private InputMethodManager getInputMethodManager() {
158 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
159 }
160
Leon Scroggins8baaa632010-12-08 19:46:53 -0500161 private Uri getUriForFolder(long folder) {
John Reck903a0722011-11-09 17:53:10 -0800162 BookmarkAccount account =
163 (BookmarkAccount) mAccountSpinner.getSelectedItem();
164 if (folder == mRootFolder && account != null) {
165 return BookmarksLoader.addAccount(
166 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
167 account.accountType, account.accountName);
168 }
John Reck2eec4c32011-05-11 15:55:32 -0700169 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500170 }
171
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800172 private String getNameFromId(long mCurrentFolder2) {
173 String title = "";
174 Cursor cursor = null;
175 try {
176 cursor = getApplicationContext().getContentResolver().query(
177 BrowserContract.Bookmarks.CONTENT_URI,
178 new String[] {
179 BrowserContract.Bookmarks.TITLE
180 },
181 BrowserContract.Bookmarks._ID + " = ? AND "
182 + BrowserContract.Bookmarks.IS_DELETED + " = ? AND "
183 + BrowserContract.Bookmarks.IS_FOLDER + " = ? ", new String[] {
184 String.valueOf(mCurrentFolder2), 0 + "", 1 + ""
185 }, null);
186 if (cursor != null && cursor.getCount() != 0) {
187 while (cursor.moveToNext()) {
188 title = cursor.getString(0);
189 }
190 }
191 } finally {
192 if (cursor != null) {
193 cursor.close();
194 }
195 }
196 return title;
197 }
198
Leon Scroggins III052ce662010-09-13 14:44:16 -0400199 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700200 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400201 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400202 Folder folderData = (Folder) data;
203 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400204 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700205 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400206 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500207 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400208 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400209 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400210 completeOrCancelFolderNaming(true);
211 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500212 setShowBookmarkIcon(level == 1);
213 }
214
215 /**
216 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
217 * @param show True if the icon should visible, false otherwise.
218 */
219 private void setShowBookmarkIcon(boolean show) {
220 Drawable drawable = show ? mHeaderIcon: null;
221 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400222 }
223
Leon Scroggins74dbe012010-10-15 10:54:27 -0400224 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400225 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
226 if (v == mFolderNamer) {
227 if (v.getText().length() > 0) {
228 if (actionId == EditorInfo.IME_NULL) {
229 // Only want to do this once.
230 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400231 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400232 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400233 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 }
Michael Kolb31829b92010-10-01 11:50:21 -0700235 // Steal the key press; otherwise a newline will be added
Vivek Sekhar5e631472014-03-31 23:59:10 -0700236 // return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800237 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400238 return false;
239 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400241 private void switchToDefaultView(boolean changedFolder) {
242 mFolderSelector.setVisibility(View.GONE);
243 mDefaultView.setVisibility(View.VISIBLE);
244 mCrumbHolder.setVisibility(View.GONE);
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 }
jrizzoli38a81f62015-10-25 18:22:38 +0100318 } else if (v == mRemoveLink) {
Pankaj Garg634bf432015-07-13 09:54:21 -0700319 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 Scroggins7e5f7352010-10-18 13:25:31 -0400419 mAddNewFolder.setVisibility(View.VISIBLE);
420 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700421 getInputMethodManager().hideSoftInputFromWindow(
422 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400423 }
424
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700425 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700426 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400427 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400428 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700429 }
430 }
431
John Reck2eec4c32011-05-11 15:55:32 -0700432 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
433 new LoaderCallbacks<EditBookmarkInfo>() {
434
435 @Override
436 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
437 // Don't care
438 }
439
440 @Override
441 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
442 EditBookmarkInfo info) {
443 boolean setAccount = false;
444 if (info.id != -1) {
445 mEditingExisting = true;
John Reck2eec4c32011-05-11 15:55:32 -0700446 mTitle.setText(info.title);
447 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
448 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
449 setAccount = true;
450 setAccount(info.accountName, info.accountType);
451 mCurrentFolder = info.parentId;
452 onCurrentFolderFound();
453 }
John Reck37894a92011-05-13 12:47:53 -0700454 // TODO: Detect if lastUsedId is a subfolder of info.id in the
455 // editing folder case. For now, just don't show the last used
456 // folder at all to prevent any chance of the user adding a parent
457 // folder to a child folder
458 if (info.lastUsedId != -1 && info.lastUsedId != info.id
459 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700460 if (setAccount && info.lastUsedId != mRootFolder
461 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
462 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
463 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
464 } else if (!setAccount) {
465 setAccount = true;
466 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
467 if (info.lastUsedId != mRootFolder) {
468 mFolderAdapter.addRecentFolder(info.lastUsedId,
469 info.lastUsedTitle);
470 }
471 }
472 }
473 if (!setAccount) {
474 mAccountSpinner.setSelection(0);
475 }
476 }
477
478 @Override
479 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
480 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
481 }
482 };
483
484 void setAccount(String accountName, String accountType) {
485 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
486 BookmarkAccount account = mAccountAdapter.getItem(i);
487 if (TextUtils.equals(account.accountName, accountName)
488 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700489 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800490 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700491 return;
492 }
493 }
494 }
495
Leon Scroggins III052ce662010-09-13 14:44:16 -0400496 @Override
497 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
498 String[] projection;
499 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700500 case LOADER_ID_ACCOUNTS:
501 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400502 case LOADER_ID_FOLDER_CONTENTS:
503 projection = new String[] {
504 BrowserContract.Bookmarks._ID,
505 BrowserContract.Bookmarks.TITLE,
506 BrowserContract.Bookmarks.IS_FOLDER
507 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500508 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700509 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500510 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700511 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
512 whereArgs = new String[] { Long.toString(mMap.getLong(
513 BrowserContract.Bookmarks._ID)) };
514 }
515 long currentFolder;
516 Object data = mCrumbs.getTopData();
517 if (data != null) {
518 currentFolder = ((Folder) data).Id;
519 } else {
520 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500521 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400522 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700523 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400524 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500525 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700526 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500527 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400528 default:
529 throw new AssertionError("Asking for nonexistant loader!");
530 }
531 }
532
533 @Override
534 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
535 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700536 case LOADER_ID_ACCOUNTS:
537 mAccountAdapter.clear();
538 while (cursor.moveToNext()) {
539 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500540 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700541
542 if (cursor.getCount() < 2) {
543 View accountView = findViewById(R.id.row_account);
544 if (accountView != null) {
545 accountView.setVisibility(View.GONE);
546 }
547 }
548
John Reck2eec4c32011-05-11 15:55:32 -0700549 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
550 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
551 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500552 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400553 case LOADER_ID_FOLDER_CONTENTS:
554 mAdapter.changeCursor(cursor);
555 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400556 }
557 }
558
Dianne Hackborn39772c82010-12-16 00:43:54 -0800559 public void onLoaderReset(Loader<Cursor> loader) {
560 switch (loader.getId()) {
561 case LOADER_ID_FOLDER_CONTENTS:
562 mAdapter.changeCursor(null);
563 break;
564 }
565 }
566
Leon Scroggins02081942010-11-01 17:52:42 -0400567 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500568 * Move cursor to the position that has folderToFind as its "_id".
569 * @param cursor Cursor containing folders in the bookmarks database
570 * @param folderToFind "_id" of the folder to move to.
571 * @param idIndex Index in cursor of "_id"
572 * @throws AssertionError if cursor is empty or there is no row with folderToFind
573 * as its "_id".
574 */
575 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
576 throws AssertionError {
577 if (!cursor.moveToFirst()) {
578 throw new AssertionError("No folders in the database!");
579 }
580 long folder;
581 do {
582 folder = cursor.getLong(idIndex);
583 } while (folder != folderToFind && cursor.moveToNext());
584 if (cursor.isAfterLast()) {
585 throw new AssertionError("Folder(id=" + folderToFind
586 + ") holding this bookmark does not exist!");
587 }
588 }
589
Leon Scroggins III052ce662010-09-13 14:44:16 -0400590 @Override
591 public void onItemClick(AdapterView<?> parent, View view, int position,
592 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400593 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400594 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400595 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400596 }
597
Leon Scroggins162f8352010-10-18 15:02:44 -0400598 private void setShowFolderNamer(boolean show) {
599 if (show != mIsFolderNamerShowing) {
600 mIsFolderNamerShowing = show;
601 if (show) {
602 // Set the selection to the folder namer so it will be in
603 // view.
604 mListView.addFooterView(mFolderNamerHolder);
605 } else {
606 mListView.removeFooterView(mFolderNamerHolder);
607 }
608 // Refresh the list.
609 mListView.setAdapter(mAdapter);
610 if (show) {
611 mListView.setSelection(mListView.getCount() - 1);
612 }
613 }
614 }
615
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 /**
617 * Shows a list of names of folders.
618 */
619 private class FolderAdapter extends CursorAdapter {
620 public FolderAdapter(Context context) {
621 super(context, null);
622 }
623
624 @Override
625 public void bindView(View view, Context context, Cursor cursor) {
626 ((TextView) view.findViewById(android.R.id.text1)).setText(
627 cursor.getString(cursor.getColumnIndexOrThrow(
628 BrowserContract.Bookmarks.TITLE)));
629 }
630
631 @Override
632 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700633 View view = LayoutInflater.from(context).inflate(
634 R.layout.folder_list_item, null);
635 view.setBackgroundDrawable(context.getResources().
636 getDrawable(android.R.drawable.list_selector_background));
637 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400638 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400639
640 @Override
641 public boolean isEmpty() {
642 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400643 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400644 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400645 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800646
Leon Scrogginsc1129902010-12-08 15:28:33 -0500647 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 protected void onCreate(Bundle icicle) {
Vivek Sekharb7b51df2016-02-06 20:55:00 +0100649 if (!EngineInitializer.isInitialized()) {
650 Log.e(LOGTAG, "Engine not Initialized");
651 EngineInitializer.initializeSync((Context) getApplicationContext());
652 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500654 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100655
656 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100657
Leon Scroggins III052ce662010-09-13 14:44:16 -0400658 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100659
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400660 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700661
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 String title = null;
663 String url = null;
Pankaj Garg21dad562015-07-02 17:17:24 -0700664 mTouchIconUrl = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100665
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800667 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
Sagar Dhawanef06f562015-09-07 15:23:22 +0200668 boolean existing = mMap.getBoolean(CHECK_FOR_DUPE, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800670 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 mMap = b;
672 mEditingExisting = true;
John Reckc8490812010-11-22 14:15:36 -0800673 if (mEditingFolder) {
674 findViewById(R.id.row_address).setVisibility(View.GONE);
675 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400676 } else {
677 int gravity = mMap.getInt("gravity", -1);
678 if (gravity != -1) {
679 WindowManager.LayoutParams l = window.getAttributes();
680 l.gravity = gravity;
681 window.setAttributes(l);
682 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800683 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400684 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
Sagar Dhawanef06f562015-09-07 15:23:22 +0200685 url = mUrl = UrlUtils.fixUpUrl(mMap.getString(BrowserContract.Bookmarks.URL));
Leon Scrogginsbc922852010-10-22 12:15:27 -0400686 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700687 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800688
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800689 // Check if title is not empty to prevent NPE
690 if (!TextUtils.isEmpty(title)) {
691 if (title.length() > MAX_TITLE_LENGTH) {
692 title = title.substring(0, MAX_TITLE_LENGTH);
693 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800694 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700695
Leon Scroggins25230d72010-09-28 20:09:25 -0400696 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800697
698 mTitle = (EditText) findViewById(R.id.title);
699 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700700 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100701
Leon Scroggins III052ce662010-09-13 14:44:16 -0400702 mAddress = (EditText) findViewById(R.id.address);
703 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800704 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705
jrizzoli38a81f62015-10-25 18:22:38 +0100706 mButton = (ImageButton) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400707 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800708
jrizzoli38a81f62015-10-25 18:22:38 +0100709 mCancelButton = (ImageButton) findViewById(R.id.book_cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400710 mCancelButton.setOnClickListener(this);
711
Leon Scroggins504433a2011-01-13 12:26:52 -0500712 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700713 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500714 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500715 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400716
717 mDefaultView = findViewById(R.id.default_view);
718 mFolderSelector = findViewById(R.id.folder_selector);
719
Leon Scroggins162f8352010-10-18 15:02:44 -0400720 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
721 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400722 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800723
724 // add for carrier test about warning limit of edit text
725 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
726 BrowserUtils.FILENAME_MAX_LENGTH);
727
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500728 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
729 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400730
731 mAddNewFolder = findViewById(R.id.add_new_folder);
732 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400733 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400734
Leon Scroggins74dbe012010-10-15 10:54:27 -0400735 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
Pankaj Garg634bf432015-07-13 09:54:21 -0700736 //mCrumbs.setUseBackButton(true);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400737 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800738 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400739 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800740 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400741
Leon Scroggins III052ce662010-09-13 14:44:16 -0400742 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400743 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400744 View empty = findViewById(R.id.empty);
745 mListView.setEmptyView(empty);
746 mListView.setAdapter(mAdapter);
747 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400748 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500749
John Reck2eec4c32011-05-11 15:55:32 -0700750 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
751 android.R.layout.simple_spinner_item);
752 mAccountAdapter.setDropDownViewResource(
753 android.R.layout.simple_spinner_dropdown_item);
754 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
755 mAccountSpinner.setAdapter(mAccountAdapter);
756 mAccountSpinner.setOnItemSelectedListener(this);
Sagar Dhawan7e9d2472015-07-20 10:39:40 -0700757 mFolder.setSelectionIgnoringSelectionChange(1); // Select Bookmarks by default
John Reck2eec4c32011-05-11 15:55:32 -0700758
Leon Scroggins504433a2011-01-13 12:26:52 -0500759 if (!window.getDecorView().isInTouchMode()) {
760 mButton.requestFocus();
761 }
762
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800763 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
764 mFolder.setSelectionIgnoringSelectionChange(2);
765 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
766 }
767
John Reck2eec4c32011-05-11 15:55:32 -0700768 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500769 }
770
771 // Called once we have determined which folder is the root folder
772 private void onRootFolderFound(long root) {
773 mRootFolder = root;
Pankaj Garg63396942015-03-06 10:05:25 -0800774 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500775 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700776 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500777 }
778
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500779 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700780 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500781 String name = getString(R.string.bookmarks);
782 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
783 new Folder(name, mRootFolder));
784 // To better match the other folders.
785 mTopLevelLabel.setCompoundDrawablePadding(6);
786 }
787
Leon Scroggins504433a2011-01-13 12:26:52 -0500788 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400789 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500790 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500791 // Since we're not in the root folder, change the selection to other
792 // folder now. The text will get changed once we select the correct
793 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500794 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500795 } else {
796 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500797 if (!mEditingFolder) {
798 // Initially the "Bookmarks" folder should be showing, rather than
799 // the home screen. In the editing folder case, home screen is not
800 // an option, so "Bookmarks" folder is already at the top.
801 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
802 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400803 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400804 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700805 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500806 }
807
Leon Scroggins02065b02010-01-04 14:30:13 -0500808 /**
809 * Runnable to save a bookmark, so it can be performed in its own thread.
810 */
811 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400812 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500813 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200814 private Context mContext;
815 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100816 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500817 mMessage = msg;
818 }
819 public void run() {
820 // Unbundle bookmark data.
821 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400822 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
823 String url = bundle.getString(BrowserContract.Bookmarks.URL);
824 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500825 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700826 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400827 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500828
829 // Save to the bookmarks DB.
830 try {
831 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400832 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700833 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500834 if (touchIconUrl != null) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700835 new DownloadTouchIcon(mContext, cr, url).execute(touchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500836 }
837 mMessage.arg1 = 1;
838 } catch (IllegalStateException e) {
839 mMessage.arg1 = 0;
840 }
841 mMessage.sendToTarget();
842 }
843 }
844
John Reckc8490812010-11-22 14:15:36 -0800845 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
846 Context mContext;
847 Long mId;
848
849 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100850 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800851 mId = id;
852 }
853
854 @Override
855 protected Void doInBackground(ContentValues... params) {
856 if (params.length != 1) {
857 throw new IllegalArgumentException("No ContentValues provided!");
858 }
859 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
860 mContext.getContentResolver().update(
861 uri,
862 params[0], null, null);
863 return null;
864 }
865 }
866
Ben Murdoch1794fe22009-09-29 18:14:30 +0100867 private void createHandler() {
868 if (mHandler == null) {
869 mHandler = new Handler() {
870 @Override
871 public void handleMessage(Message msg) {
872 switch (msg.what) {
873 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500874 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100875 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
876 Toast.LENGTH_LONG).show();
877 } else {
878 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
879 Toast.LENGTH_LONG).show();
880 }
881 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400882 case TOUCH_ICON_DOWNLOADED:
883 Bundle b = msg.getData();
884 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400885 AddBookmarkPage.this,
886 b.getString(BrowserContract.Bookmarks.URL),
887 b.getString(BrowserContract.Bookmarks.TITLE),
888 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
889 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400890 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500891 case BOOKMARK_DELETED:
892 finish();
893 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100894 }
895 }
896 };
897 }
898 }
899
luxiaoldbe4a622013-07-19 17:14:06 +0800900 static void deleteDuplicateBookmark(final Context context, final long id) {
901 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
902 context.getContentResolver().delete(uri, null, null);
903 }
904
Pankaj Garg634bf432015-07-13 09:54:21 -0700905 private void onDeleteWithConfirm() {
906 final String title = mTitle.getText().toString().trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +0200907 final String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
Pankaj Garg634bf432015-07-13 09:54:21 -0700908 final String url = unfilteredUrl.trim();
909 new AlertDialog.Builder(this)
910 .setIconAttribute(android.R.attr.alertDialogIcon)
911 .setMessage(getString(R.string.delete_bookmark_warning, title))
912 .setNegativeButton(android.R.string.cancel, null)
913 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
914 public void onClick(DialogInterface dialog, int which) {
915 ContentResolver cr = getContentResolver();
916 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
917 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +0200918 "url = ?",
Pankaj Garg634bf432015-07-13 09:54:21 -0700919 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +0200920 url
Pankaj Garg634bf432015-07-13 09:54:21 -0700921 },
922 null);
923
924 if (cursor == null) {
925 finish();
926 return;
927 }
928
929 try {
930 if (cursor.moveToFirst()) {
931 do {
932 long index = cursor.getLong(
933 cursor.getColumnIndex(BrowserContract.Bookmarks._ID));
934 cr.delete(ContentUris.withAppendedId(
935 BrowserContract.Bookmarks.CONTENT_URI, index),
936 null, null);
937 } while (cursor.moveToNext());
938 }
939 } catch (IllegalStateException e) {
940 e.printStackTrace();
941 } finally {
942 if (cursor != null)
943 cursor.close();
944 }
945 finish();
946 }
947 })
948 .show();
949 }
950
luxiaoldbe4a622013-07-19 17:14:06 +0800951 private void onSaveWithConfirm() {
Sagar Dhawanef06f562015-09-07 15:23:22 +0200952 String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
luxiaoldbe4a622013-07-19 17:14:06 +0800953 String url = unfilteredUrl.trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +0200954 Long id = (mMap == null) ?
955 -1 :
956 mMap.getLong(BrowserContract.Bookmarks._ID);
luxiaoldbe4a622013-07-19 17:14:06 +0800957 int duplicateCount;
958 final ContentResolver cr = getContentResolver();
959
960 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
961 BookmarksLoader.PROJECTION,
Sagar Dhawan05f51cc2015-10-14 10:57:44 -0700962 "(url = ? OR title = ?) AND parent = ?",
luxiaoldbe4a622013-07-19 17:14:06 +0800963 new String[] {
Sagar Dhawan05f51cc2015-10-14 10:57:44 -0700964 url, mTitle.getText().toString().trim(), Long.toString(mCurrentFolder)
luxiaoldbe4a622013-07-19 17:14:06 +0800965 },
966 null);
967
968 if (cursor == null) {
969 save();
970 return;
971 }
972
973 duplicateCount = cursor.getCount();
974 if (duplicateCount <= 0) {
975 cursor.close();
976 save();
977 return;
978 } else {
979 try {
980 while (cursor.moveToNext()) {
981 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
982 mDuplicateContext = AddBookmarkPage.this;
983 }
984 } catch (IllegalStateException e) {
985 e.printStackTrace();
986 } finally {
987 if (cursor != null)
988 cursor.close();
989 }
990 }
991
992 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
993 save();
994 return;
995 }
996
997 new AlertDialog.Builder(this)
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700998 .setTitle(getString(R.string.save_to_bookmarks))
luxiaoldbe4a622013-07-19 17:14:06 +0800999 .setMessage(getString(R.string.overwrite_bookmark_msg))
1000 .setNegativeButton(android.R.string.cancel, null)
1001 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1002 public void onClick(DialogInterface dialog, int which) {
1003 if (mDuplicateContext == null) {
1004 return;
1005 }
1006 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
1007 save();
1008 }
1009 })
1010 .show();
1011 }
1012
The Android Open Source Project0c908882009-03-03 19:32:16 -08001013 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +01001014 * 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 -08001015 */
1016 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +01001017 createHandler();
1018
The Android Open Source Project0c908882009-03-03 19:32:16 -08001019 String title = mTitle.getText().toString().trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001020 String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +01001021
The Android Open Source Project0c908882009-03-03 19:32:16 -08001022 boolean emptyTitle = title.length() == 0;
1023 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
1024 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -08001025 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001026 if (emptyTitle) {
1027 mTitle.setError(r.getText(R.string.bookmark_needs_title));
1028 }
1029 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -04001030 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001031 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -04001032 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001033 }
Ben Murdochca12cfa2009-11-17 13:57:44 +00001034 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -08001035 if (!mEditingFolder) {
1036 try {
1037 // We allow bookmarks with a javascript: scheme, but these will in most cases
1038 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +00001039
John Reckc8490812010-11-22 14:15:36 -08001040 if (!url.toLowerCase().startsWith("javascript:")) {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001041 URI uriObj = new URI(url);
John Reckc8490812010-11-22 14:15:36 -08001042 String scheme = uriObj.getScheme();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001043 if (scheme == null) { // SWE will allow bookmarking any scheme
John Reckc8490812010-11-22 14:15:36 -08001044 WebAddress address;
1045 try {
1046 address = new WebAddress(unfilteredUrl);
1047 } catch (ParseException e) {
1048 throw new URISyntaxException("", "");
1049 }
1050 if (address.getHost().length() == 0) {
1051 throw new URISyntaxException("", "");
1052 }
1053 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001054 }
Ben Murdochde353622009-10-12 10:29:00 +01001055 }
John Reckc8490812010-11-22 14:15:36 -08001056 } catch (URISyntaxException e) {
1057 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1058 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001059 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001060 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001061
Leon Scroggins88d08032010-10-21 15:17:10 -04001062 if (mSaveToHomeScreen) {
1063 mEditingExisting = false;
1064 }
1065
Sagar Dhawanef06f562015-09-07 15:23:22 +02001066 boolean urlUnmodified = url.equals(mUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001067
Ben Murdoch1794fe22009-09-29 18:14:30 +01001068 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001069 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1070 ContentValues values = new ContentValues();
1071 values.put(BrowserContract.Bookmarks.TITLE, title);
1072 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1073 if (!mEditingFolder) {
1074 values.put(BrowserContract.Bookmarks.URL, url);
1075 if (!urlUnmodified) {
1076 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1077 }
1078 }
1079 if (values.size() > 0) {
1080 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1081 }
1082 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001083 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001084 Bitmap thumbnail;
1085 Bitmap favicon;
1086 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001087 thumbnail = (Bitmap) mMap.getParcelable(
1088 BrowserContract.Bookmarks.THUMBNAIL);
1089 favicon = (Bitmap) mMap.getParcelable(
1090 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001091 } else {
1092 thumbnail = null;
1093 favicon = null;
1094 }
1095
Ben Murdoch1794fe22009-09-29 18:14:30 +01001096 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001097 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1098 bundle.putString(BrowserContract.Bookmarks.URL, url);
1099 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001100
1101 if (mSaveToHomeScreen) {
1102 if (mTouchIconUrl != null && urlUnmodified) {
1103 Message msg = Message.obtain(mHandler,
1104 TOUCH_ICON_DOWNLOADED);
1105 msg.setData(bundle);
1106 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001107 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001108 icon.execute(mTouchIconUrl);
1109 } else {
1110 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1111 title, null /*touchIcon*/, favicon));
1112 }
1113 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001114 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1115 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Pankaj Garg21dad562015-07-02 17:17:24 -07001116 if (mTouchIconUrl != null) {
1117 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
1118 }
Leon Scroggins88d08032010-10-21 15:17:10 -04001119 // Post a message to write to the DB.
1120 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1121 msg.setData(bundle);
1122 // Start a new thread so as to not slow down the UI
1123 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1124 t.start();
1125 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001126 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001127 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001128 }
luxiaoldbe4a622013-07-19 17:14:06 +08001129 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001130 return true;
1131 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001132
John Reck2eec4c32011-05-11 15:55:32 -07001133 @Override
1134 public void onItemSelected(AdapterView<?> parent, View view, int position,
1135 long id) {
1136 if (mAccountSpinner == parent) {
1137 long root = mAccountAdapter.getItem(position).rootFolderId;
1138 if (root != mRootFolder) {
1139 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001140 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001141 }
1142 }
1143 }
1144
1145 @Override
1146 public void onNothingSelected(AdapterView<?> parent) {
1147 // Don't care
1148 }
1149
Leon Scroggins162f8352010-10-18 15:02:44 -04001150 /*
1151 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1152 */
1153 public static class CustomListView extends ListView {
1154 private EditText mEditText;
1155
1156 public void addEditText(EditText editText) {
1157 mEditText = editText;
1158 }
1159
1160 public CustomListView(Context context) {
1161 super(context);
1162 }
1163
1164 public CustomListView(Context context, AttributeSet attrs) {
1165 super(context, attrs);
1166 }
1167
1168 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1169 super(context, attrs, defStyle);
1170 }
1171
1172 @Override
1173 public boolean checkInputConnectionProxy(View view) {
1174 return view == mEditText;
1175 }
1176 }
John Reck2eec4c32011-05-11 15:55:32 -07001177
John Reck3bf3cfb2011-10-17 18:00:05 -07001178 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001179
1180 static final String[] PROJECTION = new String[] {
1181 Accounts.ACCOUNT_NAME,
1182 Accounts.ACCOUNT_TYPE,
1183 Accounts.ROOT_ID,
1184 };
1185
1186 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1187 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1188 static final int COLUMN_INDEX_ROOT_ID = 2;
1189
1190 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001191 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001192 }
1193
1194 }
1195
John Reck9b8cd1e2011-05-25 18:14:01 -07001196 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001197
1198 private String mLabel;
1199 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001200 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001201
1202 public BookmarkAccount(Context context, Cursor cursor) {
1203 accountName = cursor.getString(
1204 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1205 accountType = cursor.getString(
1206 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1207 rootFolderId = cursor.getLong(
1208 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1209 mLabel = accountName;
1210 if (TextUtils.isEmpty(mLabel)) {
1211 mLabel = context.getString(R.string.local_bookmarks);
1212 }
1213 }
1214
1215 @Override
1216 public String toString() {
1217 return mLabel;
1218 }
1219 }
1220
1221 static class EditBookmarkInfo {
1222 long id = -1;
1223 long parentId = -1;
1224 String parentTitle;
1225 String title;
1226 String accountName;
1227 String accountType;
1228
1229 long lastUsedId = -1;
1230 String lastUsedTitle;
1231 String lastUsedAccountName;
1232 String lastUsedAccountType;
1233 }
1234
1235 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1236
1237 private Context mContext;
1238 private Bundle mMap;
1239
1240 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1241 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001242 mContext = context.getApplicationContext();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001243 mMap = (bundle==null) ? new Bundle() : bundle;
John Reck2eec4c32011-05-11 15:55:32 -07001244 }
1245
1246 @Override
1247 public EditBookmarkInfo loadInBackground() {
1248 final ContentResolver cr = mContext.getContentResolver();
1249 EditBookmarkInfo info = new EditBookmarkInfo();
1250 Cursor c = null;
1251
1252 try {
1253 // First, let's lookup the bookmark (check for dupes, get needed info)
1254 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1255 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1256 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1257 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1258 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1259 new String[] { BrowserContract.Bookmarks._ID},
1260 BrowserContract.Bookmarks.URL + "=?",
1261 new String[] { url }, null);
1262 if (c.getCount() == 1 && c.moveToFirst()) {
1263 info.id = c.getLong(0);
1264 }
1265 c.close();
1266 }
1267 if (info.id != -1) {
1268 c = cr.query(ContentUris.withAppendedId(
1269 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1270 new String[] {
1271 BrowserContract.Bookmarks.PARENT,
1272 BrowserContract.Bookmarks.ACCOUNT_NAME,
1273 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1274 BrowserContract.Bookmarks.TITLE},
1275 null, null, null);
1276 if (c.moveToFirst()) {
1277 info.parentId = c.getLong(0);
1278 info.accountName = c.getString(1);
1279 info.accountType = c.getString(2);
1280 info.title = c.getString(3);
1281 }
1282 c.close();
1283 c = cr.query(ContentUris.withAppendedId(
1284 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1285 new String[] {
1286 BrowserContract.Bookmarks.TITLE,},
1287 null, null, null);
1288 if (c.moveToFirst()) {
1289 info.parentTitle = c.getString(0);
1290 }
1291 c.close();
1292 }
1293
1294 // Figure out the last used folder/account
1295 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1296 new String[] {
1297 BrowserContract.Bookmarks.PARENT,
1298 }, null, null,
1299 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1300 if (c.moveToFirst()) {
1301 long parent = c.getLong(0);
1302 c.close();
1303 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1304 new String[] {
1305 BrowserContract.Bookmarks.TITLE,
1306 BrowserContract.Bookmarks.ACCOUNT_NAME,
1307 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1308 BrowserContract.Bookmarks._ID + "=?", new String[] {
1309 Long.toString(parent)}, null);
1310 if (c.moveToFirst()) {
1311 info.lastUsedId = parent;
1312 info.lastUsedTitle = c.getString(0);
1313 info.lastUsedAccountName = c.getString(1);
1314 info.lastUsedAccountType = c.getString(2);
1315 }
1316 c.close();
1317 }
1318 } finally {
1319 if (c != null) {
1320 c.close();
1321 }
1322 }
1323
1324 return info;
1325 }
1326
1327 @Override
1328 protected void onStartLoading() {
1329 forceLoad();
1330 }
1331
1332 }
1333
The Android Open Source Project0c908882009-03-03 19:32:16 -08001334}