diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java index 68bc3651..a1c7313d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java @@ -55,6 +55,7 @@ public class SyncthingRunnable implements Runnable { private String[] mCommand; private String mErrorLog; private final File mLogFile; + private final SharedPreferences mPreferences; public enum Command { generate, // Generate keys, a config file and immediately exit. @@ -71,6 +72,7 @@ public class SyncthingRunnable implements Runnable { mContext = context; mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME; mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log"); + mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext); switch (command) { case generate: mCommand = new String[]{ mSyncthingBinary, "-generate", mContext.getFilesDir().toString() }; @@ -96,6 +98,7 @@ public class SyncthingRunnable implements Runnable { mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME; mCommand = manualCommand; mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log"); + mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext); } @Override @@ -167,20 +170,23 @@ public class SyncthingRunnable implements Runnable { .setAction(SyncthingService.ACTION_RESTART)); break; default: - // Show notification to inform user about crash. - Intent intent = new Intent(); - intent.setAction(android.content.Intent.ACTION_VIEW); - intent.setDataAndType(Uri.fromFile(mLogFile), "text/plain"); - Notification n = new NotificationCompat.Builder(mContext) - .setContentTitle(mContext.getString(R.string.notification_crash_title)) - .setContentText(mContext.getString(R.string.notification_crash_text)) - .setSmallIcon(R.drawable.ic_stat_notify) - .setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0)) - .setAutoCancel(true) - .build(); - NotificationManager nm = (NotificationManager) - mContext.getSystemService(Context.NOTIFICATION_SERVICE); - nm.notify(NOTIFICATION_ID_CRASH, n); + Log.w(TAG, "Syncthing has crashed (exit code " + ret + ")"); + if (mPreferences.getBoolean("notify_crashes", false)) { + // Show notification to inform user about crash. + Intent intent = new Intent(); + intent.setAction(android.content.Intent.ACTION_VIEW); + intent.setDataAndType(Uri.fromFile(mLogFile), "text/plain"); + Notification n = new NotificationCompat.Builder(mContext) + .setContentTitle(mContext.getString(R.string.notification_crash_title)) + .setContentText(mContext.getString(R.string.notification_crash_text)) + .setSmallIcon(R.drawable.ic_stat_notify) + .setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0)) + .setAutoCancel(true) + .build(); + NotificationManager nm = (NotificationManager) + mContext.getSystemService(Context.NOTIFICATION_SERVICE); + nm.notify(NOTIFICATION_ID_CRASH, n); + } } } catch (IOException | InterruptedException e) { Log.e(TAG, "Failed to execute syncthing binary or read output", e); @@ -196,16 +202,14 @@ public class SyncthingRunnable implements Runnable { * Returns true if root is available and enabled in settings. */ private boolean useRoot() { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); - return sp.getBoolean(SyncthingService.PREF_USE_ROOT, false) && Shell.SU.available(); + return mPreferences.getBoolean(SyncthingService.PREF_USE_ROOT, false) && Shell.SU.available(); } /** * Returns true if the experimental setting for using wake locks has been enabled in settings. */ private boolean useWakeLock() { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); - return sp.getBoolean(SyncthingService.PREF_USE_WAKE_LOCK, false); + return mPreferences.getBoolean(SyncthingService.PREF_USE_WAKE_LOCK, false); } /** diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 3c6b1cf4..1d293511 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -357,6 +357,10 @@ Please report any problems you encounter via Github. Open the Syncthing and Android log window + Notify about Syncthing crashes + + Show a notification whenever a Syncthing crash is detected + Syncthing Forum Go to the Syncthing Forum https://forum.syncthing.net/ diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index ec6eb92b..6ea918a7 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -154,6 +154,11 @@ android:action=".activities.LogActivity" /> + +