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

UI: disabled the positive button for null validation and made default hotspot name as device name #348

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.odk.share.views.ui.settings;

import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
Expand All @@ -19,6 +20,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
Expand All @@ -39,8 +41,8 @@ public class SettingsActivity extends PreferenceActivity {
EditTextPreference hotspotNamePreference;
EditTextPreference bluetoothNamePreference;
Preference hotspotPasswordPreference;
CheckBoxPreference passwordRequirePreference;
CheckBoxPreference btSecureModePreference;
SwitchPreference passwordRequirePreference;
SwitchPreference btSecureModePreference;
EditTextPreference odkDestinationDirPreference;
ListPreference defaultMethodPreference;
private SharedPreferences prefs;
Expand Down Expand Up @@ -72,16 +74,20 @@ private void addPreferences() {
hotspotNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
bluetoothNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_NAME);
hotspotPasswordPreference = findPreference(PreferenceKeys.KEY_HOTSPOT_PASSWORD);
passwordRequirePreference = (CheckBoxPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
btSecureModePreference = (CheckBoxPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE);
passwordRequirePreference = (SwitchPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
btSecureModePreference = (SwitchPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE);
odkDestinationDirPreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);

prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

defaultMethodPreference.setSummary(prefs.getString(PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD,
getString(R.string.default_hotspot_ssid)));
hotspotNamePreference.setSummary(prefs.getString(PreferenceKeys.KEY_HOTSPOT_NAME,
getString(R.string.default_hotspot_ssid)));

String defaultHotspotName = getString(R.string.app_name) + getString(R.string.one_space) + Build.MODEL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this should just be a device odk-skunkworks-crow or the device name. Not a combination of both because if try to keep both the combinations, then it may get bigger for few devices then it may end up truncating or creating a double line for displaying just the name of the bluetooth/hotspot device.

Copy link
Contributor Author

@ajay-prabhakar ajay-prabhakar Feb 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we go with device name

hotspotNamePreference.setSummary(prefs.getString(PreferenceKeys.KEY_HOTSPOT_NAME, defaultHotspotName));
hotspotNamePreference.setDefaultValue(defaultHotspotName);
hotspotNamePreference.setText(prefs.getString(PreferenceKeys.KEY_HOTSPOT_NAME, defaultHotspotName));

String defaultBluetoothName = BluetoothAdapter.getDefaultAdapter().getName();
bluetoothNamePreference.setText(defaultBluetoothName);
bluetoothNamePreference.setDefaultValue(defaultBluetoothName);
Expand All @@ -103,6 +109,10 @@ private void addPreferences() {
defaultMethodPreference.setOnPreferenceChangeListener(preferenceChangeListener());

hotspotPasswordPreference.setOnPreferenceClickListener(preferenceClickListener());

checkNullEditTextPreference(odkDestinationDirPreference);
checkNullEditTextPreference(hotspotNamePreference);
checkNullEditTextPreference(bluetoothNamePreference);
}

private Preference.OnPreferenceClickListener preferenceClickListener() {
Expand All @@ -116,28 +126,49 @@ private Preference.OnPreferenceClickListener preferenceClickListener() {
};
}

private void checkNullEditTextPreference(EditTextPreference editTextPreference) {
editTextPreference.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Dialog dlg = editTextPreference.getDialog();
if (dlg instanceof android.app.AlertDialog) {
android.app.AlertDialog alertDlg = (android.app.AlertDialog) dlg;
Button positiveButton = alertDlg.getButton(AlertDialog.BUTTON_POSITIVE);

if (editTextPreference.getEditText().getText().toString().trim().equals("")) {
positiveButton.setEnabled(false);
} else {
positiveButton.setEnabled(true);
}
}

}

@Override
public void afterTextChanged(Editable s) {

}
});

}

