Alexandre Lision | 227b59c | 2013-10-03 17:37:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * Additional permission under GNU GPL version 3 section 7: |
| 22 | * |
| 23 | * If you modify this program, or any covered work, by linking or |
| 24 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 25 | * modified version of that library), containing parts covered by the |
| 26 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 27 | * grants you additional permission to convey the resulting work. |
| 28 | * Corresponding Source for a non-source form of such a combination |
| 29 | * shall include the source code for the parts of OpenSSL used as well |
| 30 | * as that of the covered work. |
| 31 | */ |
| 32 | |
| 33 | package org.sflphone.client; |
| 34 | |
| 35 | import java.net.NetworkInterface; |
| 36 | import java.net.SocketException; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Enumeration; |
| 39 | import java.util.HashMap; |
| 40 | |
| 41 | import org.sflphone.R; |
| 42 | import org.sflphone.account.AccountDetail; |
| 43 | import org.sflphone.account.AccountDetailAdvanced; |
| 44 | import org.sflphone.account.AccountDetailBasic; |
| 45 | import org.sflphone.account.AccountDetailSrtp; |
| 46 | import org.sflphone.account.AccountDetailTls; |
| 47 | |
| 48 | import android.app.Activity; |
| 49 | import android.app.AlertDialog; |
| 50 | import android.app.Dialog; |
| 51 | import android.content.DialogInterface; |
| 52 | import android.content.Intent; |
| 53 | import android.os.Bundle; |
| 54 | import android.preference.CheckBoxPreference; |
| 55 | import android.preference.EditTextPreference; |
| 56 | import android.preference.ListPreference; |
| 57 | import android.preference.Preference; |
| 58 | import android.preference.Preference.OnPreferenceChangeListener; |
| 59 | import android.preference.PreferenceFragment; |
| 60 | import android.util.Log; |
| 61 | import android.view.Menu; |
| 62 | import android.view.MenuInflater; |
| 63 | import android.view.MenuItem; |
| 64 | |
| 65 | public class AccountEditionActivity extends Activity { |
| 66 | private static final String TAG = "AccoutPreferenceActivity"; |
| 67 | |
| 68 | public static final String KEY_MODE = "mode"; |
| 69 | |
| 70 | public interface result { |
| 71 | static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 1; |
| 72 | static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2; |
| 73 | } |
| 74 | |
| 75 | // private ArrayList<String> requiredFields = null; |
| 76 | EditionFragment mEditionFragment; |
| 77 | |
| 78 | @Override |
| 79 | protected void onCreate(Bundle savedInstanceState) { |
| 80 | super.onCreate(savedInstanceState); |
| 81 | |
| 82 | setContentView(R.layout.activity_holder); |
| 83 | |
| 84 | getActionBar().setDisplayHomeAsUpEnabled(true); |
| 85 | mEditionFragment = new EditionFragment(); |
| 86 | mEditionFragment.setArguments(getIntent().getExtras()); |
| 87 | getFragmentManager().beginTransaction().replace(R.id.frag_container, mEditionFragment).commit(); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean onCreateOptionsMenu(Menu menu) { |
| 93 | MenuInflater inflater = getMenuInflater(); |
| 94 | inflater.inflate(R.menu.account_edition, menu); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onBackPressed() { |
| 100 | |
| 101 | if (mEditionFragment != null && mEditionFragment.isDifferent) { |
| 102 | AlertDialog dialog = createCancelDialog(); |
| 103 | dialog.show(); |
| 104 | } else { |
| 105 | super.onBackPressed(); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean onOptionsItemSelected(MenuItem item) { |
| 112 | |
| 113 | switch (item.getItemId()) { |
| 114 | case android.R.id.home: |
| 115 | finish(); |
| 116 | return true; |
| 117 | case R.id.menuitem_delete: |
| 118 | AlertDialog dialog = createDeleteDialog(); |
| 119 | dialog.show(); |
| 120 | break; |
| 121 | case R.id.menuitem_edit: |
| 122 | processAccount(result.ACCOUNT_MODIFIED); |
| 123 | break; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | private void processAccount(int resultCode) { |
| 131 | AlertDialog dialog; |
| 132 | ArrayList<String> missingValue = new ArrayList<String>(); |
| 133 | if (mEditionFragment.validateAccountCreation(missingValue)) { |
| 134 | |
| 135 | Bundle bundle = new Bundle(); |
| 136 | bundle.putString("AccountID", mEditionFragment.mAccountID); |
| 137 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
| 138 | |
| 139 | updateAccountDetails(accountDetails, mEditionFragment.basicDetails); |
| 140 | updateAccountDetails(accountDetails, mEditionFragment.advancedDetails); |
| 141 | updateAccountDetails(accountDetails, mEditionFragment.srtpDetails); |
| 142 | updateAccountDetails(accountDetails, mEditionFragment.tlsDetails); |
| 143 | |
| 144 | accountDetails.put("Account.type", "SIP"); |
| 145 | bundle.putSerializable(AccountDetail.TAG, accountDetails); |
| 146 | Intent resultIntent = new Intent(); |
| 147 | resultIntent.putExtras(bundle); |
| 148 | |
| 149 | setResult(resultCode, resultIntent); |
| 150 | finish(); |
| 151 | } else { |
| 152 | dialog = createCouldNotValidateDialog(missingValue); |
| 153 | dialog.show(); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) { |
| 159 | for (AccountDetail.PreferenceEntry p : det.getDetailValues()) { |
| 160 | |
| 161 | Log.i(TAG, "updateAccountDetails: pref " + p.mKey + " value " + det.getDetailString(p.mKey)); |
| 162 | if (p.isTwoState) { |
| 163 | accountDetails.put(p.mKey, det.getDetailString(p.mKey)); |
| 164 | } else { |
| 165 | accountDetails.put(p.mKey, det.getDetailString(p.mKey)); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /****************************************** |
| 171 | * |
| 172 | * AlertDialogs |
| 173 | * |
| 174 | ******************************************/ |
| 175 | |
| 176 | private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) { |
| 177 | String message = "The following parameters are missing:"; |
| 178 | |
| 179 | for (String s : missingValue) |
| 180 | message += "\n - " + s; |
| 181 | |
| 182 | Activity ownerActivity = this; |
| 183 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 184 | builder.setMessage(message).setTitle("Missing Parameters").setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 185 | @Override |
| 186 | public void onClick(DialogInterface dialog, int whichButton) { |
| 187 | /* Nothing to be done */ |
| 188 | } |
| 189 | }); |
| 190 | |
| 191 | AlertDialog alertDialog = builder.create(); |
| 192 | return alertDialog; |
| 193 | } |
| 194 | |
| 195 | private AlertDialog createCancelDialog() { |
| 196 | Activity ownerActivity = this; |
| 197 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 198 | builder.setMessage("Modifications will be lost").setTitle("Account Edition").setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 199 | @Override |
| 200 | public void onClick(DialogInterface dialog, int whichButton) { |
| 201 | Activity activity = ((Dialog) dialog).getOwnerActivity(); |
| 202 | activity.finish(); |
| 203 | } |
| 204 | }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 205 | @Override |
| 206 | public void onClick(DialogInterface dialog, int whichButton) { |
| 207 | /* Terminate with no action */ |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | AlertDialog alertDialog = builder.create(); |
| 212 | alertDialog.setOwnerActivity(ownerActivity); |
| 213 | |
| 214 | return alertDialog; |
| 215 | } |
| 216 | |
| 217 | private AlertDialog createDeleteDialog() { |
| 218 | Activity ownerActivity = this; |
| 219 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 220 | builder.setMessage("Do you really want to delete this account").setTitle("Delete Account") |
| 221 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
| 222 | @Override |
| 223 | public void onClick(DialogInterface dialog, int whichButton) { |
| 224 | Bundle bundle = new Bundle(); |
| 225 | bundle.putString("AccountID", mEditionFragment.mAccountID); |
| 226 | |
| 227 | Intent resultIntent = new Intent(); |
| 228 | resultIntent.putExtras(bundle); |
| 229 | |
| 230 | Activity activity = ((Dialog) dialog).getOwnerActivity(); |
| 231 | activity.setResult(result.ACCOUNT_DELETED, resultIntent); |
| 232 | activity.finish(); |
| 233 | } |
| 234 | }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 235 | @Override |
| 236 | public void onClick(DialogInterface dialog, int whichButton) { |
| 237 | /* Terminate with no action */ |
| 238 | } |
| 239 | }); |
| 240 | |
| 241 | AlertDialog alertDialog = builder.create(); |
| 242 | alertDialog.setOwnerActivity(ownerActivity); |
| 243 | |
| 244 | return alertDialog; |
| 245 | } |
| 246 | |
| 247 | public static class EditionFragment extends PreferenceFragment { |
| 248 | |
| 249 | private AccountDetailBasic basicDetails = null; |
| 250 | private AccountDetailAdvanced advancedDetails = null; |
| 251 | private AccountDetailSrtp srtpDetails = null; |
| 252 | private AccountDetailTls tlsDetails = null; |
| 253 | private String mAccountID; |
| 254 | private boolean isDifferent = false; |
| 255 | private ArrayList<String> requiredFields = null; |
| 256 | |
| 257 | @Override |
| 258 | public void onCreate(Bundle savedInstanceState) { |
| 259 | super.onCreate(savedInstanceState); |
| 260 | |
| 261 | |
| 262 | // Load the preferences from an XML resource |
| 263 | addPreferencesFromResource(R.xml.account_creation_preferences); |
| 264 | initEdition(); |
| 265 | requiredFields = new ArrayList<String>(); |
| 266 | requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS); |
| 267 | requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME); |
| 268 | requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME); |
| 269 | requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD); |
| 270 | |
| 271 | } |
| 272 | |
| 273 | private void initEdition() { |
| 274 | |
| 275 | Bundle b = getArguments(); |
| 276 | mAccountID = b.getString("AccountID"); |
| 277 | ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG); |
| 278 | ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG); |
| 279 | ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG); |
| 280 | ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG); |
| 281 | |
| 282 | basicDetails = new AccountDetailBasic(basicPreferenceList); |
| 283 | advancedDetails = new AccountDetailAdvanced(advancedPreferenceList); |
| 284 | srtpDetails = new AccountDetailSrtp(srtpPreferenceList); |
| 285 | tlsDetails = new AccountDetailTls(tlsPreferenceList); |
| 286 | |
| 287 | setPreferenceDetails(basicDetails); |
| 288 | setPreferenceDetails(advancedDetails); |
| 289 | setPreferenceDetails(srtpDetails); |
| 290 | setPreferenceDetails(tlsDetails); |
| 291 | |
| 292 | addPreferenceListener(basicDetails, changeBasicPreferenceListener); |
| 293 | // addPreferenceListener(advancedDetails, changeAdvancedPreferenceListener); |
| 294 | // addPreferenceListener(srtpDetails, changeSrtpPreferenceListener); |
| 295 | // addPreferenceListener(tlsDetails, changeTlsPreferenceListener); |
| 296 | } |
| 297 | |
| 298 | private void setPreferenceDetails(AccountDetail details) { |
| 299 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 300 | Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue); |
| 301 | Preference pref = findPreference(p.mKey); |
| 302 | if (pref != null) { |
| 303 | if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) { |
| 304 | ArrayList<CharSequence> entries = new ArrayList<CharSequence>(); |
| 305 | try { |
| 306 | |
| 307 | for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) { |
| 308 | NetworkInterface i = list.nextElement(); |
| 309 | Log.e("network_interfaces", "display name " + i.getDisplayName()); |
| 310 | if (i.isUp()) |
| 311 | entries.add(i.getDisplayName()); |
| 312 | } |
| 313 | } catch (SocketException e) { |
| 314 | Log.e(TAG, e.toString()); |
| 315 | } |
| 316 | CharSequence[] display = new CharSequence[entries.size()]; |
| 317 | entries.toArray(display); |
| 318 | ((ListPreference) pref).setEntries(display); |
| 319 | ((ListPreference) pref).setEntryValues(display); |
| 320 | pref.setSummary(p.mValue); |
| 321 | continue; |
| 322 | } |
| 323 | if (!p.isTwoState) { |
| 324 | ((EditTextPreference) pref).setText(p.mValue); |
| 325 | pref.setSummary(p.mValue); |
| 326 | } |
| 327 | } else { |
| 328 | Log.w(TAG, "pref not found"); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) { |
| 334 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 335 | Log.i(TAG, "addPreferenceListener: pref " + p.mKey + p.mValue); |
| 336 | Preference pref = findPreference(p.mKey); |
| 337 | if (pref != null) { |
| 338 | |
| 339 | pref.setOnPreferenceChangeListener(listener); |
| 340 | |
| 341 | } else { |
| 342 | Log.w(TAG, "addPreferenceListener: pref not found"); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 348 | @Override |
| 349 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 350 | |
| 351 | isDifferent = true; |
| 352 | if (preference instanceof CheckBoxPreference) { |
| 353 | if ((Boolean) newValue == true) |
| 354 | basicDetails.setDetailString(preference.getKey(), ((Boolean) newValue).toString()); |
| 355 | } else { |
| 356 | preference.setSummary((CharSequence) newValue); |
| 357 | Log.i(TAG, "Changing preference value:" + newValue); |
| 358 | basicDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
| 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | Preference.OnPreferenceChangeListener changeAdvancedPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 365 | @Override |
| 366 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 367 | preference.setSummary((CharSequence) newValue); |
| 368 | advancedDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
| 369 | return true; |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | Preference.OnPreferenceChangeListener changeTlsPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 374 | @Override |
| 375 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 376 | preference.setSummary((CharSequence) newValue); |
| 377 | tlsDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
| 378 | return true; |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | Preference.OnPreferenceChangeListener changeSrtpPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 383 | @Override |
| 384 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 385 | preference.setSummary((CharSequence) newValue); |
| 386 | srtpDetails.setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
| 387 | return true; |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | public boolean validateAccountCreation(ArrayList<String> missingValue) { |
| 392 | boolean valid = true; |
| 393 | |
| 394 | for (String s : requiredFields) { |
| 395 | EditTextPreference pref = (EditTextPreference) findPreference(s); |
| 396 | Log.i(TAG, "Looking for " + s); |
| 397 | Log.i(TAG, "Value " + pref.getText()); |
| 398 | if (pref.getText().isEmpty()) { |
| 399 | valid = false; |
| 400 | missingValue.add(pref.getTitle().toString()); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return valid; |
| 405 | } |
| 406 | |
| 407 | } |
| 408 | |
| 409 | } |