From 2c631eed1f75caa9f9142ecc8754988e41792fec Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 19 Sep 2014 15:19:11 +0300 Subject: [PATCH] Allow setting STTRACE parameters via app settings. --- .../fragments/SettingsFragment.java | 30 ++++++++++++++----- .../syncthingandroid/syncthing/RestApi.java | 15 +++++----- .../syncthing/SyncthingRunnable.java | 4 +++ src/main/res/values/strings.xml | 3 ++ src/main/res/xml/app_settings.xml | 5 ++++ 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index 03907fde..6531bf10 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -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; diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index db3ebbce..19d1b40e 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -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; diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java index f6ceb955..d455317e 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java @@ -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"); diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index c8b9e8c0..a83b6591 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -194,6 +194,9 @@ Please report any problems you encounter via Github. Sync only on wifi + + Debug Options + Report Issue diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index 492e4f70..06b4116e 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -90,6 +90,11 @@ + +