blob: d58fc94b763c564eeed0e4707593e6e2b2f78022 [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 Lisiona8b78722013-12-13 10:18:33 -0500107
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500108 }
109
110 @Override
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500111 public void onCreate(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113
114 // Load the preferences from an XML resource
115 addPreferencesFromResource(R.xml.account_security_prefs);
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500116 setCredentialSummary();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500117 findPreference("Credential.count").setOnPreferenceClickListener(new OnPreferenceClickListener() {
118
119 @Override
120 public boolean onPreferenceClick(Preference preference) {
Alexandre Lisionc735aa22014-02-04 10:36:54 -0500121 performFileSearch();
122 //mCallbacks.displayCredentialsScreen();
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500123 return false;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500124 }
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500125 });
126
Alexandre Lisionc735aa22014-02-04 10:36:54 -0500127 setSrtpPreferenceDetails(mCallbacks.getAccount().getSrtpDetails());
128 addPreferenceListener(mCallbacks.getAccount().getSrtpDetails(), changeSrtpModeListener);
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500129
Alexandre Lisionc735aa22014-02-04 10:36:54 -0500130 findPreference("TLS.details").setOnPreferenceClickListener(new OnPreferenceClickListener() {
131
132 @Override
133 public boolean onPreferenceClick(Preference preference) {
134 mCallbacks.displayTLSScreen();
135 return false;
136 }
137 });
138
139 }
140
141 private static final int READ_REQUEST_CODE = 42;
142 public void performFileSearch() {
143
144 // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
145 // browser.
146 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
147
148 // Filter to only show results that can be "opened", such as a
149 // file (as opposed to a list of contacts or timezones)
150 intent.addCategory(Intent.CATEGORY_OPENABLE);
151
152 // Filter to show only images, using the image MIME data type.
153 // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
154 // To search for all documents available via installed storage providers,
155 // it would be "*/*".
156 intent.setType("image/*");
157
158 startActivityForResult(intent, READ_REQUEST_CODE);
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500159 }
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500160
161 public void setCredentialSummary() {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500162 findPreference("Credential.count").setSummary("" + mCallbacks.getAccount().getCredentials().size());
163 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500164
165 private void setSrtpPreferenceDetails(AccountDetailSrtp details) {
166
167 if (details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE)) {
168 findPreference("SRTP.enable").setSummary(
169 details.getDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE).toUpperCase(Locale.getDefault()));
170
171 } else {
172 findPreference("SRTP.enable").setSummary(getResources().getString(R.string.account_srtp_deactivated));
173
174 }
175
176 findPreference("SRTP.details").setEnabled(details.getDetailBoolean(AccountDetailSrtp.CONFIG_SRTP_ENABLE));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500177 }
178
179 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500180
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500181 findPreference("SRTP.enable").setOnPreferenceChangeListener(changeSrtpModeListener);
182 findPreference("SRTP.details").setOnPreferenceClickListener(new OnPreferenceClickListener() {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500183
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500184 @Override
185 public boolean onPreferenceClick(Preference preference) {
186 mCallbacks.displaySRTPScreen();
187 return false;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500188 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500189 });
190
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500191 }
192
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500193 Preference.OnPreferenceChangeListener changeSrtpModeListener = new Preference.OnPreferenceChangeListener() {
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500194 @Override
195 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500196
197 if (((String) newValue).contentEquals("NONE")) {
198 mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.FALSE_STR);
199 preference.setSummary(getResources().getString(R.string.account_srtp_deactivated));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500200 } else {
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500201 mCallbacks.getAccount().getSrtpDetails().setDetailString(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetail.TRUE_STR);
202 mCallbacks.getAccount().getSrtpDetails()
203 .setDetailString(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, ((String) newValue).toLowerCase(Locale.getDefault()));
204 preference.setSummary(((String) newValue));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500205 }
Alexandre Lisionc9c30b72013-11-18 16:27:26 -0500206 findPreference("SRTP.details").setEnabled(!((String) newValue).contentEquals("NONE"));
207 mCallbacks.getAccount().notifyObservers();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500208 return true;
209 }
210 };
211
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500212}