Skip to content

Commit

Permalink
Added confirm password validation (fixes #94) (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Going-Gone authored and dogi committed Apr 26, 2018
1 parent 3035686 commit 3533c6f
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 7 deletions.
40 changes: 33 additions & 7 deletions app/src/main/java/io/treehouses/remote/BluetoothChatFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class BluetoothChatFragment extends android.support.v4.app.Fragment {
private static final int REQUEST_ENABLE_BT = 3;
public static final int REQUEST_DIALOG_FRAGMENT = 4;
public static final int REQUEST_DIALOG_FRAGMENT_HOTSPOT = 5;
public static final int REQUEST_DIALOG_FRAGMENT_CHPASS= 6;


// Layout Views
Expand Down Expand Up @@ -276,7 +277,7 @@ public void onClick(View view) {
CPbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog(view);
showChPasswordDialog();
}
});
EFbutton.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -364,11 +365,9 @@ public void onClick(DialogInterface dialog, int which) {
}

private AlertDialog getAlertDialog(final EditText input, View view) {
if(view.equals(view.findViewById(R.id.HN))) {
return showAlertDialog("Rename Hostname", "Please enter new hostname", "pirateship rename ", input);
}else{
return showAlertDialog("Change Password", "Please enter new password", "treehouses password ", input);
}

return showAlertDialog("Rename Hostname", "Please enter new hostname", "pirateship rename ", input );

}

/**
Expand Down Expand Up @@ -704,6 +703,23 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}else{
Log.d(TAG, "back from dialog_hotspot, fail");
}
case REQUEST_DIALOG_FRAGMENT_CHPASS:
if(resultCode == Activity.RESULT_OK){

//get password change request
String chPWD = data.getStringExtra("password") == null? "":data.getStringExtra("password");

//store password and command
String password = "pirateship password " + chPWD;

Log.d(TAG, "back from change password");

//send password to command line interface
sendMessage(password);

}else{
Log.d(TAG, "back from change password, fail");
}
}
}

Expand Down Expand Up @@ -771,13 +787,23 @@ public void showNWifiDialog() {
}

public void showHotspotDialog(){
//Reusing WifiDialogFragment code for Hotspot
// Create an instance of the dialog fragment and show it

DialogFragment hDialogFragment = HotspotDialogFragment.newInstance(123);
hDialogFragment.setTargetFragment(this,REQUEST_DIALOG_FRAGMENT_HOTSPOT);
hDialogFragment.show(getFragmentManager().beginTransaction(),"hDialog");


}

public void showChPasswordDialog(){
// Create an instance of the dialog fragment and show it

DialogFragment chPassDialogFragment = ChPasswordDialogFragment.newInstance(123);
chPassDialogFragment.setTargetFragment(this,REQUEST_DIALOG_FRAGMENT_CHPASS);
chPassDialogFragment.show(getFragmentManager().beginTransaction(),"ChangePassDialog");


}

public boolean isJson(String str) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.treehouses.remote;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

/**
* Created by going-gone on 4/19/2018.
*/

public class ChPasswordDialogFragment extends android.support.v4.app.DialogFragment {

private static String TAG = "ChPasswordDialogFragment";

protected EditText passwordEditText;
protected EditText confirmPassEditText;
TextBoxValidation textBoxValidation = new TextBoxValidation();

public static ChPasswordDialogFragment newInstance(int num) {
ChPasswordDialogFragment chPassDialogFragment = new ChPasswordDialogFragment();
return chPassDialogFragment;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d(TAG,"In onCreateDialog()");

// Build the dialog and set up the button click handlers
LayoutInflater inflater = getActivity().getLayoutInflater();
View mView = inflater.inflate(R.layout.chpass_dialog,null);
initLayoutView(mView);

final AlertDialog mDialog = getAlertDialog(mView);
mDialog.setTitle(R.string.change_password);

//initially disable button click
textBoxValidation.getListener(mDialog);
setTextChangeListener(mDialog);

return mDialog;
}
//creates the dialog for the change password dialog
protected AlertDialog getAlertDialog(View mView) {
return new AlertDialog.Builder(getActivity())
.setView(mView)
.setTitle(R.string.change_password)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.change_password,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
String chPass = passwordEditText.getText().toString();

Intent i = new Intent();
i.putExtra("password", chPass);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
}
}
)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
}
})
.create();
}
//initialize the view
protected void initLayoutView(View mView) {
passwordEditText = (EditText)mView.findViewById(R.id.changePassword);
confirmPassEditText = (EditText)mView.findViewById(R.id.confirmPassword);

}
//listener for text change within this dialog
public void setTextChangeListener(final AlertDialog mDialog) {
textBoxValidation.mDialog = mDialog;
textBoxValidation.textWatcher = passwordEditText;
textBoxValidation.PWD = passwordEditText;
textBoxValidation.changePWValidation(confirmPassEditText, getContext());

textBoxValidation.mDialog = mDialog;
textBoxValidation.textWatcher = confirmPassEditText;
textBoxValidation.PWD = passwordEditText;
textBoxValidation.changePWValidation(confirmPassEditText, getContext());

}
}
38 changes: 38 additions & 0 deletions app/src/main/java/io/treehouses/remote/TextBoxValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ public void afterTextChanged(Editable editable) {
});
}

/**
* Textwatcher for the change password dialog
*
*/
public void changePWValidation(final EditText confirmPWD, final Context context) {
textWatcher.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
validateChangedPassword(confirmPWD, context);
}

@Override
public void afterTextChanged(Editable editable) {

}
});
}

/**
* Method that sets the dialog positive button to true or false
*/
Expand Down Expand Up @@ -116,6 +139,21 @@ private void validateWIFI(final Context context) {
}
}

/**
* Change password validator
*
*/
private void validateChangedPassword(final EditText confirmPWD, final Context context) {
if (confirmPWD.getText().toString() == PWD.getText().toString()) {
dialogButtonTrueOrFalse(mDialog, true);
} else if (!confirmPWD.getText().toString().equals(PWD.getText().toString())) {
dialogButtonTrueOrFalse(mDialog, false);
confirmPWD.setError(context.getString(R.string.error_pwd_confirm));
} else {
dialogButtonTrueOrFalse(mDialog, true);
}
}

protected void getListener(final AlertDialog mDialog) {
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout-large-land/chpass_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/changePassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
<EditText
android:id="@+id/confirmPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/confirm_password"/>

</LinearLayout>
29 changes: 29 additions & 0 deletions app/src/main/res/layout-large/chpass_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/changePassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
<EditText
android:id="@+id/confirmPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/confirm_password"/>

</LinearLayout>
29 changes: 29 additions & 0 deletions app/src/main/res/layout-small-land/chpass_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/changePassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
<EditText
android:id="@+id/confirmPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/confirm_password"/>

</LinearLayout>
29 changes: 29 additions & 0 deletions app/src/main/res/layout-small/chpass_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/changePassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
<EditText
android:id="@+id/confirmPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/confirm_password"/>

</LinearLayout>
29 changes: 29 additions & 0 deletions app/src/main/res/layout-xlarge-land/chpass_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/changePassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
<EditText
android:id="@+id/confirmPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/confirm_password"/>

</LinearLayout>
Loading

0 comments on commit 3533c6f

Please sign in to comment.