Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.db.BrowserContract.Bookmarks; |
michael@0 | 9 | import org.mozilla.gecko.db.BrowserDB; |
michael@0 | 10 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 11 | import org.mozilla.gecko.util.UiAsyncTask; |
michael@0 | 12 | |
michael@0 | 13 | import android.content.Context; |
michael@0 | 14 | import android.app.AlertDialog; |
michael@0 | 15 | import android.content.DialogInterface; |
michael@0 | 16 | import android.database.Cursor; |
michael@0 | 17 | import android.text.Editable; |
michael@0 | 18 | import android.text.TextWatcher; |
michael@0 | 19 | import android.view.LayoutInflater; |
michael@0 | 20 | import android.view.View; |
michael@0 | 21 | import android.widget.EditText; |
michael@0 | 22 | import android.widget.Toast; |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * A dialog that allows editing a bookmarks url, title, or keywords |
michael@0 | 26 | * <p> |
michael@0 | 27 | * Invoked by calling one of the {@link org.mozilla.gecko.EditBookmarkDialog.show} |
michael@0 | 28 | * methods. |
michael@0 | 29 | */ |
michael@0 | 30 | public class EditBookmarkDialog { |
michael@0 | 31 | private static final String LOGTAG = "GeckoEditBookmarkDialog"; |
michael@0 | 32 | private Context mContext; |
michael@0 | 33 | |
michael@0 | 34 | public EditBookmarkDialog(Context context) { |
michael@0 | 35 | mContext = context; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * A private struct to make it easier to pass bookmark data across threads |
michael@0 | 40 | */ |
michael@0 | 41 | private class Bookmark { |
michael@0 | 42 | int id; |
michael@0 | 43 | String title; |
michael@0 | 44 | String url; |
michael@0 | 45 | String keyword; |
michael@0 | 46 | |
michael@0 | 47 | public Bookmark(int aId, String aTitle, String aUrl, String aKeyword) { |
michael@0 | 48 | id = aId; |
michael@0 | 49 | title = aTitle; |
michael@0 | 50 | url = aUrl; |
michael@0 | 51 | keyword = aKeyword; |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * This text watcher to enable or disable the OK button if the dialog contains |
michael@0 | 57 | * valid information. This class is overridden to do data checking diffferent fields. |
michael@0 | 58 | * By itself, it always enables the button. |
michael@0 | 59 | * |
michael@0 | 60 | * Callers can also assing a paired partner to the TextWatcher, and callers will check |
michael@0 | 61 | * that both are enabled before enabling the ok button. |
michael@0 | 62 | */ |
michael@0 | 63 | private class EditBookmarkTextWatcher implements TextWatcher { |
michael@0 | 64 | // A stored reference to the dialog containing the text field being watched |
michael@0 | 65 | protected AlertDialog mDialog; |
michael@0 | 66 | |
michael@0 | 67 | // A stored text watcher to do the real verification of a field |
michael@0 | 68 | protected EditBookmarkTextWatcher mPairedTextWatcher; |
michael@0 | 69 | |
michael@0 | 70 | // Whether or not the ok button should be enabled. |
michael@0 | 71 | protected boolean mEnabled = true; |
michael@0 | 72 | |
michael@0 | 73 | public EditBookmarkTextWatcher(AlertDialog aDialog) { |
michael@0 | 74 | mDialog = aDialog; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | public void setPairedTextWatcher(EditBookmarkTextWatcher aTextWatcher) { |
michael@0 | 78 | mPairedTextWatcher = aTextWatcher; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | public boolean isEnabled() { |
michael@0 | 82 | return mEnabled; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // Textwatcher interface |
michael@0 | 86 | @Override |
michael@0 | 87 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
michael@0 | 88 | // Disable if the we're disabled or the paired partner is disabled |
michael@0 | 89 | boolean enabled = mEnabled && (mPairedTextWatcher == null || mPairedTextWatcher.isEnabled()); |
michael@0 | 90 | mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | @Override |
michael@0 | 94 | public void afterTextChanged(Editable s) {} |
michael@0 | 95 | @Override |
michael@0 | 96 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /** |
michael@0 | 100 | * A version of the EditBookmarkTextWatcher for the url field of the dialog. |
michael@0 | 101 | * Only checks if the field is empty or not. |
michael@0 | 102 | */ |
michael@0 | 103 | private class LocationTextWatcher extends EditBookmarkTextWatcher { |
michael@0 | 104 | public LocationTextWatcher(AlertDialog aDialog) { |
michael@0 | 105 | super(aDialog); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | // Disables the ok button if the location field is empty. |
michael@0 | 109 | @Override |
michael@0 | 110 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
michael@0 | 111 | mEnabled = (s.toString().trim().length() > 0); |
michael@0 | 112 | super.onTextChanged(s, start, before, count); |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * A version of the EditBookmarkTextWatcher for the keyword field of the dialog. |
michael@0 | 118 | * Checks if the field has any (non leading or trailing) spaces. |
michael@0 | 119 | */ |
michael@0 | 120 | private class KeywordTextWatcher extends EditBookmarkTextWatcher { |
michael@0 | 121 | public KeywordTextWatcher(AlertDialog aDialog) { |
michael@0 | 122 | super(aDialog); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | @Override |
michael@0 | 126 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
michael@0 | 127 | // Disable if the keyword contains spaces |
michael@0 | 128 | mEnabled = (s.toString().trim().indexOf(' ') == -1); |
michael@0 | 129 | super.onTextChanged(s, start, before, count); |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | /** |
michael@0 | 134 | * Show the Edit bookmark dialog for a particular url. If the url is bookmarked multiple times |
michael@0 | 135 | * this will just edit the first instance it finds. |
michael@0 | 136 | * |
michael@0 | 137 | * @param url The url of the bookmark to edit. The dialog will look up other information like the id, |
michael@0 | 138 | * current title, or keywords associated with this url. If the url isn't bookmarked, the |
michael@0 | 139 | * dialog will fail silently. If the url is bookmarked multiple times, this will only show |
michael@0 | 140 | * information about the first it finds. |
michael@0 | 141 | */ |
michael@0 | 142 | public void show(final String url) { |
michael@0 | 143 | (new UiAsyncTask<Void, Void, Bookmark>(ThreadUtils.getBackgroundHandler()) { |
michael@0 | 144 | @Override |
michael@0 | 145 | public Bookmark doInBackground(Void... params) { |
michael@0 | 146 | Cursor cursor = BrowserDB.getBookmarkForUrl(mContext.getContentResolver(), url); |
michael@0 | 147 | if (cursor == null) { |
michael@0 | 148 | return null; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | Bookmark bookmark = null; |
michael@0 | 152 | try { |
michael@0 | 153 | cursor.moveToFirst(); |
michael@0 | 154 | bookmark = new Bookmark(cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID)), |
michael@0 | 155 | cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.TITLE)), |
michael@0 | 156 | cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.URL)), |
michael@0 | 157 | cursor.getString(cursor.getColumnIndexOrThrow(Bookmarks.KEYWORD))); |
michael@0 | 158 | } finally { |
michael@0 | 159 | cursor.close(); |
michael@0 | 160 | } |
michael@0 | 161 | return bookmark; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | @Override |
michael@0 | 165 | public void onPostExecute(Bookmark bookmark) { |
michael@0 | 166 | if (bookmark == null) |
michael@0 | 167 | return; |
michael@0 | 168 | |
michael@0 | 169 | show(bookmark.id, bookmark.title, bookmark.url, bookmark.keyword); |
michael@0 | 170 | } |
michael@0 | 171 | }).execute(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | /** |
michael@0 | 175 | * Show the Edit bookmark dialog for a set of data. This will show the dialog whether |
michael@0 | 176 | * a bookmark with this url exists or not, but the results will NOT be saved if the id |
michael@0 | 177 | * is not a valid bookmark id. |
michael@0 | 178 | * |
michael@0 | 179 | * @param id The id of the bookmark to change. If there is no bookmark with this ID, the dialog |
michael@0 | 180 | * will fail silently. |
michael@0 | 181 | * @param title The initial title to show in the dialog |
michael@0 | 182 | * @param url The initial url to show in the dialog |
michael@0 | 183 | * @param keyword The initial keyword to show in the dialog |
michael@0 | 184 | */ |
michael@0 | 185 | public void show(final int id, final String title, final String url, final String keyword) { |
michael@0 | 186 | AlertDialog.Builder editPrompt = new AlertDialog.Builder(mContext); |
michael@0 | 187 | final View editView = LayoutInflater.from(mContext).inflate(R.layout.bookmark_edit, null); |
michael@0 | 188 | editPrompt.setTitle(R.string.bookmark_edit_title); |
michael@0 | 189 | editPrompt.setView(editView); |
michael@0 | 190 | |
michael@0 | 191 | final EditText nameText = ((EditText) editView.findViewById(R.id.edit_bookmark_name)); |
michael@0 | 192 | final EditText locationText = ((EditText) editView.findViewById(R.id.edit_bookmark_location)); |
michael@0 | 193 | final EditText keywordText = ((EditText) editView.findViewById(R.id.edit_bookmark_keyword)); |
michael@0 | 194 | nameText.setText(title); |
michael@0 | 195 | locationText.setText(url); |
michael@0 | 196 | keywordText.setText(keyword); |
michael@0 | 197 | |
michael@0 | 198 | editPrompt.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() { |
michael@0 | 199 | @Override |
michael@0 | 200 | public void onClick(DialogInterface dialog, int whichButton) { |
michael@0 | 201 | (new UiAsyncTask<Void, Void, Void>(ThreadUtils.getBackgroundHandler()) { |
michael@0 | 202 | @Override |
michael@0 | 203 | public Void doInBackground(Void... params) { |
michael@0 | 204 | String newUrl = locationText.getText().toString().trim(); |
michael@0 | 205 | String newKeyword = keywordText.getText().toString().trim(); |
michael@0 | 206 | BrowserDB.updateBookmark(mContext.getContentResolver(), id, newUrl, nameText.getText().toString(), newKeyword); |
michael@0 | 207 | return null; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | @Override |
michael@0 | 211 | public void onPostExecute(Void result) { |
michael@0 | 212 | Toast.makeText(mContext, R.string.bookmark_updated, Toast.LENGTH_SHORT).show(); |
michael@0 | 213 | } |
michael@0 | 214 | }).execute(); |
michael@0 | 215 | } |
michael@0 | 216 | }); |
michael@0 | 217 | |
michael@0 | 218 | editPrompt.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() { |
michael@0 | 219 | @Override |
michael@0 | 220 | public void onClick(DialogInterface dialog, int whichButton) { |
michael@0 | 221 | // do nothing |
michael@0 | 222 | } |
michael@0 | 223 | }); |
michael@0 | 224 | |
michael@0 | 225 | final AlertDialog dialog = editPrompt.create(); |
michael@0 | 226 | |
michael@0 | 227 | // Create our TextWatchers |
michael@0 | 228 | LocationTextWatcher locationTextWatcher = new LocationTextWatcher(dialog); |
michael@0 | 229 | KeywordTextWatcher keywordTextWatcher = new KeywordTextWatcher(dialog); |
michael@0 | 230 | |
michael@0 | 231 | // Cross reference the TextWatchers |
michael@0 | 232 | locationTextWatcher.setPairedTextWatcher(keywordTextWatcher); |
michael@0 | 233 | keywordTextWatcher.setPairedTextWatcher(locationTextWatcher); |
michael@0 | 234 | |
michael@0 | 235 | // Add the TextWatcher Listeners |
michael@0 | 236 | locationText.addTextChangedListener(locationTextWatcher); |
michael@0 | 237 | keywordText.addTextChangedListener(keywordTextWatcher); |
michael@0 | 238 | |
michael@0 | 239 | dialog.show(); |
michael@0 | 240 | } |
michael@0 | 241 | } |