blob: ecb61336bb08c43896910a011448d0a4c9188880 [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;
Alexandre Savard645b29c2012-10-30 17:22:26 -040035import android.app.AlertDialog;
36import android.app.Dialog;
Alexandre Savard68838112012-10-30 11:34:43 -040037import android.content.ComponentName;
38import android.content.Context;
Alexandre Savard645b29c2012-10-30 17:22:26 -040039import android.content.DialogInterface;
Alexandre Savard68838112012-10-30 11:34:43 -040040import android.content.ServiceConnection;
41import android.content.Intent;
42import android.os.Bundle;
43import android.os.IBinder;
44import android.os.RemoteException;
45import android.preference.Preference;
46import android.preference.PreferenceActivity;
47import android.preference.PreferenceManager;
48import android.preference.PreferenceScreen;
49import android.preference.EditTextPreference;
50import android.preference.CheckBoxPreference;
51import android.util.Log;
Alexandre Savard5195d922012-10-30 16:58:50 -040052import android.view.Menu;
53import android.view.MenuItem;
Alexandre Savard68838112012-10-30 11:34:43 -040054
55import com.savoirfairelinux.sflphone.R;
Alexandre Savard68838112012-10-30 11:34:43 -040056import com.savoirfairelinux.sflphone.utils.AccountDetail;
57import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler;
58import com.savoirfairelinux.sflphone.utils.AccountDetailBasic;
59import com.savoirfairelinux.sflphone.utils.AccountDetailAdvanced;
60import com.savoirfairelinux.sflphone.utils.AccountDetailSrtp;
61import com.savoirfairelinux.sflphone.utils.AccountDetailTls;
62
63import java.util.HashMap;
64import java.util.ArrayList;
65
66public class AccountPreferenceActivity extends PreferenceActivity
67{
68 private static final String TAG = "AccoutPreferenceActivity";
69 public static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 0;
70 public static final int ACCOUNT_NOT_MODIFIED = Activity.RESULT_FIRST_USER + 1;
Alexandre Savard5195d922012-10-30 16:58:50 -040071 public static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2;
Alexandre Savard68838112012-10-30 11:34:43 -040072
Alexandre Savard833616f2012-10-30 16:02:30 -040073 private AccountDetailBasic basicDetails = null;
74 private AccountDetailAdvanced advancedDetails = null;
75 private AccountDetailSrtp srtpDetails = null;
76 private AccountDetailTls tlsDetails = null;
Alexandre Savard68838112012-10-30 11:34:43 -040077 private PreferenceManager mPreferenceManager;
78 private HashMap<String, String> mPreferenceMap;
Alexandre Savard5195d922012-10-30 16:58:50 -040079 private MenuItem deleteAccountAction = null;
Alexandre Savard68838112012-10-30 11:34:43 -040080 private String mAccountID;
81
Alexandre Savard5195d922012-10-30 16:58:50 -040082 Preference.OnPreferenceChangeListener changeBasicPreferenceListener =
83 new Preference.OnPreferenceChangeListener() {
Alexandre Savard68838112012-10-30 11:34:43 -040084 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savard833616f2012-10-30 16:02:30 -040085 preference.setSummary(getString(R.string.account_current_value_label)+(CharSequence)newValue);
86
87 // String preferenceKey = basicDetailKeys.get(preference.getOrder()).mKey;
88 // accountPreference.preferenceMap.put(preferenceKey, ((CharSequence)newValue).toString());
89 basicDetails.setDetailString(preference.getOrder(), ((CharSequence)newValue).toString());
90 // if(preferenceKey == AccountDetailBasic.CONFIG_ACCOUNT_ALIAS)
91 // accountPreference.mScreen.setTitle(((CharSequence)newValue.toString()));
Alexandre Savard68838112012-10-30 11:34:43 -040092 return true;
93 }
94 };
95
Alexandre Savard5195d922012-10-30 16:58:50 -040096 Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener =
97 new Preference.OnPreferenceChangeListener() {
Alexandre Savard68838112012-10-30 11:34:43 -040098 public boolean onPreferenceChange(Preference preference, Object newValue) {
99 return true;
100 }
101 };
102
Alexandre Savard833616f2012-10-30 16:02:30 -0400103 private void init() {
Alexandre Savard68838112012-10-30 11:34:43 -0400104
105 Bundle b = getIntent().getExtras();
106 mAccountID = b.getString("AccountID");
107 ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG);
108 ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG);
109 ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG);
110 ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG);
111
112 basicDetails = new AccountDetailBasic(basicPreferenceList);
113 advancedDetails = new AccountDetailAdvanced(advancedPreferenceList);
114 srtpDetails = new AccountDetailSrtp(srtpPreferenceList);
115 tlsDetails = new AccountDetailTls(tlsPreferenceList);
116
117 setPreferenceDetails(basicDetails);
Alexandre Savard833616f2012-10-30 16:02:30 -0400118 setPreferenceDetails(advancedDetails);
119 setPreferenceDetails(srtpDetails);
120 setPreferenceDetails(tlsDetails);
121
122 addPreferenceListener(basicDetails);
123 addPreferenceListener(advancedDetails);
124 addPreferenceListener(srtpDetails);
125 addPreferenceListener(tlsDetails);
Alexandre Savard68838112012-10-30 11:34:43 -0400126 }
127
128 private void setPreferenceDetails(AccountDetail details) {
129 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
130 Preference pref = mPreferenceManager.findPreference(p.mKey);
131 if(pref != null) {
Alexandre Savard68838112012-10-30 11:34:43 -0400132 if(!p.isTwoState) {
133 ((EditTextPreference)pref).setText(p.mValue);
134 pref.setSummary(getString(R.string.account_current_value_label) + p.mValue);
135 }
136 }
137 }
138 }
139
140 private void addPreferenceListener(AccountDetail details) {
141 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
142 Preference pref = mPreferenceManager.findPreference(p.mKey);
143 if(pref != null) {
144 if(!p.isTwoState) {
Alexandre Savard833616f2012-10-30 16:02:30 -0400145 pref.setOnPreferenceChangeListener(changeBasicPreferenceListener);
Alexandre Savard68838112012-10-30 11:34:43 -0400146 }
147 }
148 }
149 }
150
151 @Override
Alexandre Savard833616f2012-10-30 16:02:30 -0400152 protected void onCreate(Bundle savedInstanceState) {
153 super.onCreate(savedInstanceState);
154
155 addPreferencesFromResource(R.xml.account_creation_preferences);
156 mPreferenceManager = getPreferenceManager();
157
158 init();
159 }
160
161 @Override
Alexandre Savard5195d922012-10-30 16:58:50 -0400162 public boolean onCreateOptionsMenu(Menu menu) {
163 deleteAccountAction = menu.add("Delete Account");
164 deleteAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
165 return true;
166 }
167
168 @Override
Alexandre Savard68838112012-10-30 11:34:43 -0400169 protected void onStart() {
170 super.onStart();
Alexandre Savard68838112012-10-30 11:34:43 -0400171 }
172
173 @Override
174 protected void onStop() {
175 super.onStop();
Alexandre Savard68838112012-10-30 11:34:43 -0400176 }
177
178 @Override
179 protected void onDestroy() {
180 super.onDestroy();
181 }
182
183 @Override
184 public void onBackPressed() {
185 Bundle bundle = new Bundle();
186 bundle.putString("AccountID", mAccountID);
187 bundle.putStringArrayList(AccountDetailBasic.BUNDLE_TAG, basicDetails.getValuesOnly());
188 bundle.putStringArrayList(AccountDetailAdvanced.BUNDLE_TAG, advancedDetails.getValuesOnly());
189 bundle.putStringArrayList(AccountDetailSrtp.BUNDLE_TAG, srtpDetails.getValuesOnly());
190 bundle.putStringArrayList(AccountDetailTls.BUNDLE_TAG, tlsDetails.getValuesOnly());
191
192 Intent resultIntent = new Intent();
193 resultIntent.putExtras(bundle);
194
195 setResult(ACCOUNT_MODIFIED, resultIntent);
196 finish();
197 }
198
Alexandre Savard5195d922012-10-30 16:58:50 -0400199 @Override
200 public boolean onOptionsItemSelected(MenuItem item) {
Alexandre Savard645b29c2012-10-30 17:22:26 -0400201 AlertDialog dialog = createAlertDialog();
202 dialog.show();
203
Alexandre Savard5195d922012-10-30 16:58:50 -0400204 return true;
205 }
206
Alexandre Savard645b29c2012-10-30 17:22:26 -0400207 private AlertDialog createAlertDialog()
208 {
209 Activity ownerActivity = this;
210 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
211 builder.setMessage("Do you realy want to delete this account").setTitle("Delete Account")
212 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
213 public void onClick(DialogInterface dialog, int whichButton) {
214 Bundle bundle = new Bundle();
215 bundle.putString("AccountID", mAccountID);
216
217 Intent resultIntent = new Intent();
218 resultIntent.putExtras(bundle);
219
220 Activity activity = ((Dialog)dialog).getOwnerActivity();
221 activity.setResult(ACCOUNT_DELETED, resultIntent);
222 activity.finish();
223 }
224 })
225 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
226 public void onClick(DialogInterface dialog, int whichButton) {
227 /* Terminate with no action */
228 }
229 });
230
231 AlertDialog alertDialog = builder.create();
232 alertDialog.setOwnerActivity(ownerActivity);
233
234 return alertDialog;
235 }
236
Alexandre Savard68838112012-10-30 11:34:43 -0400237 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
238 for(AccountDetail.PreferenceEntry p : det.getDetailValues()) {
239 Preference pref = mPreferenceManager.findPreference(p.mKey);
240 if(pref != null) {
241 if(p.isTwoState) {
242 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
243 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
244 }
245 else {
246 EditTextPreference textPref = (EditTextPreference)pref;
247 accountDetails.put(p.mKey, textPref.getText());
248 }
249 }
250 }
251 }
252
253 private void updateAccountDetails() {
254 HashMap<String, String> accountDetails = new HashMap<String, String>();
255
256 updateAccountDetails(accountDetails, basicDetails);
257 updateAccountDetails(accountDetails, advancedDetails);
258 updateAccountDetails(accountDetails, srtpDetails);
259 updateAccountDetails(accountDetails, tlsDetails);
260 }
261}
262