blob: 1d6050195b5d40979558e4d94ccda8f092a2976c [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
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 Lisionc48c36a2013-11-12 16:26:10 -050032package org.sflphone.fragments;
33
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050034import java.util.Locale;
35
Alexandre Lisionc735aa22014-02-04 10:36:54 -050036import android.content.Intent;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050037import org.sflphone.R;
38import org.sflphone.account.AccountDetail;
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050039import org.sflphone.account.AccountDetailSrtp;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050040import org.sflphone.model.Account;
41
42import android.app.Activity;
43import android.os.Bundle;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050044import android.preference.Preference;
45import android.preference.Preference.OnPreferenceChangeListener;
Alexandre Lision3b7148e2013-11-13 17:23:06 -050046import android.preference.Preference.OnPreferenceClickListener;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050047import android.preference.PreferenceFragment;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050048
49public class SecurityAccountFragment extends PreferenceFragment {
50
Alexandre Lisionf30ff852013-12-09 17:08:40 -050051 @SuppressWarnings("unused")
Alexandre Lisiona3650992013-11-13 14:19:35 -050052 private static final String TAG = SecurityAccountFragment.class.getSimpleName();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050053
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050054 private Callbacks mCallbacks = sDummyCallbacks;
55 private static Callbacks sDummyCallbacks = new Callbacks() {
56
57 @Override
58 public Account getAccount() {
59 return null;
60 }
61
Alexandre Lision3b7148e2013-11-13 17:23:06 -050062 @Override
63 public void displayCredentialsScreen() {
64 }
65
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050066 @Override
67 public void displaySRTPScreen() {
68 }
69
70 @Override
71 public void displayTLSScreen() {
72 }
73
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050074 };
75
76 public interface Callbacks {
77
78 public Account getAccount();
79
Alexandre Lision3b7148e2013-11-13 17:23:06 -050080 public void displayCredentialsScreen();
81
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050082 public void displaySRTPScreen();
83
84 public void displayTLSScreen();
85
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050086 }
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 Lisionafff1cc2013-11-22 11:57:33 -0500105 public void onResume() {
106 super.onResume();
Alexandre Lision06412a32014-02-05 17:33:04 -0500107 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 Lisiona8b78722013-12-13 10:18:33 -0500112
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500113 }
114
115 @Override
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500116 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 Lision06412a32014-02-05 17:33:04 -0500121 updateSummaries();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500122 findPreference("Credential.count").setOnPreferenceClickListener(new OnPreferenceClickListener() {
123
124 @Override
125 public boolean onPreferenceClick(Preference preference) {
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500126 mCallbacks.displayCredentialsScreen();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500127 return false;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500128 }
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500129 });
130
Alexandre Lisionc735aa22014-02-04 10:36:54 -0500131 setSrtpPreferenceDetails(mCallbacks.getAccount().getSrtpDetails());
132 addPreferenceListener(mCallbacks.getAccount().getSrtpDetails(), changeSrtpModeListener);
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500133
Alexandre Lisionc735aa22014-02-04 10:36:54 -0500134 findPreference("TLS.details").setOnPreferenceClickListener(new OnPreferenceClickListener() {
135
136 @Override
137 public boolean onPreferenceClick(Preference preference) {
138 mCallbacks.displayTLSScreen();
139 return false;
140 }
141 });
142
143 }
144
Alexandre Lision06412a32014-02-05 17:33:04 -0500145 public void updateSummaries() {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500146 findPreference("Credential.count").setSummary("" + mCallbacks.getAccount().getCredentials().size());
Alexandre Lision06412a32014-02-05 17:33:04 -0500147 if(mCallbacks.getAccount().getTlsDetails().getDetailBoolean("TLS.enable")){
148 findPreference("TLS.details").setSummary(getString(R.string.account_tls_enabled_label));
149 } else {
150 findPreference("TLS.details").setSummary(getString(R.string.account_tls_disabled_label));
151 }
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500152 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500153
154 private void setSrtpPreferenceDetails(AccountDetailSrtp details) {
155
156 if (details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE)) {
Alexandre Lision06412a32014-02-05 17:33:04 -0500157 findPreference(AccountDetailSrtp.CONFIG_SRTP_ENABLE).setSummary(
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500158 details.getDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE).toUpperCase(Locale.getDefault()));
159
160 } else {
Alexandre Lision06412a32014-02-05 17:33:04 -0500161 findPreference(AccountDetailSrtp.CONFIG_SRTP_ENABLE).setSummary(getResources().getString(R.string.account_srtp_deactivated));
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500162
163 }
164
165 findPreference("SRTP.details").setEnabled(details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500166 }
167
168 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500169
Alexandre Lision06412a32014-02-05 17:33:04 -0500170 findPreference(AccountDetailSrtp.CONFIG_SRTP_ENABLE).setOnPreferenceChangeListener(changeSrtpModeListener);
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500171 findPreference("SRTP.details").setOnPreferenceClickListener(new OnPreferenceClickListener() {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500172
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500173 @Override
174 public boolean onPreferenceClick(Preference preference) {
175 mCallbacks.displaySRTPScreen();
176 return false;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500177 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500178 });
179
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500180 }
181
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500182 Preference.OnPreferenceChangeListener changeSrtpModeListener = new Preference.OnPreferenceChangeListener() {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500183 @Override
184 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500185
186 if (((String) newValue).contentEquals("NONE")) {
187 mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.FALSE_STR);
188 preference.setSummary(getResources().getString(R.string.account_srtp_deactivated));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500189 } else {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500190 mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.TRUE_STR);
191 mCallbacks.getAccount().getSrtpDetails()
192 .setDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, ((String) newValue).toLowerCase(Locale.getDefault()));
193 preference.setSummary(((String) newValue));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500194 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500195 findPreference("SRTP.details").setEnabled(!((String) newValue).contentEquals("NONE"));
196 mCallbacks.getAccount().notifyObservers();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500197 return true;
198 }
199 };
200
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500201}