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";

private PreferenceKeys() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.odk.share.views.ui.settings;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -13,14 +15,17 @@
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;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.textfield.TextInputLayout;

import org.odk.share.R;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import org.odk.share.application.Share;
import org.odk.share.utilities.FileUtils;


/**
Expand All @@ -31,9 +36,11 @@ public class SettingsActivity extends PreferenceActivity {

EditTextPreference hotspotNamePreference;
Preference hotspotPasswordPreference;
Preference resetPreference;
CheckBoxPreference passwordRequirePreference;
EditTextPreference odkDestinationDirPreference;
private SharedPreferences prefs;
private ProgressDialog progressDialog;

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

private void addPreferences() {
resetPreference = findPreference(PreferenceKeys.KEY_RESET_SETTINGS);
hotspotNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
hotspotPasswordPreference = findPreference(PreferenceKeys.KEY_HOTSPOT_PASSWORD);
passwordRequirePreference = (CheckBoxPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
Expand All @@ -77,6 +85,7 @@ private void addPreferences() {
passwordRequirePreference.setOnPreferenceChangeListener(preferenceChangeListener());
odkDestinationDirPreference.setOnPreferenceChangeListener(preferenceChangeListener());

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

Expand All @@ -86,6 +95,9 @@ private Preference.OnPreferenceClickListener preferenceClickListener() {
case PreferenceKeys.KEY_HOTSPOT_PASSWORD:
showPasswordDialog();
break;
case PreferenceKeys.KEY_RESET_SETTINGS:
resetApplication();
break;
}
return false;
};
Expand Down Expand Up @@ -162,4 +174,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 @@ -131,6 +131,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>
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
<string name="reset_pref_msg"> reset internal settings and saved settings.</string>
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
<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">Reset All Settings: %s</string>
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
<string name="reset_result_data">Reset Saved Forms: %s \n</string>
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
<string name="reset_success">Success</string>
<string name="reset_failed">Failed</string>

<string name="btn_refresh">Refresh</string>
<string name="method_bluetooth">Bluetooth</string>
Expand Down
43 changes: 25 additions & 18 deletions skunkworks_crow/src/main/res/xml/preferences_menu.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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"
android:key="odk_destination_dir"
android:summary="@string/default_odk_destination_dir"
android:title="@string/title_odk_destination_dir" />

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

<PreferenceCategory android:title="Hotspot Settings">

<EditTextPreference
android:defaultValue="@string/default_hotspot_ssid"
android:summary="@string/default_hotspot_ssid"
android:title="@string/title_hotspot_ssid"
android:icon="@drawable/ic_wifi_tethering_black_24dp"
android:key="hotspot_name"
android:icon="@drawable/ic_wifi_tethering_black_24dp"/>
android:summary="@string/default_hotspot_ssid"
android:title="@string/title_hotspot_ssid" />

<Preference
android:defaultValue="@string/default_hotspot_password"
android:summary="********"
android:title="@string/title_hotspot_password"
android:key="hotspot_password"
android:enabled="false"
android:icon="@drawable/ic_vpn_key_black_24dp"/>
android:icon="@drawable/ic_vpn_key_black_24dp"
android:key="hotspot_password"
android:summary="********"
android:title="@string/title_hotspot_password" />

<CheckBoxPreference
android:defaultValue="false"
android:icon="@drawable/ic_lock_black_24dp"
android:key="hotspot_pwd_require"
android:summary="@string/password_hotspot"
android:title="@string/set_password"
android:icon="@drawable/ic_lock_black_24dp"/>

<EditTextPreference
android:defaultValue="@string/default_odk_destination_dir"
android:summary="@string/default_odk_destination_dir"
android:title="@string/title_odk_destination_dir"
android:key="odk_destination_dir"
android:icon="@drawable/ic_sd_storage_black_24dp"/>
android:title="@string/set_password" />

</PreferenceCategory>
</PreferenceScreen>