factor the SharedPreferences usage into its own Prefs class

sigsegv_dump
Greg Alexander 10 years ago
parent 834e994042
commit e77351edd4

@ -0,0 +1,35 @@
package org.galexander.sshd;
import android.content.SharedPreferences;
import android.content.Context;
import android.preference.PreferenceManager;
public class Prefs {
private static SharedPreferences pref = null;
public static void init(Context c) {
if (pref == null) {
pref = PreferenceManager.getDefaultSharedPreferences(c);
}
}
public static boolean get_onboot() {
return pref.getBoolean("onboot", false);
}
public static int get_port() {
return pref.getInt("port", 2222);
}
public static String get_path() {
return pref.getString("path", "/sdcard/ssh");
}
public static String get_shell() {
return pref.getString("shell", "/system/bin/sh -l");
}
public static String get_home() {
return pref.getString("home", "/sdcard/ssh");
}
public static SharedPreferences.Editor edit() {
return pref.edit();
}
};

@ -19,6 +19,7 @@ public class SimpleSSHD extends Activity
public void onCreate(Bundle savedInstanceState) public void onCreate(Bundle savedInstanceState)
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Prefs.init(this);
setContentView(R.layout.main); setContentView(R.layout.main);
onboot_view = (CheckBox)findViewById(R.id.onboot); onboot_view = (CheckBox)findViewById(R.id.onboot);
port_view = (EditText)findViewById(R.id.port); port_view = (EditText)findViewById(R.id.port);
@ -61,48 +62,31 @@ public class SimpleSSHD extends Activity
} }
private void load_prefs() { private void load_prefs() {
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); onboot_view.setChecked(Prefs.get_onboot());
onboot_view.setChecked(get_onboot(prefs)); port_view.setText(Integer.toString(Prefs.get_port()));
port_view.setText(Integer.toString(get_port(prefs))); path_view.setText(Prefs.get_path());
path_view.setText(get_path(prefs)); shell_view.setText(Prefs.get_shell());
shell_view.setText(get_shell(prefs)); home_view.setText(Prefs.get_home());
home_view.setText(get_home(prefs));
} }
private void save_prefs() { private void save_prefs() {
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor edit = Prefs.edit();
SharedPreferences.Editor edit = prefs.edit();
boolean b = onboot_view.isChecked(); boolean b = onboot_view.isChecked();
if (b != get_onboot(prefs)) { edit.putBoolean("onboot", b); } if (b != Prefs.get_onboot()) { edit.putBoolean("onboot", b); }
int i; int i;
try { try {
i = Integer.valueOf(port_view.getText().toString()); i = Integer.valueOf(port_view.getText().toString());
} catch (Exception e) { } catch (Exception e) {
i = 0; i = 0;
} }
if (i != get_port(prefs)) { edit.putInt("port", i); } if (i != Prefs.get_port()) { edit.putInt("port", i); }
String s = path_view.getText().toString(); String s = path_view.getText().toString();
if (!s.equals(get_path(prefs))) { edit.putString("path", s); } if (!s.equals(Prefs.get_path())) { edit.putString("path", s); }
s = shell_view.getText().toString(); s = shell_view.getText().toString();
if (!s.equals(get_shell(prefs))) { edit.putString("shell", s); } if (!s.equals(Prefs.get_shell())) { edit.putString("shell", s); }
s = home_view.getText().toString(); s = home_view.getText().toString();
if (!s.equals(get_home(prefs))) { edit.putString("home", s); } if (!s.equals(Prefs.get_home())) { edit.putString("home", s); }
edit.commit(); edit.commit();
} }
public static boolean get_onboot(SharedPreferences p) {
return p.getBoolean("onboot", false);
}
public static int get_port(SharedPreferences p) {
return p.getInt("port", 2222);
}
public static String get_path(SharedPreferences p) {
return p.getString("path", "/sdcard/ssh");
}
public static String get_shell(SharedPreferences p) {
return p.getString("shell", "/system/bin/sh -l");
}
public static String get_home(SharedPreferences p) {
return p.getString("home", "/sdcard/ssh");
}
} }

@ -2,15 +2,18 @@ package org.galexander.sshd;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.Context; import android.content.Context;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
public class SimpleSSHDService extends Service { public class SimpleSSHDService extends Service {
public static int sshd_pid = 0; public static int sshd_pid = 0;
public static SimpleSSHD activity = null; public static SimpleSSHD activity = null;
public void onCreate() {
super.onCreate();
Prefs.init(this);
}
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if ((intent == null) || if ((intent == null) ||
(!intent.getBooleanExtra("stop", false))) { (!intent.getBooleanExtra("stop", false))) {
@ -46,14 +49,12 @@ public class SimpleSSHDService extends Service {
} }
private void do_start() { private void do_start() {
SharedPreferences p = PreferenceManager.
getDefaultSharedPreferences(this);
if (is_started()) { if (is_started()) {
stop_sshd(); stop_sshd();
} }
start_sshd(SimpleSSHD.get_port(p), start_sshd(Prefs.get_port(),
SimpleSSHD.get_path(p), SimpleSSHD.get_shell(p), Prefs.get_path(), Prefs.get_shell(),
SimpleSSHD.get_home(p)); Prefs.get_home());
if (sshd_pid != 0) { if (sshd_pid != 0) {
final int pid = sshd_pid; final int pid = sshd_pid;

Loading…
Cancel
Save