1fd0d7a1170d5c995c17a56efc5be7304d551a76
[saartuer.git] / AndTuer / src / de / hacksaar / andtuer / DoorSettings.java
1 package de.hacksaar.andtuer;
2
3 import android.content.SharedPreferences;
4 import android.os.Bundle;
5 import android.os.Environment;
6 import android.preference.Preference;
7 import android.preference.PreferenceActivity;
8
9 public class DoorSettings extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
10         public static final int DEFAULT_PORT = 22;
11         public static final String DEFAULT_HOST = "192.168.178.222";
12         public static final String DEFAULT_KEYFILE = Environment.getExternalStorageDirectory().getPath() + "/.ssh/id_rsa";
13         private static final String UNSET = "<unset>";
14         public static final String PREF_SERVER_PORT = "pref_server_port";
15         public static final String PREF_USER_USERNAME = "pref_user_username";
16         public static final String PREF_USER_KEYFILE = "pref_user_keyfile";
17         public static final String PREF_SERVER_HOSTNAME = "pref_server_hostname";
18
19         @Override
20         public void onCreate(Bundle savedInstanceState) {
21                 super.onCreate(savedInstanceState);
22                 addPreferencesFromResource(R.xml.preferences);
23                 Preference hostnamePreference = findPreference(PREF_SERVER_HOSTNAME);
24                 hostnamePreference.setOnPreferenceChangeListener(this);
25                 SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
26                 hostnamePreference.setSummary(sharedPreferences.getString(PREF_SERVER_HOSTNAME, DEFAULT_HOST));
27                 Preference portPreference = findPreference(PREF_SERVER_PORT);
28                 portPreference.setOnPreferenceChangeListener(this);
29                 portPreference.setSummary(Integer.toString(sharedPreferences.getInt(PREF_SERVER_PORT, DEFAULT_PORT)));
30                 Preference usernamePreference = findPreference(PREF_USER_USERNAME);
31                 usernamePreference.setOnPreferenceChangeListener(this);
32                 usernamePreference.setSummary(sharedPreferences.getString(PREF_USER_USERNAME, UNSET));
33                 Preference keyfilePreference = findPreference(PREF_USER_KEYFILE);
34                 keyfilePreference.setOnPreferenceChangeListener(this);
35                 keyfilePreference.setSummary(sharedPreferences.getString(PREF_USER_KEYFILE, DEFAULT_KEYFILE));
36         }
37
38         @Override
39         public boolean onPreferenceChange(Preference preference, Object o) {
40                 SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
41                 if (preference.getKey().equals(PREF_SERVER_PORT)) {
42                         preference.setSummary(
43                                         Integer.toString(sharedPreferences.getInt(preference.getKey(), DEFAULT_PORT)));
44                 } else {
45                         preference.setSummary(sharedPreferences.getString(preference.getKey(), UNSET));
46                 }
47                 return true;
48         }
49 }