Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 3 | * |
| 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 | |
| 32 | package org.sflphone.account; |
| 33 | |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 34 | import android.app.Fragment; |
| 35 | import android.content.Intent; |
| 36 | import org.sflphone.fragments.NestedSettingsFragment; |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 37 | import org.sflphone.model.Account; |
| 38 | |
| 39 | import android.app.Activity; |
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 40 | import android.preference.CheckBoxPreference; |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 41 | import android.preference.Preference; |
| 42 | import android.preference.Preference.OnPreferenceChangeListener; |
| 43 | import android.preference.Preference.OnPreferenceClickListener; |
| 44 | import android.preference.PreferenceScreen; |
| 45 | import android.util.Log; |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 46 | |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 47 | import java.io.File; |
| 48 | |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 49 | public class TLSManager { |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 50 | PreferenceScreen mScreen; |
| 51 | private Account mAccount; |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 52 | private Fragment mContext; |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 53 | private static final String TAG = TLSManager.class.getSimpleName(); |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 54 | |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 55 | public void onCreate(NestedSettingsFragment con, PreferenceScreen preferenceScreen, Account acc) { |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 56 | mContext = con; |
| 57 | mScreen = preferenceScreen; |
| 58 | mAccount = acc; |
| 59 | |
| 60 | setDetails(); |
| 61 | } |
| 62 | |
| 63 | private void setDetails() { |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 64 | |
| 65 | boolean activated = mAccount.getTlsDetails().getDetailBoolean(AccountDetailTls.CONFIG_TLS_ENABLE); |
| 66 | |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 67 | for (int i = 0; i < mScreen.getPreferenceCount(); ++i) { |
| 68 | |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 69 | Preference current = mScreen.getPreference(i); |
| 70 | |
| 71 | if (current instanceof CheckBoxPreference) { |
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 72 | ((CheckBoxPreference) mScreen.getPreference(i)).setChecked(mAccount.getTlsDetails().getDetailBoolean( |
| 73 | mScreen.getPreference(i).getKey())); |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 74 | } else { |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 75 | if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)) { |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 76 | current.setSummary(new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE)).getName()); |
| 77 | current.setOnPreferenceClickListener(filePickerListener); |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 78 | } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)) { |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 79 | current.setSummary(new File(mAccount.getTlsDetails().getDetailString(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE)).getName()); |
| 80 | current.setOnPreferenceClickListener(filePickerListener); |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 81 | } else if (current.getKey().contentEquals(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE)) { |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 82 | 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 Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 90 | // First Preference should remain enabled, it's the actual switch TLS.enable |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 91 | if (i > 0) |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 92 | current.setEnabled(activated); |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 93 | |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Alexandre Lision | 06412a3 | 2014-02-05 17:33:04 -0500 | [diff] [blame] | 97 | 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 Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 113 | 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 Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 123 | Log.i("TLS", "Setting " + preference.getKey() + " to" + newValue); |
| 124 | |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 125 | if (preference.getKey().contentEquals("TLS.enable")) { |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 126 | togglePreferenceScreen((Boolean) newValue); |
| 127 | } |
| 128 | |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 129 | if (preference instanceof CheckBoxPreference) { |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 130 | 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 Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 137 | mAccount.notifyObservers(); |
| 138 | return true; |
| 139 | } |
| 140 | }; |
| 141 | |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 142 | private void togglePreferenceScreen(Boolean state) { |
| 143 | for (int i = 1; i < mScreen.getPreferenceCount(); ++i) { |
| 144 | mScreen.getPreference(i).setEnabled(state); |
| 145 | } |
| 146 | } |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 147 | |
| 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 Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 168 | } |
Alexandre Lision | 8ad3959 | 2013-11-21 13:23:58 -0500 | [diff] [blame] | 169 | |
Alexandre Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 170 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 171 | // TODO Extract returned filed for intent and populate correct preference |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 172 | |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 173 | if (resultCode == Activity.RESULT_CANCELED) |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 174 | return; |
| 175 | |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 176 | File myFile = new File(data.getData().toString()); |
| 177 | Log.i(TAG, "file selected:" + data.getData()); |
| 178 | switch (requestCode) { |
Alexandre Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 179 | case SELECT_CA_LIST_RC: |
Alexandre Lision | 4cb73f5 | 2014-02-06 15:14:19 -0500 | [diff] [blame] | 180 | 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 Lision | 382f684 | 2014-02-05 15:23:50 -0500 | [diff] [blame] | 183 | 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 Lision | 6a13ac0 | 2014-02-04 15:58:20 -0500 | [diff] [blame] | 197 | } |
Alexandre Lision | dd5d8de | 2013-11-18 16:26:14 -0500 | [diff] [blame] | 198 | } |