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

Commit

Permalink
disabled the positive button instead of toast
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-prabhakar committed Jan 30, 2020
1 parent 1170e12 commit 7c80232
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.odk.share.views.ui.settings;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.Button;

/**
* @author by Chromicle ([email protected])
* @since 1/30/2020
*/
public class CustomEditTextPreference extends EditTextPreference implements DialogInterface.OnClickListener {


private EditTextWatcher editTextWatcher = new EditTextWatcher();

public CustomEditTextPreference(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
}


public CustomEditTextPreference(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}

/**
* Return true in order to enable positive button or false to disable it.
*/
private boolean onCheckValue(String value) {
return !value.trim().equals("");
}

private void onEditTextChanged() {
boolean enable = onCheckValue(getEditText().getText().toString());
Dialog dlg = getDialog();
if (dlg instanceof AlertDialog) {
AlertDialog alertDlg = (AlertDialog) dlg;
Button possitiveButton = alertDlg.getButton(AlertDialog.BUTTON_POSITIVE);
possitiveButton.setEnabled(enable);
}
}

@Override
protected void showDialog(Bundle state) {
super.showDialog(state);

getEditText().removeTextChangedListener(editTextWatcher);
getEditText().addTextChangedListener(editTextWatcher);
onEditTextChanged();
}

private class EditTextWatcher implements TextWatcher {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
onEditTextChanged();
}

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

@Override
public void afterTextChanged(Editable s) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
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 @@ -36,12 +35,13 @@

public class SettingsActivity extends PreferenceActivity {

EditTextPreference hotspotNamePreference;
EditTextPreference bluetoothNamePreference;
// changed to customEditTextPreference
CustomEditTextPreference hotspotNamePreference;
CustomEditTextPreference bluetoothNamePreference;
Preference hotspotPasswordPreference;
CheckBoxPreference passwordRequirePreference;
CheckBoxPreference btSecureModePreference;
EditTextPreference odkDestinationDirPreference;
SwitchPreference passwordRequirePreference;
SwitchPreference btSecureModePreference;
CustomEditTextPreference odkDestinationDirPreference;
ListPreference defaultMethodPreference;
private SharedPreferences prefs;
String npass;
Expand Down Expand Up @@ -69,18 +69,22 @@ public boolean onOptionsItemSelected(MenuItem item) {

private void addPreferences() {
defaultMethodPreference = (ListPreference) findPreference(PreferenceKeys.KEY_DEFAULT_TRANSFER_METHOD);
hotspotNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
bluetoothNamePreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_NAME);
hotspotNamePreference = (CustomEditTextPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
bluetoothNamePreference = (CustomEditTextPreference) 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);
odkDestinationDirPreference = (EditTextPreference) findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);
passwordRequirePreference = (SwitchPreference) findPreference(PreferenceKeys.KEY_HOTSPOT_PWD_REQUIRE);
btSecureModePreference = (SwitchPreference) findPreference(PreferenceKeys.KEY_BLUETOOTH_SECURE_MODE);
odkDestinationDirPreference = (CustomEditTextPreference) 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.app_name)+getString(R.string.one_space)+ android.os.Build.MODEL));
String defaultHotspotName = getString(R.string.app_name) + getString(R.string.one_space) + Build.MODEL;
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 Down Expand Up @@ -120,23 +124,13 @@ private Preference.OnPreferenceChangeListener preferenceChangeListener() {
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 @@ -155,12 +149,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 @@ -217,16 +206,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
7 changes: 2 additions & 5 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 @@ -183,6 +180,6 @@
<string name="saved_on_date_at_time">\'Saved on\' EEE, MMM dd, yyyy \'at\' HH:mm</string> <!-- http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html -->
<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>
<string name="one_space" /> <!-- 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>
</resources>
10 changes: 5 additions & 5 deletions skunkworks_crow/src/main/res/xml/preferences_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings">

<EditTextPreference
<org.odk.share.views.ui.settings.CustomEditTextPreference
android:defaultValue="@string/default_odk_destination_dir"
android:icon="@drawable/ic_sd_storage_black_24dp"
android:key="odk_destination_dir"
Expand All @@ -22,7 +22,7 @@

<PreferenceCategory android:title="Hotspot Settings">

<EditTextPreference
<org.odk.share.views.ui.settings.CustomEditTextPreference
android:icon="@drawable/ic_wifi_tethering_black_24dp"
android:key="hotspot_name"
android:title="@string/title_hotspot_ssid" />
Expand All @@ -35,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 @@ -46,12 +46,12 @@

<PreferenceCategory android:title="Bluetooth Settings">

<EditTextPreference
<org.odk.share.views.ui.settings.CustomEditTextPreference
android:icon="@drawable/ic_bluetooth_black_24dp"
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,18 +2,19 @@

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

import androidx.appcompat.widget.Toolbar;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.share.R;
import org.odk.share.views.ui.settings.CustomEditTextPreference;
import org.odk.share.views.ui.settings.PreferenceKeys;
import org.odk.share.views.ui.settings.SettingsActivity;
import org.robolectric.Robolectric;
Expand All @@ -31,12 +32,12 @@ public class SettingsActivityTest {
private SettingsActivity settingsActivity;
private SharedPreferences prefs;
private Preference hotspotPasswordPreference;
private CheckBoxPreference passwordRequirePreference;
private EditTextPreference hotspotNamePreference;
private EditTextPreference odkDestinationDirPreference;
private CheckBoxPreference btSecureModePreference;
private SwitchPreference passwordRequirePreference;
private CustomEditTextPreference hotspotNamePreference;
private CustomEditTextPreference odkDestinationDirPreference;
private SwitchPreference btSecureModePreference;
private ListPreference defaultMethodPreference;
private EditTextPreference bluetoothNamePreference;
private CustomEditTextPreference bluetoothNamePreference;

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -90,13 +91,13 @@ public void preferenceMenuTest() {
*/
@Test
public void preferenceSummaryTest() {
hotspotNamePreference = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
odkDestinationDirPreference = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);
bluetoothNamePreference = (EditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_BLUETOOTH_NAME);
hotspotNamePreference = (CustomEditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_HOTSPOT_NAME);
odkDestinationDirPreference = (CustomEditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_ODK_DESTINATION_DIR);
bluetoothNamePreference = (CustomEditTextPreference) settingsActivity.findPreference(PreferenceKeys.KEY_BLUETOOTH_NAME);

//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

0 comments on commit 7c80232

Please sign in to comment.