Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

[MRG] Add reset settings option. #300

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.odk.share.utilities;

import java.io.File;
import java.util.Locale;

public final class FileUtils {
Expand All @@ -15,4 +16,30 @@ public static String getFileExtension(String fileName) {
}
return fileName.substring(dotIndex + 1).toLowerCase(Locale.ROOT);
}

/**
* Delete all contents under the path, would not remove the folder.
*/
public static boolean deleteFolderContents(String path) {
boolean result = true;
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
result = deleteRecursive(f);
}
}
}
return result;
}

private static boolean deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
deleteRecursive(child);
}
}
return fileOrDirectory.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class PreferenceKeys {
public static final String KEY_HOTSPOT_PASSWORD = "hotspot_password";
public static final String KEY_HOTSPOT_PWD_REQUIRE = "hotspot_pwd_require";
public static final String KEY_ODK_DESTINATION_DIR = "odk_destination_dir";
public static final String KEY_RESET_SETTINGS = "reset_settings";
public static final String KEY_DEFAULT_TRANSFER_METHOD = "default_transfer_method";
public static final String KEY_BLUETOOTH_NAME = "bluetooth_name";
public static final String KEY_BLUETOOTH_SECURE_MODE = "bluetooth_secure_mode";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.odk.share.views.ui.settings;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -16,6 +18,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
Expand All @@ -24,6 +27,8 @@
import com.google.android.material.textfield.TextInputLayout;

import org.odk.share.R;
import org.odk.share.application.Share;
import org.odk.share.utilities.FileUtils;


/**
Expand All @@ -35,11 +40,13 @@ public class SettingsActivity extends PreferenceActivity {
EditTextPreference hotspotNamePreference;
EditTextPreference bluetoothNamePreference;
Preference hotspotPasswordPreference;
Preference resetPreference;
CheckBoxPreference passwordRequirePreference;
CheckBoxPreference btSecureModePreference;
EditTextPreference odkDestinationDirPreference;
ListPreference defaultMethodPreference;
private SharedPreferences prefs;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -62,6 +69,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

private void addPreferences() {
resetPreference = findPreference(PreferenceKeys.KEY_RESET_SETTINGS);
defaultMethodPreference = (ListPreference) findPreference(PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD);
hotspotNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
bluetoothNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_NAME);
Expand All @@ -78,8 +86,8 @@ private void addPreferences() {
getString(R.string.default_hotspot_ssid)));
String defaultBluetoothName = BluetoothAdapter.getDefaultAdapter().getName();
bluetoothNamePreference.setText(defaultBluetoothName);
bluetoothNamePreference.setDefaultValue(defaultBluetoothName);
bluetoothNamePreference.setSummary(prefs.getString(PreferenceKeys.KEY_BLUETOOTH_NAME, defaultBluetoothName));

boolean isPasswordSet = prefs.getBoolean(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE, false);
odkDestinationDirPreference.setSummary(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR,
getString(R.string.default_odk_destination_dir)));
Expand All @@ -96,6 +104,7 @@ private void addPreferences() {
odkDestinationDirPreference.setOnPreferenceChangeListener(preferenceChangeListener());
defaultMethodPreference.setOnPreferenceChangeListener(preferenceChangeListener());

resetPreference.setOnPreferenceClickListener(preferenceClickListener());
hotspotPasswordPreference.setOnPreferenceClickListener(preferenceClickListener());
}

Expand All @@ -105,6 +114,9 @@ private Preference.OnPreferenceClickListener preferenceClickListener() {
case PreferenceKeys.KEY_HOTSPOT_PASSWORD:
showPasswordDialog();
break;
case PreferenceKeys.KEY_RESET_SETTINGS:
resetApplication();
break;
}
return false;
};
Expand All @@ -129,8 +141,7 @@ private Preference.OnPreferenceChangeListener preferenceChangeListener() {
return false;
} else {
bluetoothNamePreference.setSummary(bluetoothName);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.setName(bluetoothName);
BluetoothAdapter.getDefaultAdapter().setName(bluetoothName);
}
break;
case PreferenceKeys.KEY_HOTSPOT_PASSWORD:
Expand Down Expand Up @@ -198,4 +209,86 @@ private void showPasswordDialog() {
alertDialog.setCancelable(true);
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

/**
* Reset the application settings and the database.
*/
private void resetApplication() {
View checkBoxView = View.inflate(this, R.layout.pref_reset_dialog, null);
CheckBox cbResetPref = checkBoxView.findViewById(R.id.cb_reset_pref);
CheckBox cbResetData = checkBoxView.findViewById(R.id.cb_clear_db);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog resetDialog = builder.setTitle(getString(R.string.title_reset_settings))
.setView(checkBoxView)
.setCancelable(false)
.setPositiveButton(getString(R.string.ok), (DialogInterface dialog, int which) -> {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle(getString(R.string.resetting));
progressDialog.setMessage(getString(R.string.resetting_msg));
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
dialog.dismiss();

if (!cbResetData.isChecked() && !cbResetPref.isChecked()) {
progressDialog.dismiss();
Toast.makeText(this, getString(R.string.reset_select_nothing), Toast.LENGTH_LONG).show();
} else {
startReset(cbResetPref, cbResetData);
}
})
.setNegativeButton(getString(R.string.cancel), (DialogInterface dialog, int which) -> {
dialog.dismiss();
})
.create();
resetDialog.show();
}

/**
* Start resetting the application and presenting the reset result in an {@link AlertDialog}.
*/
private void startReset(CheckBox cbResetPref, CheckBox cbResetData) {
StringBuilder stringBuilder = new StringBuilder();
if (cbResetData.isChecked()) {
stringBuilder.append(getString(R.string.reset_result_data,
resetData() ? getString(R.string.reset_success) : getString(R.string.reset_failed)));
}

if (cbResetPref.isChecked()) {
stringBuilder.append(getString(R.string.reset_result_pref,
resetPreference() ? getString(R.string.reset_success) : getString(R.string.reset_failed)));
}

progressDialog.dismiss();
new AlertDialog.Builder(this)
.setTitle(getString(R.string.reset_result))
.setMessage(stringBuilder.toString())
.setCancelable(false)
.setPositiveButton(getString(R.string.ok), (DialogInterface dialog, int which) -> {
dialog.dismiss();
finish();
})
.create()
.show();
}

/**
* Reset the preference by resetting the {@link SharedPreferences}.
*/
private boolean resetPreference() {
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
return editor.commit();
}

/**
* Removing the cache and database table by deleting the share
* folder and create a new one.
*/
private boolean resetData() {
boolean result = FileUtils.deleteFolderContents(Share.ODK_ROOT);
Share.createODKDirs(this);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>
66 changes: 66 additions & 0 deletions skunkworks_crow/src/main/res/layout/pref_reset_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/MatchParentWidth">

<LinearLayout
style="@style/MatchParent"
android:orientation="vertical"
android:padding="4dp">

<LinearLayout
style="@style/MatchParentWidth"
android:orientation="horizontal">

<CheckBox
android:id="@+id/cb_reset_pref"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp" />

<LinearLayout
style="@style/MatchParentWidth"
android:orientation="vertical"
android:padding="8dp">

<TextView
style="@style/MatchParent"
android:text="@string/reset_pref"
android:textStyle="bold" />

<TextView
style="@style/MatchParent"
android:text="@string/reset_pref_msg" />
</LinearLayout>
</LinearLayout>

<LinearLayout
style="@style/MatchParentWidth"
android:orientation="horizontal">

<CheckBox
android:id="@+id/cb_clear_db"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp" />

<LinearLayout
style="@style/MatchParentWidth"
android:orientation="vertical"
android:padding="12dp">

<TextView
style="@style/MatchParent"
android:text="@string/reset_database"
android:textStyle="bold" />

<TextView
style="@style/MatchParent"
android:text="@string/reset_database_msg" />
</LinearLayout>

</LinearLayout>
</LinearLayout>
</FrameLayout>
13 changes: 13 additions & 0 deletions skunkworks_crow/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@
<string name="odk_destination_dir_error">The destination should not be empty</string>
<string name="location_permission_needed">Location Permission Needed</string>
<string name="location_settings_dialog">Enable location from the settings.</string>
<string name="title_reset_settings">Reset Application</string>
<string name="reset_database">Reset Saved Forms</string>
<string name="reset_database_msg">reset forms sent for review and forms received for review.</string>
<string name="reset_pref">Reset All Settings</string>
<string name="reset_pref_msg">reset internal settings and saved settings.</string>
<string name="reset_select_nothing">Please select items to reset</string>
<string name="resetting">Resetting</string>
<string name="resetting_msg">Resetting application, please wait&#8230;</string>
<string name="reset_result">Reset Results</string>
<string name="reset_result_pref">All Settings: %s</string>
<string name="reset_result_data">Saved Forms: %s \n</string>
<string name="reset_success">Success</string>
<string name="reset_failed">Failed</string>

<string name="switch_method">Switch</string>
<string name="btn_refresh">Refresh</string>
Expand Down
6 changes: 5 additions & 1 deletion skunkworks_crow/src/main/res/xml/preferences_menu.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">

<EditTextPreference
android:defaultValue="@string/default_odk_destination_dir"
android:icon="@drawable/ic_sd_storage_black_24dp"
Expand All @@ -18,6 +17,10 @@
android:summary="@string/preference_default_method_summary"
android:title="@string/preference_default_method" />

<Preference
android:icon="@drawable/ic_refresh_black_24dp"
android:key="reset_settings"
android:title="@string/title_reset_settings" />
</PreferenceCategory>

<PreferenceCategory android:title="Hotspot Settings">
Expand Down Expand Up @@ -59,6 +62,7 @@
android:key="bluetooth_secure_mode"
android:summary="@string/summary_security_bluetooth"
android:title="@string/secure_mode" />
>>>>>>> upstream/master
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved

</PreferenceCategory>
</PreferenceScreen>