Java tutorial
/* * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen * * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany] * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; if * not, see <http://www.gnu.org/licenses/>. */ package de.aw.monma.hbci; /** * Created by alex on 20.08.2016. */ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.util.Base64; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.TextView; import de.aw.awlib.application.AWApplication; import de.aw.awlib.security.AWAESEncrypter; import de.aw.monma.R; /** * LoginFragment */ public class FragmentMasterPassword extends DialogFragment implements View.OnClickListener { private int layout = R.layout.fragment_login; private byte[] mKey; private TextView newPasswordView; private TextView passwordView; private SharedPreferences prefs; @Override public void onClick(View v) { String password = passwordView.getText().toString().trim(); String newPassword = newPasswordView.getText().toString().trim(); if (mKey == null) { if (password.equals(newPassword)) { try { mKey = AWAESEncrypter.generateKey(password); SharedPreferences.Editor editor = prefs.edit(); String key = Base64.encodeToString(mKey, Base64.NO_WRAP); editor.putString(getString(R.string.key), key).apply(); } catch (Exception e) { //TODO Execption bearbeiten e.printStackTrace(); } } else { passwordView.setText(null); passwordView.requestFocus(); newPasswordView.setText(null); newPasswordView.setError(getString(R.string.passwordNichtIdentischError)); return; } } if (AWAESEncrypter.checkPassword(password, mKey)) { dismiss(); } else { passwordView.setText(null); passwordView.setError(getString(R.string.passwordError)); } } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(layout, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); passwordView = view.findViewById(R.id.etPassword); passwordView.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { onClick(passwordView); return true; } return false; } }); newPasswordView = view.findViewById(R.id.etNewPassword); AWApplication.Log("Passwort Abfrage gestartet"); String key = prefs.getString(getString(R.string.key), null); if (key != null) { mKey = Base64.decode(key, Base64.NO_WRAP); } if (mKey != null) { newPasswordView.setVisibility(View.GONE); } view.findViewById(R.id.loginBtn).setOnClickListener(this); } }