1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Locale;
6
7import eu.siacs.conversations.entities.Account;
8import eu.siacs.conversations.xmpp.XmppConnection;
9
10import android.content.SharedPreferences;
11import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
12import android.os.Build;
13import android.os.Bundle;
14import android.preference.ListPreference;
15import android.preference.PreferenceManager;
16
17public class SettingsActivity extends XmppActivity implements
18 OnSharedPreferenceChangeListener {
19 private SettingsFragment mSettingsFragment;
20
21 @Override
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 mSettingsFragment = new SettingsFragment();
25 getFragmentManager().beginTransaction()
26 .replace(android.R.id.content, mSettingsFragment).commit();
27 }
28
29 @Override
30 void onBackendConnected() {
31
32 }
33
34 @Override
35 public void onStart() {
36 super.onStart();
37 PreferenceManager.getDefaultSharedPreferences(this)
38 .registerOnSharedPreferenceChangeListener(this);
39 ListPreference resources = (ListPreference) mSettingsFragment
40 .findPreference("resource");
41 if (resources != null) {
42 ArrayList<CharSequence> entries = new ArrayList<CharSequence>(
43 Arrays.asList(resources.getEntries()));
44 entries.add(0, Build.MODEL);
45 resources.setEntries(entries.toArray(new CharSequence[entries
46 .size()]));
47 resources.setEntryValues(entries.toArray(new CharSequence[entries
48 .size()]));
49 }
50 }
51
52 @Override
53 public void onStop() {
54 super.onStop();
55 PreferenceManager.getDefaultSharedPreferences(this)
56 .unregisterOnSharedPreferenceChangeListener(this);
57 }
58
59 @Override
60 public void onSharedPreferenceChanged(SharedPreferences preferences,
61 String name) {
62 if (name.equals("resource")) {
63 String resource = preferences.getString("resource", "mobile")
64 .toLowerCase(Locale.US);
65 if (xmppConnectionServiceBound) {
66 for (Account account : xmppConnectionService.getAccounts()) {
67 if (account.setResource(resource)) {
68 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
69 XmppConnection connection = account.getXmppConnection();
70 if (connection != null) {
71 connection.resetStreamId();
72 }
73 xmppConnectionService.reconnectAccountInBackground(account);
74 }
75 }
76 }
77 }
78 } else if (name.equals("keep_foreground_service")) {
79 xmppConnectionService.toggleForegroundService();
80 } else if (name.equals("confirm_messages")) {
81 if (xmppConnectionServiceBound) {
82 for (Account account : xmppConnectionService.getAccounts()) {
83 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
84 xmppConnectionService.sendPresence(account);
85 }
86 }
87 }
88 }
89 }
90
91}