Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 32 | package org.sflphone.fragments; |
| 33 | |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 34 | import java.util.Locale; |
| 35 | |
Alexandre Lision | c735aa2 | 2014-02-04 10:36:54 -0500 | [diff] [blame] | 36 | import android.content.Intent; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 37 | import org.sflphone.R; |
| 38 | import org.sflphone.account.AccountDetail; |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 39 | import org.sflphone.account.AccountDetailSrtp; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 40 | import org.sflphone.model.Account; |
| 41 | |
| 42 | import android.app.Activity; |
| 43 | import android.os.Bundle; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 44 | import android.preference.Preference; |
| 45 | import android.preference.Preference.OnPreferenceChangeListener; |
Alexandre Lision | 3b7148e | 2013-11-13 17:23:06 -0500 | [diff] [blame] | 46 | import android.preference.Preference.OnPreferenceClickListener; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 47 | import android.preference.PreferenceFragment; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 48 | |
| 49 | public class SecurityAccountFragment extends PreferenceFragment { |
| 50 | |
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 51 | @SuppressWarnings("unused") |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 52 | private static final String TAG = SecurityAccountFragment.class.getSimpleName(); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 53 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 54 | private Callbacks mCallbacks = sDummyCallbacks; |
| 55 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 56 | |
| 57 | @Override |
| 58 | public Account getAccount() { |
| 59 | return null; |
| 60 | } |
| 61 | |
Alexandre Lision | 3b7148e | 2013-11-13 17:23:06 -0500 | [diff] [blame] | 62 | @Override |
| 63 | public void displayCredentialsScreen() { |
| 64 | } |
| 65 | |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 66 | @Override |
| 67 | public void displaySRTPScreen() { |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void displayTLSScreen() { |
| 72 | } |
| 73 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | public interface Callbacks { |
| 77 | |
| 78 | public Account getAccount(); |
| 79 | |
Alexandre Lision | 3b7148e | 2013-11-13 17:23:06 -0500 | [diff] [blame] | 80 | public void displayCredentialsScreen(); |
| 81 | |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 82 | public void displaySRTPScreen(); |
| 83 | |
| 84 | public void displayTLSScreen(); |
| 85 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onAttach(Activity activity) { |
| 90 | super.onAttach(activity); |
| 91 | if (!(activity instanceof Callbacks)) { |
| 92 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 93 | } |
| 94 | |
| 95 | mCallbacks = (Callbacks) activity; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onDetach() { |
| 100 | super.onDetach(); |
| 101 | mCallbacks = sDummyCallbacks; |
| 102 | } |
| 103 | |
| 104 | @Override |
Alexandre Lision | afff1cc | 2013-11-22 11:57:33 -0500 | [diff] [blame] | 105 | public void onResume() { |
| 106 | super.onResume(); |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 107 | if(mCallbacks.getAccount().getTlsDetails().getDetailBoolean("TLS.enable")){ |
| 108 | findPreference("TLS.details").setSummary(getString(R.string.account_tls_enabled_label)); |
| 109 | } else { |
| 110 | findPreference("TLS.details").setSummary(getString(R.string.account_tls_disabled_label)); |
| 111 | } |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 112 | |
Alexandre Lision | afff1cc | 2013-11-22 11:57:33 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 116 | public void onCreate(Bundle savedInstanceState) { |
| 117 | super.onCreate(savedInstanceState); |
| 118 | |
| 119 | // Load the preferences from an XML resource |
| 120 | addPreferencesFromResource(R.xml.account_security_prefs); |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 121 | updateSummaries(); |
Alexandre Lision | 3b7148e | 2013-11-13 17:23:06 -0500 | [diff] [blame] | 122 | findPreference("Credential.count").setOnPreferenceClickListener(new OnPreferenceClickListener() { |
| 123 | |
| 124 | @Override |
| 125 | public boolean onPreferenceClick(Preference preference) { |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 126 | |
| 127 | mCallbacks.displayCredentialsScreen(); |
Alexandre Lision | 3b7148e | 2013-11-13 17:23:06 -0500 | [diff] [blame] | 128 | return false; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 129 | } |
Alexandre Lision | 3b7148e | 2013-11-13 17:23:06 -0500 | [diff] [blame] | 130 | }); |
| 131 | |
Alexandre Lision | c735aa2 | 2014-02-04 10:36:54 -0500 | [diff] [blame] | 132 | setSrtpPreferenceDetails(mCallbacks.getAccount().getSrtpDetails()); |
| 133 | addPreferenceListener(mCallbacks.getAccount().getSrtpDetails(), changeSrtpModeListener); |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 134 | |
Alexandre Lision | c735aa2 | 2014-02-04 10:36:54 -0500 | [diff] [blame] | 135 | findPreference("TLS.details").setOnPreferenceClickListener(new OnPreferenceClickListener() { |
| 136 | |
| 137 | @Override |
| 138 | public boolean onPreferenceClick(Preference preference) { |
| 139 | mCallbacks.displayTLSScreen(); |
| 140 | return false; |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | } |
| 145 | |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 146 | public void updateSummaries() { |
Alexandre Lision | afff1cc | 2013-11-22 11:57:33 -0500 | [diff] [blame] | 147 | findPreference("Credential.count").setSummary("" + mCallbacks.getAccount().getCredentials().size()); |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 148 | if(mCallbacks.getAccount().getTlsDetails().getDetailBoolean("TLS.enable")){ |
| 149 | findPreference("TLS.details").setSummary(getString(R.string.account_tls_enabled_label)); |
| 150 | } else { |
| 151 | findPreference("TLS.details").setSummary(getString(R.string.account_tls_disabled_label)); |
| 152 | } |
Alexandre Lision | afff1cc | 2013-11-22 11:57:33 -0500 | [diff] [blame] | 153 | } |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 154 | |
| 155 | private void setSrtpPreferenceDetails(AccountDetailSrtp details) { |
| 156 | |
| 157 | if (details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE)) { |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 158 | findPreference(AccountDetailSrtp.CONFIG_SRTP_ENABLE).setSummary( |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 159 | details.getDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE).toUpperCase(Locale.getDefault())); |
| 160 | |
| 161 | } else { |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 162 | findPreference(AccountDetailSrtp.CONFIG_SRTP_ENABLE).setSummary(getResources().getString(R.string.account_srtp_deactivated)); |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 163 | |
| 164 | } |
| 165 | |
| 166 | findPreference("SRTP.details").setEnabled(details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE)); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) { |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 170 | |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 171 | findPreference(AccountDetailSrtp.CONFIG_SRTP_ENABLE).setOnPreferenceChangeListener(changeSrtpModeListener); |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 172 | findPreference("SRTP.details").setOnPreferenceClickListener(new OnPreferenceClickListener() { |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 173 | |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 174 | @Override |
| 175 | public boolean onPreferenceClick(Preference preference) { |
| 176 | mCallbacks.displaySRTPScreen(); |
| 177 | return false; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 178 | } |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 179 | }); |
| 180 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 183 | Preference.OnPreferenceChangeListener changeSrtpModeListener = new Preference.OnPreferenceChangeListener() { |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 184 | @Override |
| 185 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 186 | |
| 187 | if (((String) newValue).contentEquals("NONE")) { |
| 188 | mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.FALSE_STR); |
| 189 | preference.setSummary(getResources().getString(R.string.account_srtp_deactivated)); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 190 | } else { |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 191 | mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.TRUE_STR); |
| 192 | mCallbacks.getAccount().getSrtpDetails() |
| 193 | .setDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, ((String) newValue).toLowerCase(Locale.getDefault())); |
| 194 | preference.setSummary(((String) newValue)); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 195 | } |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame] | 196 | findPreference("SRTP.details").setEnabled(!((String) newValue).contentEquals("NONE")); |
| 197 | mCallbacks.getAccount().notifyObservers(); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | }; |
| 201 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 202 | } |