SettingsActivity.java

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