Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PassSettings.java
- package com.joythis.android.passwordsgenerator;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.SeekBar;
- import android.widget.TextView;
- public class PassSettings extends AppCompatActivity {
- Context mContext;
- TextView mTvDashboard; // feedback quando se utiliza a SeekBar
- SeekBar mSbSize; // controla o tamanho das passwords a produzir
- CheckBox mCbCapitalLetters, mCbSmallLetters, mCbDigits;
- EditText mEtSS; // onde se entram os símbolos "especiais"
- Button mBtnConfirmSettings; // quando clicked, fará a escrita das escolhas em SharedPreferences
- SharedPreferences mSp; // membro de dados para aceder às SP (nesta Activity, escrevê-las)
- AmUtil mUtil;
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnConfirmSettings:
- actionConfirmSettings();
- break;
- }//switch
- } // onClick
- }; // mClickHandler
- /*
- gravar os settings
- *e*
- regressar à Activity principal
- */
- void actionConfirmSettings(){
- saveSettingsToSharedPreferences();
- //TODO
- }
- /*
- observar as opções assumidas pela UI
- e gravá-las em SP
- */
- void saveSettingsToSharedPreferences(){
- int iPassSize = mSbSize.getProgress();
- boolean bCL = mCbCapitalLetters.isChecked();
- boolean bSL = mCbSmallLetters.isChecked();
- boolean bDigits = mCbDigits.isChecked();
- String ss = mEtSS.getText().toString().trim();
- if (mSp!=null){
- SharedPreferences.Editor ed =
- mSp.edit();
- if (ed!=null){
- ed.putInt(PassGen.KEY_SIZE, iPassSize);
- ed.putBoolean(PassGen.KEY_BCL, bCL);
- ed.putBoolean(PassGen.KEY_BSL, bSL);
- ed.putBoolean(PassGen.KEY_BDigits, bDigits);
- ed.putString(PassGen.KEY_SS, ss);
- ed.commit(); // síncrono / bloqueante
- //ed.apply(); // assíncrono
- mUtil.feedback(
- this.getResources().getString(
- //"Info: settings written"
- R.string.strInfoUpdatedSettings
- )
- );
- }//if
- }//if
- } // saveSettingsToSharedPreferences
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_pass_settings);
- init();
- }//onCreate
- void init(){
- //1 - assocs
- mContext = this;
- mTvDashboard = findViewById(R.id.idTvDashboard);
- mSbSize = findViewById(R.id.idSbSize);
- mCbCapitalLetters = findViewById(R.id.idCbCapitalLetters);
- mCbSmallLetters = findViewById(R.id.idCbSmallLetters);
- mCbDigits = findViewById(R.id.idCbDigits);
- mEtSS = findViewById(R.id.idEtSS);
- mBtnConfirmSettings = findViewById(R.id.idBtnConfirmSettings);
- mUtil = new AmUtil(this);
- mSp = this.getSharedPreferences(
- PassGen.SETTINGS_DB, // nome do ficheiro com a DB SP
- MODE_PRIVATE
- );
- //2 - behaviors / comportamentos
- mBtnConfirmSettings.setOnClickListener(mClickHandler);
- mEtSS.setOnKeyListener(mKeyHandler);
- }//init
- View.OnKeyListener mKeyHandler = new View.OnKeyListener() {
- @Override
- public boolean onKey(
- View v,
- int keyCode,
- KeyEvent event
- )
- {
- boolean bActionKeyDown =
- event.getAction()==KeyEvent.ACTION_DOWN;
- if (bActionKeyDown){
- boolean bWasENTER =
- keyCode == KeyEvent.KEYCODE_ENTER;
- if (bWasENTER){
- mUtil.hideSoftKeyboard();
- saveSettingsToSharedPreferences();
- return true; // evento completamente consumido
- }// if
- }//if
- return false;
- } // onKey
- }; // mKeyHandler
- }
Advertisement
Add Comment
Please, Sign In to add comment