Guest User

Untitled

a guest
Jul 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package com.grawl.passgen;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class PassGenActivity extends Activity {
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16.  
  17. // Interface -- Default
  18.  
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21.  
  22. // Interface -- Custom
  23.  
  24. final Button button_generate = (Button) findViewById(R.id.button_generate);
  25. final EditText text_pass = (EditText) findViewById(R.id.textPassWord);
  26. final EditText edit_length = (EditText) findViewById(R.id.editLength);
  27.  
  28. // Set up Arrays
  29.  
  30. final String[] lowerCase;
  31. final String[] upperCase;
  32. final String[] numbers;
  33. final String[] symbols;
  34.  
  35. // Fill Arrays
  36.  
  37. createArray characters = new createArray();
  38. lowerCase = characters.getArrayLower();
  39. upperCase = characters.getArrayUpper();
  40. numbers = characters.getArrayNumbers();
  41. symbols = characters.getArraySymbols();
  42.  
  43. // Pressing the button WOOOSH!
  44.  
  45. button_generate.setOnClickListener(new View.OnClickListener() {
  46.  
  47. // Set up parameters
  48.  
  49. boolean lowerCaseEnabled = true; // needs interface option
  50. boolean upperCaseEnabled = true; // needs interface option
  51. boolean numbersEnabled = true; // needs interface option
  52. boolean symbolsEnabled = true; // needs interface option
  53. int length;
  54.  
  55. public void onClick(View v) {
  56.  
  57.  
  58. // Set up length based on input from EditText
  59.  
  60. length = 0;
  61.  
  62. try {
  63. length = Integer.parseInt(edit_length.getText().toString());
  64. } catch(NumberFormatException nfe) {
  65. Toast.makeText(PassGenActivity.this, "Can't parse " + nfe, Toast.LENGTH_LONG).show();
  66. }
  67.  
  68. // If password is 0 (or negative), set length to 1
  69.  
  70. if (length < 1) {
  71. length = 1;
  72. edit_length.setText("1");
  73. Toast.makeText(PassGenActivity.this, "Password length can't be less than 1, set it to 1.", Toast.LENGTH_LONG).show();
  74. };
  75.  
  76. // Problem: Need a way to call the "generate password" feature
  77.  
  78. if (length < 100) {
  79. Password password = new Password();
  80. password.fillPassword(lowerCase, upperCase, numbers, symbols);
  81.  
  82. // Generate password
  83.  
  84. password.setPassword(lowerCaseEnabled, upperCaseEnabled, numbersEnabled, symbolsEnabled, length);
  85. text_pass.setText(password.getPassword());
  86. }
  87.  
  88. // Create a dialog to confirm user wants to create big password. Currently not working.
  89. // Problem: Password is generated only after the dialog has been agreed upon once. <-- FIXED
  90. // Problem: Need a way to call the "generate password" feature
  91.  
  92. if (length > 99) {
  93.  
  94. AlertDialog.Builder alert = new AlertDialog.Builder(PassGenActivity.this);
  95.  
  96. alert.setTitle("Warning");
  97. alert.setMessage("You are trying to create quite a long password, are you sure?");
  98.  
  99. alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  100. public void onClick(DialogInterface dialog, int whichButton) {
  101. // Toast.makeText(PassGenActivity.this, "ignoreLength is now: " + ignoreLength, Toast.LENGTH_LONG).show();
  102.  
  103. Password password = new Password();
  104. password.fillPassword(lowerCase, upperCase, numbers, symbols);
  105.  
  106. // Generate password
  107.  
  108. password.setPassword(lowerCaseEnabled, upperCaseEnabled, numbersEnabled, symbolsEnabled, length);
  109. text_pass.setText(password.getPassword());
  110.  
  111. }
  112. });
  113.  
  114. alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
  115. public void onClick(DialogInterface dialog, int whichButton) {
  116. // Toast.makeText(PassGenActivity.this, "ignoreLength is now: " + ignoreLength, Toast.LENGTH_LONG).show();
  117. }
  118. });
  119.  
  120. alert.show();
  121.  
  122. }
  123.  
  124.  
  125. }
  126. });
  127.  
  128. }
  129.  
  130. }
Add Comment
Please, Sign In to add comment