Aegis/app/src/main/java/me/impy/aegis/AddProfileActivity.java
2017-11-26 19:03:13 +01:00

101 lines
3.3 KiB
Java

package me.impy.aegis;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import me.impy.aegis.crypto.KeyInfo;
import me.impy.aegis.crypto.otp.OTP;
public class AddProfileActivity extends AppCompatActivity {
KeyProfile _keyProfile;
EditText _profileName;
TextView _textAlgorithm;
TextView _textIssuer;
TextView _textPeriod;
TextView _textOtp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPreferredTheme();
setContentView(R.layout.activity_add_profile);
_profileName = (EditText) findViewById(R.id.addProfileName);
_textAlgorithm = (TextView) findViewById(R.id.tvAlgorithm);
_textIssuer = (TextView) findViewById(R.id.tvIssuer);
_textPeriod = (TextView) findViewById(R.id.tvPeriod);
_textOtp = (TextView) findViewById(R.id.tvOtp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
_keyProfile = (KeyProfile) getIntent().getSerializableExtra("KeyProfile");
initializeForm();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent resultIntent = new Intent();
_keyProfile.getEntry().setName(_profileName.getText().toString());
resultIntent.putExtra("KeyProfile", _keyProfile);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
//_profileName.setText(_keyProfile.Info.getAccountName());
}
private void initializeForm() {
KeyInfo info = _keyProfile.getEntry().getInfo();
_profileName.setText(info.getAccountName());
_textAlgorithm.setText(info.getAlgorithm());
_textIssuer.setText(info.getIssuer());
_textPeriod.setText(info.getPeriod() + " seconds");
String otp;
try {
otp = OTP.generateOTP(info);
} catch (Exception e) {
e.printStackTrace();
return;
}
_keyProfile.setCode(otp);
_textOtp.setText(otp.substring(0, 3) + " " + otp.substring(3));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setPreferredTheme() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if (sharedPreferences.getBoolean("pref_night_mode", false)) {
setTheme(R.style.AppTheme_Dark_TransparentActionBar);
} else {
setTheme(R.style.AppTheme_Default_TransparentActionBar);
}
}
}