From 8a2fe3ceecb3f18529bfc433eb0360de1fb9c515 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Thu, 3 Jan 2019 18:37:01 +0100 Subject: [PATCH] Properly dismiss usage reporting dialog on orientation change (fixes #184) (#186) * Add RestApi#isUsageReportingAccepted (fixes #184) * Add verbose log when usage report question dialog triggers (fixes #184) * Remove asynchronous requests to determine if user opted-in to usage reporting (fixes #184) * Check if the config is loaded before evaluating current ur user wish * Properly dismiss mUsageReportingDialog on orientation change (fixes #184) - Consolidate createRestartDialog into showRestartDialog - Remove unused var mBatteryOptimizationsDialog --- .../activities/MainActivity.java | 17 ++++++++--------- .../activities/SettingsActivity.java | 10 +++------- .../syncthingandroid/service/RestApi.java | 9 +++++++++ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 4632df54..8b255188 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -83,8 +83,8 @@ public class MainActivity extends SyncthingActivity */ private static final long USAGE_REPORTING_DIALOG_DELAY = TimeUnit.DAYS.toMillis(3); - private AlertDialog mBatteryOptimizationsDialog; private AlertDialog mQrCodeDialog; + private AlertDialog mUsageReportingDialog; private Dialog mRestartDialog; private SyncthingService.State mSyncthingServiceState = SyncthingService.State.INIT; @@ -121,7 +121,7 @@ public class MainActivity extends SyncthingActivity // Check if the usage reporting minimum delay passed by. Boolean usageReportingDelayPassed = (new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY); RestApi restApi = getApi(); - if (usageReportingDelayPassed && restApi != null && !restApi.isUsageReportingDecided()) { + if (usageReportingDelayPassed && restApi != null && restApi.isConfigLoaded() && !restApi.isUsageReportingDecided()) { showUsageReportingDialog(restApi); } break; @@ -350,6 +350,7 @@ public class MainActivity extends SyncthingActivity outState.putString(DEVICEID_KEY, deviceID.getText().toString()); } Util.dismissDialogSafe(mRestartDialog, this); + Util.dismissDialogSafe(mUsageReportingDialog, this); } @Override @@ -371,17 +372,13 @@ public class MainActivity extends SyncthingActivity } public void showRestartDialog(){ - mRestartDialog = createRestartDialog(); - mRestartDialog.show(); - } - - private Dialog createRestartDialog(){ - return new AlertDialog.Builder(this) + mRestartDialog = new AlertDialog.Builder(this) .setMessage(R.string.dialog_confirm_restart) .setPositiveButton(android.R.string.yes, (dialogInterface, i1) -> this.startService(new Intent(this, SyncthingService.class) .setAction(SyncthingService.ACTION_RESTART))) .setNegativeButton(android.R.string.no, null) .create(); + mRestartDialog.show(); } public void showQrCodeDialog(String deviceId, Bitmap qrCode) { @@ -492,6 +489,7 @@ public class MainActivity extends SyncthingActivity * Displays dialog asking user to accept/deny usage reporting. */ private void showUsageReportingDialog(RestApi restApi) { + Log.v(TAG, "showUsageReportingDialog triggered."); final DialogInterface.OnClickListener listener = (dialog, which) -> { try { switch (which) { @@ -519,7 +517,8 @@ public class MainActivity extends SyncthingActivity .inflate(R.layout.dialog_usage_reporting, null); TextView tv = v.findViewById(R.id.example); tv.setText(report); - new AlertDialog.Builder(MainActivity.this) + Util.dismissDialogSafe(mUsageReportingDialog, MainActivity.this); + mUsageReportingDialog = new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.usage_reporting_dialog_title) .setView(v) .setPositiveButton(R.string.yes, listener) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 129f48c0..2d7a9cce 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -388,8 +388,7 @@ public class SettingsActivity extends SyncthingActivity { } mRestartOnWakeup.setChecked(mOptions.restartOnWakeup); - mRestApi.getSystemStatus(systemStatus -> - mUrAccepted.setChecked(mOptions.isUsageReportingAccepted(systemStatus.urVersionMax))); + mUrAccepted.setChecked(mRestApi.isUsageReportingAccepted()); } @Override @@ -504,11 +503,8 @@ public class SettingsActivity extends SyncthingActivity { mOptions.restartOnWakeup = (boolean) o; break; case "urAccepted": - mRestApi.getSystemStatus(systemStatus -> { - mOptions.urAccepted = ((boolean) o) - ? systemStatus.urVersionMax - : Options.USAGE_REPORTING_DENIED; - }); + mRestApi.setUsageReporting((boolean) o); + mOptions = mRestApi.getOptions(); break; default: throw new InvalidParameterException(); } diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 4c124374..3091b6f2 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -784,6 +784,15 @@ public class RestApi { return mUrl; } + public Boolean isUsageReportingAccepted() { + Options options = getOptions(); + if (options == null) { + Log.e(TAG, "isUsageReportingAccepted called while options == null"); + return false; + } + return options.isUsageReportingAccepted(mUrVersionMax); + } + public Boolean isUsageReportingDecided() { Options options = getOptions(); if (options == null) {