From 1442eb9fce38b51da94a82702351e74cf0af9035 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Wed, 20 Jun 2018 23:47:13 +0200 Subject: [PATCH] Fix multiple chances of a usage reporting dialog NPE (fixes #1154) --- .../activities/MainActivity.java | 51 ++++++++++--------- .../syncthingandroid/service/RestApi.java | 25 +++++++++ 2 files changed, 52 insertions(+), 24 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 0b2b4dce..49d3fab5 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -50,6 +50,7 @@ import com.nutomic.syncthingandroid.fragments.DeviceListFragment; import com.nutomic.syncthingandroid.fragments.DrawerFragment; import com.nutomic.syncthingandroid.fragments.FolderListFragment; import com.nutomic.syncthingandroid.model.Options; +import com.nutomic.syncthingandroid.service.RestApi; import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingServiceBinder; import com.nutomic.syncthingandroid.util.Util; @@ -112,12 +113,13 @@ public class MainActivity extends StateDialogActivity showBatteryOptimizationDialogIfNecessary(); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerFragment.requestGuiUpdate(); - getApi().getSystemInfo(systemInfo -> { - if (new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY && - !getApi().getOptions().isUsageReportingDecided(systemInfo.urVersionMax)) { - showUsageReportingDialog(); - } - }); + + // 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()) { + showUsageReportingDialog(restApi); + } break; case ERROR: finish(); @@ -473,28 +475,29 @@ public class MainActivity extends StateDialogActivity /** * Displays dialog asking user to accept/deny usage reporting. */ - private void showUsageReportingDialog() { + private void showUsageReportingDialog(RestApi restApi) { final DialogInterface.OnClickListener listener = (dialog, which) -> { - Options options = getApi().getOptions(); - switch (which) { - case DialogInterface.BUTTON_POSITIVE: - getApi().getSystemInfo(systemInfo -> { - options.urAccepted = systemInfo.urVersionMax; - }); - break; - case DialogInterface.BUTTON_NEGATIVE: - options.urAccepted = Options.USAGE_REPORTING_DENIED; - break; - case DialogInterface.BUTTON_NEUTRAL: - Uri uri = Uri.parse("https://data.syncthing.net"); - startActivity(new Intent(Intent.ACTION_VIEW, uri)); - break; + try { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: + restApi.setUsageReporting(true); + restApi.saveConfigAndRestart(); + break; + case DialogInterface.BUTTON_NEGATIVE: + restApi.setUsageReporting(false); + restApi.saveConfigAndRestart(); + break; + case DialogInterface.BUTTON_NEUTRAL: + Uri uri = Uri.parse("https://data.syncthing.net"); + startActivity(new Intent(Intent.ACTION_VIEW, uri)); + break; + } + } catch (Exception e) { + Log.e(TAG, "showUsageReportingDialog:OnClickListener", e); } - getApi().editSettings(getApi().getGui(), options); - getApi().saveConfigAndRestart(); }; - getApi().getUsageReport(report -> { + restApi.getUsageReport(report -> { @SuppressLint("InflateParams") View v = LayoutInflater.from(MainActivity.this) .inflate(R.layout.dialog_usage_reporting, null); 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 856d1524..384ae9d4 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -83,7 +83,12 @@ public class RestApi { private String mVersion; private Config mConfig; + + /** + * Results cached from systemInfo + */ private String mLocalDeviceId; + private Integer mUrVersionMax; /** * Stores the result of the last successful request to {@link GetRequest#URI_CONNECTIONS}, @@ -173,6 +178,7 @@ public class RestApi { }); getSystemInfo(info -> { mLocalDeviceId = info.myID; + mUrVersionMax = info.urVersionMax; synchronized (mAsyncQueryCompleteLock) { asyncQuerySystemInfoComplete = true; checkReadConfigFromRestApiCompleted(); @@ -536,4 +542,23 @@ public class RestApi { public URL getUrl() { return mUrl; } + + public Boolean isUsageReportingDecided() { + Options options = getOptions(); + if (options == null) { + Log.e(TAG, "isUsageReportingDecided called while options == null"); + return true; + } + return options.isUsageReportingDecided(mUrVersionMax); + } + + public void setUsageReporting(Boolean acceptUsageReporting) { + Options options = getOptions(); + if (options == null) { + Log.e(TAG, "setUsageReporting called while options == null"); + return; + } + options.urAccepted = acceptUsageReporting ? mUrVersionMax : Options.USAGE_REPORTING_DENIED; + mConfig.options = options; + } }