Drop AddProfileActivity in favor of EditProfileActivity

Also automatically update the TextDrawable on name changes in EditProfileActivity
This commit is contained in:
Alexander Bakker 2018-01-02 21:50:07 +01:00
parent 22c5237129
commit ca210de78e
9 changed files with 63 additions and 298 deletions

View file

@ -32,10 +32,6 @@
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<activity
android:name=".AddProfileActivity"
android:theme="@style/AppTheme.TransparentActionBar">
</activity>
<activity
android:name=".EditProfileActivity"
android:label="Edit profile"

View file

@ -1,88 +0,0 @@
package me.impy.aegis;
import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
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;
public class AddProfileActivity extends AegisActivity {
private KeyProfile _keyProfile;
private EditText _profileName;
private TextView _textAlgorithm;
private TextView _textIssuer;
private TextView _textPeriod;
private TextView _textOtp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_profile);
_profileName = findViewById(R.id.addProfileName);
_textAlgorithm = findViewById(R.id.tvAlgorithm);
_textIssuer = findViewById(R.id.tvIssuer);
_textPeriod = findViewById(R.id.tvPeriod);
_textOtp = findViewById(R.id.tvOtp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
_keyProfile = (KeyProfile) getIntent().getSerializableExtra("KeyProfile");
initializeForm();
FloatingActionButton fab = 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());
}
@Override
protected void setPreferredTheme(boolean nightMode) {
if (nightMode) {
setTheme(R.style.AppTheme_Dark_TransparentActionBar);
} else {
setTheme(R.style.AppTheme_Default_TransparentActionBar);
}
}
private void initializeForm() {
KeyInfo info = _keyProfile.getEntry().getInfo();
_profileName.setText(info.getAccountName());
_textAlgorithm.setText(info.getAlgorithm(false));
_textIssuer.setText(info.getIssuer());
_textPeriod.setText(info.getPeriod() + " seconds");
String otp = _keyProfile.refreshCode();
_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);
}
}
}

View file

