Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.passwordgenerator;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.SeekBar;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //data members
- CheckBox mCbAZ, mCbaz, mCbDigits;
- EditText mEtSS; //for the Special Symbols
- SeekBar mSbLength; //for the password length / size
- Button mBtnGenPass;
- ListView mLvPasswords;
- ArrayList<String> mAlPasswords;
- ArrayAdapter<String> mAd;
- Context mContext;
- AmUtil mUtil;
- SeekBar.OnSeekBarChangeListener mSbHandler =
- new SeekBar.OnSeekBarChangeListener() {
- //auto-called whenever the progress value changes
- //idea: put a new TextView in the layout to display this value
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- //TODO
- }
- //auto-called when the "progress drag" starts
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- //auto-called when the user frees the progress handler
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- int iProgress = mSbLength.getProgress();
- String strProgress = String.valueOf(iProgress);
- String strMsg =
- "Length "+strProgress+" picked.";
- mUtil.fb(strMsg);
- }
- };//mSbHandler
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String[] aParts={};
- switch(v.getId()){
- case R.id.idBtnGenPass:
- String strUser = mEtSS.getText().toString().trim();
- if (!strUser.isEmpty()){
- aParts = strUser.split(" ");
- }//if
- String strPassword = AmUtil.genPassword(
- mSbLength.getProgress(),
- mCbaz.isChecked(),
- mCbAZ.isChecked(),
- mCbDigits.isChecked(),
- aParts
- );
- mAlPasswords.add(0, strPassword);
- mAd.notifyDataSetChanged();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.rl_pass_gen_v1);
- init(savedInstanceState);
- }//onCreate
- void init (Bundle pB){
- //bindings
- mContext = this;
- //mContext = MainActivity.this;
- mUtil = new AmUtil(this);
- mCbaz = findViewById(R.id.idCbaz);
- mCbAZ = findViewById(R.id.idCbAZ);
- mCbDigits = findViewById(R.id.idCbDigits);
- mEtSS = findViewById(R.id.idEtSS);
- mSbLength = findViewById(R.id.idSbLength);
- mBtnGenPass = findViewById(R.id.idBtnGenPass);
- mLvPasswords = findViewById(R.id.idLvPasswords);
- if (mLvPasswords!=null){
- //data source + adapter
- mAlPasswords = new ArrayList<>();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlPasswords
- );
- mLvPasswords.setAdapter(mAd);
- }//if (ListView pattern)
- //behaviors
- mBtnGenPass.setOnClickListener(mClickHandler);
- //testing();
- mSbLength.setOnSeekBarChangeListener(mSbHandler);
- }//init
- void testing(){
- String strTest_az = "";
- String strTest_AZ = "";
- String strTest_Digits = "";
- String strTest_SS = "";
- //" exemplo ".trim() --> "exemplo"
- String strUserSS = mEtSS.getText().toString().trim();
- //"A B C".split(" ") ---> ["A", "B", "C"]
- String[] aUserSS = strUserSS.split(" ");
- for(int idx=0; idx<30; idx++) {
- strTest_az += AmUtil.random_az() + " ";
- strTest_AZ += AmUtil.random_AZ() + " ";
- strTest_Digits += AmUtil.random_09() + " ";
- strTest_SS += AmUtil.randomSpecialSymbol(
- aUserSS
- ) + " ";
- }//for
- mAlPasswords.add(strTest_az);
- mAlPasswords.add(strTest_AZ);
- mAlPasswords.add(strTest_Digits);
- mAlPasswords.add(strTest_SS);
- mAd.notifyDataSetChanged();
- }//testing
- }
- ******
- package com.joythis.android.passwordgenerator;
- import android.app.Activity;
- import android.view.Gravity;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.Random;
- public class AmUtil {
- private Activity mA;
- public AmUtil(Activity pA){
- this.mA = pA;
- }//AmUtil
- public void fb (String pStrMsg){
- Toast t = Toast.makeText(
- this.mA,
- pStrMsg,
- Toast.LENGTH_SHORT
- );
- t.setGravity(Gravity.CENTER, 0, 0);
- t.show();
- }//fb
- //tools related to randomness
- public static int randomInt(int pMin, int pMax){
- Random r = new Random();
- int iAmplitude = pMax-pMin+1;
- int iJump = r.nextInt(iAmplitude);
- int iDestination = pMin + iJump;
- return iDestination;
- }//randomInt
- public static char random_az(){
- int code_a = (int)'a';
- int code_z = (int)'z';
- int codeRandom = randomInt(code_a, code_z);
- char letterRandom = (char)codeRandom;
- return letterRandom;
- }//random_az
- public static char random_AZ(){
- int code_A = (int)'A';
- int code_Z = (int)'Z';
- int codeRandom = randomInt(code_A, code_Z);
- char letterRandom = (char)codeRandom;
- return letterRandom;
- }//random_AZ
- public static char random_09(){
- int code_0 = (int)'0';
- int code_9 = (int)'9';
- int codeRandom = randomInt(code_0, code_9);
- char letterRandom = (char)codeRandom;
- return letterRandom;
- }//random_09
- public static char randomSymbol(char pS1, char pS2){
- int code_s1 = (int)pS1;
- int code_s2 = (int)pS2;
- int codeRandom = randomInt(code_s1, code_s2);
- char letterRandom = (char)codeRandom;
- return letterRandom;
- }//randomSymbol
- public static char rAZ(){return randomSymbol('A', 'Z');}
- public static char raz(){return randomSymbol('a', 'z');}
- public static char r09(){return randomSymbol('0', '9');}
- /*
- ["*", "?", ":"]
- */
- public static char randomSpecialSymbol(
- ArrayList<String> pAlAcceptableSpecialSymbols
- ){
- int iHowMany = pAlAcceptableSpecialSymbols.size();
- int iRandomAddress = randomInt(0, iHowMany-1);
- String strRandomSymbol =
- pAlAcceptableSpecialSymbols.get(iRandomAddress);
- return strRandomSymbol.charAt(0);
- }//randomSpecialSymbol
- public static char randomSpecialSymbol(
- String[] paAcceptableSymbols
- ){
- int iHowMany = paAcceptableSymbols.length;
- int iRandomAddress = randomInt(0, iHowMany-1);
- String strRandomSymbol =
- paAcceptableSymbols[iRandomAddress];
- return strRandomSymbol.charAt(0);
- }//randomSpecialSymbol
- public static String genPassword(
- int pPassLength,
- boolean pb_az,
- boolean pb_AZ,
- boolean pb_09,
- String[] paSS //array of special symbols
- ){
- String strPassword = "";
- boolean bPasswordReady = false;
- char cRandomSymbol;
- boolean bAllTypesRejected =
- !pb_az && !pb_AZ && !pb_09 && paSS.length==0;
- if (!bAllTypesRejected){
- while(!bPasswordReady){
- int iType = randomInt(1, 4);
- cRandomSymbol = 'x';
- switch(iType){
- case 1: //az
- cRandomSymbol = random_az();
- break;
- case 2: //AZ
- cRandomSymbol = random_AZ();
- break;
- case 3: //09
- cRandomSymbol = random_09();
- break;
- case 4: //SS
- if (paSS.length>0) {
- int index = randomInt(0, paSS.length - 1);
- cRandomSymbol = paSS[index].charAt(0);
- }//if
- break;
- }//switch
- //cRandomSymbol
- if (pb_az && iType==1) strPassword+=cRandomSymbol;
- if (pb_AZ && iType==2) strPassword+=cRandomSymbol;
- if (pb_09 && iType==3) strPassword+=cRandomSymbol;
- if (paSS.length>0 && iType==4) strPassword+=cRandomSymbol;
- if (strPassword.length()==pPassLength)
- bPasswordReady=true;
- }//while
- }//if not everything rejected
- else{
- return "empty password";
- }
- return strPassword;
- }//genPassword
- }//AmUtil
Advertisement
Add Comment
Please, Sign In to add comment