blob: 81908acf70f6086a0cbc71d04b4448840fb85878 [file] [log] [blame]
Alexandre Savard68838112012-10-30 11:34:43 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@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 com.savoirfairelinux.sflphone.client;
33
34import android.app.Activity;
35import android.content.ComponentName;
36import android.content.Context;
37import android.content.ServiceConnection;
38import android.content.Intent;
39import android.os.Bundle;
40import android.os.IBinder;
41import android.os.RemoteException;
42import android.preference.Preference;
43import android.preference.PreferenceActivity;
44import android.preference.PreferenceManager;
45import android.preference.PreferenceScreen;
46import android.preference.EditTextPreference;
47import android.preference.CheckBoxPreference;
48import android.util.Log;
Alexandre Savard5195d922012-10-30 16:58:50 -040049import android.view.Menu;
50import android.view.MenuItem;
Alexandre Savard68838112012-10-30 11:34:43 -040051
52import com.savoirfairelinux.sflphone.R;
Alexandre Savard68838112012-10-30 11:34:43 -040053import com.savoirfairelinux.sflphone.utils.AccountDetail;
54import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler;
55import com.savoirfairelinux.sflphone.utils.AccountDetailBasic;
56import com.savoirfairelinux.sflphone.utils.AccountDetailAdvanced;
57import com.savoirfairelinux.sflphone.utils.AccountDetailSrtp;
58import com.savoirfairelinux.sflphone.utils.AccountDetailTls;
59
60import java.util.HashMap;
61import java.util.ArrayList;
62
63public class AccountPreferenceActivity extends PreferenceActivity
64{
65 private static final String TAG = "AccoutPreferenceActivity";
66 public static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 0;
67 public static final int ACCOUNT_NOT_MODIFIED = Activity.RESULT_FIRST_USER + 1;
Alexandre Savard5195d922012-10-30 16:58:50 -040068 public static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2;
Alexandre Savard68838112012-10-30 11:34:43 -040069
Alexandre Savard833616f2012-10-30 16:02:30 -040070 private AccountDetailBasic basicDetails = null;
71 private AccountDetailAdvanced advancedDetails = null;
72 private AccountDetailSrtp srtpDetails = null;
73 private AccountDetailTls tlsDetails = null;
Alexandre Savard68838112012-10-30 11:34:43 -040074 private PreferenceManager mPreferenceManager;
75 private HashMap<String, String> mPreferenceMap;
Alexandre Savard5195d922012-10-30 16:58:50 -040076 private MenuItem deleteAccountAction = null;
Alexandre Savard68838112012-10-30 11:34:43 -040077 private String mAccountID;
78
Alexandre Savard5195d922012-10-30 16:58:50 -040079 Preference.OnPreferenceChangeListener changeBasicPreferenceListener =
80 new Preference.OnPreferenceChangeListener() {
Alexandre Savard68838112012-10-30 11:34:43 -040081 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savard833616f2012-10-30 16:02:30 -040082 preference.setSummary(getString(R.string.account_current_value_label)+(CharSequence)newValue);
83
84 // String preferenceKey = basicDetailKeys.get(preference.getOrder()).mKey;
85 // accountPreference.preferenceMap.put(preferenceKey, ((CharSequence)newValue).toString());
86 basicDetails.setDetailString(preference.getOrder(), ((CharSequence)newValue).toString());
87 // if(preferenceKey == AccountDetailBasic.CONFIG_ACCOUNT_ALIAS)
88 // accountPreference.mScreen.setTitle(((CharSequence)newValue.toString()));
Alexandre Savard68838112012-10-30 11:34:43 -040089 return true;
90 }
91 };
92
Alexandre Savard5195d922012-10-30 16:58:50 -040093 Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener =
94 new Preference.OnPreferenceChangeListener() {
Alexandre Savard68838112012-10-30 11:34:43 -040095 public boolean onPreferenceChange(Preference preference, Object newValue) {
96 return true;
97 }
98 };
99
Alexandre Savard833616f2012-10-30 16:02:30 -0400100 private void init() {
Alexandre Savard68838112012-10-30 11:34:43 -0400101
102 Bundle b = getIntent().getExtras();
103 mAccountID = b.getString("AccountID");
104 ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG);
105 ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG);
106 ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG);
107 ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG);
108
109 basicDetails = new AccountDetailBasic(basicPreferenceList);
110 advancedDetails = new AccountDetailAdvanced(advancedPreferenceList);
111 srtpDetails = new AccountDetailSrtp(srtpPreferenceList);
112 tlsDetails = new AccountDetailTls(tlsPreferenceList);
113
114 setPreferenceDetails(basicDetails);
Alexandre Savard833616f2012-10-30 16:02:30 -0400115 setPreferenceDetails(advancedDetails);
116 setPreferenceDetails(srtpDetails);
117 setPreferenceDetails(tlsDetails);
118
119 addPreferenceListener(basicDetails);
120 addPreferenceListener(advancedDetails);
121 addPreferenceListener(srtpDetails);
122 addPreferenceListener(tlsDetails);
Alexandre Savard68838112012-10-30 11:34:43 -0400123 }
124
125 private void setPreferenceDetails(AccountDetail details) {
126 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
127 Preference pref = mPreferenceManager.findPreference(p.mKey);
128 if(pref != null) {
129 Log.i(TAG, "setPreferenceDetails FOUND " + p.mKey + " = " + p.mValue);
130 if(!p.isTwoState) {
131 ((EditTextPreference)pref).setText(p.mValue);
132 pref.setSummary(getString(R.string.account_current_value_label) + p.mValue);
133 }
134 }
135 }
136 }
137
138 private void addPreferenceListener(AccountDetail details) {
139 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
140 Preference pref = mPreferenceManager.findPreference(p.mKey);
141 if(pref != null) {
142 if(!p.isTwoState) {
Alexandre Savard833616f2012-10-30 16:02:30 -0400143 pref.setOnPreferenceChangeListener(changeBasicPreferenceListener);
Alexandre Savard68838112012-10-30 11:34:43 -0400144 }
145 }
146 }
147 }
148
149 @Override
Alexandre Savard833616f2012-10-30 16:02:30 -0400150 protected void onCreate(Bundle savedInstanceState) {
151 super.onCreate(savedInstanceState);
152
153 addPreferencesFromResource(R.xml.account_creation_preferences);
154 mPreferenceManager = getPreferenceManager();
155
156 init();
157 }
158
159 @Override
Alexandre Savard5195d922012-10-30 16:58:50 -0400160 public boolean onCreateOptionsMenu(Menu menu) {
161 deleteAccountAction = menu.add("Delete Account");
162 deleteAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
163 return true;
164 }
165
166 @Override
Alexandre Savard68838112012-10-30 11:34:43 -0400167 protected void onStart() {
168 super.onStart();
Alexandre Savard68838112012-10-30 11:34:43 -0400169 }
170
171 @Override
172 protected void onStop() {
173 super.onStop();
Alexandre Savard68838112012-10-30 11:34:43 -0400174 }
175
176 @Override
177 protected void onDestroy() {
178 super.onDestroy();
179 }
180
181 @Override
182 public void onBackPressed() {
183 Bundle bundle = new Bundle();
184 bundle.putString("AccountID", mAccountID);
185 bundle.putStringArrayList(AccountDetailBasic.BUNDLE_TAG, basicDetails.getValuesOnly());
186 bundle.putStringArrayList(AccountDetailAdvanced.BUNDLE_TAG, advancedDetails.getValuesOnly());
187 bundle.putStringArrayList(AccountDetailSrtp.BUNDLE_TAG, srtpDetails.getValuesOnly());
188 bundle.putStringArrayList(AccountDetailTls.BUNDLE_TAG, tlsDetails.getValuesOnly());
189
190 Intent resultIntent = new Intent();
191 resultIntent.putExtras(bundle);
192
193 setResult(ACCOUNT_MODIFIED, resultIntent);
194 finish();
195 }
196
Alexandre Savard5195d922012-10-30 16:58:50 -0400197 @Override
198 public boolean onOptionsItemSelected(MenuItem item) {
199 Bundle bundle = new Bundle();
200 bundle.putString("AccountID", mAccountID);
201
202 Intent resultIntent = new Intent();
203 resultIntent.putExtras(bundle);
204
205 setResult(ACCOUNT_DELETED, resultIntent);
206 finish();
207 return true;
208 }
209
Alexandre Savard68838112012-10-30 11:34:43 -0400210 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
211 for(AccountDetail.PreferenceEntry p : det.getDetailValues()) {
212 Preference pref = mPreferenceManager.findPreference(p.mKey);
213 if(pref != null) {
214 if(p.isTwoState) {
215 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
216 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
217 }
218 else {
219 EditTextPreference textPref = (EditTextPreference)pref;
220 accountDetails.put(p.mKey, textPref.getText());
221 }
222 }
223 }
224 }
225
226 private void updateAccountDetails() {
227 HashMap<String, String> accountDetails = new HashMap<String, String>();
228
229 updateAccountDetails(accountDetails, basicDetails);
230 updateAccountDetails(accountDetails, advancedDetails);
231 updateAccountDetails(accountDetails, srtpDetails);
232 updateAccountDetails(accountDetails, tlsDetails);
233 }
234}
235