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

Use integer constants for usage reporting.

This commit is contained in:
Felix Ableitner 2016-04-01 22:53:03 +02:00
parent b75f025238
commit 09bba7140a
3 changed files with 28 additions and 30 deletions

View file

@ -114,7 +114,7 @@ public class MainActivity extends SyncthingActivity
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerFragment.requestGuiUpdate();
if (new Date().getTime() > getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY &&
getApi().getUsageReportAccepted() == RestApi.UsageReportSetting.UNDECIDED) {
getApi().getUsageReportAccepted() == RestApi.USAGE_REPORTING_UNDECIDED) {
showUsageReportingDialog();
}
break;
@ -422,11 +422,11 @@ public class MainActivity extends SyncthingActivity
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
getApi().setUsageReportAccepted(RestApi.UsageReportSetting.ACCEPTED,
getApi().setUsageReportAccepted(RestApi.USAGE_REPORTING_ACCEPTED,
MainActivity.this);
break;
case DialogInterface.BUTTON_NEGATIVE:
getApi().setUsageReportAccepted(RestApi.UsageReportSetting.DENIED,
getApi().setUsageReportAccepted(RestApi.USAGE_REPORTING_DENIED,
MainActivity.this);
break;
case DialogInterface.BUTTON_NEUTRAL:

View file

@ -84,8 +84,8 @@ public class SettingsFragment extends PreferenceFragment
value = api.getLocalDevice().name;
break;
case USAGE_REPORT_ACCEPTED:
RestApi.UsageReportSetting setting = api.getUsageReportAccepted();
value = Boolean.toString(setting == RestApi.UsageReportSetting.ACCEPTED);
int setting = api.getUsageReportAccepted();
value = Boolean.toString(setting == RestApi.USAGE_REPORTING_ACCEPTED);
break;
default:
value = api.getValue(RestApi.TYPE_OPTIONS, pref.getKey());
@ -272,9 +272,9 @@ public class SettingsFragment extends PreferenceFragment
updated.name = (String) o;
mSyncthingService.getApi().editDevice(updated, getActivity(), null);
} else if (preference.getKey().equals(USAGE_REPORT_ACCEPTED)) {
RestApi.UsageReportSetting setting = ((Boolean) o)
? RestApi.UsageReportSetting.ACCEPTED
: RestApi.UsageReportSetting.DENIED;
int setting = ((Boolean) o)
? RestApi.USAGE_REPORTING_ACCEPTED
: RestApi.USAGE_REPORTING_DENIED;
mSyncthingService.getApi().setUsageReportAccepted(setting, getActivity());
} else if (mOptionsScreen.findPreference(preference.getKey()) != null) {
boolean isArray = preference.getKey().equals("listenAddress") ||

View file

@ -58,6 +58,12 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
*/
public static final String TOTAL_STATS = "total";
public static final int USAGE_REPORTING_UNDECIDED = 0;
public static final int USAGE_REPORTING_ACCEPTED = 2;
public static final int USAGE_REPORTING_DENIED = -1;
private static final List<Integer> USAGE_REPORTING_DECIDED =
Arrays.asList(USAGE_REPORTING_ACCEPTED, USAGE_REPORTING_DENIED);
public static class Device implements Serializable {
public List<String> addresses;
public String name;
@ -1025,41 +1031,33 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
return mGuiPassword;
}
public enum UsageReportSetting {
UNDECIDED,
ACCEPTED,
DENIED,
}
/**
* Returns value of usage reporting preference.
*/
public UsageReportSetting getUsageReportAccepted() {
public int getUsageReportAccepted() {
try {
switch (mConfig.getJSONObject(TYPE_OPTIONS).getInt("urAccepted")) {
case 0: return UsageReportSetting.UNDECIDED;
case 1: return UsageReportSetting.UNDECIDED;
case 2: return UsageReportSetting.ACCEPTED;
case -1: return UsageReportSetting.DENIED;
default: throw new RuntimeException("Invalid usage report value");
}
int value = mConfig.getJSONObject(TYPE_OPTIONS).getInt("urAccepted");
if (value > USAGE_REPORTING_ACCEPTED)
throw new RuntimeException("Inalid usage reporting value");
if (!USAGE_REPORTING_DECIDED.contains(value))
value = USAGE_REPORTING_UNDECIDED;
return value;
} catch (JSONException e) {
Log.w(TAG, "Failed to read usage report value", e);
return UsageReportSetting.DENIED;
return USAGE_REPORTING_DENIED;
}
}
/**
* Sets new value for usage reporting preference.
*/
public void setUsageReportAccepted(UsageReportSetting value, Activity activity) {
int v = 0;
switch (value) {
case ACCEPTED: v = 2; break;
case DENIED: v = -1; break;
}
public void setUsageReportAccepted(int value, Activity activity) {
if (BuildConfig.DEBUG && !USAGE_REPORTING_DECIDED.contains(value))
throw new IllegalArgumentException("Invalid value for usage report");
try {
mConfig.getJSONObject(TYPE_OPTIONS).put("urAccepted", v);
mConfig.getJSONObject(TYPE_OPTIONS).put("urAccepted", value);
} catch (JSONException e) {
Log.w(TAG, "Failed to set usage report value", e);
}