1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 11:21:29 +00:00

Allow setting STTRACE parameters via app settings.

This commit is contained in:
Felix Ableitner 2014-09-19 15:19:11 +03:00
parent a8b9950101
commit 2c631eed1f
5 changed files with 43 additions and 14 deletions

View file

@ -4,10 +4,12 @@ import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.v4.app.NavUtils;
import android.support.v4.preference.PreferenceFragment;
import android.text.InputType;
import android.util.Log;
import android.view.MenuItem;
import com.nutomic.syncthingandroid.R;
@ -19,6 +21,8 @@ public class SettingsFragment extends PreferenceFragment
implements SyncthingActivity.OnServiceConnectedListener,
SyncthingService.OnApiChangeListener, Preference.OnPreferenceChangeListener {
private static final String TAG = "SettingsFragment";
private static final String SYNCTHING_OPTIONS_KEY = "syncthing_options";
private static final String SYNCTHING_GUI_KEY = "syncthing_gui";
@ -97,6 +101,10 @@ public class SettingsFragment extends PreferenceFragment
mSyncOnlyCharging.setOnPreferenceChangeListener(this);
mSyncOnlyWifi = (CheckBoxPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI);
mSyncOnlyWifi.setOnPreferenceChangeListener(this);
Preference sttrace = findPreference("sttrace");
sttrace.setOnPreferenceChangeListener(this);
sttrace.setSummary(PreferenceManager
.getDefaultSharedPreferences(getActivity()).getString("sttrace", ""));
mVersion = screen.findPreference(SYNCTHING_VERSION_KEY);
mOptionsScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_OPTIONS_KEY);
mGuiScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_GUI_KEY);
@ -128,13 +136,9 @@ public class SettingsFragment extends PreferenceFragment
*/
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if (preference instanceof EditTextPreference) {
String value = (String) o;
preference.setSummary(value);
EditTextPreference etp = (EditTextPreference) preference;
if (etp.getEditText().getInputType() == InputType.TYPE_CLASS_NUMBER) {
o = Integer.parseInt((String) o);
}
EditTextPreference etp = (EditTextPreference) preference;
if (etp.getEditText().getInputType() == InputType.TYPE_CLASS_NUMBER) {
o = Integer.parseInt((String) o);
}
if (preference.equals(mSyncOnlyCharging) || preference.equals(mSyncOnlyWifi)) {
@ -145,6 +149,18 @@ public class SettingsFragment extends PreferenceFragment
} else if (mGuiScreen.findPreference(preference.getKey()) != null) {
mSyncthingService.getApi().setValue(
RestApi.TYPE_GUI, preference.getKey(), o, false, getActivity());
} else if (preference.getKey().equals("sttrace")) {
// Avoid any code injection.
if (!((String) o).matches("[a-z,]*")) {
Log.w(TAG, "Only a-z and ',' are allowed in STTRACE options");
return false;
}
((SyncthingActivity) getActivity()).getApi().requireRestart(getActivity());
}
if (preference instanceof EditTextPreference) {
String value = (String) o;
preference.setSummary(value);
}
return true;

View file

@ -299,7 +299,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
mConfig.getJSONObject(name).put(key, (isArray)
? listToJson(((String) value).split(" "))
: value);
configUpdated(activity);
requireRestart(activity);
} catch (JSONException e) {
Log.w(TAG, "Failed to set value for " + key, e);
}
@ -318,10 +318,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
}
/**
* Sends the updated mConfig via Rest API to syncthing and displays a "restart" notification.
* Sends the updated mConfig via Rest API to syncthing and displays a "restart"
* dialog or notification.
*/
@TargetApi(11)
private void configUpdated(Activity activity) {
public void requireRestart(Activity activity) {
new PostTask().execute(mUrl, PostTask.URI_CONFIG, mApiKey, mConfig.toString());
if (mRestartPostponed)
@ -708,7 +709,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
n.put("NodeID", node.NodeID);
n.put("Name", node.Name);
n.put("Addresses", listToJson(node.Addresses.split(" ")));
configUpdated(activity);
requireRestart(activity);
} catch (JSONException e) {
Log.w(TAG, "Failed to read nodes", e);
}
@ -732,7 +733,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
break;
}
}
configUpdated(activity);
requireRestart(activity);
} catch (JSONException e) {
Log.w(TAG, "Failed to edit repo", e);
return false;
@ -780,7 +781,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
params.put(key, repo.Versioning.getParams().get(key));
}
r.put("Versioning", versioning);
configUpdated(activity);
requireRestart(activity);
} catch (JSONException e) {
Log.w(TAG, "Failed to edit repo " + repo.ID + " at " + repo.Directory, e);
return false;
@ -803,7 +804,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
break;
}
}
configUpdated(activity);
requireRestart(activity);
} catch (JSONException e) {
Log.w(TAG, "Failed to edit repo", e);
return false;

View file

@ -3,6 +3,8 @@ package com.nutomic.syncthingandroid.syncthing;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
@ -56,6 +58,7 @@ public class SyncthingRunnable implements Runnable {
@Override
public void run() {
SharedPreferences pm = PreferenceManager.getDefaultSharedPreferences(mContext);
DataOutputStream dos = null;
int ret = 1;
Process process = null;
@ -65,6 +68,7 @@ public class SyncthingRunnable implements Runnable {
// Set home directory to data folder for syncthing to use.
dos.writeBytes("HOME=" + mContext.getFilesDir() + " ");
dos.writeBytes("STGUIAPIKEY=" + mApiKey + " ");
dos.writeBytes("STTRACE=" + pm.getString("sttrace", "") + " ");
// Call syncthing with -home (as it would otherwise use "~/.config/syncthing/".
dos.writeBytes(mCommand + " -home " + mContext.getFilesDir() + "\n");
dos.writeBytes("exit\n");

View file

@ -194,6 +194,9 @@ Please report any problems you encounter via Github.</string>
<string name="sync_only_wifi">Sync only on wifi</string>
<!-- Title for the preference to set STTRACE parameters -->
<string name="sttrace_title">Debug Options</string>
<!-- Settings item that opens issue tracker -->
<string name="report_issue_title">Report Issue</string>

View file

@ -90,6 +90,11 @@
</PreferenceScreen>
<EditTextPreference
android:key="sttrace"
android:title="@string/sttrace_title"
android:singleLine="true" />
<Preference
android:title="@string/report_issue_title"
android:summary="@string/report_issue_summary">