blob: 55f2fbf96632f614e38cfc332ac6728fda1319f1 [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 Lisionc48c36a2013-11-12 16:26:10 -050036import org.sflphone.R;
37import org.sflphone.account.AccountDetail;
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050038import org.sflphone.account.AccountDetailSrtp;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050039import org.sflphone.model.Account;
40
41import android.app.Activity;
42import android.os.Bundle;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050043import android.preference.Preference;
44import android.preference.Preference.OnPreferenceChangeListener;
Alexandre Lision3b7148e2013-11-13 17:23:06 -050045import android.preference.Preference.OnPreferenceClickListener;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050046import android.preference.PreferenceFragment;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050047
48public class SecurityAccountFragment extends PreferenceFragment {
49
Alexandre Lisionf30ff852013-12-09 17:08:40 -050050 @SuppressWarnings("unused")
Alexandre Lisiona3650992013-11-13 14:19:35 -050051 private static final String TAG = SecurityAccountFragment.class.getSimpleName();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050052
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050053 private Callbacks mCallbacks = sDummyCallbacks;
54 private static Callbacks sDummyCallbacks = new Callbacks() {
55
56 @Override
57 public Account getAccount() {
58 return null;
59 }
60
Alexandre Lision3b7148e2013-11-13 17:23:06 -050061 @Override
62 public void displayCredentialsScreen() {
63 }
64
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050065 @Override
66 public void displaySRTPScreen() {
67 }
68
69 @Override
70 public void displayTLSScreen() {
71 }
72
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050073 };
74
75 public interface Callbacks {
76
77 public Account getAccount();
78
Alexandre Lision3b7148e2013-11-13 17:23:06 -050079 public void displayCredentialsScreen();
80
Alexandre Lisionc9c30b72013-11-18 16:27:26 -050081 public void displaySRTPScreen();
82
83 public void displayTLSScreen();
84
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050085 }
86
87 @Override
88 public void onAttach(Activity activity) {
89 super.onAttach(activity);
90 if (!(activity instanceof Callbacks)) {
91 throw new IllegalStateException("Activity must implement fragment's callbacks.");
92 }
93
94 mCallbacks = (Callbacks) activity;
95 }
96
97 @Override
98 public void onDetach() {
99 super.onDetach();
100 mCallbacks = sDummyCallbacks;
101 }
102
103 @Override
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500104 public void onResume() {
105 super.onResume();
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500106
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500107 }
108
109 @Override
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500110 public void onCreate(Bundle savedInstanceState) {
111 super.onCreate(savedInstanceState);
112
113 // Load the preferences from an XML resource
114 addPreferencesFromResource(R.xml.account_security_prefs);
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500115 setCredentialSummary();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500116 findPreference("Credential.count").setOnPreferenceClickListener(new OnPreferenceClickListener() {
117
118 @Override
119 public boolean onPreferenceClick(Preference preference) {
120 mCallbacks.displayCredentialsScreen();
121 return false;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500122 }
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500123 });
124
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500125 // setSrtpPreferenceDetails(mCallbacks.getAccount().getSrtpDetails());
126 // addPreferenceListener(mCallbacks.getAccount().getSrtpDetails(), changeSrtpModeListener);
127 //
128 // findPreference("TLS.details").setOnPreferenceClickListener(new OnPreferenceClickListener() {
129 //
130 // @Override
131 // public boolean onPreferenceClick(Preference preference) {
132 // mCallbacks.displayTLSScreen();
133 // return false;
134 // }
135 // });
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500136
137 }
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500138
139 public void setCredentialSummary() {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500140 findPreference("Credential.count").setSummary("" + mCallbacks.getAccount().getCredentials().size());
141 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500142
143 private void setSrtpPreferenceDetails(AccountDetailSrtp details) {
144
145 if (details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE)) {
146 findPreference("SRTP.enable").setSummary(
147 details.getDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE).toUpperCase(Locale.getDefault()));
148
149 } else {
150 findPreference("SRTP.enable").setSummary(getResources().getString(R.string.account_srtp_deactivated));
151
152 }
153
154 findPreference("SRTP.details").setEnabled(details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500155 }
156
157 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500158
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500159 findPreference("SRTP.enable").setOnPreferenceChangeListener(changeSrtpModeListener);
160 findPreference("SRTP.details").setOnPreferenceClickListener(new OnPreferenceClickListener() {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500161
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500162 @Override
163 public boolean onPreferenceClick(Preference preference) {
164 mCallbacks.displaySRTPScreen();
165 return false;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500166 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500167 });
168
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500169 }
170
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500171 Preference.OnPreferenceChangeListener changeSrtpModeListener = new Preference.OnPreferenceChangeListener() {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500172 @Override
173 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500174
175 if (((String) newValue).contentEquals("NONE")) {
176 mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.FALSE_STR);
177 preference.setSummary(getResources().getString(R.string.account_srtp_deactivated));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500178 } else {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500179 mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.TRUE_STR);
180 mCallbacks.getAccount().getSrtpDetails()
181 .setDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, ((String) newValue).toLowerCase(Locale.getDefault()));
182 preference.setSummary(((String) newValue));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500183 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500184 findPreference("SRTP.details").setEnabled(!((String) newValue).contentEquals("NONE"));
185 mCallbacks.getAccount().notifyObservers();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500186 return true;
187 }
188 };
189
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500190}