mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-22 12:21:15 +00:00
Fix multiple chances of a usage reporting dialog NPE (fixes #1154)
This commit is contained in:
parent
bea4bc3d01
commit
1442eb9fce
2 changed files with 52 additions and 24 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue