The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 18 | |
| 19 | import android.app.Activity; |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 21 | import android.app.LoaderManager; |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 22 | import android.app.LoaderManager.LoaderCallbacks; |
| 23 | import android.content.AsyncTaskLoader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 25 | import android.content.ContentUris; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 26 | import android.content.ContentValues; |
| 27 | import android.content.Context; |
| 28 | import android.content.CursorLoader; |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 29 | import android.content.DialogInterface; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 30 | import android.content.Loader; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.content.res.Resources; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 32 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.net.ParseException; |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 36 | import android.net.Uri; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 37 | import android.os.AsyncTask; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.os.Message; |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 41 | import android.text.TextUtils; |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 42 | import android.util.AttributeSet; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 43 | import android.view.KeyEvent; |
| 44 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | import android.view.View; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 46 | import android.view.ViewGroup; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | import android.view.Window; |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 48 | import android.view.WindowManager; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 49 | import android.view.inputmethod.EditorInfo; |
| 50 | import android.view.inputmethod.InputMethodManager; |
| 51 | import android.widget.AdapterView; |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 52 | import android.widget.AdapterView.OnItemSelectedListener; |
| 53 | import android.widget.ArrayAdapter; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 54 | import android.widget.CursorAdapter; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 55 | import android.widget.EditText; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 56 | import android.widget.ListView; |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 57 | import android.widget.Spinner; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | import android.widget.TextView; |
| 59 | import android.widget.Toast; |
| 60 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 61 | import com.android.browser.BrowserUtils; |
| 62 | import com.android.browser.R; |
| 63 | import com.android.browser.addbookmark.FolderSpinner; |
| 64 | import com.android.browser.addbookmark.FolderSpinnerAdapter; |
| 65 | import com.android.browser.platformsupport.BrowserContract; |
| 66 | import com.android.browser.platformsupport.WebAddress; |
| 67 | import com.android.browser.platformsupport.BrowserContract.Accounts; |
| 68 | import com.android.browser.reflect.ReflectHelper; |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 69 | |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 70 | import java.net.URI; |
Vivek Sekhar | f95fb38 | 2014-07-23 11:13:44 -0700 | [diff] [blame] | 71 | import java.net.URLEncoder; |
Cary Clark | f2407c6 | 2009-09-04 12:25:10 -0400 | [diff] [blame] | 72 | import java.net.URISyntaxException; |
Vivek Sekhar | f95fb38 | 2014-07-23 11:13:44 -0700 | [diff] [blame] | 73 | import java.io.UnsupportedEncodingException; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 74 | |
| 75 | public class AddBookmarkPage extends Activity |
| 76 | implements View.OnClickListener, TextView.OnEditorActionListener, |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 77 | AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>, |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 78 | BreadCrumbView.Controller, FolderSpinner.OnSetSelectionListener, |
| 79 | OnItemSelectedListener { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 80 | |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 81 | public static final long DEFAULT_FOLDER_ID = -1; |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 82 | public static final String TOUCH_ICON_URL = "touch_icon_url"; |
| 83 | // Place on an edited bookmark to remove the saved thumbnail |
| 84 | public static final String REMOVE_THUMBNAIL = "remove_thumbnail"; |
| 85 | public static final String USER_AGENT = "user_agent"; |
Leon Scroggins | bdff8a7 | 2011-02-11 15:49:04 -0500 | [diff] [blame] | 86 | public static final String CHECK_FOR_DUPE = "check_for_dupe"; |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 87 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 88 | /* package */ static final String EXTRA_EDIT_BOOKMARK = "bookmark"; |
| 89 | /* package */ static final String EXTRA_IS_FOLDER = "is_folder"; |
| 90 | |
kaiyiz | 3d7e4d5 | 2013-09-17 17:56:27 +0800 | [diff] [blame] | 91 | private static final int MAX_CRUMBS_SHOWN = 1; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 92 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | private final String LOGTAG = "Bookmarks"; |
| 94 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 95 | // IDs for the CursorLoaders that are used. |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 96 | private final int LOADER_ID_ACCOUNTS = 0; |
| 97 | private final int LOADER_ID_FOLDER_CONTENTS = 1; |
| 98 | private final int LOADER_ID_EDIT_INFO = 2; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 99 | |
Axesh R. Ajmera | c815034 | 2014-10-28 11:21:09 -0700 | [diff] [blame] | 100 | final static int MAX_TITLE_LENGTH = 80; |
| 101 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 102 | private EditText mTitle; |
| 103 | private EditText mAddress; |
| 104 | private TextView mButton; |
| 105 | private View mCancelButton; |
| 106 | private boolean mEditingExisting; |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 107 | private boolean mEditingFolder; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 108 | private Bundle mMap; |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 109 | private String mTouchIconUrl; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 110 | private String mOriginalUrl; |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 111 | private FolderSpinner mFolder; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 112 | private View mDefaultView; |
| 113 | private View mFolderSelector; |
| 114 | private EditText mFolderNamer; |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 115 | private View mFolderCancel; |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 116 | private boolean mIsFolderNamerShowing; |
| 117 | private View mFolderNamerHolder; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 118 | private View mAddNewFolder; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 119 | private View mAddSeparator; |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 120 | private long mCurrentFolder; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 121 | private FolderAdapter mAdapter; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 122 | private BreadCrumbView mCrumbs; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 123 | private TextView mFakeTitle; |
| 124 | private View mCrumbHolder; |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 125 | private CustomListView mListView; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 126 | private boolean mSaveToHomeScreen; |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 127 | private long mRootFolder; |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 128 | private TextView mTopLevelLabel; |
| 129 | private Drawable mHeaderIcon; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 130 | private View mRemoveLink; |
| 131 | private View mFakeTitleHolder; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 132 | private FolderSpinnerAdapter mFolderAdapter; |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 133 | private Spinner mAccountSpinner; |
| 134 | private ArrayAdapter<BookmarkAccount> mAccountAdapter; |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 135 | // add for carrier which requires same title or address can not exist. |
| 136 | private long mDuplicateId; |
| 137 | private Context mDuplicateContext; |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 138 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 139 | private static class Folder { |
| 140 | String Name; |
| 141 | long Id; |
| 142 | Folder(String name, long id) { |
| 143 | Name = name; |
| 144 | Id = id; |
| 145 | } |
| 146 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 147 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 148 | // Message IDs |
| 149 | private static final int SAVE_BOOKMARK = 100; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 150 | private static final int TOUCH_ICON_DOWNLOADED = 101; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 151 | private static final int BOOKMARK_DELETED = 102; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 152 | |
| 153 | private Handler mHandler; |
| 154 | |
Bjorn Bringert | b1402a5 | 2010-10-12 10:53:12 +0100 | [diff] [blame] | 155 | private InputMethodManager getInputMethodManager() { |
| 156 | return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); |
| 157 | } |
| 158 | |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 159 | private Uri getUriForFolder(long folder) { |
John Reck | 903a072 | 2011-11-09 17:53:10 -0800 | [diff] [blame] | 160 | BookmarkAccount account = |
| 161 | (BookmarkAccount) mAccountSpinner.getSelectedItem(); |
| 162 | if (folder == mRootFolder && account != null) { |
| 163 | return BookmarksLoader.addAccount( |
| 164 | BrowserContract.Bookmarks.CONTENT_URI_DEFAULT_FOLDER, |
| 165 | account.accountType, account.accountName); |
| 166 | } |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 167 | return BrowserContract.Bookmarks.buildFolderUri(folder); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Pankaj Garg | 4e82a9b | 2015-02-05 16:58:17 -0800 | [diff] [blame] | 170 | private String getNameFromId(long mCurrentFolder2) { |
| 171 | String title = ""; |
| 172 | Cursor cursor = null; |
| 173 | try { |
| 174 | cursor = getApplicationContext().getContentResolver().query( |
| 175 | BrowserContract.Bookmarks.CONTENT_URI, |
| 176 | new String[] { |
| 177 | BrowserContract.Bookmarks.TITLE |
| 178 | }, |
| 179 | BrowserContract.Bookmarks._ID + " = ? AND " |
| 180 | + BrowserContract.Bookmarks.IS_DELETED + " = ? AND " |
| 181 | + BrowserContract.Bookmarks.IS_FOLDER + " = ? ", new String[] { |
| 182 | String.valueOf(mCurrentFolder2), 0 + "", 1 + "" |
| 183 | }, null); |
| 184 | if (cursor != null && cursor.getCount() != 0) { |
| 185 | while (cursor.moveToNext()) { |
| 186 | title = cursor.getString(0); |
| 187 | } |
| 188 | } |
| 189 | } finally { |
| 190 | if (cursor != null) { |
| 191 | cursor.close(); |
| 192 | } |
| 193 | } |
| 194 | return title; |
| 195 | } |
| 196 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 197 | @Override |
John Reck | 71efc2b | 2011-05-09 16:54:28 -0700 | [diff] [blame] | 198 | public void onTop(BreadCrumbView view, int level, Object data) { |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 199 | if (null == data) return; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 200 | Folder folderData = (Folder) data; |
| 201 | long folder = folderData.Id; |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 202 | LoaderManager manager = getLoaderManager(); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 203 | CursorLoader loader = (CursorLoader) ((Loader<?>) manager.getLoader( |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 204 | LOADER_ID_FOLDER_CONTENTS)); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 205 | loader.setUri(getUriForFolder(folder)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 206 | loader.forceLoad(); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 207 | if (mIsFolderNamerShowing) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 208 | completeOrCancelFolderNaming(true); |
| 209 | } |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 210 | setShowBookmarkIcon(level == 1); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Show or hide the icon for bookmarks next to "Bookmarks" in the crumb view. |
| 215 | * @param show True if the icon should visible, false otherwise. |
| 216 | */ |
| 217 | private void setShowBookmarkIcon(boolean show) { |
| 218 | Drawable drawable = show ? mHeaderIcon: null; |
| 219 | mTopLevelLabel.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 222 | @Override |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 223 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 224 | if (v == mFolderNamer) { |
| 225 | if (v.getText().length() > 0) { |
| 226 | if (actionId == EditorInfo.IME_NULL) { |
| 227 | // Only want to do this once. |
| 228 | if (event.getAction() == KeyEvent.ACTION_UP) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 229 | completeOrCancelFolderNaming(false); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 230 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 231 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 232 | } |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 233 | // Steal the key press; otherwise a newline will be added |
Vivek Sekhar | 5e63147 | 2014-03-31 23:59:10 -0700 | [diff] [blame] | 234 | // return true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 235 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 236 | return false; |
| 237 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 238 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 239 | private void switchToDefaultView(boolean changedFolder) { |
| 240 | mFolderSelector.setVisibility(View.GONE); |
| 241 | mDefaultView.setVisibility(View.VISIBLE); |
| 242 | mCrumbHolder.setVisibility(View.GONE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 243 | mFakeTitleHolder.setVisibility(View.VISIBLE); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 244 | if (changedFolder) { |
| 245 | Object data = mCrumbs.getTopData(); |
| 246 | if (data != null) { |
| 247 | Folder folder = (Folder) data; |
| 248 | mCurrentFolder = folder.Id; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 249 | if (mCurrentFolder == mRootFolder) { |
| 250 | // The Spinner changed to show "Other folder ..." Change |
| 251 | // it back to "Bookmarks", which is position 0 if we are |
| 252 | // editing a folder, 1 otherwise. |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 253 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 254 | } else { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 255 | mFolderAdapter.setOtherFolderDisplayText(folder.Name); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } else { |
| 259 | // The user canceled selecting a folder. Revert back to the earlier |
| 260 | // selection. |
| 261 | if (mSaveToHomeScreen) { |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 262 | mFolder.setSelectionIgnoringSelectionChange(0); |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 263 | } else { |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 264 | if (mCurrentFolder == mRootFolder) { |
| 265 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 0 : 1); |
| 266 | } else { |
| 267 | Object data = mCrumbs.getTopData(); |
| 268 | if (data != null && ((Folder) data).Id == mCurrentFolder) { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 269 | // We are showing the correct folder hierarchy. The |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 270 | // folder selector will say "Other folder..." Change it |
| 271 | // to say the name of the folder once again. |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 272 | mFolderAdapter.setOtherFolderDisplayText(((Folder) data).Name); |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 273 | } else { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 274 | // We are not showing the correct folder hierarchy. |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 275 | // Clear the Crumbs and find the proper folder |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 276 | setupTopCrumb(); |
| 277 | LoaderManager manager = getLoaderManager(); |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 278 | manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
| 279 | |
| 280 | } |
| 281 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 286 | @Override |
| 287 | public void onClick(View v) { |
| 288 | if (v == mButton) { |
| 289 | if (mFolderSelector.getVisibility() == View.VISIBLE) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 290 | // We are showing the folder selector. |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 291 | if (mIsFolderNamerShowing) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 292 | completeOrCancelFolderNaming(false); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 293 | } else { |
| 294 | // User has selected a folder. Go back to the opening page |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 295 | mSaveToHomeScreen = false; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 296 | switchToDefaultView(true); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 297 | } |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 298 | } else { |
| 299 | // add for carrier which requires same title or address can not |
| 300 | // exist. |
| 301 | if (mSaveToHomeScreen) { |
| 302 | if (save()) { |
| 303 | return; |
| 304 | } |
| 305 | } else { |
| 306 | onSaveWithConfirm(); |
| 307 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 308 | } |
| 309 | } else if (v == mCancelButton) { |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 310 | if (mIsFolderNamerShowing) { |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 311 | completeOrCancelFolderNaming(true); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 312 | } else if (mFolderSelector.getVisibility() == View.VISIBLE) { |
| 313 | switchToDefaultView(false); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 314 | } else { |
| 315 | finish(); |
| 316 | } |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 317 | } else if (v == mFolderCancel) { |
| 318 | completeOrCancelFolderNaming(true); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 319 | } else if (v == mAddNewFolder) { |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 320 | setShowFolderNamer(true); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 321 | mFolderNamer.setText(R.string.new_folder); |
| 322 | mFolderNamer.requestFocus(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 323 | mAddNewFolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 324 | mAddSeparator.setVisibility(View.GONE); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 325 | InputMethodManager imm = getInputMethodManager(); |
| 326 | // Set the InputMethodManager to focus on the ListView so that it |
| 327 | // can transfer the focus to mFolderNamer. |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 328 | //imm.focusIn(mListView); |
| 329 | Object[] params = {mListView}; |
| 330 | Class[] type = new Class[] {View.class}; |
| 331 | ReflectHelper.invokeMethod(imm, "focusIn", type, params); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 332 | imm.showSoftInput(mFolderNamer, InputMethodManager.SHOW_IMPLICIT); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 333 | } else if (v == mRemoveLink) { |
| 334 | if (!mEditingExisting) { |
| 335 | throw new AssertionError("Remove button should not be shown for" |
| 336 | + " new bookmarks"); |
| 337 | } |
| 338 | long id = mMap.getLong(BrowserContract.Bookmarks._ID); |
| 339 | createHandler(); |
| 340 | Message msg = Message.obtain(mHandler, BOOKMARK_DELETED); |
| 341 | BookmarkUtils.displayRemoveBookmarkDialog(id, |
| 342 | mTitle.getText().toString(), this, msg); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 343 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 346 | // FolderSpinner.OnSetSelectionListener |
| 347 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 348 | @Override |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 349 | public void onSetSelection(long id) { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 350 | int intId = (int) id; |
| 351 | switch (intId) { |
| 352 | case FolderSpinnerAdapter.ROOT_FOLDER: |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 353 | mCurrentFolder = mRootFolder; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 354 | mSaveToHomeScreen = false; |
| 355 | break; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 356 | case FolderSpinnerAdapter.HOME_SCREEN: |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 357 | // Create a short cut to the home screen |
| 358 | mSaveToHomeScreen = true; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 359 | break; |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 360 | case FolderSpinnerAdapter.OTHER_FOLDER: |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 361 | switchToFolderSelector(); |
| 362 | break; |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 363 | case FolderSpinnerAdapter.RECENT_FOLDER: |
| 364 | mCurrentFolder = mFolderAdapter.recentFolderId(); |
| 365 | mSaveToHomeScreen = false; |
| 366 | // In case the user decides to select OTHER_FOLDER |
| 367 | // and choose a different one, so that we will start from |
| 368 | // the correct place. |
| 369 | LoaderManager manager = getLoaderManager(); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 370 | manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
Leon Scroggins | 0b95ad4 | 2011-02-23 15:23:48 -0500 | [diff] [blame] | 371 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 372 | default: |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 373 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 374 | } |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 377 | /** |
| 378 | * Finish naming a folder, and close the IME |
| 379 | * @param cancel If true, the new folder is not created. If false, the new |
| 380 | * folder is created and the user is taken inside it. |
| 381 | */ |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 382 | private void completeOrCancelFolderNaming(boolean cancel) { |
| 383 | if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) { |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 384 | String name = mFolderNamer.getText().toString(); |
| 385 | long id = addFolderToCurrent(mFolderNamer.getText().toString()); |
| 386 | descendInto(name, id); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 387 | } |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 388 | setShowFolderNamer(false); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 389 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 390 | mAddSeparator.setVisibility(View.VISIBLE); |
| 391 | getInputMethodManager().hideSoftInputFromWindow( |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 392 | mListView.getWindowToken(), 0); |
Michael Kolb | 31829b9 | 2010-10-01 11:50:21 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 395 | private long addFolderToCurrent(String name) { |
| 396 | // Add the folder to the database |
| 397 | ContentValues values = new ContentValues(); |
| 398 | values.put(BrowserContract.Bookmarks.TITLE, |
| 399 | name); |
| 400 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 1); |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 401 | long currentFolder; |
| 402 | Object data = mCrumbs.getTopData(); |
| 403 | if (data != null) { |
| 404 | currentFolder = ((Folder) data).Id; |
| 405 | } else { |
Leon Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 406 | currentFolder = mRootFolder; |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 407 | } |
| 408 | values.put(BrowserContract.Bookmarks.PARENT, currentFolder); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 409 | Uri uri = getContentResolver().insert( |
| 410 | BrowserContract.Bookmarks.CONTENT_URI, values); |
| 411 | if (uri != null) { |
| 412 | return ContentUris.parseId(uri); |
| 413 | } else { |
| 414 | return -1; |
| 415 | } |
| 416 | } |
| 417 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 418 | private void switchToFolderSelector() { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 419 | // Set the list to the top in case it is scrolled. |
| 420 | mListView.setSelection(0); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 421 | mDefaultView.setVisibility(View.GONE); |
| 422 | mFolderSelector.setVisibility(View.VISIBLE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 423 | mCrumbHolder.setVisibility(View.VISIBLE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 424 | mFakeTitleHolder.setVisibility(View.GONE); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 425 | mAddNewFolder.setVisibility(View.VISIBLE); |
| 426 | mAddSeparator.setVisibility(View.VISIBLE); |
John Reck | 95f88e4 | 2011-09-26 09:25:43 -0700 | [diff] [blame] | 427 | getInputMethodManager().hideSoftInputFromWindow( |
| 428 | mListView.getWindowToken(), 0); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 431 | private void descendInto(String foldername, long id) { |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 432 | if (id != DEFAULT_FOLDER_ID) { |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 433 | mCrumbs.pushView(foldername, new Folder(foldername, id)); |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 434 | mCrumbs.notifyController(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 438 | private LoaderCallbacks<EditBookmarkInfo> mEditInfoLoaderCallbacks = |
| 439 | new LoaderCallbacks<EditBookmarkInfo>() { |
| 440 | |
| 441 | @Override |
| 442 | public void onLoaderReset(Loader<EditBookmarkInfo> loader) { |
| 443 | // Don't care |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public void onLoadFinished(Loader<EditBookmarkInfo> loader, |
| 448 | EditBookmarkInfo info) { |
| 449 | boolean setAccount = false; |
| 450 | if (info.id != -1) { |
| 451 | mEditingExisting = true; |
| 452 | showRemoveButton(); |
| 453 | mFakeTitle.setText(R.string.edit_bookmark); |
| 454 | mTitle.setText(info.title); |
| 455 | mFolderAdapter.setOtherFolderDisplayText(info.parentTitle); |
| 456 | mMap.putLong(BrowserContract.Bookmarks._ID, info.id); |
| 457 | setAccount = true; |
| 458 | setAccount(info.accountName, info.accountType); |
| 459 | mCurrentFolder = info.parentId; |
| 460 | onCurrentFolderFound(); |
| 461 | } |
John Reck | 37894a9 | 2011-05-13 12:47:53 -0700 | [diff] [blame] | 462 | // TODO: Detect if lastUsedId is a subfolder of info.id in the |
| 463 | // editing folder case. For now, just don't show the last used |
| 464 | // folder at all to prevent any chance of the user adding a parent |
| 465 | // folder to a child folder |
| 466 | if (info.lastUsedId != -1 && info.lastUsedId != info.id |
| 467 | && !mEditingFolder) { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 468 | if (setAccount && info.lastUsedId != mRootFolder |
| 469 | && TextUtils.equals(info.lastUsedAccountName, info.accountName) |
| 470 | && TextUtils.equals(info.lastUsedAccountType, info.accountType)) { |
| 471 | mFolderAdapter.addRecentFolder(info.lastUsedId, info.lastUsedTitle); |
| 472 | } else if (!setAccount) { |
| 473 | setAccount = true; |
| 474 | setAccount(info.lastUsedAccountName, info.lastUsedAccountType); |
| 475 | if (info.lastUsedId != mRootFolder) { |
| 476 | mFolderAdapter.addRecentFolder(info.lastUsedId, |
| 477 | info.lastUsedTitle); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | if (!setAccount) { |
| 482 | mAccountSpinner.setSelection(0); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public Loader<EditBookmarkInfo> onCreateLoader(int id, Bundle args) { |
| 488 | return new EditBookmarkInfoLoader(AddBookmarkPage.this, mMap); |
| 489 | } |
| 490 | }; |
| 491 | |
| 492 | void setAccount(String accountName, String accountType) { |
| 493 | for (int i = 0; i < mAccountAdapter.getCount(); i++) { |
| 494 | BookmarkAccount account = mAccountAdapter.getItem(i); |
| 495 | if (TextUtils.equals(account.accountName, accountName) |
| 496 | && TextUtils.equals(account.accountType, accountType)) { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 497 | mAccountSpinner.setSelection(i); |
John Reck | 903a072 | 2011-11-09 17:53:10 -0800 | [diff] [blame] | 498 | onRootFolderFound(account.rootFolderId); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 504 | @Override |
| 505 | public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
| 506 | String[] projection; |
| 507 | switch (id) { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 508 | case LOADER_ID_ACCOUNTS: |
| 509 | return new AccountsLoader(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 510 | case LOADER_ID_FOLDER_CONTENTS: |
| 511 | projection = new String[] { |
| 512 | BrowserContract.Bookmarks._ID, |
| 513 | BrowserContract.Bookmarks.TITLE, |
| 514 | BrowserContract.Bookmarks.IS_FOLDER |
| 515 | }; |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 516 | String where = BrowserContract.Bookmarks.IS_FOLDER + " != 0"; |
John Reck | 1dd8cd4 | 2011-05-13 11:22:09 -0700 | [diff] [blame] | 517 | String whereArgs[] = null; |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 518 | if (mEditingFolder) { |
John Reck | 1dd8cd4 | 2011-05-13 11:22:09 -0700 | [diff] [blame] | 519 | where += " AND " + BrowserContract.Bookmarks._ID + " != ?"; |
| 520 | whereArgs = new String[] { Long.toString(mMap.getLong( |
| 521 | BrowserContract.Bookmarks._ID)) }; |
| 522 | } |
| 523 | long currentFolder; |
| 524 | Object data = mCrumbs.getTopData(); |
| 525 | if (data != null) { |
| 526 | currentFolder = ((Folder) data).Id; |
| 527 | } else { |
| 528 | currentFolder = mRootFolder; |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 529 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 530 | return new CursorLoader(this, |
John Reck | 1dd8cd4 | 2011-05-13 11:22:09 -0700 | [diff] [blame] | 531 | getUriForFolder(currentFolder), |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 532 | projection, |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 533 | where, |
John Reck | 1dd8cd4 | 2011-05-13 11:22:09 -0700 | [diff] [blame] | 534 | whereArgs, |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 535 | BrowserContract.Bookmarks._ID + " ASC"); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 536 | default: |
| 537 | throw new AssertionError("Asking for nonexistant loader!"); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { |
| 543 | switch (loader.getId()) { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 544 | case LOADER_ID_ACCOUNTS: |
| 545 | mAccountAdapter.clear(); |
| 546 | while (cursor.moveToNext()) { |
| 547 | mAccountAdapter.add(new BookmarkAccount(this, cursor)); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 548 | } |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 549 | getLoaderManager().destroyLoader(LOADER_ID_ACCOUNTS); |
| 550 | getLoaderManager().restartLoader(LOADER_ID_EDIT_INFO, null, |
| 551 | mEditInfoLoaderCallbacks); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 552 | break; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 553 | case LOADER_ID_FOLDER_CONTENTS: |
| 554 | mAdapter.changeCursor(cursor); |
| 555 | break; |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Dianne Hackborn | 39772c8 | 2010-12-16 00:43:54 -0800 | [diff] [blame] | 559 | 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 Scroggins | 0208194 | 2010-11-01 17:52:42 -0400 | [diff] [blame] | 567 | /** |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 568 | * 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 III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 590 | @Override |
| 591 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 592 | long id) { |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 593 | TextView tv = (TextView) view.findViewById(android.R.id.text1); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 594 | // Switch to the folder that was clicked on. |
Leon Scroggins | d14cb12 | 2010-09-29 16:01:48 -0400 | [diff] [blame] | 595 | descendInto(tv.getText().toString(), id); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 598 | 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 III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 616 | /** |
| 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 Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 633 | 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 III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 638 | } |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 639 | |
| 640 | @Override |
| 641 | public boolean isEmpty() { |
| 642 | // Do not show the empty view if the user is creating a new folder. |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 643 | return super.isEmpty() && !mIsFolderNamerShowing; |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 644 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 645 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 646 | |
Leon Scroggins | c112990 | 2010-12-08 15:28:33 -0500 | [diff] [blame] | 647 | @Override |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 648 | protected void onCreate(Bundle icicle) { |
| 649 | super.onCreate(icicle); |
Leon Scroggins | 7453ff2 | 2010-12-15 16:03:59 -0500 | [diff] [blame] | 650 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 651 | |
| 652 | mMap = getIntent().getExtras(); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 653 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 654 | setContentView(R.layout.browser_add_bookmark); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 655 | |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 656 | Window window = getWindow(); |
Michael Kolb | d40ac1a | 2010-09-29 00:23:46 -0700 | [diff] [blame] | 657 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 658 | String title = null; |
| 659 | String url = null; |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 660 | |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 661 | mFakeTitle = (TextView) findViewById(R.id.fake_title); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 662 | if (mMap != null) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 663 | Bundle b = mMap.getBundle(EXTRA_EDIT_BOOKMARK); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 664 | if (b != null) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 665 | mEditingFolder = mMap.getBoolean(EXTRA_IS_FOLDER, false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 666 | mMap = b; |
| 667 | mEditingExisting = true; |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 668 | mFakeTitle.setText(R.string.edit_bookmark); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 669 | if (mEditingFolder) { |
| 670 | findViewById(R.id.row_address).setVisibility(View.GONE); |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 671 | } else { |
| 672 | showRemoveButton(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 673 | } |
Leon Scroggins III | 76a0e9c | 2010-10-05 16:10:01 -0400 | [diff] [blame] | 674 | } else { |
| 675 | int gravity = mMap.getInt("gravity", -1); |
| 676 | if (gravity != -1) { |
| 677 | WindowManager.LayoutParams l = window.getAttributes(); |
| 678 | l.gravity = gravity; |
| 679 | window.setAttributes(l); |
| 680 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 681 | } |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 682 | title = mMap.getString(BrowserContract.Bookmarks.TITLE); |
| 683 | url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL); |
| 684 | mTouchIconUrl = mMap.getString(TOUCH_ICON_URL); |
Michael Kolb | 370a4f3 | 2010-10-06 10:45:32 -0700 | [diff] [blame] | 685 | mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID); |
Axesh R. Ajmera | 5712096 | 2014-11-13 13:37:24 -0800 | [diff] [blame] | 686 | |
Axesh R. Ajmera | db14e45 | 2015-01-21 17:42:59 -0800 | [diff] [blame] | 687 | // Check if title is not empty to prevent NPE |
| 688 | if (!TextUtils.isEmpty(title)) { |
| 689 | if (title.length() > MAX_TITLE_LENGTH) { |
| 690 | title = title.substring(0, MAX_TITLE_LENGTH); |
| 691 | } |
Axesh R. Ajmera | 5712096 | 2014-11-13 13:37:24 -0800 | [diff] [blame] | 692 | } |
Leon Scroggins | 25230d7 | 2010-09-28 20:09:25 -0400 | [diff] [blame] | 693 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 694 | |
| 695 | mTitle = (EditText) findViewById(R.id.title); |
| 696 | mTitle.setText(title); |
Axesh R. Ajmera | c815034 | 2014-10-28 11:21:09 -0700 | [diff] [blame] | 697 | BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mTitle, MAX_TITLE_LENGTH); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 698 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 699 | mAddress = (EditText) findViewById(R.id.address); |
| 700 | mAddress.setText(url); |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 701 | BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mAddress, BrowserUtils.ADDRESS_MAX_LENGTH); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 702 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 703 | mButton = (TextView) findViewById(R.id.OK); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 704 | mButton.setOnClickListener(this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 705 | |
| 706 | mCancelButton = findViewById(R.id.cancel); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 707 | mCancelButton.setOnClickListener(this); |
| 708 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 709 | mFolder = (FolderSpinner) findViewById(R.id.folder); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 710 | mFolderAdapter = new FolderSpinnerAdapter(this, !mEditingFolder); |
Leon Scroggins | 2f24e99 | 2011-02-11 14:02:10 -0500 | [diff] [blame] | 711 | mFolder.setAdapter(mFolderAdapter); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 712 | mFolder.setOnSetSelectionListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 713 | |
| 714 | mDefaultView = findViewById(R.id.default_view); |
| 715 | mFolderSelector = findViewById(R.id.folder_selector); |
| 716 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 717 | mFolderNamerHolder = getLayoutInflater().inflate(R.layout.new_folder_layout, null); |
| 718 | mFolderNamer = (EditText) mFolderNamerHolder.findViewById(R.id.folder_namer); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 719 | mFolderNamer.setOnEditorActionListener(this); |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 720 | |
| 721 | // add for carrier test about warning limit of edit text |
| 722 | BrowserUtils.maxLengthFilter(AddBookmarkPage.this, mFolderNamer, |
| 723 | BrowserUtils.FILENAME_MAX_LENGTH); |
| 724 | |
Leon Scroggins | 0e3a7b8 | 2011-01-11 19:08:03 -0500 | [diff] [blame] | 725 | mFolderCancel = mFolderNamerHolder.findViewById(R.id.close); |
| 726 | mFolderCancel.setOnClickListener(this); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 727 | |
| 728 | mAddNewFolder = findViewById(R.id.add_new_folder); |
| 729 | mAddNewFolder.setOnClickListener(this); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 730 | mAddSeparator = findViewById(R.id.add_divider); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 731 | |
Leon Scroggins | 74dbe01 | 2010-10-15 10:54:27 -0400 | [diff] [blame] | 732 | mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs); |
| 733 | mCrumbs.setUseBackButton(true); |
| 734 | mCrumbs.setController(this); |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 735 | mHeaderIcon = getResources().getDrawable(R.drawable.ic_deco_folder_normal); |
Leon Scroggins | 7e5f735 | 2010-10-18 13:25:31 -0400 | [diff] [blame] | 736 | mCrumbHolder = findViewById(R.id.crumb_holder); |
John Reck | 89f73c1 | 2010-12-01 10:10:14 -0800 | [diff] [blame] | 737 | mCrumbs.setMaxVisible(MAX_CRUMBS_SHOWN); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 738 | |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 739 | mAdapter = new FolderAdapter(this); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 740 | mListView = (CustomListView) findViewById(R.id.list); |
Leon Scroggins | 6573f9e | 2010-10-18 17:22:35 -0400 | [diff] [blame] | 741 | View empty = findViewById(R.id.empty); |
| 742 | mListView.setEmptyView(empty); |
| 743 | mListView.setAdapter(mAdapter); |
| 744 | mListView.setOnItemClickListener(this); |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 745 | mListView.addEditText(mFolderNamer); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 746 | |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 747 | mAccountAdapter = new ArrayAdapter<BookmarkAccount>(this, |
| 748 | android.R.layout.simple_spinner_item); |
| 749 | mAccountAdapter.setDropDownViewResource( |
| 750 | android.R.layout.simple_spinner_dropdown_item); |
| 751 | mAccountSpinner = (Spinner) findViewById(R.id.accounts); |
| 752 | mAccountSpinner.setAdapter(mAccountAdapter); |
| 753 | mAccountSpinner.setOnItemSelectedListener(this); |
| 754 | |
| 755 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 756 | mFakeTitleHolder = findViewById(R.id.title_holder); |
| 757 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 758 | if (!window.getDecorView().isInTouchMode()) { |
| 759 | mButton.requestFocus(); |
| 760 | } |
| 761 | |
Pankaj Garg | 4e82a9b | 2015-02-05 16:58:17 -0800 | [diff] [blame] | 762 | if (!(mCurrentFolder == -1 || mCurrentFolder == 2)) { |
| 763 | mFolder.setSelectionIgnoringSelectionChange(2); |
| 764 | mFolderAdapter.setOtherFolderDisplayText(getNameFromId(mCurrentFolder)); |
| 765 | } |
| 766 | |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 767 | getLoaderManager().restartLoader(LOADER_ID_ACCOUNTS, null, this); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 768 | } |
| 769 | |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 770 | private void showRemoveButton() { |
| 771 | findViewById(R.id.remove_divider).setVisibility(View.VISIBLE); |
| 772 | mRemoveLink = findViewById(R.id.remove); |
| 773 | mRemoveLink.setVisibility(View.VISIBLE); |
| 774 | mRemoveLink.setOnClickListener(this); |
| 775 | } |
| 776 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 777 | // Called once we have determined which folder is the root folder |
| 778 | private void onRootFolderFound(long root) { |
| 779 | mRootFolder = root; |
Pankaj Garg | 6339694 | 2015-03-06 10:05:25 -0800 | [diff] [blame^] | 780 | mCurrentFolder = (mCurrentFolder == -1) ? mRootFolder : mCurrentFolder; |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 781 | setupTopCrumb(); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 782 | onCurrentFolderFound(); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 783 | } |
| 784 | |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 785 | private void setupTopCrumb() { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 786 | mCrumbs.clear(); |
Leon Scroggins | b3ae880 | 2011-01-18 11:55:27 -0500 | [diff] [blame] | 787 | String name = getString(R.string.bookmarks); |
| 788 | mTopLevelLabel = (TextView) mCrumbs.pushView(name, false, |
| 789 | new Folder(name, mRootFolder)); |
| 790 | // To better match the other folders. |
| 791 | mTopLevelLabel.setCompoundDrawablePadding(6); |
| 792 | } |
| 793 | |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 794 | private void onCurrentFolderFound() { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 795 | LoaderManager manager = getLoaderManager(); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 796 | if (mCurrentFolder != mRootFolder) { |
Leon Scroggins | dd13bad | 2011-01-06 20:25:54 -0500 | [diff] [blame] | 797 | // Since we're not in the root folder, change the selection to other |
| 798 | // folder now. The text will get changed once we select the correct |
| 799 | // folder. |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 800 | mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2); |
Leon Scroggins | 905250c | 2010-12-17 15:25:33 -0500 | [diff] [blame] | 801 | } else { |
| 802 | setShowBookmarkIcon(true); |
Leon Scroggins | 504433a | 2011-01-13 12:26:52 -0500 | [diff] [blame] | 803 | if (!mEditingFolder) { |
| 804 | // Initially the "Bookmarks" folder should be showing, rather than |
| 805 | // the home screen. In the editing folder case, home screen is not |
| 806 | // an option, so "Bookmarks" folder is already at the top. |
| 807 | mFolder.setSelectionIgnoringSelectionChange(FolderSpinnerAdapter.ROOT_FOLDER); |
| 808 | } |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 809 | } |
Leon Scroggins | 52f7daa | 2010-10-18 16:57:46 -0400 | [diff] [blame] | 810 | // Find the contents of the current folder |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 811 | manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this); |
Leon Scroggins | 8baaa63 | 2010-12-08 19:46:53 -0500 | [diff] [blame] | 812 | } |
| 813 | |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 814 | /** |
| 815 | * Runnable to save a bookmark, so it can be performed in its own thread. |
| 816 | */ |
| 817 | private class SaveBookmarkRunnable implements Runnable { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 818 | // FIXME: This should be an async task. |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 819 | private Message mMessage; |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 820 | private Context mContext; |
| 821 | public SaveBookmarkRunnable(Context ctx, Message msg) { |
Ben Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 822 | mContext = ctx.getApplicationContext(); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 823 | mMessage = msg; |
| 824 | } |
| 825 | public void run() { |
| 826 | // Unbundle bookmark data. |
| 827 | Bundle bundle = mMessage.getData(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 828 | String title = bundle.getString(BrowserContract.Bookmarks.TITLE); |
| 829 | String url = bundle.getString(BrowserContract.Bookmarks.URL); |
| 830 | boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 831 | Bitmap thumbnail = invalidateThumbnail ? null |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 832 | : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL); |
| 833 | String touchIconUrl = bundle.getString(TOUCH_ICON_URL); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 834 | |
| 835 | // Save to the bookmarks DB. |
| 836 | try { |
| 837 | final ContentResolver cr = getContentResolver(); |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 838 | Bookmarks.addBookmark(AddBookmarkPage.this, false, url, |
John Reck | af262e7 | 2011-07-25 13:55:44 -0700 | [diff] [blame] | 839 | title, thumbnail, mCurrentFolder); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 840 | if (touchIconUrl != null) { |
Henrik Baard | 980e995 | 2010-09-06 18:13:23 +0200 | [diff] [blame] | 841 | new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl); |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 842 | } |
| 843 | mMessage.arg1 = 1; |
| 844 | } catch (IllegalStateException e) { |
| 845 | mMessage.arg1 = 0; |
| 846 | } |
| 847 | mMessage.sendToTarget(); |
| 848 | } |
| 849 | } |
| 850 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 851 | private static class UpdateBookmarkTask extends AsyncTask<ContentValues, Void, Void> { |
| 852 | Context mContext; |
| 853 | Long mId; |
| 854 | |
| 855 | public UpdateBookmarkTask(Context context, long id) { |
Ben Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 856 | mContext = context.getApplicationContext(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 857 | mId = id; |
| 858 | } |
| 859 | |
| 860 | @Override |
| 861 | protected Void doInBackground(ContentValues... params) { |
| 862 | if (params.length != 1) { |
| 863 | throw new IllegalArgumentException("No ContentValues provided!"); |
| 864 | } |
| 865 | Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(mContext), mId); |
| 866 | mContext.getContentResolver().update( |
| 867 | uri, |
| 868 | params[0], null, null); |
| 869 | return null; |
| 870 | } |
| 871 | } |
| 872 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 873 | private void createHandler() { |
| 874 | if (mHandler == null) { |
| 875 | mHandler = new Handler() { |
| 876 | @Override |
| 877 | public void handleMessage(Message msg) { |
| 878 | switch (msg.what) { |
| 879 | case SAVE_BOOKMARK: |
Leon Scroggins | 02065b0 | 2010-01-04 14:30:13 -0500 | [diff] [blame] | 880 | if (1 == msg.arg1) { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 881 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved, |
| 882 | Toast.LENGTH_LONG).show(); |
| 883 | } else { |
| 884 | Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved, |
| 885 | Toast.LENGTH_LONG).show(); |
| 886 | } |
| 887 | break; |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 888 | case TOUCH_ICON_DOWNLOADED: |
| 889 | Bundle b = msg.getData(); |
| 890 | sendBroadcast(BookmarkUtils.createAddToHomeIntent( |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 891 | AddBookmarkPage.this, |
| 892 | b.getString(BrowserContract.Bookmarks.URL), |
| 893 | b.getString(BrowserContract.Bookmarks.TITLE), |
| 894 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON), |
| 895 | (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON))); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 896 | break; |
Leon Scroggins | 7563067 | 2011-01-13 17:56:15 -0500 | [diff] [blame] | 897 | case BOOKMARK_DELETED: |
| 898 | finish(); |
| 899 | break; |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | }; |
| 903 | } |
| 904 | } |
| 905 | |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 906 | static void deleteDuplicateBookmark(final Context context, final long id) { |
| 907 | Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, id); |
| 908 | context.getContentResolver().delete(uri, null, null); |
| 909 | } |
| 910 | |
| 911 | private void onSaveWithConfirm() { |
| 912 | String title = mTitle.getText().toString().trim(); |
| 913 | String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString()); |
| 914 | String url = unfilteredUrl.trim(); |
| 915 | Long id = mMap.getLong(BrowserContract.Bookmarks._ID); |
| 916 | int duplicateCount; |
| 917 | final ContentResolver cr = getContentResolver(); |
| 918 | |
| 919 | Cursor cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI, |
| 920 | BookmarksLoader.PROJECTION, |
| 921 | "( title = ? OR url = ? ) AND parent = ?", |
| 922 | new String[] { |
| 923 | title, url, Long.toString(mCurrentFolder) |
| 924 | }, |
| 925 | null); |
| 926 | |
| 927 | if (cursor == null) { |
| 928 | save(); |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | duplicateCount = cursor.getCount(); |
| 933 | if (duplicateCount <= 0) { |
| 934 | cursor.close(); |
| 935 | save(); |
| 936 | return; |
| 937 | } else { |
| 938 | try { |
| 939 | while (cursor.moveToNext()) { |
| 940 | mDuplicateId = cursor.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 941 | mDuplicateContext = AddBookmarkPage.this; |
| 942 | } |
| 943 | } catch (IllegalStateException e) { |
| 944 | e.printStackTrace(); |
| 945 | } finally { |
| 946 | if (cursor != null) |
| 947 | cursor.close(); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | if (mEditingExisting && duplicateCount == 1 && mDuplicateId == id) { |
| 952 | save(); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | new AlertDialog.Builder(this) |
| 957 | .setTitle(getString(R.string.save_to_bookmarks_title)) |
| 958 | .setMessage(getString(R.string.overwrite_bookmark_msg)) |
| 959 | .setNegativeButton(android.R.string.cancel, null) |
| 960 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 961 | public void onClick(DialogInterface dialog, int which) { |
| 962 | if (mDuplicateContext == null) { |
| 963 | return; |
| 964 | } |
| 965 | deleteDuplicateBookmark(mDuplicateContext, mDuplicateId); |
| 966 | save(); |
| 967 | } |
| 968 | }) |
| 969 | .show(); |
| 970 | } |
| 971 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 972 | /** |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 973 | * Parse the data entered in the dialog and post a message to update the bookmarks database. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 974 | */ |
| 975 | boolean save() { |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 976 | createHandler(); |
| 977 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 978 | String title = mTitle.getText().toString().trim(); |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 979 | String unfilteredUrl = UrlUtils.fixUrl(mAddress.getText().toString()); |
Ben Murdoch | eecb4e6 | 2010-07-06 16:30:38 +0100 | [diff] [blame] | 980 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 981 | boolean emptyTitle = title.length() == 0; |
| 982 | boolean emptyUrl = unfilteredUrl.trim().length() == 0; |
| 983 | Resources r = getResources(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 984 | if (emptyTitle || (emptyUrl && !mEditingFolder)) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 985 | if (emptyTitle) { |
| 986 | mTitle.setError(r.getText(R.string.bookmark_needs_title)); |
| 987 | } |
| 988 | if (emptyUrl) { |
Leon Scroggins III | 052ce66 | 2010-09-13 14:44:16 -0400 | [diff] [blame] | 989 | mAddress.setError(r.getText(R.string.bookmark_needs_url)); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 990 | } |
Leon Scroggins III | 6e3faea | 2010-09-07 15:22:14 -0400 | [diff] [blame] | 991 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 992 | } |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 993 | String url = unfilteredUrl.trim(); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 994 | if (!mEditingFolder) { |
| 995 | try { |
| 996 | // We allow bookmarks with a javascript: scheme, but these will in most cases |
| 997 | // fail URI parsing, so don't try it if that's the kind of bookmark we have. |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 998 | |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 999 | if (!url.toLowerCase().startsWith("javascript:")) { |
Vivek Sekhar | f95fb38 | 2014-07-23 11:13:44 -0700 | [diff] [blame] | 1000 | String encodedUrl = URLEncoder.encode(url, "UTF-8"); |
| 1001 | URI uriObj = new URI(encodedUrl); |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 1002 | String scheme = uriObj.getScheme(); |
| 1003 | if (!Bookmarks.urlHasAcceptableScheme(url)) { |
| 1004 | // If the scheme was non-null, let the user know that we |
| 1005 | // can't save their bookmark. If it was null, we'll assume |
| 1006 | // they meant http when we parse it in the WebAddress class. |
| 1007 | if (scheme != null) { |
| 1008 | mAddress.setError(r.getText(R.string.bookmark_cannot_save_url)); |
| 1009 | return false; |
| 1010 | } |
| 1011 | WebAddress address; |
| 1012 | try { |
| 1013 | address = new WebAddress(unfilteredUrl); |
| 1014 | } catch (ParseException e) { |
| 1015 | throw new URISyntaxException("", ""); |
| 1016 | } |
| 1017 | if (address.getHost().length() == 0) { |
| 1018 | throw new URISyntaxException("", ""); |
| 1019 | } |
| 1020 | url = address.toString(); |
Ben Murdoch | ca12cfa | 2009-11-17 13:57:44 +0000 | [diff] [blame] | 1021 | } |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 1022 | } |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 1023 | } catch (URISyntaxException e) { |
| 1024 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
| 1025 | return false; |
Vivek Sekhar | f95fb38 | 2014-07-23 11:13:44 -0700 | [diff] [blame] | 1026 | } catch (UnsupportedEncodingException e) { |
| 1027 | mAddress.setError(r.getText(R.string.bookmark_url_not_valid)); |
| 1028 | return false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1029 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1030 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 1031 | |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1032 | if (mSaveToHomeScreen) { |
| 1033 | mEditingExisting = false; |
| 1034 | } |
| 1035 | |
| 1036 | boolean urlUnmodified = url.equals(mOriginalUrl); |
| 1037 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1038 | if (mEditingExisting) { |
John Reck | c849081 | 2010-11-22 14:15:36 -0800 | [diff] [blame] | 1039 | Long id = mMap.getLong(BrowserContract.Bookmarks._ID); |
| 1040 | ContentValues values = new ContentValues(); |
| 1041 | values.put(BrowserContract.Bookmarks.TITLE, title); |
| 1042 | values.put(BrowserContract.Bookmarks.PARENT, mCurrentFolder); |
| 1043 | if (!mEditingFolder) { |
| 1044 | values.put(BrowserContract.Bookmarks.URL, url); |
| 1045 | if (!urlUnmodified) { |
| 1046 | values.putNull(BrowserContract.Bookmarks.THUMBNAIL); |
| 1047 | } |
| 1048 | } |
| 1049 | if (values.size() > 0) { |
| 1050 | new UpdateBookmarkTask(getApplicationContext(), id).execute(values); |
| 1051 | } |
| 1052 | setResult(RESULT_OK); |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1053 | } else { |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1054 | Bitmap thumbnail; |
| 1055 | Bitmap favicon; |
| 1056 | if (urlUnmodified) { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1057 | thumbnail = (Bitmap) mMap.getParcelable( |
| 1058 | BrowserContract.Bookmarks.THUMBNAIL); |
| 1059 | favicon = (Bitmap) mMap.getParcelable( |
| 1060 | BrowserContract.Bookmarks.FAVICON); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1061 | } else { |
| 1062 | thumbnail = null; |
| 1063 | favicon = null; |
| 1064 | } |
| 1065 | |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1066 | Bundle bundle = new Bundle(); |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1067 | bundle.putString(BrowserContract.Bookmarks.TITLE, title); |
| 1068 | bundle.putString(BrowserContract.Bookmarks.URL, url); |
| 1069 | bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1070 | |
| 1071 | if (mSaveToHomeScreen) { |
| 1072 | if (mTouchIconUrl != null && urlUnmodified) { |
| 1073 | Message msg = Message.obtain(mHandler, |
| 1074 | TOUCH_ICON_DOWNLOADED); |
| 1075 | msg.setData(bundle); |
| 1076 | DownloadTouchIcon icon = new DownloadTouchIcon(this, msg, |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1077 | mMap.getString(USER_AGENT)); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1078 | icon.execute(mTouchIconUrl); |
| 1079 | } else { |
| 1080 | sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url, |
| 1081 | title, null /*touchIcon*/, favicon)); |
| 1082 | } |
| 1083 | } else { |
Leon Scroggins | bc92285 | 2010-10-22 12:15:27 -0400 | [diff] [blame] | 1084 | bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail); |
| 1085 | bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified); |
| 1086 | bundle.putString(TOUCH_ICON_URL, mTouchIconUrl); |
Leon Scroggins | 88d0803 | 2010-10-21 15:17:10 -0400 | [diff] [blame] | 1087 | // Post a message to write to the DB. |
| 1088 | Message msg = Message.obtain(mHandler, SAVE_BOOKMARK); |
| 1089 | msg.setData(bundle); |
| 1090 | // Start a new thread so as to not slow down the UI |
| 1091 | Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg)); |
| 1092 | t.start(); |
| 1093 | } |
Ben Murdoch | 1794fe2 | 2009-09-29 18:14:30 +0100 | [diff] [blame] | 1094 | setResult(RESULT_OK); |
Kristian Monsen | 0a1d838 | 2010-02-01 18:41:07 +0000 | [diff] [blame] | 1095 | LogTag.logBookmarkAdded(url, "bookmarkview"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1096 | } |
luxiaol | dbe4a62 | 2013-07-19 17:14:06 +0800 | [diff] [blame] | 1097 | finish(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1098 | return true; |
| 1099 | } |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 1100 | |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1101 | @Override |
| 1102 | public void onItemSelected(AdapterView<?> parent, View view, int position, |
| 1103 | long id) { |
| 1104 | if (mAccountSpinner == parent) { |
| 1105 | long root = mAccountAdapter.getItem(position).rootFolderId; |
| 1106 | if (root != mRootFolder) { |
| 1107 | onRootFolderFound(root); |
John Reck | e890c90 | 2011-07-11 17:44:43 -0700 | [diff] [blame] | 1108 | mFolderAdapter.clearRecentFolder(); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | @Override |
| 1114 | public void onNothingSelected(AdapterView<?> parent) { |
| 1115 | // Don't care |
| 1116 | } |
| 1117 | |
Leon Scroggins | 162f835 | 2010-10-18 15:02:44 -0400 | [diff] [blame] | 1118 | /* |
| 1119 | * Class used as a proxy for the InputMethodManager to get to mFolderNamer |
| 1120 | */ |
| 1121 | public static class CustomListView extends ListView { |
| 1122 | private EditText mEditText; |
| 1123 | |
| 1124 | public void addEditText(EditText editText) { |
| 1125 | mEditText = editText; |
| 1126 | } |
| 1127 | |
| 1128 | public CustomListView(Context context) { |
| 1129 | super(context); |
| 1130 | } |
| 1131 | |
| 1132 | public CustomListView(Context context, AttributeSet attrs) { |
| 1133 | super(context, attrs); |
| 1134 | } |
| 1135 | |
| 1136 | public CustomListView(Context context, AttributeSet attrs, int defStyle) { |
| 1137 | super(context, attrs, defStyle); |
| 1138 | } |
| 1139 | |
| 1140 | @Override |
| 1141 | public boolean checkInputConnectionProxy(View view) { |
| 1142 | return view == mEditText; |
| 1143 | } |
| 1144 | } |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1145 | |
John Reck | 3bf3cfb | 2011-10-17 18:00:05 -0700 | [diff] [blame] | 1146 | static class AccountsLoader extends CursorLoader { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1147 | |
| 1148 | static final String[] PROJECTION = new String[] { |
| 1149 | Accounts.ACCOUNT_NAME, |
| 1150 | Accounts.ACCOUNT_TYPE, |
| 1151 | Accounts.ROOT_ID, |
| 1152 | }; |
| 1153 | |
| 1154 | static final int COLUMN_INDEX_ACCOUNT_NAME = 0; |
| 1155 | static final int COLUMN_INDEX_ACCOUNT_TYPE = 1; |
| 1156 | static final int COLUMN_INDEX_ROOT_ID = 2; |
| 1157 | |
| 1158 | public AccountsLoader(Context context) { |
John Reck | 1e9815d | 2011-08-08 17:45:05 -0700 | [diff] [blame] | 1159 | super(context, Accounts.CONTENT_URI, PROJECTION, null, null, null); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | } |
| 1163 | |
John Reck | 9b8cd1e | 2011-05-25 18:14:01 -0700 | [diff] [blame] | 1164 | public static class BookmarkAccount { |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1165 | |
| 1166 | private String mLabel; |
| 1167 | String accountName, accountType; |
John Reck | 9b8cd1e | 2011-05-25 18:14:01 -0700 | [diff] [blame] | 1168 | public long rootFolderId; |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1169 | |
| 1170 | public BookmarkAccount(Context context, Cursor cursor) { |
| 1171 | accountName = cursor.getString( |
| 1172 | AccountsLoader.COLUMN_INDEX_ACCOUNT_NAME); |
| 1173 | accountType = cursor.getString( |
| 1174 | AccountsLoader.COLUMN_INDEX_ACCOUNT_TYPE); |
| 1175 | rootFolderId = cursor.getLong( |
| 1176 | AccountsLoader.COLUMN_INDEX_ROOT_ID); |
| 1177 | mLabel = accountName; |
| 1178 | if (TextUtils.isEmpty(mLabel)) { |
| 1179 | mLabel = context.getString(R.string.local_bookmarks); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | @Override |
| 1184 | public String toString() { |
| 1185 | return mLabel; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | static class EditBookmarkInfo { |
| 1190 | long id = -1; |
| 1191 | long parentId = -1; |
| 1192 | String parentTitle; |
| 1193 | String title; |
| 1194 | String accountName; |
| 1195 | String accountType; |
| 1196 | |
| 1197 | long lastUsedId = -1; |
| 1198 | String lastUsedTitle; |
| 1199 | String lastUsedAccountName; |
| 1200 | String lastUsedAccountType; |
| 1201 | } |
| 1202 | |
| 1203 | static class EditBookmarkInfoLoader extends AsyncTaskLoader<EditBookmarkInfo> { |
| 1204 | |
| 1205 | private Context mContext; |
| 1206 | private Bundle mMap; |
| 1207 | |
| 1208 | public EditBookmarkInfoLoader(Context context, Bundle bundle) { |
| 1209 | super(context); |
Ben Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 1210 | mContext = context.getApplicationContext(); |
John Reck | 2eec4c3 | 2011-05-11 15:55:32 -0700 | [diff] [blame] | 1211 | mMap = bundle; |
| 1212 | } |
| 1213 | |
| 1214 | @Override |
| 1215 | public EditBookmarkInfo loadInBackground() { |
| 1216 | final ContentResolver cr = mContext.getContentResolver(); |
| 1217 | EditBookmarkInfo info = new EditBookmarkInfo(); |
| 1218 | Cursor c = null; |
| 1219 | |
| 1220 | try { |
| 1221 | // First, let's lookup the bookmark (check for dupes, get needed info) |
| 1222 | String url = mMap.getString(BrowserContract.Bookmarks.URL); |
| 1223 | info.id = mMap.getLong(BrowserContract.Bookmarks._ID, -1); |
| 1224 | boolean checkForDupe = mMap.getBoolean(CHECK_FOR_DUPE); |
| 1225 | if (checkForDupe && info.id == -1 && !TextUtils.isEmpty(url)) { |
| 1226 | c = cr.query(BrowserContract.Bookmarks.CONTENT_URI, |
| 1227 | new String[] { BrowserContract.Bookmarks._ID}, |
| 1228 | BrowserContract.Bookmarks.URL + "=?", |
| 1229 | new String[] { url }, null); |
| 1230 | if (c.getCount() == 1 && c.moveToFirst()) { |
| 1231 | info.id = c.getLong(0); |
| 1232 | } |
| 1233 | c.close(); |
| 1234 | } |
| 1235 | if (info.id != -1) { |
| 1236 | c = cr.query(ContentUris.withAppendedId( |
| 1237 | BrowserContract.Bookmarks.CONTENT_URI, info.id), |
| 1238 | new String[] { |
| 1239 | BrowserContract.Bookmarks.PARENT, |
| 1240 | BrowserContract.Bookmarks.ACCOUNT_NAME, |
| 1241 | BrowserContract.Bookmarks.ACCOUNT_TYPE, |
| 1242 | BrowserContract.Bookmarks.TITLE}, |
| 1243 | null, null, null); |
| 1244 | if (c.moveToFirst()) { |
| 1245 | info.parentId = c.getLong(0); |
| 1246 | info.accountName = c.getString(1); |
| 1247 | info.accountType = c.getString(2); |
| 1248 | info.title = c.getString(3); |
| 1249 | } |
| 1250 | c.close(); |
| 1251 | c = cr.query(ContentUris.withAppendedId( |
| 1252 | BrowserContract.Bookmarks.CONTENT_URI, info.parentId), |
| 1253 | new String[] { |
| 1254 | BrowserContract.Bookmarks.TITLE,}, |
| 1255 | null, null, null); |
| 1256 | if (c.moveToFirst()) { |
| 1257 | info.parentTitle = c.getString(0); |
| 1258 | } |
| 1259 | c.close(); |
| 1260 | } |
| 1261 | |
| 1262 | // Figure out the last used folder/account |
| 1263 | c = cr.query(BrowserContract.Bookmarks.CONTENT_URI, |
| 1264 | new String[] { |
| 1265 | BrowserContract.Bookmarks.PARENT, |
| 1266 | }, null, null, |
| 1267 | BrowserContract.Bookmarks.DATE_MODIFIED + " DESC LIMIT 1"); |
| 1268 | if (c.moveToFirst()) { |
| 1269 | long parent = c.getLong(0); |
| 1270 | c.close(); |
| 1271 | c = cr.query(BrowserContract.Bookmarks.CONTENT_URI, |
| 1272 | new String[] { |
| 1273 | BrowserContract.Bookmarks.TITLE, |
| 1274 | BrowserContract.Bookmarks.ACCOUNT_NAME, |
| 1275 | BrowserContract.Bookmarks.ACCOUNT_TYPE}, |
| 1276 | BrowserContract.Bookmarks._ID + "=?", new String[] { |
| 1277 | Long.toString(parent)}, null); |
| 1278 | if (c.moveToFirst()) { |
| 1279 | info.lastUsedId = parent; |
| 1280 | info.lastUsedTitle = c.getString(0); |
| 1281 | info.lastUsedAccountName = c.getString(1); |
| 1282 | info.lastUsedAccountType = c.getString(2); |
| 1283 | } |
| 1284 | c.close(); |
| 1285 | } |
| 1286 | } finally { |
| 1287 | if (c != null) { |
| 1288 | c.close(); |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | return info; |
| 1293 | } |
| 1294 | |
| 1295 | @Override |
| 1296 | protected void onStartLoading() { |
| 1297 | forceLoad(); |
| 1298 | } |
| 1299 | |
| 1300 | } |
| 1301 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1302 | } |