| 1 | /* |
| 2 | * Tomdroid |
| 3 | * Tomboy on Android |
| 4 | * http://www.launchpad.net/tomdroid |
| 5 | * |
| 6 | * Copyright 2009, Benoit Garret <benoit.garret_launchpad@gadz.org> |
| 7 | * Copyright 2010, 2011 Olivier Bilodeau <olivier@bottomlesspit.org> |
| 8 | * |
| 9 | * This file is part of Tomdroid. |
| 10 | * |
| 11 | * Tomdroid is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * Tomdroid is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 23 | */ |
| 24 | package org.tomdroid.ui; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | |
| 28 | import org.tomdroid.NoteManager; |
| 29 | import org.tomdroid.R; |
| 30 | import org.tomdroid.sync.ServiceAuth; |
| 31 | import org.tomdroid.sync.SyncManager; |
| 32 | import org.tomdroid.sync.SyncService; |
| 33 | import org.tomdroid.util.FirstNote; |
| 34 | import org.tomdroid.util.Preferences; |
| 35 | import org.tomdroid.util.SearchSuggestionProvider; |
| 36 | |
| 37 | import android.app.AlertDialog; |
| 38 | import android.app.ProgressDialog; |
| 39 | import android.content.DialogInterface; |
| 40 | import android.content.Intent; |
| 41 | import android.content.DialogInterface.OnClickListener; |
| 42 | import android.net.Uri; |
| 43 | import android.os.Bundle; |
| 44 | import android.os.Handler; |
| 45 | import android.os.Message; |
| 46 | import android.preference.EditTextPreference; |
| 47 | import android.preference.ListPreference; |
| 48 | import android.preference.Preference; |
| 49 | import android.preference.PreferenceActivity; |
| 50 | import android.preference.Preference.OnPreferenceChangeListener; |
| 51 | import android.preference.Preference.OnPreferenceClickListener; |
| 52 | import android.provider.SearchRecentSuggestions; |
| 53 | import android.util.Log; |
| 54 | import android.widget.Toast; |
| 55 | |
| 56 | public class PreferencesActivity extends PreferenceActivity { |
| 57 | |
| 58 | private static final String TAG = "PreferencesActivity"; |
| 59 | |
| 60 | // TODO: put the various preferences in fields and figure out what to do on activity suspend/resume |
| 61 | private EditTextPreference syncServer = null; |
| 62 | private ListPreference syncService = null; |
| 63 | private Preference clearSearchHistory = null; |
| 64 | |
| 65 | @Override |
| 66 | protected void onCreate(Bundle savedInstanceState) { |
| 67 | |
| 68 | super.onCreate(savedInstanceState); |
| 69 | addPreferencesFromResource(R.xml.preferences); |
| 70 | |
| 71 | // Fill the Preferences fields |
| 72 | syncServer = (EditTextPreference)findPreference(Preferences.Key.SYNC_SERVER.getName()); |
| 73 | syncService = (ListPreference)findPreference(Preferences.Key.SYNC_SERVICE.getName()); |
| 74 | clearSearchHistory = (Preference)findPreference(Preferences.Key.CLEAR_SEARCH_HISTORY.getName()); |
| 75 | |
| 76 | // Set the default values if nothing exists |
| 77 | this.setDefaults(); |
| 78 | |
| 79 | // Fill the services combo list |
| 80 | this.fillServices(); |
| 81 | |
| 82 | // Enable or disable the server field depending on the selected sync service |
| 83 | setServer(syncService.getValue()); |
| 84 | |
| 85 | syncService.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { |
| 86 | |
| 87 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 88 | String selectedSyncServiceKey = (String)newValue; |
| 89 | |
| 90 | // did the selection change? |
| 91 | if (!syncService.getValue().contentEquals(selectedSyncServiceKey)) { |
| 92 | Log.d(TAG, "preference change triggered"); |
| 93 | |
| 94 | syncServiceChanged(selectedSyncServiceKey); |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | // Re-authenticate if the sync server changes |
| 101 | syncServer.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { |
| 102 | |
| 103 | public boolean onPreferenceChange(Preference preference, |
| 104 | Object serverUri) { |
| 105 | |
| 106 | if (serverUri == null) { |
| 107 | Toast.makeText(PreferencesActivity.this, |
| 108 | getString(R.string.prefServerEmpty), |
| 109 | Toast.LENGTH_SHORT).show(); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | authenticate((String) serverUri); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | }); |
| 118 | |
| 119 | //delete Search History |
| 120 | clearSearchHistory.setOnPreferenceClickListener(new OnPreferenceClickListener() { |
| 121 | public boolean onPreferenceClick(Preference preference) { |
| 122 | SearchRecentSuggestions suggestions = new SearchRecentSuggestions(PreferencesActivity.this, |
| 123 | SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE); |
| 124 | suggestions.clearHistory(); |
| 125 | |
| 126 | Toast.makeText(getBaseContext(), |
| 127 | getString(R.string.deletedSearchHistory), |
| 128 | Toast.LENGTH_LONG).show(); |
| 129 | Log.d(TAG, "Deleted search history."); |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | private void authenticate(String serverUri) { |
| 137 | |
| 138 | // update the value before doing anything |
| 139 | Preferences.putString(Preferences.Key.SYNC_SERVER, serverUri); |
| 140 | |
| 141 | SyncService currentService = SyncManager.getInstance().getCurrentService(); |
| 142 | |
| 143 | if (!currentService.needsAuth()) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // service needs authentication |
| 148 | Log.i(TAG, "Creating dialog"); |
| 149 | |
| 150 | final ProgressDialog authProgress = ProgressDialog.show(this, "", |
| 151 | getString(R.string.prefSyncCompleteAuth), true, false); |
| 152 | |
| 153 | Handler handler = new Handler() { |
| 154 | |
| 155 | @Override |
| 156 | public void handleMessage(Message msg) { |
| 157 | |
| 158 | boolean wasSuccsessful = false; |
| 159 | Uri authorizationUri = (Uri) msg.obj; |
| 160 | if (authorizationUri != null) { |
| 161 | |
| 162 | Intent i = new Intent(Intent.ACTION_VIEW, authorizationUri); |
| 163 | startActivity(i); |
| 164 | wasSuccsessful = true; |
| 165 | |
| 166 | } else { |
| 167 | // Auth failed, don't update the value |
| 168 | wasSuccsessful = false; |
| 169 | } |
| 170 | |
| 171 | if (authProgress != null) |
| 172 | authProgress.dismiss(); |
| 173 | |
| 174 | if (wasSuccsessful) { |
| 175 | resetLocalDatabase(); |
| 176 | } else { |
| 177 | connectionFailed(); |
| 178 | } |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | ((ServiceAuth) currentService).getAuthUri(serverUri, handler); |
| 183 | } |
| 184 | |
| 185 | private void fillServices() |
| 186 | { |
| 187 | ArrayList<SyncService> availableServices = SyncManager.getInstance().getServices(); |
| 188 | CharSequence[] entries = new CharSequence[availableServices.size()]; |
| 189 | CharSequence[] entryValues = new CharSequence[availableServices.size()]; |
| 190 | |
| 191 | for (int i = 0; i < availableServices.size(); i++) { |
| 192 | entries[i] = availableServices.get(i).getDescription(); |
| 193 | entryValues[i] = availableServices.get(i).getName(); |
| 194 | } |
| 195 | |
| 196 | syncService.setEntries(entries); |
| 197 | syncService.setEntryValues(entryValues); |
| 198 | } |
| 199 | |
| 200 | private void setDefaults() |
| 201 | { |
| 202 | String defaultServer = (String)Preferences.Key.SYNC_SERVER.getDefault(); |
| 203 | syncServer.setDefaultValue(defaultServer); |
| 204 | if(syncServer.getText() == null) |
| 205 | syncServer.setText(defaultServer); |
| 206 | |
| 207 | String defaultService = (String)Preferences.Key.SYNC_SERVICE.getDefault(); |
| 208 | syncService.setDefaultValue(defaultService); |
| 209 | if(syncService.getValue() == null) |
| 210 | syncService.setValue(defaultService); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | private void setServer(String syncServiceKey) { |
| 215 | |
| 216 | SyncService service = SyncManager.getInstance().getService(syncServiceKey); |
| 217 | |
| 218 | if (service == null) |
| 219 | return; |
| 220 | |
| 221 | syncServer.setEnabled(service.needsServer()); |
| 222 | syncService.setSummary(service.getDescription()); |
| 223 | } |
| 224 | |
| 225 | private void connectionFailed() { |
| 226 | new AlertDialog.Builder(this) |
| 227 | .setMessage(getString(R.string.prefSyncConnectionFailed)) |
| 228 | .setNeutralButton(getString(R.string.btnOk), new OnClickListener() { |
| 229 | public void onClick(DialogInterface dialog, int which) { |
| 230 | dialog.dismiss(); |
| 231 | }}) |
| 232 | .show(); |
| 233 | } |
| 234 | |
| 235 | //TODO use LocalStorage wrapper from two-way-sync branch when it get's merged |
| 236 | private void resetLocalDatabase() { |
| 237 | getContentResolver().delete(Tomdroid.CONTENT_URI, null, null); |
| 238 | Preferences.putLong(Preferences.Key.LATEST_SYNC_REVISION, 0); |
| 239 | |
| 240 | // add a first explanatory note |
| 241 | // TODO this will be problematic with two-way sync |
| 242 | NoteManager.putNote(this, FirstNote.createFirstNote()); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Housekeeping when a syncServer changes |
| 247 | * @param syncServiceKey - key of the new sync service |
| 248 | */ |
| 249 | private void syncServiceChanged(String syncServiceKey) { |
| 250 | |
| 251 | setServer(syncServiceKey); |
| 252 | |
| 253 | // TODO this should be refactored further, notice that setServer performs the same operations |
| 254 | SyncService service = SyncManager.getInstance().getService(syncServiceKey); |
| 255 | |
| 256 | if (service == null) |
| 257 | return; |
| 258 | |
| 259 | // reset if no-auth required |
| 260 | // I believe it's done this way because if needsAuth the database is reset when they successfully auth for the first time |
| 261 | // TODO we should graphically warn the user that his database is about to be dropped |
| 262 | if (!service.needsAuth()){ |
| 263 | resetLocalDatabase(); |
| 264 | } |
| 265 | } |
| 266 | } |