am_dot_com

DDM 20211103

Nov 3rd, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. package com.joythis.android.passwordgenerator;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.Button;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.ListView;
  13. import android.widget.SeekBar;
  14.  
  15. import java.util.ArrayList;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18. //data members
  19. CheckBox mCbAZ, mCbaz, mCbDigits;
  20. EditText mEtSS; //for the Special Symbols
  21. SeekBar mSbLength; //for the password length / size
  22. Button mBtnGenPass;
  23. ListView mLvPasswords;
  24. ArrayList<String> mAlPasswords;
  25. ArrayAdapter<String> mAd;
  26.  
  27. Context mContext;
  28. AmUtil mUtil;
  29.  
  30. SeekBar.OnSeekBarChangeListener mSbHandler =
  31. new SeekBar.OnSeekBarChangeListener() {
  32. //auto-called whenever the progress value changes
  33. //idea: put a new TextView in the layout to display this value
  34. @Override
  35. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  36. //TODO
  37. }
  38.  
  39. //auto-called when the "progress drag" starts
  40. @Override
  41. public void onStartTrackingTouch(SeekBar seekBar) {
  42. }
  43.  
  44. //auto-called when the user frees the progress handler
  45. @Override
  46. public void onStopTrackingTouch(SeekBar seekBar) {
  47. int iProgress = mSbLength.getProgress();
  48. String strProgress = String.valueOf(iProgress);
  49. String strMsg =
  50. "Length "+strProgress+" picked.";
  51. mUtil.fb(strMsg);
  52. }
  53. };//mSbHandler
  54.  
  55. View.OnClickListener mClickHandler = new View.OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. String[] aParts={};
  59. switch(v.getId()){
  60. case R.id.idBtnGenPass:
  61. String strUser = mEtSS.getText().toString().trim();
  62. if (!strUser.isEmpty()){
  63. aParts = strUser.split(" ");
  64. }//if
  65. String strPassword = AmUtil.genPassword(
  66. mSbLength.getProgress(),
  67. mCbaz.isChecked(),
  68. mCbAZ.isChecked(),
  69. mCbDigits.isChecked(),
  70. aParts
  71. );
  72. mAlPasswords.add(0, strPassword);
  73. mAd.notifyDataSetChanged();
  74. break;
  75. }//switch
  76. }//onClick
  77. };//mClickHandler
  78.  
  79. @Override
  80. protected void onCreate(Bundle savedInstanceState) {
  81. super.onCreate(savedInstanceState);
  82. setContentView(R.layout.rl_pass_gen_v1);
  83.  
  84. init(savedInstanceState);
  85. }//onCreate
  86.  
  87. void init (Bundle pB){
  88. //bindings
  89. mContext = this;
  90. //mContext = MainActivity.this;
  91. mUtil = new AmUtil(this);
  92.  
  93. mCbaz = findViewById(R.id.idCbaz);
  94. mCbAZ = findViewById(R.id.idCbAZ);
  95. mCbDigits = findViewById(R.id.idCbDigits);
  96. mEtSS = findViewById(R.id.idEtSS);
  97. mSbLength = findViewById(R.id.idSbLength);
  98. mBtnGenPass = findViewById(R.id.idBtnGenPass);
  99. mLvPasswords = findViewById(R.id.idLvPasswords);
  100.  
  101. if (mLvPasswords!=null){
  102. //data source + adapter
  103. mAlPasswords = new ArrayList<>();
  104. mAd = new ArrayAdapter<>(
  105. mContext,
  106. android.R.layout.simple_list_item_1,
  107. mAlPasswords
  108. );
  109. mLvPasswords.setAdapter(mAd);
  110. }//if (ListView pattern)
  111.  
  112. //behaviors
  113. mBtnGenPass.setOnClickListener(mClickHandler);
  114. //testing();
  115. mSbLength.setOnSeekBarChangeListener(mSbHandler);
  116. }//init
  117.  
  118. void testing(){
  119. String strTest_az = "";
  120. String strTest_AZ = "";
  121. String strTest_Digits = "";
  122. String strTest_SS = "";
  123. //" exemplo ".trim() --> "exemplo"
  124. String strUserSS = mEtSS.getText().toString().trim();
  125. //"A B C".split(" ") ---> ["A", "B", "C"]
  126. String[] aUserSS = strUserSS.split(" ");
  127. for(int idx=0; idx<30; idx++) {
  128. strTest_az += AmUtil.random_az() + " ";
  129. strTest_AZ += AmUtil.random_AZ() + " ";
  130. strTest_Digits += AmUtil.random_09() + " ";
  131. strTest_SS += AmUtil.randomSpecialSymbol(
  132. aUserSS
  133. ) + " ";
  134. }//for
  135. mAlPasswords.add(strTest_az);
  136. mAlPasswords.add(strTest_AZ);
  137. mAlPasswords.add(strTest_Digits);
  138. mAlPasswords.add(strTest_SS);
  139.  
  140. mAd.notifyDataSetChanged();
  141. }//testing
  142.  
  143.  
  144. }
  145.  
  146. ******
  147.  
  148. package com.joythis.android.passwordgenerator;
  149.  
  150. import android.app.Activity;
  151. import android.view.Gravity;
  152. import android.widget.Toast;
  153.  
  154. import java.util.ArrayList;
  155. import java.util.Random;
  156.  
  157. public class AmUtil {
  158. private Activity mA;
  159.  
  160. public AmUtil(Activity pA){
  161. this.mA = pA;
  162. }//AmUtil
  163.  
  164. public void fb (String pStrMsg){
  165. Toast t = Toast.makeText(
  166. this.mA,
  167. pStrMsg,
  168. Toast.LENGTH_SHORT
  169. );
  170.  
  171. t.setGravity(Gravity.CENTER, 0, 0);
  172.  
  173. t.show();
  174. }//fb
  175.  
  176. //tools related to randomness
  177. public static int randomInt(int pMin, int pMax){
  178. Random r = new Random();
  179. int iAmplitude = pMax-pMin+1;
  180. int iJump = r.nextInt(iAmplitude);
  181. int iDestination = pMin + iJump;
  182. return iDestination;
  183. }//randomInt
  184.  
  185. public static char random_az(){
  186. int code_a = (int)'a';
  187. int code_z = (int)'z';
  188. int codeRandom = randomInt(code_a, code_z);
  189. char letterRandom = (char)codeRandom;
  190. return letterRandom;
  191. }//random_az
  192.  
  193. public static char random_AZ(){
  194. int code_A = (int)'A';
  195. int code_Z = (int)'Z';
  196. int codeRandom = randomInt(code_A, code_Z);
  197. char letterRandom = (char)codeRandom;
  198. return letterRandom;
  199. }//random_AZ
  200.  
  201. public static char random_09(){
  202. int code_0 = (int)'0';
  203. int code_9 = (int)'9';
  204. int codeRandom = randomInt(code_0, code_9);
  205. char letterRandom = (char)codeRandom;
  206. return letterRandom;
  207. }//random_09
  208.  
  209. public static char randomSymbol(char pS1, char pS2){
  210. int code_s1 = (int)pS1;
  211. int code_s2 = (int)pS2;
  212. int codeRandom = randomInt(code_s1, code_s2);
  213. char letterRandom = (char)codeRandom;
  214. return letterRandom;
  215. }//randomSymbol
  216.  
  217. public static char rAZ(){return randomSymbol('A', 'Z');}
  218. public static char raz(){return randomSymbol('a', 'z');}
  219. public static char r09(){return randomSymbol('0', '9');}
  220.  
  221. /*
  222. ["*", "?", ":"]
  223. */
  224. public static char randomSpecialSymbol(
  225. ArrayList<String> pAlAcceptableSpecialSymbols
  226. ){
  227. int iHowMany = pAlAcceptableSpecialSymbols.size();
  228. int iRandomAddress = randomInt(0, iHowMany-1);
  229. String strRandomSymbol =
  230. pAlAcceptableSpecialSymbols.get(iRandomAddress);
  231. return strRandomSymbol.charAt(0);
  232. }//randomSpecialSymbol
  233.  
  234. public static char randomSpecialSymbol(
  235. String[] paAcceptableSymbols
  236. ){
  237. int iHowMany = paAcceptableSymbols.length;
  238. int iRandomAddress = randomInt(0, iHowMany-1);
  239. String strRandomSymbol =
  240. paAcceptableSymbols[iRandomAddress];
  241. return strRandomSymbol.charAt(0);
  242. }//randomSpecialSymbol
  243.  
  244. public static String genPassword(
  245. int pPassLength,
  246. boolean pb_az,
  247. boolean pb_AZ,
  248. boolean pb_09,
  249. String[] paSS //array of special symbols
  250. ){
  251. String strPassword = "";
  252. boolean bPasswordReady = false;
  253. char cRandomSymbol;
  254. boolean bAllTypesRejected =
  255. !pb_az && !pb_AZ && !pb_09 && paSS.length==0;
  256. if (!bAllTypesRejected){
  257. while(!bPasswordReady){
  258. int iType = randomInt(1, 4);
  259. cRandomSymbol = 'x';
  260. switch(iType){
  261. case 1: //az
  262. cRandomSymbol = random_az();
  263. break;
  264. case 2: //AZ
  265. cRandomSymbol = random_AZ();
  266. break;
  267. case 3: //09
  268. cRandomSymbol = random_09();
  269. break;
  270. case 4: //SS
  271. if (paSS.length>0) {
  272. int index = randomInt(0, paSS.length - 1);
  273. cRandomSymbol = paSS[index].charAt(0);
  274. }//if
  275. break;
  276. }//switch
  277. //cRandomSymbol
  278. if (pb_az && iType==1) strPassword+=cRandomSymbol;
  279. if (pb_AZ && iType==2) strPassword+=cRandomSymbol;
  280. if (pb_09 && iType==3) strPassword+=cRandomSymbol;
  281. if (paSS.length>0 && iType==4) strPassword+=cRandomSymbol;
  282.  
  283. if (strPassword.length()==pPassLength)
  284. bPasswordReady=true;
  285. }//while
  286. }//if not everything rejected
  287. else{
  288. return "empty password";
  289. }
  290.  
  291. return strPassword;
  292. }//genPassword
  293. }//AmUtil
Advertisement
Add Comment
Please, Sign In to add comment