From a9ca75acccf7e2ef41aa89cf2f5e4cbd2c53e518 Mon Sep 17 00:00:00 2001 From: Greg Alexander Date: Tue, 16 Dec 2014 15:50:30 -0500 Subject: [PATCH] communicate the preferences to the C code --- jni/interface.c | 24 +++++++++++++++++-- src/org/galexander/sshd/SimpleSSHD.java | 10 ++++---- .../galexander/sshd/SimpleSSHDService.java | 12 ++++++++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/jni/interface.c b/jni/interface.c index 099c833..96387f9 100644 --- a/jni/interface.c +++ b/jni/interface.c @@ -36,19 +36,39 @@ jni_init(JNIEnv *env_) return 1; } +static const char * +from_java_string(jobject s) +{ + const char *ret, *t; + t = (*env)->GetStringUTFChars(env, s, NULL); + if (!t) { + return NULL; + } + ret = strdup(t); + (*env)->ReleaseStringUTFChars(env, s, t); +} + JNIEXPORT void JNICALL Java_org_galexander_sshd_SimpleSSHDService_start_1sshd(JNIEnv *env_, - jobject this) + jobject this, + jint port, jobject jpath, jobject jshell, jobject jhome) { pid_t pid; + const char *path, *shell, *home; + if (!jni_init(env_)) { return; } + path = from_java_string(jpath); + shell = from_java_string(jshell); + home = from_java_string(jhome); + pid = fork(); if (pid == 0) { /* XXX - call dropbear main() */ } else { - (*env)->SetStaticIntField(env, cl_simplesshdservice, fid_sss_sshd_pid, pid); + (*env)->SetStaticIntField(env, cl_simplesshdservice, + fid_sss_sshd_pid, pid); } } diff --git a/src/org/galexander/sshd/SimpleSSHD.java b/src/org/galexander/sshd/SimpleSSHD.java index cdaded9..932ec27 100644 --- a/src/org/galexander/sshd/SimpleSSHD.java +++ b/src/org/galexander/sshd/SimpleSSHD.java @@ -83,19 +83,19 @@ public class SimpleSSHD extends Activity edit.commit(); } - private boolean get_onboot(SharedPreferences p) { + public static boolean get_onboot(SharedPreferences p) { return p.getBoolean("onboot", false); } - private int get_port(SharedPreferences p) { + public static int get_port(SharedPreferences p) { return p.getInt("port", 2222); } - private String get_path(SharedPreferences p) { + public static String get_path(SharedPreferences p) { return p.getString("path", "/sdcard/ssh"); } - private String get_shell(SharedPreferences p) { + public static String get_shell(SharedPreferences p) { return p.getString("shell", "/system/bin/sh -l"); } - private String get_home(SharedPreferences p) { + public static String get_home(SharedPreferences p) { return p.getString("home", "/sdcard/ssh"); } } diff --git a/src/org/galexander/sshd/SimpleSSHDService.java b/src/org/galexander/sshd/SimpleSSHDService.java index a17d93e..1e1c238 100644 --- a/src/org/galexander/sshd/SimpleSSHDService.java +++ b/src/org/galexander/sshd/SimpleSSHDService.java @@ -2,7 +2,10 @@ package org.galexander.sshd; import android.app.Service; import android.content.Intent; +import android.content.SharedPreferences; +import android.content.Context; import android.os.IBinder; +import android.preference.PreferenceManager; public class SimpleSSHDService extends Service { public static int sshd_pid = 0; @@ -11,10 +14,14 @@ public class SimpleSSHDService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { if ((intent == null) || (!intent.getBooleanExtra("stop", false))) { + SharedPreferences p = PreferenceManager. + getDefaultSharedPreferences(this); if (is_started()) { stop_sshd(); } - start_sshd(); + start_sshd(SimpleSSHD.get_port(p), + SimpleSSHD.get_path(p), SimpleSSHD.get_shell(p), + SimpleSSHD.get_home(p)); if (activity != null) { activity.update_startstop(); } @@ -35,7 +42,8 @@ public class SimpleSSHDService extends Service { return (sshd_pid != 0); } - private native void start_sshd(); + private native void start_sshd(int port, String path, + String shell, String home); private native void stop_sshd(); static { System.loadLibrary("simplesshd-jni");