private Preference.OnPreferenceChangeListener preferenceChangeListener() {
return (preference, newValue) -> {
switch (preference.getKey()) {
case PreferenceKeys.KEY_HOTSPOT_NAME:
String name = newValue.toString();
if (name.length() == 0) {
Toast.makeText(getBaseContext(), getString(R.string.hotspot_name_error), Toast.LENGTH_LONG).show();
return false;
} else {
hotspotNamePreference.setSummary(name);
}
hotspotNamePreference.setSummary(name);
break;
case PreferenceKeys.KEY_BLUETOOTH_NAME:
String bluetoothName = newValue.toString();
if (bluetoothName.length() == 0) {
Toast.makeText(getBaseContext(), getString(R.string.bluetooth_name_error), Toast.LENGTH_LONG).show();
return false;
} else {
bluetoothNamePreference.setSummary(bluetoothName);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.setName(bluetoothName);
}
bluetoothNamePreference.setSummary(bluetoothName);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.setName(bluetoothName);
break;
case PreferenceKeys.KEY_HOTSPOT_PASSWORD:
String password = newValue.toString();
Expand All @@ -156,12 +187,7 @@ private Preference.OnPreferenceChangeListener preferenceChangeListener() {
break;
case PreferenceKeys.KEY_ODK_DESTINATION_DIR:
String dir = newValue.toString();
if (dir.length() == 0) {
Toast.makeText(getApplicationContext(), getString(R.string.odk_destination_dir_error), Toast.LENGTH_LONG).show();
return false;
} else {
odkDestinationDirPreference.setSummary(dir);
}
odkDestinationDirPreference.setSummary(dir);
break;
case PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD:
String method = newValue.toString();
Expand Down Expand Up @@ -218,16 +244,16 @@ public void onShow(DialogInterface dialog) {
edtpass.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

if (edtpass.getText().toString().length() >= 8) {
((AlertDialog) dialog) .getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
} else {
((AlertDialog) dialog) .getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}

Expand Down
5 changes: 2 additions & 3 deletions skunkworks_crow/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
<string name="set_password">Set Password</string>
<string name="secure_mode">Enable Secure Mode</string>
<string name="password_hotspot">Password required to connect with hotspot</string>
<string name="hotspot_name_error">Hotspot name should not be empty</string>
<string name="bluetooth_name_error">Bluetooth name should not be empty</string>
<string name="hotspot_password_error">Password length should be atleast 8 characters long</string>
<string name="waiting_connection">Waiting for connection to accept</string>
<string name="waiting_hotspot">Waiting for hotspot to connect</string>
Expand Down Expand Up @@ -141,7 +139,6 @@

<string name="default_odk_destination_dir">\/sdcard\/odk</string>
<string name="title_odk_destination_dir">ODK Destination Directory</string>
<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>

Expand Down Expand Up @@ -184,4 +181,6 @@
<string name="finalized_on_date_at_time">\'Finalized on\' EEE, MMM dd, yyyy \'at\' HH:mm</string> <!-- http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html -->
<string name="sent_on_date_at_time">\'Sent on\' EEE, MMM dd, yyyy \'at\' HH:mm</string> <!-- http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html -->
<string name="sending_failed_on_date_at_time">\'Sending failed on\' EEE, MMM dd, yyyy \'at\' HH:mm</string> <!-- http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html -->

<string name="one_space">\u0020</string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to create a string resources for space.

</resources>
6 changes: 2 additions & 4 deletions skunkworks_crow/src/main/res/xml/preferences_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
<PreferenceCategory android:title="Hotspot Settings">

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

<Preference
Expand All @@ -37,7 +35,7 @@
android:summary="********"
android:title="@string/title_hotspot_password" />

<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:icon="@drawable/ic_lock_black_24dp"
android:key="hotspot_pwd_require"
Expand All @@ -53,7 +51,7 @@
android:key="bluetooth_name"
android:title="@string/title_bluetooth_name" />

<CheckBoxPreference
<SwitchPreference
android:defaultValue="true"
android:icon="@drawable/ic_security_black_24dp"
android:key="bluetooth_secure_mode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import android.bluetooth.BluetoothAdapter;
import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.os.Build;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;

import androidx.appcompat.widget.Toolbar;

Expand All @@ -31,10 +32,10 @@ public class SettingsActivityTest {
private SettingsActivity settingsActivity;
private SharedPreferences prefs;
private Preference hotspotPasswordPreference;
private CheckBoxPreference passwordRequirePreference;
private SwitchPreference passwordRequirePreference;
private EditTextPreference hotspotNamePreference;
private EditTextPreference odkDestinationDirPreference;
private CheckBoxPreference btSecureModePreference;
private SwitchPreference btSecureModePreference;
private ListPreference defaultMethodPreference;
private EditTextPreference bluetoothNamePreference;

Expand Down Expand Up @@ -96,7 +97,7 @@ public void preferenceSummaryTest() {

//test the summary
assertEquals(prefs.getString(PreferenceKeys.KEY_HOTSPOT_NAME,
settingsActivity.getString(R.string.default_hotspot_ssid)), hotspotNamePreference.getSummary());
settingsActivity.getString(R.string.app_name) + settingsActivity.getString(R.string.one_space) + Build.MODEL), hotspotNamePreference.getSummary());

assertEquals(prefs.getString(PreferenceKeys.KEY_ODK_DESTINATION_DIR,
settingsActivity.getString(R.string.default_odk_destination_dir)), odkDestinationDirPreference.getSummary());
Expand All @@ -110,9 +111,9 @@ public void preferenceSummaryTest() {
*/
@Test
public void preferenceStatusTest() {
btSecureModePreference = (CheckBoxPreference) settingsActivity.findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE);
btSecureModePreference = (SwitchPreference) settingsActivity.findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE);
hotspotPasswordPreference = settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_PASSWORD);
passwordRequirePreference = (CheckBoxPreference) settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
passwordRequirePreference = (SwitchPreference) settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);

hotspotPasswordPreference.setEnabled(false);
assertFalse(hotspotPasswordPreference.isEnabled());
Expand Down