blob: 5032933cec470486b9adcfbec130f43d234d64f5 [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 Lision6a13ac02014-02-04 15:58:20 -050034import android.app.Fragment;
35import android.content.Intent;
36import org.sflphone.fragments.NestedSettingsFragment;
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050037import org.sflphone.model.Account;
38
39import android.app.Activity;
Alexandre Lision8ad39592013-11-21 13:23:58 -050040import android.preference.CheckBoxPreference;
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050041import android.preference.Preference;
42import android.preference.Preference.OnPreferenceChangeListener;
43import android.preference.Preference.OnPreferenceClickListener;
44import android.preference.PreferenceScreen;
45import android.util.Log;
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 Lisiondd5d8de2013-11-18 16:26:14 -050050 PreferenceScreen mScreen;
51 private Account mAccount;
Alexandre Lision6a13ac02014-02-04 15:58:20 -050052 private Fragment mContext;
Alexandre Lision4cb73f52014-02-06 15:14:19 -050053 private static final String TAG = TLSManager.class.getSimpleName();
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050054
Alexandre Lision6a13ac02014-02-04 15:58:20 -050055 public void onCreate(NestedSettingsFragment con, PreferenceScreen preferenceScreen, Account acc) {
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050056 mContext = con;
57 mScreen = preferenceScreen;
58 mAccount = acc;
59
60 setDetails();
61 }
62
63 private void setDetails() {
Alexandre Lision382f6842014-02-05 15:23:50 -050064
65 boolean activated = mAccount.getTlsDetails().getDetailBoolean(AccountDetailTls.CONFIG_TLS_ENABLE);
66
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050067 for (int i = 0; i < mScreen.getPreferenceCount(); ++i) {
68
Alexandre Lision06412a32014-02-05 17:33:04 -050069 Preference current = mScreen.getPreference(i);
70
71 if (current instanceof CheckBoxPreference) {
Alexandre Lision8ad39592013-11-21 13:23:58 -050072 ((CheckBoxPreference) mScreen.getPreference(i)).setChecked(mAccount.getTlsDetails().getDetailBoolean(
73 mScreen.getPreference(i).getKey()));
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050074 } else {
Alexandre Lision4cb73f52014-02-06 15:14:19 -050075 if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)) {
Alexandre Lision06412a32014-02-05 17:33:04 -050076 current.setSummary(new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)).getName());
77 current.setOnPreferenceClickListener(filePickerListener);
Alexandre Lision4cb73f52014-02-06 15:14:19 -050078 } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)) {
Alexandre Lision06412a32014-02-05 17:33:04 -050079 current.setSummary(new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)).getName());
80 current.setOnPreferenceClickListener(filePickerListener);
Alexandre Lision4cb73f52014-02-06 15:14:19 -050081 } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE)) {
Alexandre Lision06412a32014-02-05 17:33:04 -050082 current.setSummary(new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE)).getName());
83 current.setOnPreferenceClickListener(filePickerListener);
84 } else {
85 current.setSummary(mAccount.getTlsDetails().getDetailString(mScreen.getPreference(i).getKey()));
86 }
87
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050088 }
89
Alexandre Lision06412a32014-02-05 17:33:04 -050090 // First Preference should remain enabled, it's the actual switch TLS.enable
Alexandre Lision4cb73f52014-02-06 15:14:19 -050091 if (i > 0)
Alexandre Lision06412a32014-02-05 17:33:04 -050092 current.setEnabled(activated);
Alexandre Lision382f6842014-02-05 15:23:50 -050093
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -050094 }
95 }
96
Alexandre Lision06412a32014-02-05 17:33:04 -050097 private OnPreferenceClickListener filePickerListener = new OnPreferenceClickListener() {
98 @Override
99 public boolean onPreferenceClick(Preference preference) {
100 if (preference.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)) {
101 performFileSearch(SELECT_CA_LIST_RC);
102 }
103 if (preference.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)) {
104 performFileSearch(SELECT_PRIVATE_KEY_RC);
105 }
106 if (preference.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE)) {
107 performFileSearch(SELECT_CERTIFICATE_RC);
108 }
109 return true;
110 }
111 };
112
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500113 public void setTLSListener() {
114 for (int i = 0; i < mScreen.getPreferenceCount(); ++i) {
115 mScreen.getPreference(i).setOnPreferenceChangeListener(tlsListener);
116 }
117 }
118
119 private OnPreferenceChangeListener tlsListener = new OnPreferenceChangeListener() {
120
121 @Override
122 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lision382f6842014-02-05 15:23:50 -0500123 Log.i("TLS", "Setting " + preference.getKey() + " to" + newValue);
124
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500125 if (preference.getKey().contentEquals("TLS.enable")) {
Alexandre Lision382f6842014-02-05 15:23:50 -0500126 togglePreferenceScreen((Boolean) newValue);
127 }
128
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500129 if (preference instanceof CheckBoxPreference) {
Alexandre Lision382f6842014-02-05 15:23:50 -0500130 mAccount.getTlsDetails().setDetailString(preference.getKey(), Boolean.toString((Boolean) newValue));
131 } else {
132 preference.setSummary((String) newValue);
133 mAccount.getTlsDetails().setDetailString(preference.getKey(), (String) newValue);
134 }
135
136
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500137 mAccount.notifyObservers();
138 return true;
139 }
140 };
141
Alexandre Lision382f6842014-02-05 15:23:50 -0500142 private void togglePreferenceScreen(Boolean state) {
143 for (int i = 1; i < mScreen.getPreferenceCount(); ++i) {
144 mScreen.getPreference(i).setEnabled(state);
145 }
146 }
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500147
148 private static final int SELECT_CA_LIST_RC = 42;
149 private static final int SELECT_PRIVATE_KEY_RC = 43;
150 private static final int SELECT_CERTIFICATE_RC = 44;
151
152 public void performFileSearch(int requestCodeToSet) {
153
154 // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
155 // browser.
156 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
157
158 // Filter to only show results that can be "opened", such as a
159 // file (as opposed to a list of contacts or timezones)
160 intent.addCategory(Intent.CATEGORY_OPENABLE);
161
162 // Filter to show only images, using the image MIME data type.
163 // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
164 // To search for all documents available via installed storage providers,
165 // it would be "*/*".
166 intent.setType("*/*");
167 mContext.startActivityForResult(intent, requestCodeToSet);
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500168 }
Alexandre Lision8ad39592013-11-21 13:23:58 -0500169
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500170 public void onActivityResult(int requestCode, int resultCode, Intent data) {
171 // TODO Extract returned filed for intent and populate correct preference
Alexandre Lision382f6842014-02-05 15:23:50 -0500172
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500173 if (resultCode == Activity.RESULT_CANCELED)
Alexandre Lision382f6842014-02-05 15:23:50 -0500174 return;
175
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500176 File myFile = new File(data.getData().toString());
177 Log.i(TAG, "file selected:" + data.getData());
178 switch (requestCode) {
Alexandre Lision382f6842014-02-05 15:23:50 -0500179 case SELECT_CA_LIST_RC:
Alexandre Lision4cb73f52014-02-06 15:14:19 -0500180 mScreen.findPreference(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE).setSummary(myFile.getName());
181 mAccount.getTlsDetails().setDetailString(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE, myFile.getAbsolutePath());
182 mAccount.notifyObservers();
Alexandre Lision382f6842014-02-05 15:23:50 -0500183 break;
184 case SELECT_PRIVATE_KEY_RC:
185 mScreen.findPreference(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE).setSummary(myFile.getName());
186 mAccount.getTlsDetails().setDetailString(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE, myFile.getAbsolutePath());
187 mAccount.notifyObservers();
188 break;
189 case SELECT_CERTIFICATE_RC:
190 mScreen.findPreference(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE).setSummary(myFile.getName());
191 mAccount.getTlsDetails().setDetailString(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE, myFile.getAbsolutePath());
192 mAccount.notifyObservers();
193 break;
194 }
195
196
Alexandre Lision6a13ac02014-02-04 15:58:20 -0500197 }
Alexandre Lisiondd5d8de2013-11-18 16:26:14 -0500198}