blob: 1bc79a7e52ae298b40b58f0c2a34585733a9d9c6 [file] [log] [blame]
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -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
32package org.sflphone.account;
33
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050034import android.app.Activity;
Alexandre Lision509a7552014-03-04 13:37:50 -050035import android.content.Intent;
Alexandre Lision8ad39592013-11-21 13:23:58 -050036import android.preference.CheckBoxPreference;
Alexandre Lision509a7552014-03-04 13:37:50 -050037import android.preference.ListPreference;
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050038import android.preference.Preference;
39import android.preference.Preference.OnPreferenceChangeListener;
40import android.preference.Preference.OnPreferenceClickListener;
41import android.preference.PreferenceScreen;
42import android.util.Log;
Alexandre Lision1e61e302014-04-10 15:05:03 -040043import org.sflphone.R;
Alexandre Lision509a7552014-03-04 13:37:50 -050044import org.sflphone.fragments.NestedSettingsFragment;
45import org.sflphone.model.Account;
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050046
Alexandre Lision382f6842014-02-05 15:23:50 -050047import java.io.File;
48
Alexandre Lision6a13ac02014-02-04 15:58:20 -050049public class TLSManager {
Alexandre Lision1e61e302014-04-10 15:05:03 -040050 private static final String TAG = TLSManager.class.getSimpleName();
51 private static final int SELECT_CA_LIST_RC = 42;
52 private static final int SELECT_PRIVATE_KEY_RC = 43;
53 private static final int SELECT_CERTIFICATE_RC = 44;
54 private OnPreferenceClickListener filePickerListener = new OnPreferenceClickListener() {
55 @Override
56 public boolean onPreferenceClick(Preference preference) {
57 if (preference.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)) {
58 performFileSearch(SELECT_CA_LIST_RC);
59 }
60 if (preference.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)) {
61 performFileSearch(SELECT_PRIVATE_KEY_RC);
62 }
63 if (preference.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE)) {
64 performFileSearch(SELECT_CERTIFICATE_RC);
65 }
66 return true;
67 }
68 };
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050069 PreferenceScreen mScreen;
70 private Account mAccount;
Alexandre Lision509a7552014-03-04 13:37:50 -050071 private NestedSettingsFragment mFrag;
Alexandre Lision1e61e302014-04-10 15:05:03 -040072 private OnPreferenceChangeListener tlsListener = new OnPreferenceChangeListener() {
73
74 @Override
75 public boolean onPreferenceChange(Preference preference, Object newValue) {
76 Log.i("TLS", "Setting " + preference.getKey() + " to" + newValue);
77
78 if (preference.getKey().contentEquals("TLS.enable")) {
79 togglePreferenceScreen((Boolean) newValue);
80 }
81
82 if (preference instanceof CheckBoxPreference) {
83 mAccount.getTlsDetails().setDetailString(preference.getKey(), Boolean.toString((Boolean) newValue));
84 } else {
85 preference.setSummary((String) newValue);
86 mAccount.getTlsDetails().setDetailString(preference.getKey(), (String) newValue);
87 }
88 mAccount.notifyObservers();
89 return true;
90 }
91 };
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050092
Alexandre Lision6a13ac02014-02-04 15:58:20 -050093 public void onCreate(NestedSettingsFragment con, PreferenceScreen preferenceScreen, Account acc) {
Alexandre Lision509a7552014-03-04 13:37:50 -050094 mFrag = con;
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050095 mScreen = preferenceScreen;
96 mAccount = acc;
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050097 setDetails();
98 }
99
100 private void setDetails() {
Alexandre Lision382f6842014-02-05 15:23:50 -0500101 boolean activated = mAccount.getTlsDetails().getDetailBoolean(AccountDetailTls.CONFIG_TLS_ENABLE);
102
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500103 for (int i = 0; i < mScreen.getPreferenceCount(); ++i) {
104
Alexandre Lision06412a32014-02-05 17:33:04 -0500105 Preference current = mScreen.getPreference(i);
106
107 if (current instanceof CheckBoxPreference) {
Alexandre Lision8ad39592013-11-21 13:23:58 -0500108 ((CheckBoxPreference) mScreen.getPreference(i)).setChecked(mAccount.getTlsDetails().getDetailBoolean(
109 mScreen.getPreference(i).getKey()));
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500110 } else {
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500111 if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)) {
Alexandre Lision1e61e302014-04-10 15:05:03 -0400112 File crt = new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE));
113 current.setSummary(crt.getName());
Alexandre Lision06412a32014-02-05 17:33:04 -0500114 current.setOnPreferenceClickListener(filePickerListener);
Alexandre Lision056c4942014-04-11 12:42:12 -0400115 setFeedbackIcon(current, crt.getAbsolutePath());
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500116 } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)) {
Alexandre Lision06412a32014-02-05 17:33:04 -0500117 current.setSummary(new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)).getName());
118 current.setOnPreferenceClickListener(filePickerListener);
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500119 } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE)) {
Alexandre Lision056c4942014-04-11 12:42:12 -0400120 File pem = new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE));
121 current.setSummary(pem.getName());
Alexandre Lision06412a32014-02-05 17:33:04 -0500122 current.setOnPreferenceClickListener(filePickerListener);
Alexandre Lision056c4942014-04-11 12:42:12 -0400123 setFeedbackIcon(current, pem.getAbsolutePath());
124 checkForRSAKey(pem.getAbsolutePath());
Alexandre Lision509a7552014-03-04 13:37:50 -0500125 } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_METHOD)) {
126 String[] values = mFrag.getTlsMethods();
127 ((ListPreference)current).setEntries(values);
128 ((ListPreference)current).setEntryValues(values);
Alexandre Lision21906ba2014-04-15 17:11:08 -0400129 current.setSummary(mAccount.getTlsDetails().getDetailString(mScreen.getPreference(i).getKey()));
Alexandre Lision06412a32014-02-05 17:33:04 -0500130 } else {
131 current.setSummary(mAccount.getTlsDetails().getDetailString(mScreen.getPreference(i).getKey()));
132 }
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500133 }
134
Alexandre Lision06412a32014-02-05 17:33:04 -0500135 // First Preference should remain enabled, it's the actual switch TLS.enable
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500136 if (i > 0)
Alexandre Lision06412a32014-02-05 17:33:04 -0500137 current.setEnabled(activated);
Alexandre Lision382f6842014-02-05 15:23:50 -0500138
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500139 }
140 }
141
Alexandre Lision056c4942014-04-11 12:42:12 -0400142 private void checkForRSAKey(String path) {
143 if(mFrag.findRSAKey(path)){
144 mScreen.findPreference(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE).setEnabled(false);
145 }else {
146 mScreen.findPreference(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE).setEnabled(true);
147 }
148 }
149
150 private void setFeedbackIcon(Preference current, String crtPath) {
Alexandre Lision1e61e302014-04-10 15:05:03 -0400151 if(!mFrag.checkCertificate(crtPath)){
Alexandre Lision056c4942014-04-11 12:42:12 -0400152 current.setIcon(R.drawable.ic_error);
Alexandre Lision1e61e302014-04-10 15:05:03 -0400153 } else {
Alexandre Lision056c4942014-04-11 12:42:12 -0400154 current.setIcon(R.drawable.ic_good);
Alexandre Lision06412a32014-02-05 17:33:04 -0500155 }
Alexandre Lision1e61e302014-04-10 15:05:03 -0400156 }
Alexandre Lision06412a32014-02-05 17:33:04 -0500157
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500158 public void setTLSListener() {
159 for (int i = 0; i < mScreen.getPreferenceCount(); ++i) {
160 mScreen.getPreference(i).setOnPreferenceChangeListener(tlsListener);
161 }
162 }
163
Alexandre Lision382f6842014-02-05 15:23:50 -0500164 private void togglePreferenceScreen(Boolean state) {
165 for (int i = 1; i < mScreen.getPreferenceCount(); ++i) {
166 mScreen.getPreference(i).setEnabled(state);
167 }
168 }
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500169
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500170 public void performFileSearch(int requestCodeToSet) {
171
172 // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
173 // browser.
174 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
175
176 // Filter to only show results that can be "opened", such as a
177 // file (as opposed to a list of contacts or timezones)
178 intent.addCategory(Intent.CATEGORY_OPENABLE);
179
180 // Filter to show only images, using the image MIME data type.
181 // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
182 // To search for all documents available via installed storage providers,
183 // it would be "*/*".
184 intent.setType("*/*");
Alexandre Lision509a7552014-03-04 13:37:50 -0500185 mFrag.startActivityForResult(intent, requestCodeToSet);
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500186 }
Alexandre Lision8ad39592013-11-21 13:23:58 -0500187
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500188 public void onActivityResult(int requestCode, int resultCode, Intent data) {
189 // TODO Extract returned filed for intent and populate correct preference
Alexandre Lision382f6842014-02-05 15:23:50 -0500190
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500191 if (resultCode == Activity.RESULT_CANCELED)
Alexandre Lision382f6842014-02-05 15:23:50 -0500192 return;
193
Alexandre Lision1e61e302014-04-10 15:05:03 -0400194 File myFile = new File(data.getData().getEncodedPath());
Alexandre Lision056c4942014-04-11 12:42:12 -0400195 Preference pref;
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500196 switch (requestCode) {
Alexandre Lision382f6842014-02-05 15:23:50 -0500197 case SELECT_CA_LIST_RC:
Alexandre Lision056c4942014-04-11 12:42:12 -0400198 pref = mScreen.findPreference(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE);
199 pref.setSummary(myFile.getName());
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500200 mAccount.getTlsDetails().setDetailString(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE, myFile.getAbsolutePath());
201 mAccount.notifyObservers();
Alexandre Lision056c4942014-04-11 12:42:12 -0400202 setFeedbackIcon(pref, myFile.getAbsolutePath());
Alexandre Lision382f6842014-02-05 15:23:50 -0500203 break;
204 case SELECT_PRIVATE_KEY_RC:
205 mScreen.findPreference(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE).setSummary(myFile.getName());
206 mAccount.getTlsDetails().setDetailString(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE, myFile.getAbsolutePath());
207 mAccount.notifyObservers();
208 break;
209 case SELECT_CERTIFICATE_RC:
Alexandre Lision056c4942014-04-11 12:42:12 -0400210 pref = mScreen.findPreference(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE);
211 pref.setSummary(myFile.getName());
Alexandre Lision382f6842014-02-05 15:23:50 -0500212 mAccount.getTlsDetails().setDetailString(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE, myFile.getAbsolutePath());
213 mAccount.notifyObservers();
Alexandre Lision056c4942014-04-11 12:42:12 -0400214 setFeedbackIcon(pref, myFile.getAbsolutePath());
215 checkForRSAKey(myFile.getAbsolutePath());
Alexandre Lision382f6842014-02-05 15:23:50 -0500216 break;
217 }
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500218 }
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500219}