@ -16,6 +16,8 @@ import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import com.amulyakhare.textdrawable.TextDrawable;
import me.impy.aegis.crypto.CryptoUtils;
import me.impy.aegis.crypto.KeyInfo;
import me.impy.aegis.crypto.KeyInfoException;
@ -23,12 +25,15 @@ import me.impy.aegis.db.DatabaseEntry;
import me.impy.aegis.encoding.Base32;
import me.impy.aegis.helpers.AuthHelper;
import me.impy.aegis.helpers.SpinnerHelper;
import me.impy.aegis.helpers.TextDrawableHelper;
public class EditProfileActivity extends AegisActivity {
private boolean _isNew = false;
private boolean _edited = false;
private KeyProfile _profile;
private ImageView _iconView;
private EditText _textName;
private EditText _textIssuer;
private EditText _textPeriod;
@ -49,13 +54,17 @@ public class EditProfileActivity extends AegisActivity {
bar.setDisplayHomeAsUpEnabled(true);
// if the intent doesn't contain a KeyProfile, create a new one
_profile = (KeyProfile) getIntent().getSerializableExtra("KeyProfile");
Intent intent = getIntent();
_profile = (KeyProfile) intent.getSerializableExtra("KeyProfile");
_isNew = intent.getBooleanExtra("isNew", false);
if (_profile == null) {
_isNew = true;
_profile = new KeyProfile();
}
if (_isNew) {
setTitle("Add profile");
}
_iconView = findViewById(R.id.profile_drawable);
_textName = findViewById(R.id.text_name);
_textIssuer = findViewById(R.id.text_issuer);
_textPeriod = findViewById(R.id.text_period);
@ -79,12 +88,24 @@ public class EditProfileActivity extends AegisActivity {
_spinnerAlgo.setOnItemSelectedListener(_selectedListener);
_spinnerDigits.setOnTouchListener(_selectedListener);
_spinnerDigits.setOnItemSelectedListener(_selectedListener);
// update the icon if the text changed
_textName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
TextDrawable drawable = TextDrawableHelper.generate(s.toString());
_iconView.setImageDrawable(drawable);
}
});
}
private void updateFields() {
DatabaseEntry entry = _profile.getEntry();
ImageView imageView = findViewById(R.id.profile_drawable);
imageView.setImageDrawable(_profile.getDrawable());
_iconView.setImageDrawable(_profile.getDrawable());
_textName.setText(entry.getName());
_textIssuer.setText(entry.getInfo().getIssuer());

View file

@ -8,6 +8,7 @@ import java.lang.reflect.UndeclaredThrowableException;
import me.impy.aegis.crypto.otp.OTP;
import me.impy.aegis.db.DatabaseEntry;
import me.impy.aegis.helpers.TextDrawableHelper;
public class KeyProfile implements Serializable {
private String _code;
@ -38,14 +39,6 @@ public class KeyProfile implements Serializable {
}
public TextDrawable getDrawable() {
String name = _entry.getName();
if (name == null || name.length() <= 1) {
return null;
}
ColorGenerator generator = ColorGenerator.MATERIAL;
int color = generator.getColor(name);
return TextDrawable.builder().buildRound(name.substring(0, 1).toUpperCase(), color);
return TextDrawableHelper.generate(getEntry().getName());
}
}

View file

@ -326,9 +326,17 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
saveDatabase();
}
private void onEnterKeyInfo() {
private void startEditProfileActivity(int requestCode, KeyProfile profile, boolean isNew) {
Intent intent = new Intent(this, EditProfileActivity.class);
startActivityForResult(intent, CODE_ENTER_KEYINFO);
if (profile != null) {
intent.putExtra("KeyProfile", profile);
}
intent.putExtra("isNew", isNew);
startActivityForResult(intent, requestCode);
}
private void onEnterKeyInfo() {
startEditProfileActivity(CODE_ENTER_KEYINFO, null, true);
}
private void onScanKeyInfo() {
@ -341,10 +349,8 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
private void onScanKeyInfoResult(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
KeyProfile keyProfile = (KeyProfile)data.getSerializableExtra("KeyProfile");
Intent intent = new Intent(this, AddProfileActivity.class);
intent.putExtra("KeyProfile", keyProfile);
startActivityForResult(intent, CODE_ADD_KEYINFO);
KeyProfile profile = (KeyProfile)data.getSerializableExtra("KeyProfile");
startEditProfileActivity(CODE_ADD_KEYINFO, profile, true);
}
}
@ -509,9 +515,7 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
editLayout.setOnClickListener(view -> {
bottomDialog.dismiss();
Intent intent = new Intent(this, EditProfileActivity.class);
intent.putExtra("KeyProfile", profile);
startActivityForResult(intent, CODE_EDIT_KEYINFO);
startEditProfileActivity(CODE_EDIT_KEYINFO, profile, false);
});
return bottomDialog;

View file

@ -58,6 +58,7 @@ public class ScannerActivity extends AegisActivity implements ZXingScannerView.R
try {
KeyInfo info = KeyInfo.fromURL(rawResult.getText());
KeyProfile profile = new KeyProfile(new DatabaseEntry(info));
profile.getEntry().setName(info.getAccountName());
Intent resultIntent = new Intent();
resultIntent.putExtra("KeyProfile", profile);

View file

@ -0,0 +1,20 @@
package me.impy.aegis.helpers;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
public class TextDrawableHelper {
private TextDrawableHelper() {
}
public static TextDrawable generate(String s) {
if (s == null || s.length() <= 1) {
return null;
}
ColorGenerator generator = ColorGenerator.MATERIAL;
int color = generator.getColor(s);
return TextDrawable.builder().buildRound(s.substring(0, 1).toUpperCase(), color);
}
}

View file

@ -1,182 +0,0 @@
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@color/colorPrimary"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="360dp"
android:layout_height="80dp"
android:paddingLeft="50dp"
android:layout_gravity="center_vertical">
<TextView
android:text="Profile name"
android:layout_width="111dp"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_weight="0.53"
android:gravity="bottom"
android:textSize="16sp"
android:textColor="@color/secondary_text_inverted"
android:layout_marginLeft="3dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:textColor="@color/primary_text_inverted"
android:paddingTop="0dp"
android:id="@+id/addProfileName"
android:textSize="28sp"
android:paddingRight="0dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/viewB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="?attr/background"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_vpn_key_black_24dp"
android:id="@+id/imageView3"
android:layout_weight="1"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:tint="@color/cardview_dark_background"/>
<TextView
android:text="SHA1"
android:layout_height="wrap_content"
android:id="@+id/tvAlgorithm"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:textColor="?attr/primaryText"
android:textSize="18sp"
android:layout_alignBottom="@+id/imageView3"
android:layout_toRightOf="@+id/imageView3"
android:layout_toEndOf="@+id/imageView3"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_person_black_24dp"
android:id="@+id/ivIssuer"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:tint="@color/cardview_dark_background"
android:layout_below="@+id/imageView3"
android:layout_alignLeft="@+id/imageView3"
android:layout_alignStart="@+id/imageView3"/>
<TextView
android:text="GitHub"
android:layout_height="wrap_content"
android:id="@+id/tvIssuer"
android:layout_weight="1"
android:layout_width="wrap_content"
android:textSize="18sp"
android:textColor="?attr/primaryText"
android:layout_alignBottom="@+id/ivIssuer"
android:layout_alignLeft="@+id/tvAlgorithm"
android:layout_alignStart="@+id/tvAlgorithm"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_timelapse_black_24dp"
android:id="@+id/ivPeriod"
android:layout_weight="1"
android:layout_marginTop="13dp"
android:tint="@color/cardview_dark_background"
android:layout_below="@+id/ivIssuer"
android:layout_alignLeft="@+id/ivIssuer"
android:layout_alignStart="@+id/ivIssuer"/>
<TextView
android:text="30 seconds"
android:layout_height="wrap_content"
android:id="@+id/tvPeriod"
android:layout_weight="1"
android:layout_width="wrap_content"
android:textSize="18sp"
android:textColor="?attr/primaryText"
android:layout_alignBottom="@+id/ivPeriod"
android:layout_alignLeft="@+id/tvIssuer"
android:layout_alignStart="@+id/tvIssuer"
android:layout_marginBottom="2dp"/>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/tvOtp"
android:textSize="36sp"
android:textColor="?attr/primaryText"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/tvAlgorithm"
android:layout_toEndOf="@+id/tvAlgorithm"
android:layout_weight="1"
android:gravity="center_horizontal|center"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/ic_check"/>
<!---->
</android.support.design.widget.CoordinatorLayout>

View file

@ -124,7 +124,7 @@
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
@ -147,7 +147,7 @@
android:textColor="#808080"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>