blob: 0ddcc7686094889d6ac0162bad0372a2d5826dda [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
19import android.app.Activity;
luxiaoldbe4a622013-07-19 17:14:06 +080020import android.app.AlertDialog;
Leon Scroggins III052ce662010-09-13 14:44:16 -040021import android.app.LoaderManager;
John Reck2eec4c32011-05-11 15:55:32 -070022import android.app.LoaderManager.LoaderCallbacks;
23import android.content.AsyncTaskLoader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070025import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040026import android.content.ContentValues;
27import android.content.Context;
28import android.content.CursorLoader;
luxiaoldbe4a622013-07-19 17:14:06 +080029import android.content.DialogInterface;
Leon Scroggins III052ce662010-09-13 14:44:16 -040030import android.content.Loader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040032import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010033import android.graphics.Bitmap;
Leon Scroggins02081942010-11-01 17:52:42 -040034import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070036import android.net.Uri;
John Reckc8490812010-11-22 14:15:36 -080037import android.os.AsyncTask;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010039import android.os.Handler;
40import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040041import android.text.TextUtils;
Leon Scroggins162f8352010-10-18 15:02:44 -040042import android.util.AttributeSet;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.view.KeyEvent;
44import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040046import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040048import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
51import android.widget.AdapterView;
John Reck2eec4c32011-05-11 15:55:32 -070052import android.widget.AdapterView.OnItemSelectedListener;
53import android.widget.ArrayAdapter;
Leon Scroggins III052ce662010-09-13 14:44:16 -040054import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040056import android.widget.ListView;
John Reck2eec4c32011-05-11 15:55:32 -070057import android.widget.Spinner;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.widget.TextView;
59import android.widget.Toast;
60
Bijan Amirzada41242f22014-03-21 12:12:18 -070061import com.android.browser.BrowserUtils;
62import com.android.browser.R;
63import com.android.browser.addbookmark.FolderSpinner;
64import com.android.browser.addbookmark.FolderSpinnerAdapter;
65import com.android.browser.platformsupport.BrowserContract;
66import com.android.browser.platformsupport.WebAddress;
67import com.android.browser.platformsupport.BrowserContract.Accounts;
68import com.android.browser.reflect.ReflectHelper;
luxiaoldbe4a622013-07-19 17:14:06 +080069
Sagar Dhawanef06f562015-09-07 15:23:22 +020070import org.codeaurora.swe.util.SWEUrlUtils;
71
Cary Clarkf2407c62009-09-04 12:25:10 -040072import java.net.URI;
Vivek Sekharf95fb382014-07-23 11:13:44 -070073import java.net.URLEncoder;
Cary Clarkf2407c62009-09-04 12:25:10 -040074import java.net.URISyntaxException;
Vivek Sekharf95fb382014-07-23 11:13:44 -070075import java.io.UnsupportedEncodingException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040076
77public class AddBookmarkPage extends Activity
78 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040079 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
John Reck2eec4c32011-05-11 15:55:32 -070080 BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener,
81 OnItemSelectedListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080082
Michael Kolb370a4f32010-10-06 10:45:32 -070083 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040084 public static final String TOUCH_ICON_URL = "touch_icon_url";
85 // Place on an edited bookmark to remove the saved thumbnail
86 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
87 public static final String USER_AGENT = "user_agent";
Leon Scrogginsbdff8a72011-02-11 15:49:04 -050088 public static final String CHECK_FOR_DUPE = "check_for_dupe";
Michael Kolb370a4f32010-10-06 10:45:32 -070089
John Reckc8490812010-11-22 14:15:36 -080090 /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark";
91 /* package */ static final String EXTRA_IS_FOLDER = "is_folder";
92
kaiyiz3d7e4d52013-09-17 17:56:27 +080093 private static final int MAX_CRUMBS_SHOWN = 1;
Leon Scroggins74dbe012010-10-15 10:54:27 -040094
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 private final String LOGTAG = "Bookmarks";
96
Leon Scroggins III052ce662010-09-13 14:44:16 -040097 // IDs for the CursorLoaders that are used.
John Reck2eec4c32011-05-11 15:55:32 -070098 private final int LOADER_ID_ACCOUNTS = 0;
99 private final int LOADER_ID_FOLDER_CONTENTS = 1;
100 private final int LOADER_ID_EDIT_INFO = 2;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400101
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700102 final static int MAX_TITLE_LENGTH = 80;
103
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 private EditText mTitle;
105 private EditText mAddress;
106 private TextView mButton;
107 private View mCancelButton;
Pankaj Garg634bf432015-07-13 09:54:21 -0700108 private View mDeleteButton;
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 TextView mFakeTitle;
127 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;
Leon Scroggins75630672011-01-13 17:56:15 -0500133 private View mRemoveLink;
134 private View mFakeTitleHolder;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500135 private FolderSpinnerAdapter mFolderAdapter;
John Reck2eec4c32011-05-11 15:55:32 -0700136 private Spinner mAccountSpinner;
137 private ArrayAdapter<BookmarkAccount> mAccountAdapter;
luxiaoldbe4a622013-07-19 17:14:06 +0800138 // add for carrier which requires same title or address can not exist.
139 private long mDuplicateId;
140 private Context mDuplicateContext;
John Reck2eec4c32011-05-11 15:55:32 -0700141
Leon Scroggins III052ce662010-09-13 14:44:16 -0400142 private static class Folder {
143 String Name;
144 long Id;
145 Folder(String name, long id) {
146 Name = name;
147 Id = id;
148 }
149 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150
Ben Murdoch1794fe22009-09-29 18:14:30 +0100151 // Message IDs
152 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400153 private static final int TOUCH_ICON_DOWNLOADED = 101;
Leon Scroggins75630672011-01-13 17:56:15 -0500154 private static final int BOOKMARK_DELETED = 102;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100155
156 private Handler mHandler;
157
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100158 private InputMethodManager getInputMethodManager() {
159 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
160 }
161
Leon Scroggins8baaa632010-12-08 19:46:53 -0500162 private Uri getUriForFolder(long folder) {
John Reck903a0722011-11-09 17:53:10 -0800163 BookmarkAccount account =
164 (BookmarkAccount) mAccountSpinner.getSelectedItem();
165 if (folder == mRootFolder && account != null) {
166 return BookmarksLoader.addAccount(
167 BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER,
168 account.accountType, account.accountName);
169 }
John Reck2eec4c32011-05-11 15:55:32 -0700170 return BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500171 }
172
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800173 private String getNameFromId(long mCurrentFolder2) {
174 String title = "";
175 Cursor cursor = null;
176 try {
177 cursor = getApplicationContext().getContentResolver().query(
178 BrowserContract.Bookmarks.CONTENT_URI,
179 new String[] {
180 BrowserContract.Bookmarks.TITLE
181 },
182 BrowserContract.Bookmarks._ID + " = ? AND "
183 + BrowserContract.Bookmarks.IS_DELETED + " = ? AND "
184 + BrowserContract.Bookmarks.IS_FOLDER + " = ? ", new String[] {
185 String.valueOf(mCurrentFolder2), 0 + "", 1 + ""
186 }, null);
187 if (cursor != null && cursor.getCount() != 0) {
188 while (cursor.moveToNext()) {
189 title = cursor.getString(0);
190 }
191 }
192 } finally {
193 if (cursor != null) {
194 cursor.close();
195 }
196 }
197 return title;
198 }
199
Leon Scroggins III052ce662010-09-13 14:44:16 -0400200 @Override
John Reck71efc2b2011-05-09 16:54:28 -0700201 public void onTop(BreadCrumbView view, int level, Object data) {
Leon Scroggins74dbe012010-10-15 10:54:27 -0400202 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400203 Folder folderData = (Folder) data;
204 long folder = folderData.Id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400205 LoaderManager manager = getLoaderManager();
John Reck2eec4c32011-05-11 15:55:32 -0700206 CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader(
Leon Scroggins74dbe012010-10-15 10:54:27 -0400207 LOADER_ID_FOLDER_CONTENTS));
Leon Scroggins8baaa632010-12-08 19:46:53 -0500208 loader.setUri(getUriForFolder(folder));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400209 loader.forceLoad();
Leon Scroggins162f8352010-10-18 15:02:44 -0400210 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400211 completeOrCancelFolderNaming(true);
212 }
Leon Scroggins905250c2010-12-17 15:25:33 -0500213 setShowBookmarkIcon(level == 1);
214 }
215
216 /**
217 * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view.
218 * @param show True if the icon should visible, false otherwise.
219 */
220 private void setShowBookmarkIcon(boolean show) {
221 Drawable drawable = show ? mHeaderIcon: null;
222 mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400223 }
224
Leon Scroggins74dbe012010-10-15 10:54:27 -0400225 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400226 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
227 if (v == mFolderNamer) {
228 if (v.getText().length() > 0) {
229 if (actionId == EditorInfo.IME_NULL) {
230 // Only want to do this once.
231 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400232 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400233 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400234 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 }
Michael Kolb31829b92010-10-01 11:50:21 -0700236 // Steal the key press; otherwise a newline will be added
Vivek Sekhar5e631472014-03-31 23:59:10 -0700237 // return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400239 return false;
240 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400242 private void switchToDefaultView(boolean changedFolder) {
243 mFolderSelector.setVisibility(View.GONE);
244 mDefaultView.setVisibility(View.VISIBLE);
245 mCrumbHolder.setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500246 mFakeTitleHolder.setVisibility(View.VISIBLE);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400247 if (changedFolder) {
248 Object data = mCrumbs.getTopData();
249 if (data != null) {
250 Folder folder = (Folder) data;
251 mCurrentFolder = folder.Id;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500252 if (mCurrentFolder == mRootFolder) {
253 // The Spinner changed to show "Other folder ..." Change
254 // it back to "Bookmarks", which is position 0 if we are
255 // editing a folder, 1 otherwise.
Leon Scroggins504433a2011-01-13 12:26:52 -0500256 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500257 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700258 mFolderAdapter.setOtherFolderDisplayText(folder.Name);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500259 }
260 }
261 } else {
262 // The user canceled selecting a folder. Revert back to the earlier
263 // selection.
264 if (mSaveToHomeScreen) {
Leon Scroggins504433a2011-01-13 12:26:52 -0500265 mFolder.setSelectionIgnoringSelectionChange(0);
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500266 } else {
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500267 if (mCurrentFolder == mRootFolder) {
268 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1);
269 } else {
270 Object data = mCrumbs.getTopData();
271 if (data != null && ((Folder) data).Id == mCurrentFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700272 // We are showing the correct folder hierarchy. The
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500273 // folder selector will say "Other folder..." Change it
274 // to say the name of the folder once again.
John Reck2eec4c32011-05-11 15:55:32 -0700275 mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name);
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500276 } else {
John Reck2eec4c32011-05-11 15:55:32 -0700277 // We are not showing the correct folder hierarchy.
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500278 // Clear the Crumbs and find the proper folder
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500279 setupTopCrumb();
280 LoaderManager manager = getLoaderManager();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500281 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
282
283 }
284 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400285 }
286 }
287 }
288
Leon Scroggins III052ce662010-09-13 14:44:16 -0400289 @Override
290 public void onClick(View v) {
291 if (v == mButton) {
292 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400293 // We are showing the folder selector.
Leon Scroggins162f8352010-10-18 15:02:44 -0400294 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400295 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700296 } else {
297 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400298 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400299 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700300 }
luxiaoldbe4a622013-07-19 17:14:06 +0800301 } else {
302 // add for carrier which requires same title or address can not
303 // exist.
304 if (mSaveToHomeScreen) {
305 if (save()) {
306 return;
307 }
308 } else {
309 onSaveWithConfirm();
310 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400311 }
312 } else if (v == mCancelButton) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400313 if (mIsFolderNamerShowing) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400314 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400315 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
316 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700317 } else {
318 finish();
319 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700320 } else if (v == mDeleteButton || v == mRemoveLink) {
321 onDeleteWithConfirm();
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500322 } else if (v == mFolderCancel) {
323 completeOrCancelFolderNaming(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400324 } else if (v == mAddNewFolder) {
Leon Scroggins162f8352010-10-18 15:02:44 -0400325 setShowFolderNamer(true);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400326 mFolderNamer.setText(R.string.new_folder);
327 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700328 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400329 mAddSeparator.setVisibility(View.GONE);
Leon Scroggins162f8352010-10-18 15:02:44 -0400330 InputMethodManager imm = getInputMethodManager();
331 // Set the InputMethodManager to focus on the ListView so that it
332 // can transfer the focus to mFolderNamer.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800333 //imm.focusIn(mListView);
334 Object[] params = {mListView};
335 Class[] type = new Class[] {View.class};
336 ReflectHelper.invokeMethod(imm, "focusIn", type, params);
Leon Scroggins162f8352010-10-18 15:02:44 -0400337 imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400339 }
340
Leon Scroggins504433a2011-01-13 12:26:52 -0500341 // FolderSpinner.OnSetSelectionListener
342
Leon Scroggins88d08032010-10-21 15:17:10 -0400343 @Override
Leon Scroggins504433a2011-01-13 12:26:52 -0500344 public void onSetSelection(long id) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500345 int intId = (int) id;
346 switch (intId) {
347 case FolderSpinnerAdapter.ROOT_FOLDER:
Leon Scroggins02081942010-11-01 17:52:42 -0400348 mCurrentFolder = mRootFolder;
Leon Scroggins88d08032010-10-21 15:17:10 -0400349 mSaveToHomeScreen = false;
350 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500351 case FolderSpinnerAdapter.HOME_SCREEN:
Leon Scroggins88d08032010-10-21 15:17:10 -0400352 // Create a short cut to the home screen
353 mSaveToHomeScreen = true;
Leon Scroggins88d08032010-10-21 15:17:10 -0400354 break;
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500355 case FolderSpinnerAdapter.OTHER_FOLDER:
Pankaj Garg52d36492015-09-07 15:09:40 +0200356 setupTopCrumb();
357 getLoaderManager().restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins88d08032010-10-21 15:17:10 -0400358 switchToFolderSelector();
359 break;
Leon Scroggins2f24e992011-02-11 14:02:10 -0500360 case FolderSpinnerAdapter.RECENT_FOLDER:
361 mCurrentFolder = mFolderAdapter.recentFolderId();
362 mSaveToHomeScreen = false;
363 // In case the user decides to select OTHER_FOLDER
364 // and choose a different one, so that we will start from
365 // the correct place.
366 LoaderManager manager = getLoaderManager();
Leon Scroggins2f24e992011-02-11 14:02:10 -0500367 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins0b95ad42011-02-23 15:23:48 -0500368 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400369 default:
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500370 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400371 }
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500372 }
373
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500374 /**
375 * Finish naming a folder, and close the IME
376 * @param cancel If true, the new folder is not created. If false, the new
377 * folder is created and the user is taken inside it.
378 */
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400379 private void completeOrCancelFolderNaming(boolean cancel) {
380 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700381 String name = mFolderNamer.getText().toString();
382 long id = addFolderToCurrent(mFolderNamer.getText().toString());
383 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700384 }
Leon Scroggins162f8352010-10-18 15:02:44 -0400385 setShowFolderNamer(false);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400386 mAddNewFolder.setVisibility(View.VISIBLE);
387 mAddSeparator.setVisibility(View.VISIBLE);
388 getInputMethodManager().hideSoftInputFromWindow(
Pankaj Garg52d36492015-09-07 15:09:40 +0200389 mFolderNamer.getWindowToken(), 0);
Michael Kolb31829b92010-10-01 11:50:21 -0700390 }
391
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700392 private long addFolderToCurrent(String name) {
393 // Add the folder to the database
394 ContentValues values = new ContentValues();
395 values.put(BrowserContract.Bookmarks.TITLE,
396 name);
397 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400398 long currentFolder;
399 Object data = mCrumbs.getTopData();
400 if (data != null) {
401 currentFolder = ((Folder) data).Id;
402 } else {
Leon Scroggins02081942010-11-01 17:52:42 -0400403 currentFolder = mRootFolder;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400404 }
405 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700406 Uri uri = getContentResolver().insert(
407 BrowserContract.Bookmarks.CONTENT_URI, values);
408 if (uri != null) {
409 return ContentUris.parseId(uri);
410 } else {
411 return -1;
412 }
413 }
414
Leon Scroggins III052ce662010-09-13 14:44:16 -0400415 private void switchToFolderSelector() {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500416 // Set the list to the top in case it is scrolled.
417 mListView.setSelection(0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400418 mDefaultView.setVisibility(View.GONE);
419 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400420 mCrumbHolder.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500421 mFakeTitleHolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400422 mAddNewFolder.setVisibility(View.VISIBLE);
423 mAddSeparator.setVisibility(View.VISIBLE);
John Reck95f88e42011-09-26 09:25:43 -0700424 getInputMethodManager().hideSoftInputFromWindow(
425 mListView.getWindowToken(), 0);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400426 }
427
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700428 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700429 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400430 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400431 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700432 }
433 }
434
John Reck2eec4c32011-05-11 15:55:32 -0700435 private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks =
436 new LoaderCallbacks<EditBookmarkInfo>() {
437
438 @Override
439 public void onLoaderReset(Loader<EditBookmarkInfo> loader) {
440 // Don't care
441 }
442
443 @Override
444 public void onLoadFinished(Loader<EditBookmarkInfo> loader,
445 EditBookmarkInfo info) {
446 boolean setAccount = false;
447 if (info.id != -1) {
448 mEditingExisting = true;
449 showRemoveButton();
450 mFakeTitle.setText(R.string.edit_bookmark);
451 mTitle.setText(info.title);
452 mFolderAdapter.setOtherFolderDisplayText(info.parentTitle);
453 mMap.putLong(BrowserContract.Bookmarks._ID, info.id);
454 setAccount = true;
455 setAccount(info.accountName, info.accountType);
456 mCurrentFolder = info.parentId;
457 onCurrentFolderFound();
458 }
John Reck37894a92011-05-13 12:47:53 -0700459 // TODO: Detect if lastUsedId is a subfolder of info.id in the
460 // editing folder case. For now, just don't show the last used
461 // folder at all to prevent any chance of the user adding a parent
462 // folder to a child folder
463 if (info.lastUsedId != -1 && info.lastUsedId != info.id
464 && !mEditingFolder) {
John Reck2eec4c32011-05-11 15:55:32 -0700465 if (setAccount && info.lastUsedId != mRootFolder
466 && TextUtils.equals(info.lastUsedAccountName, info.accountName)
467 && TextUtils.equals(info.lastUsedAccountType, info.accountType)) {
468 mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle);
469 } else if (!setAccount) {
470 setAccount = true;
471 setAccount(info.lastUsedAccountName, info.lastUsedAccountType);
472 if (info.lastUsedId != mRootFolder) {
473 mFolderAdapter.addRecentFolder(info.lastUsedId,
474 info.lastUsedTitle);
475 }
476 }
477 }
478 if (!setAccount) {
479 mAccountSpinner.setSelection(0);
480 }
481 }
482
483 @Override
484 public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) {
485 return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap);
486 }
487 };
488
489 void setAccount(String accountName, String accountType) {
490 for (int i = 0; i < mAccountAdapter.getCount(); i++) {
491 BookmarkAccount account = mAccountAdapter.getItem(i);
492 if (TextUtils.equals(account.accountName, accountName)
493 && TextUtils.equals(account.accountType, accountType)) {
John Reck2eec4c32011-05-11 15:55:32 -0700494 mAccountSpinner.setSelection(i);
John Reck903a0722011-11-09 17:53:10 -0800495 onRootFolderFound(account.rootFolderId);
John Reck2eec4c32011-05-11 15:55:32 -0700496 return;
497 }
498 }
499 }
500
Leon Scroggins III052ce662010-09-13 14:44:16 -0400501 @Override
502 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
503 String[] projection;
504 switch (id) {
John Reck2eec4c32011-05-11 15:55:32 -0700505 case LOADER_ID_ACCOUNTS:
506 return new AccountsLoader(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400507 case LOADER_ID_FOLDER_CONTENTS:
508 projection = new String[] {
509 BrowserContract.Bookmarks._ID,
510 BrowserContract.Bookmarks.TITLE,
511 BrowserContract.Bookmarks.IS_FOLDER
512 };
Leon Scrogginsc1129902010-12-08 15:28:33 -0500513 String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0";
John Reck1dd8cd42011-05-13 11:22:09 -0700514 String whereArgs[] = null;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500515 if (mEditingFolder) {
John Reck1dd8cd42011-05-13 11:22:09 -0700516 where += " AND " + BrowserContract.Bookmarks._ID + " != ?";
517 whereArgs = new String[] { Long.toString(mMap.getLong(
518 BrowserContract.Bookmarks._ID)) };
519 }
520 long currentFolder;
521 Object data = mCrumbs.getTopData();
522 if (data != null) {
523 currentFolder = ((Folder) data).Id;
524 } else {
525 currentFolder = mRootFolder;
Leon Scrogginsc1129902010-12-08 15:28:33 -0500526 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400527 return new CursorLoader(this,
John Reck1dd8cd42011-05-13 11:22:09 -0700528 getUriForFolder(currentFolder),
Leon Scroggins III052ce662010-09-13 14:44:16 -0400529 projection,
Leon Scrogginsc1129902010-12-08 15:28:33 -0500530 where,
John Reck1dd8cd42011-05-13 11:22:09 -0700531 whereArgs,
Leon Scroggins8baaa632010-12-08 19:46:53 -0500532 BrowserContract.Bookmarks._ID + " ASC");
Leon Scroggins III052ce662010-09-13 14:44:16 -0400533 default:
534 throw new AssertionError("Asking for nonexistant loader!");
535 }
536 }
537
538 @Override
539 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
540 switch (loader.getId()) {
John Reck2eec4c32011-05-11 15:55:32 -0700541 case LOADER_ID_ACCOUNTS:
542 mAccountAdapter.clear();
543 while (cursor.moveToNext()) {
544 mAccountAdapter.add(new BookmarkAccount(this, cursor));
Leon Scroggins504433a2011-01-13 12:26:52 -0500545 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700546
547 if (cursor.getCount() < 2) {
548 View accountView = findViewById(R.id.row_account);
549 if (accountView != null) {
550 accountView.setVisibility(View.GONE);
551 }
552 }
553
John Reck2eec4c32011-05-11 15:55:32 -0700554 getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS);
555 getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null,
556 mEditInfoLoaderCallbacks);
Leon Scroggins504433a2011-01-13 12:26:52 -0500557 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400558 case LOADER_ID_FOLDER_CONTENTS:
559 mAdapter.changeCursor(cursor);
560 break;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400561 }
562 }
563
Dianne Hackborn39772c82010-12-16 00:43:54 -0800564 public void onLoaderReset(Loader<Cursor> loader) {
565 switch (loader.getId()) {
566 case LOADER_ID_FOLDER_CONTENTS:
567 mAdapter.changeCursor(null);
568 break;
569 }
570 }
571
Leon Scroggins02081942010-11-01 17:52:42 -0400572 /**
Leon Scroggins8baaa632010-12-08 19:46:53 -0500573 * Move cursor to the position that has folderToFind as its "_id".
574 * @param cursor Cursor containing folders in the bookmarks database
575 * @param folderToFind "_id" of the folder to move to.
576 * @param idIndex Index in cursor of "_id"
577 * @throws AssertionError if cursor is empty or there is no row with folderToFind
578 * as its "_id".
579 */
580 void moveCursorToFolder(Cursor cursor, long folderToFind, int idIndex)
581 throws AssertionError {
582 if (!cursor.moveToFirst()) {
583 throw new AssertionError("No folders in the database!");
584 }
585 long folder;
586 do {
587 folder = cursor.getLong(idIndex);
588 } while (folder != folderToFind && cursor.moveToNext());
589 if (cursor.isAfterLast()) {
590 throw new AssertionError("Folder(id=" + folderToFind
591 + ") holding this bookmark does not exist!");
592 }
593 }
594
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 @Override
596 public void onItemClick(AdapterView<?> parent, View view, int position,
597 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400598 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400599 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400600 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400601 }
602
Leon Scroggins162f8352010-10-18 15:02:44 -0400603 private void setShowFolderNamer(boolean show) {
604 if (show != mIsFolderNamerShowing) {
605 mIsFolderNamerShowing = show;
606 if (show) {
607 // Set the selection to the folder namer so it will be in
608 // view.
609 mListView.addFooterView(mFolderNamerHolder);
610 } else {
611 mListView.removeFooterView(mFolderNamerHolder);
612 }
613 // Refresh the list.
614 mListView.setAdapter(mAdapter);
615 if (show) {
616 mListView.setSelection(mListView.getCount() - 1);
617 }
618 }
619 }
620
Leon Scroggins III052ce662010-09-13 14:44:16 -0400621 /**
622 * Shows a list of names of folders.
623 */
624 private class FolderAdapter extends CursorAdapter {
625 public FolderAdapter(Context context) {
626 super(context, null);
627 }
628
629 @Override
630 public void bindView(View view, Context context, Cursor cursor) {
631 ((TextView) view.findViewById(android.R.id.text1)).setText(
632 cursor.getString(cursor.getColumnIndexOrThrow(
633 BrowserContract.Bookmarks.TITLE)));
634 }
635
636 @Override
637 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700638 View view = LayoutInflater.from(context).inflate(
639 R.layout.folder_list_item, null);
640 view.setBackgroundDrawable(context.getResources().
641 getDrawable(android.R.drawable.list_selector_background));
642 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400643 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400644
645 @Override
646 public boolean isEmpty() {
647 // Do not show the empty view if the user is creating a new folder.
Leon Scroggins162f8352010-10-18 15:02:44 -0400648 return super.isEmpty() && !mIsFolderNamerShowing;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400649 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400650 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651
Leon Scrogginsc1129902010-12-08 15:28:33 -0500652 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800653 protected void onCreate(Bundle icicle) {
654 super.onCreate(icicle);
Leon Scroggins7453ff22010-12-15 16:03:59 -0500655 requestWindowFeature(Window.FEATURE_NO_TITLE);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100656
657 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100658
Leon Scroggins III052ce662010-09-13 14:44:16 -0400659 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100660
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400661 Window window = getWindow();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700662
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663 String title = null;
664 String url = null;
Pankaj Garg21dad562015-07-02 17:17:24 -0700665 mTouchIconUrl = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100666
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400667 mFakeTitle = (TextView) findViewById(R.id.fake_title);
Pankaj Garg634bf432015-07-13 09:54:21 -0700668
669 mDeleteButton = findViewById(R.id.delete);
670 mDeleteButton.setOnClickListener(this);
671
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 if (mMap != null) {
John Reckc8490812010-11-22 14:15:36 -0800673 Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK);
Sagar Dhawanef06f562015-09-07 15:23:22 +0200674 boolean existing = mMap.getBoolean(CHECK_FOR_DUPE, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675 if (b != null) {
John Reckc8490812010-11-22 14:15:36 -0800676 mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677 mMap = b;
678 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400679 mFakeTitle.setText(R.string.edit_bookmark);
John Reckc8490812010-11-22 14:15:36 -0800680 if (mEditingFolder) {
681 findViewById(R.id.row_address).setVisibility(View.GONE);
Leon Scroggins75630672011-01-13 17:56:15 -0500682 } else {
683 showRemoveButton();
John Reckc8490812010-11-22 14:15:36 -0800684 }
Sagar Dhawanef06f562015-09-07 15:23:22 +0200685 } else if (existing) {
686 showRemoveButton();
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400687 } else {
688 int gravity = mMap.getInt("gravity", -1);
689 if (gravity != -1) {
690 WindowManager.LayoutParams l = window.getAttributes();
691 l.gravity = gravity;
692 window.setAttributes(l);
693 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400695 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
Sagar Dhawanef06f562015-09-07 15:23:22 +0200696 url = mUrl = UrlUtils.fixUpUrl(mMap.getString(BrowserContract.Bookmarks.URL));
Leon Scrogginsbc922852010-10-22 12:15:27 -0400697 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700698 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800699
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -0800700 // Check if title is not empty to prevent NPE
701 if (!TextUtils.isEmpty(title)) {
702 if (title.length() > MAX_TITLE_LENGTH) {
703 title = title.substring(0, MAX_TITLE_LENGTH);
704 }
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800705 }
Pankaj Garg634bf432015-07-13 09:54:21 -0700706
Leon Scroggins25230d72010-09-28 20:09:25 -0400707 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800708
709 mTitle = (EditText) findViewById(R.id.title);
710 mTitle.setText(title);
Axesh R. Ajmerac8150342014-10-28 11:21:09 -0700711 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100712
Leon Scroggins III052ce662010-09-13 14:44:16 -0400713 mAddress = (EditText) findViewById(R.id.address);
714 mAddress.setText(url);
luxiaoldbe4a622013-07-19 17:14:06 +0800715 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716
The Android Open Source Project0c908882009-03-03 19:32:16 -0800717 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400718 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800719
720 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400721 mCancelButton.setOnClickListener(this);
722
Leon Scroggins504433a2011-01-13 12:26:52 -0500723 mFolder = (FolderSpinner) findViewById(R.id.folder);
John Reck2eec4c32011-05-11 15:55:32 -0700724 mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder);
Leon Scroggins2f24e992011-02-11 14:02:10 -0500725 mFolder.setAdapter(mFolderAdapter);
Leon Scroggins504433a2011-01-13 12:26:52 -0500726 mFolder.setOnSetSelectionListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400727
728 mDefaultView = findViewById(R.id.default_view);
729 mFolderSelector = findViewById(R.id.folder_selector);
730
Leon Scroggins162f8352010-10-18 15:02:44 -0400731 mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null);
732 mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400733 mFolderNamer.setOnEditorActionListener(this);
luxiaoldbe4a622013-07-19 17:14:06 +0800734
735 // add for carrier test about warning limit of edit text
736 BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer,
737 BrowserUtils.FILENAME_MAX_LENGTH);
738
Leon Scroggins0e3a7b82011-01-11 19:08:03 -0500739 mFolderCancel = mFolderNamerHolder.findViewById(R.id.close);
740 mFolderCancel.setOnClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400741
742 mAddNewFolder = findViewById(R.id.add_new_folder);
743 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400744 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400745
Leon Scroggins74dbe012010-10-15 10:54:27 -0400746 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
Pankaj Garg634bf432015-07-13 09:54:21 -0700747 //mCrumbs.setUseBackButton(true);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400748 mCrumbs.setController(this);
Enrico Rosd6efa972014-12-02 19:49:59 -0800749 mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400750 mCrumbHolder = findViewById(R.id.crumb_holder);
John Reck89f73c12010-12-01 10:10:14 -0800751 mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400752
Leon Scroggins III052ce662010-09-13 14:44:16 -0400753 mAdapter = new FolderAdapter(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400754 mListView = (CustomListView) findViewById(R.id.list);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400755 View empty = findViewById(R.id.empty);
756 mListView.setEmptyView(empty);
757 mListView.setAdapter(mAdapter);
758 mListView.setOnItemClickListener(this);
Leon Scroggins162f8352010-10-18 15:02:44 -0400759 mListView.addEditText(mFolderNamer);
Leon Scroggins504433a2011-01-13 12:26:52 -0500760
John Reck2eec4c32011-05-11 15:55:32 -0700761 mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this,
762 android.R.layout.simple_spinner_item);
763 mAccountAdapter.setDropDownViewResource(
764 android.R.layout.simple_spinner_dropdown_item);
765 mAccountSpinner = (Spinner) findViewById(R.id.accounts);
766 mAccountSpinner.setAdapter(mAccountAdapter);
767 mAccountSpinner.setOnItemSelectedListener(this);
Sagar Dhawan7e9d2472015-07-20 10:39:40 -0700768 mFolder.setSelectionIgnoringSelectionChange(1); // Select Bookmarks by default
John Reck2eec4c32011-05-11 15:55:32 -0700769
Leon Scroggins75630672011-01-13 17:56:15 -0500770 mFakeTitleHolder = findViewById(R.id.title_holder);
771
Leon Scroggins504433a2011-01-13 12:26:52 -0500772 if (!window.getDecorView().isInTouchMode()) {
773 mButton.requestFocus();
774 }
775
Pankaj Garg4e82a9b2015-02-05 16:58:17 -0800776 if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) {
777 mFolder.setSelectionIgnoringSelectionChange(2);
778 mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder));
779 }
780
John Reck2eec4c32011-05-11 15:55:32 -0700781 getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this);
Leon Scroggins504433a2011-01-13 12:26:52 -0500782 }
783
Leon Scroggins75630672011-01-13 17:56:15 -0500784 private void showRemoveButton() {
Pankaj Garg634bf432015-07-13 09:54:21 -0700785 mDeleteButton.setVisibility(View.VISIBLE);
Leon Scroggins75630672011-01-13 17:56:15 -0500786 findViewById(R.id.remove_divider).setVisibility(View.VISIBLE);
787 mRemoveLink = findViewById(R.id.remove);
788 mRemoveLink.setVisibility(View.VISIBLE);
789 mRemoveLink.setOnClickListener(this);
790 }
791
Leon Scroggins504433a2011-01-13 12:26:52 -0500792 // Called once we have determined which folder is the root folder
793 private void onRootFolderFound(long root) {
794 mRootFolder = root;
Pankaj Garg63396942015-03-06 10:05:25 -0800795 mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder;
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500796 setupTopCrumb();
John Reck2eec4c32011-05-11 15:55:32 -0700797 onCurrentFolderFound();
Leon Scroggins504433a2011-01-13 12:26:52 -0500798 }
799
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500800 private void setupTopCrumb() {
John Reck2eec4c32011-05-11 15:55:32 -0700801 mCrumbs.clear();
Leon Scrogginsb3ae8802011-01-18 11:55:27 -0500802 String name = getString(R.string.bookmarks);
803 mTopLevelLabel = (TextView) mCrumbs.pushView(name, false,
804 new Folder(name, mRootFolder));
805 // To better match the other folders.
806 mTopLevelLabel.setCompoundDrawablePadding(6);
807 }
808
Leon Scroggins504433a2011-01-13 12:26:52 -0500809 private void onCurrentFolderFound() {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400810 LoaderManager manager = getLoaderManager();
Leon Scroggins8baaa632010-12-08 19:46:53 -0500811 if (mCurrentFolder != mRootFolder) {
Leon Scrogginsdd13bad2011-01-06 20:25:54 -0500812 // Since we're not in the root folder, change the selection to other
813 // folder now. The text will get changed once we select the correct
814 // folder.
Leon Scroggins504433a2011-01-13 12:26:52 -0500815 mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
Leon Scroggins905250c2010-12-17 15:25:33 -0500816 } else {
817 setShowBookmarkIcon(true);
Leon Scroggins504433a2011-01-13 12:26:52 -0500818 if (!mEditingFolder) {
819 // Initially the "Bookmarks" folder should be showing, rather than
820 // the home screen. In the editing folder case, home screen is not
821 // an option, so "Bookmarks" folder is already at the top.
822 mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER);
823 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400824 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400825 // Find the contents of the current folder
John Reck2eec4c32011-05-11 15:55:32 -0700826 manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
Leon Scroggins8baaa632010-12-08 19:46:53 -0500827 }
828
Leon Scroggins02065b02010-01-04 14:30:13 -0500829 /**
830 * Runnable to save a bookmark, so it can be performed in its own thread.
831 */
832 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400833 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500834 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200835 private Context mContext;
836 public SaveBookmarkRunnable(Context ctx, Message msg) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100837 mContext = ctx.getApplicationContext();
Leon Scroggins02065b02010-01-04 14:30:13 -0500838 mMessage = msg;
839 }
840 public void run() {
841 // Unbundle bookmark data.
842 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400843 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
844 String url = bundle.getString(BrowserContract.Bookmarks.URL);
845 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500846 Bitmap thumbnail = invalidateThumbnail ? null
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700847 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.FAVICON);
Leon Scrogginsbc922852010-10-22 12:15:27 -0400848 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500849
850 // Save to the bookmarks DB.
851 try {
852 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400853 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
John Reckaf262e72011-07-25 13:55:44 -0700854 title, thumbnail, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500855 if (touchIconUrl != null) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700856 new DownloadTouchIcon(mContext, cr, url).execute(touchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500857 }
858 mMessage.arg1 = 1;
859 } catch (IllegalStateException e) {
860 mMessage.arg1 = 0;
861 }
862 mMessage.sendToTarget();
863 }
864 }
865
John Reckc8490812010-11-22 14:15:36 -0800866 private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> {
867 Context mContext;
868 Long mId;
869
870 public UpdateBookmarkTask(Context context, long id) {
Ben Murdoch914c5592011-08-01 13:58:47 +0100871 mContext = context.getApplicationContext();
John Reckc8490812010-11-22 14:15:36 -0800872 mId = id;
873 }
874
875 @Override
876 protected Void doInBackground(ContentValues... params) {
877 if (params.length != 1) {
878 throw new IllegalArgumentException("No ContentValues provided!");
879 }
880 Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId);
881 mContext.getContentResolver().update(
882 uri,
883 params[0], null, null);
884 return null;
885 }
886 }
887
Ben Murdoch1794fe22009-09-29 18:14:30 +0100888 private void createHandler() {
889 if (mHandler == null) {
890 mHandler = new Handler() {
891 @Override
892 public void handleMessage(Message msg) {
893 switch (msg.what) {
894 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500895 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100896 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
897 Toast.LENGTH_LONG).show();
898 } else {
899 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
900 Toast.LENGTH_LONG).show();
901 }
902 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400903 case TOUCH_ICON_DOWNLOADED:
904 Bundle b = msg.getData();
905 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400906 AddBookmarkPage.this,
907 b.getString(BrowserContract.Bookmarks.URL),
908 b.getString(BrowserContract.Bookmarks.TITLE),
909 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
910 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400911 break;
Leon Scroggins75630672011-01-13 17:56:15 -0500912 case BOOKMARK_DELETED:
913 finish();
914 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100915 }
916 }
917 };
918 }
919 }
920
luxiaoldbe4a622013-07-19 17:14:06 +0800921 static void deleteDuplicateBookmark(final Context context, final long id) {
922 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id);
923 context.getContentResolver().delete(uri, null, null);
924 }
925
Pankaj Garg634bf432015-07-13 09:54:21 -0700926 private void onDeleteWithConfirm() {
927 final String title = mTitle.getText().toString().trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +0200928 final String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
Pankaj Garg634bf432015-07-13 09:54:21 -0700929 final String url = unfilteredUrl.trim();
930 new AlertDialog.Builder(this)
931 .setIconAttribute(android.R.attr.alertDialogIcon)
932 .setMessage(getString(R.string.delete_bookmark_warning, title))
933 .setNegativeButton(android.R.string.cancel, null)
934 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
935 public void onClick(DialogInterface dialog, int which) {
936 ContentResolver cr = getContentResolver();
937 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
938 BookmarksLoader.PROJECTION,
Sagar Dhawanef06f562015-09-07 15:23:22 +0200939 "url = ?",
Pankaj Garg634bf432015-07-13 09:54:21 -0700940 new String[] {
Sagar Dhawanef06f562015-09-07 15:23:22 +0200941 url
Pankaj Garg634bf432015-07-13 09:54:21 -0700942 },
943 null);
944
945 if (cursor == null) {
946 finish();
947 return;
948 }
949
950 try {
951 if (cursor.moveToFirst()) {
952 do {
953 long index = cursor.getLong(
954 cursor.getColumnIndex(BrowserContract.Bookmarks._ID));
955 cr.delete(ContentUris.withAppendedId(
956 BrowserContract.Bookmarks.CONTENT_URI, index),
957 null, null);
958 } while (cursor.moveToNext());
959 }
960 } catch (IllegalStateException e) {
961 e.printStackTrace();
962 } finally {
963 if (cursor != null)
964 cursor.close();
965 }
966 finish();
967 }
968 })
969 .show();
970 }
971
luxiaoldbe4a622013-07-19 17:14:06 +0800972 private void onSaveWithConfirm() {
Sagar Dhawanef06f562015-09-07 15:23:22 +0200973 String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
luxiaoldbe4a622013-07-19 17:14:06 +0800974 String url = unfilteredUrl.trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +0200975 Long id = (mMap == null) ?
976 -1 :
977 mMap.getLong(BrowserContract.Bookmarks._ID);
luxiaoldbe4a622013-07-19 17:14:06 +0800978 int duplicateCount;
979 final ContentResolver cr = getContentResolver();
980
981 Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
982 BookmarksLoader.PROJECTION,
Sagar Dhawan05f51cc2015-10-14 10:57:44 -0700983 "(url = ? OR title = ?) AND parent = ?",
luxiaoldbe4a622013-07-19 17:14:06 +0800984 new String[] {
Sagar Dhawan05f51cc2015-10-14 10:57:44 -0700985 url, mTitle.getText().toString().trim(), Long.toString(mCurrentFolder)
luxiaoldbe4a622013-07-19 17:14:06 +0800986 },
987 null);
988
989 if (cursor == null) {
990 save();
991 return;
992 }
993
994 duplicateCount = cursor.getCount();
995 if (duplicateCount <= 0) {
996 cursor.close();
997 save();
998 return;
999 } else {
1000 try {
1001 while (cursor.moveToNext()) {
1002 mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID);
1003 mDuplicateContext = AddBookmarkPage.this;
1004 }
1005 } catch (IllegalStateException e) {
1006 e.printStackTrace();
1007 } finally {
1008 if (cursor != null)
1009 cursor.close();
1010 }
1011 }
1012
1013 if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) {
1014 save();
1015 return;
1016 }
1017
1018 new AlertDialog.Builder(this)
Sagar Dhawan53b2ba72015-08-05 15:58:20 -07001019 .setTitle(getString(R.string.save_to_bookmarks))
luxiaoldbe4a622013-07-19 17:14:06 +08001020 .setMessage(getString(R.string.overwrite_bookmark_msg))
1021 .setNegativeButton(android.R.string.cancel, null)
1022 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1023 public void onClick(DialogInterface dialog, int which) {
1024 if (mDuplicateContext == null) {
1025 return;
1026 }
1027 deleteDuplicateBookmark(mDuplicateContext, mDuplicateId);
1028 save();
1029 }
1030 })
1031 .show();
1032 }
1033
The Android Open Source Project0c908882009-03-03 19:32:16 -08001034 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +01001035 * 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 -08001036 */
1037 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +01001038 createHandler();
1039
The Android Open Source Project0c908882009-03-03 19:32:16 -08001040 String title = mTitle.getText().toString().trim();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001041 String unfilteredUrl = UrlUtils.fixUpUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +01001042
The Android Open Source Project0c908882009-03-03 19:32:16 -08001043 boolean emptyTitle = title.length() == 0;
1044 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
1045 Resources r = getResources();
John Reckc8490812010-11-22 14:15:36 -08001046 if (emptyTitle || (emptyUrl && !mEditingFolder)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001047 if (emptyTitle) {
1048 mTitle.setError(r.getText(R.string.bookmark_needs_title));
1049 }
1050 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -04001051 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001052 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -04001053 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001054 }
Ben Murdochca12cfa2009-11-17 13:57:44 +00001055 String url = unfilteredUrl.trim();
John Reckc8490812010-11-22 14:15:36 -08001056 if (!mEditingFolder) {
1057 try {
1058 // We allow bookmarks with a javascript: scheme, but these will in most cases
1059 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
Ben Murdochca12cfa2009-11-17 13:57:44 +00001060
John Reckc8490812010-11-22 14:15:36 -08001061 if (!url.toLowerCase().startsWith("javascript:")) {
Sagar Dhawanef06f562015-09-07 15:23:22 +02001062 URI uriObj = new URI(url);
John Reckc8490812010-11-22 14:15:36 -08001063 String scheme = uriObj.getScheme();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001064 if (scheme == null) { // SWE will allow bookmarking any scheme
John Reckc8490812010-11-22 14:15:36 -08001065 WebAddress address;
1066 try {
1067 address = new WebAddress(unfilteredUrl);
1068 } catch (ParseException e) {
1069 throw new URISyntaxException("", "");
1070 }
1071 if (address.getHost().length() == 0) {
1072 throw new URISyntaxException("", "");
1073 }
1074 url = address.toString();
Ben Murdochca12cfa2009-11-17 13:57:44 +00001075 }
Ben Murdochde353622009-10-12 10:29:00 +01001076 }
John Reckc8490812010-11-22 14:15:36 -08001077 } catch (URISyntaxException e) {
1078 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
1079 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001080 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001081 }
Ben Murdochaac7aa62009-09-17 16:57:40 +01001082
Leon Scroggins88d08032010-10-21 15:17:10 -04001083 if (mSaveToHomeScreen) {
1084 mEditingExisting = false;
1085 }
1086
Sagar Dhawanef06f562015-09-07 15:23:22 +02001087 boolean urlUnmodified = url.equals(mUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001088
Ben Murdoch1794fe22009-09-29 18:14:30 +01001089 if (mEditingExisting) {
John Reckc8490812010-11-22 14:15:36 -08001090 Long id = mMap.getLong(BrowserContract.Bookmarks._ID);
1091 ContentValues values = new ContentValues();
1092 values.put(BrowserContract.Bookmarks.TITLE, title);
1093 values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
1094 if (!mEditingFolder) {
1095 values.put(BrowserContract.Bookmarks.URL, url);
1096 if (!urlUnmodified) {
1097 values.putNull(BrowserContract.Bookmarks.THUMBNAIL);
1098 }
1099 }
1100 if (values.size() > 0) {
1101 new UpdateBookmarkTask(getApplicationContext(), id).execute(values);
1102 }
1103 setResult(RESULT_OK);
Ben Murdoch1794fe22009-09-29 18:14:30 +01001104 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -04001105 Bitmap thumbnail;
1106 Bitmap favicon;
1107 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001108 thumbnail = (Bitmap) mMap.getParcelable(
1109 BrowserContract.Bookmarks.THUMBNAIL);
1110 favicon = (Bitmap) mMap.getParcelable(
1111 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -04001112 } else {
1113 thumbnail = null;
1114 favicon = null;
1115 }
1116
Ben Murdoch1794fe22009-09-29 18:14:30 +01001117 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001118 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
1119 bundle.putString(BrowserContract.Bookmarks.URL, url);
1120 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -04001121
1122 if (mSaveToHomeScreen) {
1123 if (mTouchIconUrl != null && urlUnmodified) {
1124 Message msg = Message.obtain(mHandler,
1125 TOUCH_ICON_DOWNLOADED);
1126 msg.setData(bundle);
1127 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -04001128 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -04001129 icon.execute(mTouchIconUrl);
1130 } else {
1131 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
1132 title, null /*touchIcon*/, favicon));
1133 }
1134 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001135 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
1136 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Pankaj Garg21dad562015-07-02 17:17:24 -07001137 if (mTouchIconUrl != null) {
1138 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
1139 }
Leon Scroggins88d08032010-10-21 15:17:10 -04001140 // Post a message to write to the DB.
1141 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
1142 msg.setData(bundle);
1143 // Start a new thread so as to not slow down the UI
1144 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
1145 t.start();
1146 }
Ben Murdoch1794fe22009-09-29 18:14:30 +01001147 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +00001148 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -08001149 }
luxiaoldbe4a622013-07-19 17:14:06 +08001150 finish();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001151 return true;
1152 }
Leon Scroggins162f8352010-10-18 15:02:44 -04001153
John Reck2eec4c32011-05-11 15:55:32 -07001154 @Override
1155 public void onItemSelected(AdapterView<?> parent, View view, int position,
1156 long id) {
1157 if (mAccountSpinner == parent) {
1158 long root = mAccountAdapter.getItem(position).rootFolderId;
1159 if (root != mRootFolder) {
1160 onRootFolderFound(root);
John Recke890c902011-07-11 17:44:43 -07001161 mFolderAdapter.clearRecentFolder();
John Reck2eec4c32011-05-11 15:55:32 -07001162 }
1163 }
1164 }
1165
1166 @Override
1167 public void onNothingSelected(AdapterView<?> parent) {
1168 // Don't care
1169 }
1170
Leon Scroggins162f8352010-10-18 15:02:44 -04001171 /*
1172 * Class used as a proxy for the InputMethodManager to get to mFolderNamer
1173 */
1174 public static class CustomListView extends ListView {
1175 private EditText mEditText;
1176
1177 public void addEditText(EditText editText) {
1178 mEditText = editText;
1179 }
1180
1181 public CustomListView(Context context) {
1182 super(context);
1183 }
1184
1185 public CustomListView(Context context, AttributeSet attrs) {
1186 super(context, attrs);
1187 }
1188
1189 public CustomListView(Context context, AttributeSet attrs, int defStyle) {
1190 super(context, attrs, defStyle);
1191 }
1192
1193 @Override
1194 public boolean checkInputConnectionProxy(View view) {
1195 return view == mEditText;
1196 }
1197 }
John Reck2eec4c32011-05-11 15:55:32 -07001198
John Reck3bf3cfb2011-10-17 18:00:05 -07001199 static class AccountsLoader extends CursorLoader {
John Reck2eec4c32011-05-11 15:55:32 -07001200
1201 static final String[] PROJECTION = new String[] {
1202 Accounts.ACCOUNT_NAME,
1203 Accounts.ACCOUNT_TYPE,
1204 Accounts.ROOT_ID,
1205 };
1206
1207 static final int COLUMN_INDEX_ACCOUNT_NAME = 0;
1208 static final int COLUMN_INDEX_ACCOUNT_TYPE = 1;
1209 static final int COLUMN_INDEX_ROOT_ID = 2;
1210
1211 public AccountsLoader(Context context) {
John Reck1e9815d2011-08-08 17:45:05 -07001212 super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null);
John Reck2eec4c32011-05-11 15:55:32 -07001213 }
1214
1215 }
1216
John Reck9b8cd1e2011-05-25 18:14:01 -07001217 public static class BookmarkAccount {
John Reck2eec4c32011-05-11 15:55:32 -07001218
1219 private String mLabel;
1220 String accountName, accountType;
John Reck9b8cd1e2011-05-25 18:14:01 -07001221 public long rootFolderId;
John Reck2eec4c32011-05-11 15:55:32 -07001222
1223 public BookmarkAccount(Context context, Cursor cursor) {
1224 accountName = cursor.getString(
1225 AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME);
1226 accountType = cursor.getString(
1227 AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE);
1228 rootFolderId = cursor.getLong(
1229 AccountsLoader.COLUMN_INDEX_ROOT_ID);
1230 mLabel = accountName;
1231 if (TextUtils.isEmpty(mLabel)) {
1232 mLabel = context.getString(R.string.local_bookmarks);
1233 }
1234 }
1235
1236 @Override
1237 public String toString() {
1238 return mLabel;
1239 }
1240 }
1241
1242 static class EditBookmarkInfo {
1243 long id = -1;
1244 long parentId = -1;
1245 String parentTitle;
1246 String title;
1247 String accountName;
1248 String accountType;
1249
1250 long lastUsedId = -1;
1251 String lastUsedTitle;
1252 String lastUsedAccountName;
1253 String lastUsedAccountType;
1254 }
1255
1256 static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> {
1257
1258 private Context mContext;
1259 private Bundle mMap;
1260
1261 public EditBookmarkInfoLoader(Context context, Bundle bundle) {
1262 super(context);
Ben Murdoch914c5592011-08-01 13:58:47 +01001263 mContext = context.getApplicationContext();
Sagar Dhawanef06f562015-09-07 15:23:22 +02001264 mMap = (bundle==null) ? new Bundle() : bundle;
John Reck2eec4c32011-05-11 15:55:32 -07001265 }
1266
1267 @Override
1268 public EditBookmarkInfo loadInBackground() {
1269 final ContentResolver cr = mContext.getContentResolver();
1270 EditBookmarkInfo info = new EditBookmarkInfo();
1271 Cursor c = null;
1272
1273 try {
1274 // First, let's lookup the bookmark (check for dupes, get needed info)
1275 String url = mMap.getString(BrowserContract.Bookmarks.URL);
1276 info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1);
1277 boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE);
1278 if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) {
1279 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1280 new String[] { BrowserContract.Bookmarks._ID},
1281 BrowserContract.Bookmarks.URL + "=?",
1282 new String[] { url }, null);
1283 if (c.getCount() == 1 && c.moveToFirst()) {
1284 info.id = c.getLong(0);
1285 }
1286 c.close();
1287 }
1288 if (info.id != -1) {
1289 c = cr.query(ContentUris.withAppendedId(
1290 BrowserContract.Bookmarks.CONTENT_URI, info.id),
1291 new String[] {
1292 BrowserContract.Bookmarks.PARENT,
1293 BrowserContract.Bookmarks.ACCOUNT_NAME,
1294 BrowserContract.Bookmarks.ACCOUNT_TYPE,
1295 BrowserContract.Bookmarks.TITLE},
1296 null, null, null);
1297 if (c.moveToFirst()) {
1298 info.parentId = c.getLong(0);
1299 info.accountName = c.getString(1);
1300 info.accountType = c.getString(2);
1301 info.title = c.getString(3);
1302 }
1303 c.close();
1304 c = cr.query(ContentUris.withAppendedId(
1305 BrowserContract.Bookmarks.CONTENT_URI, info.parentId),
1306 new String[] {
1307 BrowserContract.Bookmarks.TITLE,},
1308 null, null, null);
1309 if (c.moveToFirst()) {
1310 info.parentTitle = c.getString(0);
1311 }
1312 c.close();
1313 }
1314
1315 // Figure out the last used folder/account
1316 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1317 new String[] {
1318 BrowserContract.Bookmarks.PARENT,
1319 }, null, null,
1320 BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1");
1321 if (c.moveToFirst()) {
1322 long parent = c.getLong(0);
1323 c.close();
1324 c = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
1325 new String[] {
1326 BrowserContract.Bookmarks.TITLE,
1327 BrowserContract.Bookmarks.ACCOUNT_NAME,
1328 BrowserContract.Bookmarks.ACCOUNT_TYPE},
1329 BrowserContract.Bookmarks._ID + "=?", new String[] {
1330 Long.toString(parent)}, null);
1331 if (c.moveToFirst()) {
1332 info.lastUsedId = parent;
1333 info.lastUsedTitle = c.getString(0);
1334 info.lastUsedAccountName = c.getString(1);
1335 info.lastUsedAccountType = c.getString(2);
1336 }
1337 c.close();
1338 }
1339 } finally {
1340 if (c != null) {
1341 c.close();
1342 }
1343 }
1344
1345 return info;
1346 }
1347
1348 @Override
1349 protected void onStartLoading() {
1350 forceLoad();
1351 }
1352
1353 }
1354
The Android Open Source Project0c908882009-03-03 19:32:16 -08001355}