am_dot_com

DDM 2022-11-15

Nov 15th, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. //PassSettings.java
  2. package com.joythis.android.passwordsgenerator;
  3.  
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.content.Context;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.view.KeyEvent;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.CheckBox;
  13. import android.widget.EditText;
  14. import android.widget.SeekBar;
  15. import android.widget.TextView;
  16.  
  17. public class PassSettings extends AppCompatActivity {
  18. Context mContext;
  19.  
  20. TextView mTvDashboard; // feedback quando se utiliza a SeekBar
  21. SeekBar mSbSize; // controla o tamanho das passwords a produzir
  22. CheckBox mCbCapitalLetters, mCbSmallLetters, mCbDigits;
  23. EditText mEtSS; // onde se entram os símbolos "especiais"
  24. Button mBtnConfirmSettings; // quando clicked, fará a escrita das escolhas em SharedPreferences
  25.  
  26. SharedPreferences mSp; // membro de dados para aceder às SP (nesta Activity, escrevê-las)
  27.  
  28. AmUtil mUtil;
  29.  
  30. View.OnClickListener mClickHandler = new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. switch(v.getId()){
  34. case R.id.idBtnConfirmSettings:
  35. actionConfirmSettings();
  36. break;
  37. }//switch
  38. } // onClick
  39. }; // mClickHandler
  40.  
  41. /*
  42. gravar os settings
  43. *e*
  44. regressar à Activity principal
  45. */
  46. void actionConfirmSettings(){
  47. saveSettingsToSharedPreferences();
  48. //TODO
  49. }
  50.  
  51. /*
  52. observar as opções assumidas pela UI
  53. e gravá-las em SP
  54. */
  55. void saveSettingsToSharedPreferences(){
  56. int iPassSize = mSbSize.getProgress();
  57. boolean bCL = mCbCapitalLetters.isChecked();
  58. boolean bSL = mCbSmallLetters.isChecked();
  59. boolean bDigits = mCbDigits.isChecked();
  60. String ss = mEtSS.getText().toString().trim();
  61.  
  62. if (mSp!=null){
  63. SharedPreferences.Editor ed =
  64. mSp.edit();
  65. if (ed!=null){
  66. ed.putInt(PassGen.KEY_SIZE, iPassSize);
  67. ed.putBoolean(PassGen.KEY_BCL, bCL);
  68. ed.putBoolean(PassGen.KEY_BSL, bSL);
  69. ed.putBoolean(PassGen.KEY_BDigits, bDigits);
  70. ed.putString(PassGen.KEY_SS, ss);
  71. ed.commit(); // síncrono / bloqueante
  72. //ed.apply(); // assíncrono
  73. mUtil.feedback(
  74. this.getResources().getString(
  75. //"Info: settings written"
  76. R.string.strInfoUpdatedSettings
  77. )
  78. );
  79. }//if
  80.  
  81. }//if
  82. } // saveSettingsToSharedPreferences
  83.  
  84. @Override
  85. protected void onCreate(Bundle savedInstanceState) {
  86. super.onCreate(savedInstanceState);
  87. setContentView(R.layout.activity_pass_settings);
  88.  
  89. init();
  90. }//onCreate
  91.  
  92. void init(){
  93. //1 - assocs
  94. mContext = this;
  95.  
  96. mTvDashboard = findViewById(R.id.idTvDashboard);
  97. mSbSize = findViewById(R.id.idSbSize);
  98. mCbCapitalLetters = findViewById(R.id.idCbCapitalLetters);
  99. mCbSmallLetters = findViewById(R.id.idCbSmallLetters);
  100. mCbDigits = findViewById(R.id.idCbDigits);
  101. mEtSS = findViewById(R.id.idEtSS);
  102. mBtnConfirmSettings = findViewById(R.id.idBtnConfirmSettings);
  103.  
  104. mUtil = new AmUtil(this);
  105.  
  106. mSp = this.getSharedPreferences(
  107. PassGen.SETTINGS_DB, // nome do ficheiro com a DB SP
  108. MODE_PRIVATE
  109. );
  110.  
  111. //2 - behaviors / comportamentos
  112. mBtnConfirmSettings.setOnClickListener(mClickHandler);
  113.  
  114. mEtSS.setOnKeyListener(mKeyHandler);
  115. }//init
  116.  
  117. View.OnKeyListener mKeyHandler = new View.OnKeyListener() {
  118. @Override
  119. public boolean onKey(
  120. View v,
  121. int keyCode,
  122. KeyEvent event
  123. )
  124. {
  125. boolean bActionKeyDown =
  126. event.getAction()==KeyEvent.ACTION_DOWN;
  127.  
  128. if (bActionKeyDown){
  129. boolean bWasENTER =
  130. keyCode == KeyEvent.KEYCODE_ENTER;
  131.  
  132. if (bWasENTER){
  133. mUtil.hideSoftKeyboard();
  134. saveSettingsToSharedPreferences();
  135. return true; // evento completamente consumido
  136. }// if
  137. }//if
  138. return false;
  139. } // onKey
  140. }; // mKeyHandler
  141. }
Advertisement
Add Comment
Please, Sign In to add comment