Skip to content
Permalink
Browse files

Better auth toggle error handling (#42)

* Better auth toggle error handling

* Bump recyclerview version
  • Loading branch information
mathiascode authored and Cl1608Ho committed Jan 3, 2020
1 parent c270c45 commit c2e622d38ea8b63a2f846055d48976da87d48ee1
Showing with 49 additions and 25 deletions.
  1. +2 −1 app/build.gradle
  2. +47 −24 app/src/main/java/org/cuberite/android/fragments/SettingsFragment.java
@@ -30,8 +30,9 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha01'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation 'org.ini4j:ini4j:0.5.4'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}
@@ -116,7 +116,14 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
try {
final Ini ini = createWebadminIni(webadminFile);
final String ip = CuberiteService.getIpAddress(getContext());
final String port = ini.get("WebAdmin", "Ports");
int port;
try {
port = Integer.parseInt(ini.get("WebAdmin", "Ports"));
} catch (NumberFormatException e) {
ini.put("WebAdmin", "Ports", 8080);
ini.store(webadminFile);
port = Integer.parseInt(ini.get("WebAdmin", "Ports"));
}
url = "http://" + ip + ":" + port;

Preference webadminDescription = findPreference("webadminDescription");
@@ -221,37 +228,30 @@ public boolean onPreferenceClick(Preference preference) {

// Authentication
final SwitchPreference toggleAuthentication = findPreference("troubleshootingAuthenticationToggle");
toggleAuthentication.setShouldDisableView(false);
toggleAuthentication.setEnabled(true);

final File settingsFile = new File(cuberiteDir.getAbsolutePath() + "/settings.ini");
int enabled = 0;

try {
Ini ini = new Ini(settingsFile);
enabled = Integer.parseInt(ini.get("Authentication", "Authenticate"));

if (enabled == 1) {
toggleAuthentication.setChecked(true);
} else {
toggleAuthentication.setChecked(false);
}
} catch(IOException e) {
Log.e(LOG, "Settings.ini doesn't exist, disabling authentication toggle");
toggleAuthentication.setShouldDisableView(true);
toggleAuthentication.setEnabled(false);
}
updateAuthenticationToggle(settingsFile, toggleAuthentication);

toggleAuthentication.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
Ini ini = new Ini(settingsFile);
int enabled = Integer.parseInt(ini.get("Authentication", "Authenticate"));
int newenabled = enabled ^ 1; // XOR: 0 ^ 1 => 1, 1 ^ 1 => 0
ini.put("Authentication", "Authenticate", newenabled);
final Ini ini = new Ini(settingsFile);
int newEnabled;

try {
final int enabled = Integer.parseInt(ini.get("Authentication", "Authenticate"));
newEnabled = enabled ^ 1; // XOR: 0 ^ 1 => 1, 1 ^ 1 => 0
} catch (NumberFormatException e) {
newEnabled = 1;
}

boolean newEnabledBool = newEnabled != 0;
toggleAuthentication.setChecked(newEnabledBool);

ini.put("Authentication", "Authenticate", newEnabled);
ini.store(settingsFile);
MainActivity.showSnackBar(getActivity(), String.format(getString(R.string.settings_authentication_toggle_success), getString((newenabled == 1 ? R.string.enabled : R.string.disabled))));
MainActivity.showSnackBar(getActivity(), String.format(getString(R.string.settings_authentication_toggle_success), getString(newEnabled == 1 ? R.string.enabled : R.string.disabled)));
} catch(IOException e) {
Log.e(LOG, "Something went wrong while opening the ini file", e);
MainActivity.showSnackBar(getActivity(), getString(R.string.settings_authentication_toggle_error));
@@ -434,6 +434,29 @@ public void onClick(DialogInterface dialog, int id) {
dialog.show();
}

private void updateAuthenticationToggle(File settingsFile, SwitchPreference toggle) {
try {
final Ini ini = new Ini(settingsFile);

try {
final int enabled = Integer.parseInt(ini.get("Authentication", "Authenticate"));

if (enabled == 0) {
toggle.setChecked(false);
}
} catch (NumberFormatException e) {
ini.put("Authentication", "Authenticate", 1);
ini.store(settingsFile);
toggle.setChecked(true);
}
} catch(IOException e) {
Log.e(LOG, "Settings.ini doesn't exist, disabling authentication toggle");
toggle.setShouldDisableView(true);
toggle.setEnabled(false);
toggle.setChecked(true);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

0 comments on commit c2e622d

Please sign in to comment.
You can’t perform that action at this time.