blob: 72c8efd07f0780794f1bd6f960375c0d21594ae9 [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.Intent;
41import android.os.Bundle;
42import android.os.IBinder;
43import android.os.RemoteException;
44import android.preference.Preference;
45import android.preference.PreferenceActivity;
46import android.preference.PreferenceManager;
47import android.preference.PreferenceScreen;
48import android.preference.EditTextPreference;
49import android.preference.CheckBoxPreference;
50import android.util.Log;
Alexandre Savard5195d922012-10-30 16:58:50 -040051import android.view.Menu;
52import android.view.MenuItem;
Alexandre Savard68838112012-10-30 11:34:43 -040053
54import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040055import com.savoirfairelinux.sflphone.account.AccountDetail;
56import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced;
57import com.savoirfairelinux.sflphone.account.AccountDetailBasic;
58import com.savoirfairelinux.sflphone.account.AccountDetailSrtp;
59import com.savoirfairelinux.sflphone.account.AccountDetailTls;
60import com.savoirfairelinux.sflphone.account.AccountDetailsHandler;
Alexandre Savard68838112012-10-30 11:34:43 -040061
62import java.util.HashMap;
63import java.util.ArrayList;
64
65public class AccountPreferenceActivity extends PreferenceActivity
66{
67 private static final String TAG = "AccoutPreferenceActivity";
68 public static final int ACCOUNT_MODIFIED = Activity.RESULT_FIRST_USER + 0;
69 public static final int ACCOUNT_NOT_MODIFIED = Activity.RESULT_FIRST_USER + 1;
Alexandre Savard5195d922012-10-30 16:58:50 -040070 public static final int ACCOUNT_DELETED = Activity.RESULT_FIRST_USER + 2;
Alexandre Savard68838112012-10-30 11:34:43 -040071
Alexandre Savard833616f2012-10-30 16:02:30 -040072 private AccountDetailBasic basicDetails = null;
73 private AccountDetailAdvanced advancedDetails = null;
74 private AccountDetailSrtp srtpDetails = null;
75 private AccountDetailTls tlsDetails = null;
Alexandre Savard68838112012-10-30 11:34:43 -040076 private PreferenceManager mPreferenceManager;
77 private HashMap<String, String> mPreferenceMap;
Alexandre Savard5195d922012-10-30 16:58:50 -040078 private MenuItem deleteAccountAction = null;
Alexandre Savard68838112012-10-30 11:34:43 -040079 private String mAccountID;
80
Alexandre Savard5195d922012-10-30 16:58:50 -040081 Preference.OnPreferenceChangeListener changeBasicPreferenceListener =
82 new Preference.OnPreferenceChangeListener() {
Alexandre Savard68838112012-10-30 11:34:43 -040083 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savard833616f2012-10-30 16:02:30 -040084 preference.setSummary(getString(R.string.account_current_value_label)+(CharSequence)newValue);
85
86 // String preferenceKey = basicDetailKeys.get(preference.getOrder()).mKey;
87 // accountPreference.preferenceMap.put(preferenceKey, ((CharSequence)newValue).toString());
88 basicDetails.setDetailString(preference.getOrder(), ((CharSequence)newValue).toString());
89 // if(preferenceKey == AccountDetailBasic.CONFIG_ACCOUNT_ALIAS)
90 // accountPreference.mScreen.setTitle(((CharSequence)newValue.toString()));
Alexandre Savard68838112012-10-30 11:34:43 -040091 return true;
92 }
93 };
94
Alexandre Savard5195d922012-10-30 16:58:50 -040095 Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener =
96 new Preference.OnPreferenceChangeListener() {
Alexandre Savard68838112012-10-30 11:34:43 -040097 public boolean onPreferenceChange(Preference preference, Object newValue) {
98 return true;
99 }
100 };
101
Alexandre Savard833616f2012-10-30 16:02:30 -0400102 private void init() {
Alexandre Savard68838112012-10-30 11:34:43 -0400103
104 Bundle b = getIntent().getExtras();
105 mAccountID = b.getString("AccountID");
106 ArrayList<String> basicPreferenceList = b.getStringArrayList(AccountDetailBasic.BUNDLE_TAG);
107 ArrayList<String> advancedPreferenceList = b.getStringArrayList(AccountDetailAdvanced.BUNDLE_TAG);
108 ArrayList<String> srtpPreferenceList = b.getStringArrayList(AccountDetailSrtp.BUNDLE_TAG);
109 ArrayList<String> tlsPreferenceList = b.getStringArrayList(AccountDetailTls.BUNDLE_TAG);
110
111 basicDetails = new AccountDetailBasic(basicPreferenceList);
112 advancedDetails = new AccountDetailAdvanced(advancedPreferenceList);
113 srtpDetails = new AccountDetailSrtp(srtpPreferenceList);
114 tlsDetails = new AccountDetailTls(tlsPreferenceList);
115
116 setPreferenceDetails(basicDetails);
Alexandre Savard833616f2012-10-30 16:02:30 -0400117 setPreferenceDetails(advancedDetails);
118 setPreferenceDetails(srtpDetails);
119 setPreferenceDetails(tlsDetails);
120
121 addPreferenceListener(basicDetails);
122 addPreferenceListener(advancedDetails);
123 addPreferenceListener(srtpDetails);
124 addPreferenceListener(tlsDetails);
Alexandre Savard68838112012-10-30 11:34:43 -0400125 }
126
127 private void setPreferenceDetails(AccountDetail details) {
128 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
129 Preference pref = mPreferenceManager.findPreference(p.mKey);
130 if(pref != null) {
Alexandre Savard68838112012-10-30 11:34:43 -0400131 if(!p.isTwoState) {
132 ((EditTextPreference)pref).setText(p.mValue);
133 pref.setSummary(getString(R.string.account_current_value_label) + p.mValue);
134 }
135 }
136 }
137 }
138
139 private void addPreferenceListener(AccountDetail details) {
140 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
141 Preference pref = mPreferenceManager.findPreference(p.mKey);
142 if(pref != null) {
143 if(!p.isTwoState) {
Alexandre Savard833616f2012-10-30 16:02:30 -0400144 pref.setOnPreferenceChangeListener(changeBasicPreferenceListener);
Alexandre Savard68838112012-10-30 11:34:43 -0400145 }
146 }
147 }
148 }
149
150 @Override
Alexandre Savard833616f2012-10-30 16:02:30 -0400151 protected void onCreate(Bundle savedInstanceState) {
152 super.onCreate(savedInstanceState);
153
154 addPreferencesFromResource(R.xml.account_creation_preferences);
155 mPreferenceManager = getPreferenceManager();
156
157 init();
158 }
159
160 @Override
Alexandre Savard5195d922012-10-30 16:58:50 -0400161 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Savarde32a1732012-11-01 13:17:32 -0400162 Log.i(TAG, "onCreateOptionsMenu: " + mAccountID);
163 if(mAccountID.equals("IP2IP"))
164 return true;
165
166
Alexandre Savard5195d922012-10-30 16:58:50 -0400167 deleteAccountAction = menu.add("Delete Account");
168 deleteAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
169 return true;
170 }
171
172 @Override
Alexandre Savard68838112012-10-30 11:34:43 -0400173 protected void onStart() {
174 super.onStart();
Alexandre Savard68838112012-10-30 11:34:43 -0400175 }
176
177 @Override
178 protected void onStop() {
179 super.onStop();
Alexandre Savard68838112012-10-30 11:34:43 -0400180 }
181
182 @Override
183 protected void onDestroy() {
184 super.onDestroy();
185 }
186
187 @Override
188 public void onBackPressed() {
189 Bundle bundle = new Bundle();
190 bundle.putString("AccountID", mAccountID);
191 bundle.putStringArrayList(AccountDetailBasic.BUNDLE_TAG, basicDetails.getValuesOnly());
192 bundle.putStringArrayList(AccountDetailAdvanced.BUNDLE_TAG, advancedDetails.getValuesOnly());
193 bundle.putStringArrayList(AccountDetailSrtp.BUNDLE_TAG, srtpDetails.getValuesOnly());
194 bundle.putStringArrayList(AccountDetailTls.BUNDLE_TAG, tlsDetails.getValuesOnly());
195
196 Intent resultIntent = new Intent();
197 resultIntent.putExtras(bundle);
198
199 setResult(ACCOUNT_MODIFIED, resultIntent);
200 finish();
201 }
202
Alexandre Savard5195d922012-10-30 16:58:50 -0400203 @Override
204 public boolean onOptionsItemSelected(MenuItem item) {
Alexandre Savard645b29c2012-10-30 17:22:26 -0400205 AlertDialog dialog = createAlertDialog();
206 dialog.show();
207
Alexandre Savard5195d922012-10-30 16:58:50 -0400208 return true;
209 }
210
Alexandre Savard645b29c2012-10-30 17:22:26 -0400211 private AlertDialog createAlertDialog()
212 {
213 Activity ownerActivity = this;
214 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
215 builder.setMessage("Do you realy want to delete this account").setTitle("Delete Account")
216 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
217 public void onClick(DialogInterface dialog, int whichButton) {
218 Bundle bundle = new Bundle();
219 bundle.putString("AccountID", mAccountID);
220
221 Intent resultIntent = new Intent();
222 resultIntent.putExtras(bundle);
223
224 Activity activity = ((Dialog)dialog).getOwnerActivity();
225 activity.setResult(ACCOUNT_DELETED, resultIntent);
226 activity.finish();
227 }
228 })
229 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
230 public void onClick(DialogInterface dialog, int whichButton) {
231 /* Terminate with no action */
232 }
233 });
234
235 AlertDialog alertDialog = builder.create();
236 alertDialog.setOwnerActivity(ownerActivity);
237
238 return alertDialog;
239 }
240
Alexandre Savard68838112012-10-30 11:34:43 -0400241 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
242 for(AccountDetail.PreferenceEntry p : det.getDetailValues()) {
243 Preference pref = mPreferenceManager.findPreference(p.mKey);
244 if(pref != null) {
245 if(p.isTwoState) {
246 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
247 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
248 }
249 else {
250 EditTextPreference textPref = (EditTextPreference)pref;
251 accountDetails.put(p.mKey, textPref.getText());
252 }
253 }
254 }
255 }
256
257 private void updateAccountDetails() {
258 HashMap<String, String> accountDetails = new HashMap<String, String>();
259
260 updateAccountDetails(accountDetails, basicDetails);
261 updateAccountDetails(accountDetails, advancedDetails);
262 updateAccountDetails(accountDetails, srtpDetails);
263 updateAccountDetails(accountDetails, tlsDetails);
264 }
265}
266