1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-25 22:01:16 +00:00

Added preference to enable/disable crash notifications

This commit is contained in:
Felix Ableitner 2017-04-11 13:10:05 +09:00
parent fddc555a31
commit 71839eb550
3 changed files with 31 additions and 18 deletions

View file

@ -55,6 +55,7 @@ public class SyncthingRunnable implements Runnable {
private String[] mCommand; private String[] mCommand;
private String mErrorLog; private String mErrorLog;
private final File mLogFile; private final File mLogFile;
private final SharedPreferences mPreferences;
public enum Command { public enum Command {
generate, // Generate keys, a config file and immediately exit. generate, // Generate keys, a config file and immediately exit.
@ -71,6 +72,7 @@ public class SyncthingRunnable implements Runnable {
mContext = context; mContext = context;
mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME; mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME;
mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log"); mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log");
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
switch (command) { switch (command) {
case generate: case generate:
mCommand = new String[]{ mSyncthingBinary, "-generate", mContext.getFilesDir().toString() }; mCommand = new String[]{ mSyncthingBinary, "-generate", mContext.getFilesDir().toString() };
@ -96,6 +98,7 @@ public class SyncthingRunnable implements Runnable {
mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME; mSyncthingBinary = mContext.getApplicationInfo().nativeLibraryDir + "/" + BINARY_NAME;
mCommand = manualCommand; mCommand = manualCommand;
mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log"); mLogFile = new File(mContext.getExternalFilesDir(null), "syncthing.log");
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
} }
@Override @Override
@ -167,20 +170,23 @@ public class SyncthingRunnable implements Runnable {
.setAction(SyncthingService.ACTION_RESTART)); .setAction(SyncthingService.ACTION_RESTART));
break; break;
default: default:
// Show notification to inform user about crash. Log.w(TAG, "Syncthing has crashed (exit code " + ret + ")");
Intent intent = new Intent(); if (mPreferences.getBoolean("notify_crashes", false)) {
intent.setAction(android.content.Intent.ACTION_VIEW); // Show notification to inform user about crash.
intent.setDataAndType(Uri.fromFile(mLogFile), "text/plain"); Intent intent = new Intent();
Notification n = new NotificationCompat.Builder(mContext) intent.setAction(android.content.Intent.ACTION_VIEW);
.setContentTitle(mContext.getString(R.string.notification_crash_title)) intent.setDataAndType(Uri.fromFile(mLogFile), "text/plain");
.setContentText(mContext.getString(R.string.notification_crash_text)) Notification n = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_stat_notify) .setContentTitle(mContext.getString(R.string.notification_crash_title))
.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0)) .setContentText(mContext.getString(R.string.notification_crash_text))
.setAutoCancel(true) .setSmallIcon(R.drawable.ic_stat_notify)
.build(); .setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0))
NotificationManager nm = (NotificationManager) .setAutoCancel(true)
mContext.getSystemService(Context.NOTIFICATION_SERVICE); .build();
nm.notify(NOTIFICATION_ID_CRASH, n); NotificationManager nm = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID_CRASH, n);
}
} }
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
Log.e(TAG, "Failed to execute syncthing binary or read output", 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. * Returns true if root is available and enabled in settings.
*/ */
private boolean useRoot() { private boolean useRoot() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); return mPreferences.getBoolean(SyncthingService.PREF_USE_ROOT, false) && Shell.SU.available();
return sp.getBoolean(SyncthingService.PREF_USE_ROOT, false) && Shell.SU.available();
} }
/** /**
* Returns true if the experimental setting for using wake locks has been enabled in settings. * Returns true if the experimental setting for using wake locks has been enabled in settings.
*/ */
private boolean useWakeLock() { private boolean useWakeLock() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); return mPreferences.getBoolean(SyncthingService.PREF_USE_WAKE_LOCK, false);
return sp.getBoolean(SyncthingService.PREF_USE_WAKE_LOCK, false);
} }
/** /**

View file

@ -357,6 +357,10 @@ Please report any problems you encounter via Github.</string>
<!-- Summary for the log activity --> <!-- Summary for the log activity -->
<string name="open_log_summary">Open the Syncthing and Android log window</string> <string name="open_log_summary">Open the Syncthing and Android log window</string>
<string name="notify_crashes_title">Notify about Syncthing crashes</string>
<string name="notify_crashes_summary">Show a notification whenever a Syncthing crash is detected</string>
<string name="syncthing_forum_title">Syncthing Forum</string> <string name="syncthing_forum_title">Syncthing Forum</string>
<string name="syncthing_forum_summary">Go to the Syncthing Forum</string> <string name="syncthing_forum_summary">Go to the Syncthing Forum</string>
<string name="syncthing_forum_url" translatable="false">https://forum.syncthing.net/</string> <string name="syncthing_forum_url" translatable="false">https://forum.syncthing.net/</string>

View file

@ -154,6 +154,11 @@
android:action=".activities.LogActivity" /> android:action=".activities.LogActivity" />
</Preference> </Preference>
<CheckBoxPreference
android:key="notify_crashes"
android:title="@string/notify_crashes_title"
android:summary="@string/notify_crashes_summary"/>
<EditTextPreference <EditTextPreference
android:key="sttrace" android:key="sttrace"
android:title="@string/sttrace_title" android:title="@string/sttrace_title"