0% found this document useful (0 votes)
113 views

InitData Java

This Java code defines an InitData class that extends the AppCompatActivity class. The InitData class initializes data by validating user input for username and password. It checks that the username and password are not blank, validates the username, checks the password strength and history, and updates the password in the database if valid. It also defines methods to check the password policy and validate the last five passwords.

Uploaded by

Arindam Basu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

InitData Java

This Java code defines an InitData class that extends the AppCompatActivity class. The InitData class initializes data by validating user input for username and password. It checks that the username and password are not blank, validates the username, checks the password strength and history, and updates the password in the database if valid. It also defines methods to check the password policy and validate the last five passwords.

Uploaded by

Arindam Basu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

package com.itc.pspd.

woodPro;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.StrictMode;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InitData extends AppCompatActivity {

EditText txtUser;
EditText pwd;
EditText pwdCnf;

Button btnLogin ;

private String blockChars = "'\\;";

private InputFilter inputfilter = new InputFilter() {


@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned
dest, int dstart, int dend) {

if(source!=null && blockChars.contains((""+source))){


return "";
}

return null;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_init_data);

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

ActionBar actionBar = getSupportActionBar();


actionBar.setLogo(R.drawable.itc_42) ;
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);

txtUser = (EditText)findViewById(R.id.txtUsr);
pwd = (EditText)findViewById(R.id.txtpwd);
pwdCnf =(EditText)findViewById(R.id.txtcnfpwd);

txtUser.setFilters(new InputFilter[]{inputfilter});
pwd.setFilters(new InputFilter[]{inputfilter});
pwdCnf.setFilters(new InputFilter[]{inputfilter});

btnLogin = (Button)findViewById(R.id.btnLoginInit);

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!checkBlank())
{
Toast.makeText(getApplicationContext(),"Username/Password can't
be blank!",Toast.LENGTH_SHORT).show();
}
else if (!ValidateUserName())
{
Toast.makeText(getApplicationContext(),"Username
Invalid!",Toast.LENGTH_SHORT).show();
}
else
if(txtUser.getText().toString().trim().equalsIgnoreCase(pwd.getText().toString().tr
im()))
{
Toast.makeText(getApplicationContext(),"Password can't be same
as username!",Toast.LENGTH_SHORT).show();
}

else if (!ValidatePwdPolicy(pwd.getText().toString()))
{
Toast.makeText(getApplicationContext(),"Password should be
alphanumeric with special characters \n and 8 or more characters
long!",Toast.LENGTH_SHORT).show();
}
else if(isLastFive(pwd.getText().toString()))
{
Toast.makeText(getApplicationContext(),"Password should not be
same as last 5 passwords!",Toast.LENGTH_SHORT).show();
}
else if (!
pwd.getText().toString().equals(pwdCnf.getText().toString()))
{
Toast.makeText(getApplicationContext(),"Password and Confirm
Password not same!",Toast.LENGTH_SHORT).show();
}
else
{

if(updatePwd()) {
Session lock = new Session(getApplicationContext());
lock.setVersion("0");
Toast.makeText(getApplicationContext(), "Password updated
Successfully!", Toast.LENGTH_SHORT).show();
InitData.this.finish();
}
else{
Toast.makeText(getApplicationContext(), "Failed to Update
Password!", Toast.LENGTH_SHORT).show();
}

}
}
});

public boolean checkBlank()


{
if(txtUser.getText().toString().trim().isEmpty() ||
pwd.getText().toString().trim().isEmpty() ||
pwdCnf.getText().toString().trim().isEmpty())
return false;
else
return true;
}

public boolean ValidateUserName()


{
Context c = WoodProcurement.getContext();
DBHelper db = new DBHelper(c);

ConfigRecord cfUsr = new ConfigRecord();


cfUsr = db.getConfig("USER");

if(commonUtil.sha256(txtUser.getText().toString().toUpperCase().trim()).equalsIgnor
eCase(cfUsr.getValue()))
return true;
else
return false;
}

public boolean ValidatePwdPolicy(String pswrd)


{

if(pswrd.length()>=8 && isAlpha(pswrd) &&isNumeric(pswrd) &&


isSpecialChar(pswrd))
return true;
else
return false;
}
//*********************PAssword Policy

private boolean isAlpha(String pwd)


{
Boolean flag = false;
flag=pwd.matches(".*[a-zA-Z]+.*");
return flag;
}

public boolean isNumeric(String pwd)


{
Boolean flag = false;
flag=pwd.matches(".*[0-9]+.*");
return flag;

public boolean isSpecialChar(String pwd)


{

if(pwd.matches("[a-zA-Z0-9]*"))
return false;
else
return true;

public boolean isLastFive(String pswrd)


{
DBHelper db = new DBHelper(getApplicationContext());

if(db.isLastFive(pswrd))
return true;
else
return false;
}
//*************************************

public boolean updatePwd()


{
try{
String strCred = pwd.getText().toString();
DBHelper db = new DBHelper(getApplicationContext());
db.updatePwd(strCred);
db.updatePwdHis(strCred);
db.close();
return true;

}
catch (Exception ex)
{
commonUtil.appendLog("Update Password Exception:"+ex.getMessage());
return false;
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_exit, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_exit:

AlertDialog.Builder builder = new


AlertDialog.Builder(InitData.this);
builder.setMessage("Exit Application?").setPositiveButton("Yes",
dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
return true;
case R.id.action_settings:

Intent i = new
Intent(getApplicationContext(),SettingsActivity.class);
startActivity(i);

return true;
default:
return super.onOptionsItemSelected(item);
}
}
DialogInterface.OnClickListener dialogClickListener = new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
finishAffinity();
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
dialog.dismiss();
break;
}
}
};
@Override
public void onBackPressed() {
// do nothing.
AlertDialog.Builder builder = new AlertDialog.Builder(InitData.this);
builder.setTitle(R.string.dialog_title);
builder.setMessage("Exit Application?").setPositiveButton("Yes",
dialogClickListener)
.setNegativeButton("No",
dialogClickListener).setIcon(R.drawable.dialog).show();
}
}

You might also like