mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-04-22 23:09:13 +00:00
Split KeyProfile adapter and holder, clean stuff up and fix some bugs
This commit is contained in:
parent
ff38af8e6b
commit
ed86d6ac8e
5 changed files with 114 additions and 102 deletions
|
@ -1,20 +1,10 @@
|
||||||
package me.impy.aegis;
|
package me.impy.aegis;
|
||||||
|
|
||||||
import android.animation.ObjectAnimator;
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.animation.LinearInterpolator;
|
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.ProgressBar;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import com.amulyakhare.textdrawable.TextDrawable;
|
|
||||||
import com.amulyakhare.textdrawable.util.ColorGenerator;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -22,15 +12,13 @@ import java.util.List;
|
||||||
|
|
||||||
import me.impy.aegis.helpers.ItemTouchHelperAdapter;
|
import me.impy.aegis.helpers.ItemTouchHelperAdapter;
|
||||||
|
|
||||||
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> implements ItemTouchHelperAdapter {
|
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> implements ItemTouchHelperAdapter {
|
||||||
private final List<KeyProfileHolder> _holders;
|
|
||||||
private ArrayList<KeyProfile> _keyProfiles;
|
private ArrayList<KeyProfile> _keyProfiles;
|
||||||
private Handler _uiHandler;
|
private Handler _uiHandler;
|
||||||
private static Listener _listener;
|
private static Listener _listener;
|
||||||
|
|
||||||
public KeyProfileAdapter(Listener listener) {
|
public KeyProfileAdapter(Listener listener) {
|
||||||
_keyProfiles = new ArrayList<>();
|
_keyProfiles = new ArrayList<>();
|
||||||
_holders = new ArrayList<>();
|
|
||||||
_uiHandler = new Handler();
|
_uiHandler = new Handler();
|
||||||
_listener = listener;
|
_listener = listener;
|
||||||
}
|
}
|
||||||
|
@ -73,27 +61,44 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
public KeyProfileHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_keyprofile, parent, false);
|
||||||
return new KeyProfileHolder(v);
|
return new KeyProfileHolder(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewRecycled(KeyProfileHolder holder) {
|
||||||
|
holder.setData(null);
|
||||||
|
super.onViewRecycled(holder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
|
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
|
||||||
holder.setData(_keyProfiles.get(position));
|
final KeyProfile profile = _keyProfiles.get(position);
|
||||||
|
holder.setData(profile);
|
||||||
holder.updateCode();
|
holder.updateCode();
|
||||||
_holders.add(holder);
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
int position = holder.getAdapterPosition();
|
||||||
|
_listener.onKeyProfileClick(_keyProfiles.get(position));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onLongClick(View v) {
|
||||||
|
int position = holder.getAdapterPosition();
|
||||||
|
return _listener.onLongKeyProfileClick(_keyProfiles.get(position));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
_uiHandler.postDelayed(new Runnable() {
|
_uiHandler.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// check if this key profile still exists
|
if (holder.updateCode()) {
|
||||||
if (_holders.contains(holder)) {
|
_uiHandler.postDelayed(this, profile.getEntry().getInfo().getPeriod() * 1000);
|
||||||
holder.updateCode();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_uiHandler.postDelayed(this, holder._keyProfile.getEntry().getInfo().getPeriod() * 1000);
|
|
||||||
}
|
}
|
||||||
}, holder._keyProfile.getEntry().getInfo().getMillisTillNextRotation());
|
}, profile.getEntry().getInfo().getMillisTillNextRotation());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -101,83 +106,6 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
|
||||||
return _keyProfiles.size();
|
return _keyProfiles.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class KeyProfileHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
|
||||||
private TextView _profileName;
|
|
||||||
private TextView _profileCode;
|
|
||||||
private TextView _profileIssuer;
|
|
||||||
private ImageView _profileDrawable;
|
|
||||||
private KeyProfile _keyProfile;
|
|
||||||
private ProgressBar _progressBar;
|
|
||||||
private View _itemView;
|
|
||||||
|
|
||||||
KeyProfileHolder(final View itemView) {
|
|
||||||
super(itemView);
|
|
||||||
_itemView = itemView;
|
|
||||||
_profileName = itemView.findViewById(R.id.profile_name);
|
|
||||||
_profileCode = itemView.findViewById(R.id.profile_code);
|
|
||||||
_profileIssuer = itemView.findViewById(R.id.profile_issuer);
|
|
||||||
_profileDrawable = itemView.findViewById(R.id.ivTextDrawable);
|
|
||||||
_progressBar = itemView.findViewById(R.id.progressBar);
|
|
||||||
|
|
||||||
itemView.setOnClickListener(this);
|
|
||||||
itemView.setOnLongClickListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setData(KeyProfile profile) {
|
|
||||||
_keyProfile = profile;
|
|
||||||
_profileName.setText(profile.getEntry().getName());
|
|
||||||
_profileCode.setText(profile.getCode());
|
|
||||||
|
|
||||||
// So that we can have text in the designer without showing it to our user
|
|
||||||
_profileIssuer.setText("");
|
|
||||||
|
|
||||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_itemView.getContext());
|
|
||||||
if (sharedPreferences.getBoolean("pref_issuer", false)) {
|
|
||||||
_profileIssuer.setText(" - " + profile.getEntry().getInfo().getIssuer());
|
|
||||||
}
|
|
||||||
|
|
||||||
_profileDrawable.setImageDrawable(generateTextDrawable(profile));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateCode() {
|
|
||||||
_progressBar.setProgress(1000);
|
|
||||||
if (_keyProfile == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String otp = _keyProfile.refreshCode();
|
|
||||||
_profileCode.setText(otp.substring(0, 3) + " " + otp.substring(3));
|
|
||||||
|
|
||||||
long millisTillRotation = _keyProfile.getEntry().getInfo().getMillisTillNextRotation();
|
|
||||||
long period = _keyProfile.getEntry().getInfo().getPeriod() * 1000;
|
|
||||||
int currentProgress = 1000 - (int) ((((double) period - millisTillRotation) / period) * 1000);
|
|
||||||
ObjectAnimator animation = ObjectAnimator.ofInt(_progressBar, "progress", currentProgress, 0);
|
|
||||||
animation.setDuration(millisTillRotation);
|
|
||||||
animation.setInterpolator(new LinearInterpolator());
|
|
||||||
animation.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private TextDrawable generateTextDrawable(KeyProfile profile) {
|
|
||||||
if (_profileName == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColorGenerator generator = ColorGenerator.MATERIAL;
|
|
||||||
int profileKeyColor = generator.getColor(profile.getEntry().getName());
|
|
||||||
|
|
||||||
return TextDrawable.builder().buildRound(profile.getEntry().getName().substring(0, 1).toUpperCase(), profileKeyColor);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClick(View view) {
|
|
||||||
_listener.onKeyProfileClick(_keyProfiles.get(getAdapterPosition()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onLongClick(View view) {
|
|
||||||
return _listener.onLongKeyProfileClick(_keyProfiles.get(getAdapterPosition()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface Listener {
|
public interface Listener {
|
||||||
void onKeyProfileClick(KeyProfile profile);
|
void onKeyProfileClick(KeyProfile profile);
|
||||||
boolean onLongKeyProfileClick(KeyProfile profile);
|
boolean onLongKeyProfileClick(KeyProfile profile);
|
||||||
|
|
83
app/src/main/java/me/impy/aegis/KeyProfileHolder.java
Normal file
83
app/src/main/java/me/impy/aegis/KeyProfileHolder.java
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
package me.impy.aegis;
|
||||||
|
|
||||||
|
import android.animation.ObjectAnimator;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.animation.LinearInterpolator;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.amulyakhare.textdrawable.TextDrawable;
|
||||||
|
import com.amulyakhare.textdrawable.util.ColorGenerator;
|
||||||
|
|
||||||
|
public class KeyProfileHolder extends RecyclerView.ViewHolder {
|
||||||
|
private TextView _profileName;
|
||||||
|
private TextView _profileCode;
|
||||||
|
private TextView _profileIssuer;
|
||||||
|
private ImageView _profileDrawable;
|
||||||
|
private KeyProfile _keyProfile;
|
||||||
|
private ProgressBar _progressBar;
|
||||||
|
private View _itemView;
|
||||||
|
|
||||||
|
KeyProfileHolder(final View itemView) {
|
||||||
|
super(itemView);
|
||||||
|
_itemView = itemView;
|
||||||
|
_profileName = itemView.findViewById(R.id.profile_name);
|
||||||
|
_profileCode = itemView.findViewById(R.id.profile_code);
|
||||||
|
_profileIssuer = itemView.findViewById(R.id.profile_issuer);
|
||||||
|
_profileDrawable = itemView.findViewById(R.id.ivTextDrawable);
|
||||||
|
_progressBar = itemView.findViewById(R.id.progressBar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(KeyProfile profile) {
|
||||||
|
_keyProfile = profile;
|
||||||
|
if (_keyProfile == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_profileName.setText(profile.getEntry().getName());
|
||||||
|
_profileCode.setText(profile.getCode());
|
||||||
|
|
||||||
|
// So that we can have text in the designer without showing it to our user
|
||||||
|
_profileIssuer.setText("");
|
||||||
|
|
||||||
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(_itemView.getContext());
|
||||||
|
if (sharedPreferences.getBoolean("pref_issuer", false)) {
|
||||||
|
_profileIssuer.setText(" - " + profile.getEntry().getInfo().getIssuer());
|
||||||
|
}
|
||||||
|
|
||||||
|
_profileDrawable.setImageDrawable(generateTextDrawable(profile));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean updateCode() {
|
||||||
|
_progressBar.setProgress(1000);
|
||||||
|
if (_keyProfile == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String otp = _keyProfile.refreshCode();
|
||||||
|
_profileCode.setText(otp.substring(0, 3) + " " + otp.substring(3));
|
||||||
|
|
||||||
|
long millisTillRotation = _keyProfile.getEntry().getInfo().getMillisTillNextRotation();
|
||||||
|
long period = _keyProfile.getEntry().getInfo().getPeriod() * 1000;
|
||||||
|
int currentProgress = 1000 - (int) ((((double) period - millisTillRotation) / period) * 1000);
|
||||||
|
ObjectAnimator animation = ObjectAnimator.ofInt(_progressBar, "progress", currentProgress, 0);
|
||||||
|
animation.setDuration(millisTillRotation);
|
||||||
|
animation.setInterpolator(new LinearInterpolator());
|
||||||
|
animation.start();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TextDrawable generateTextDrawable(KeyProfile profile) {
|
||||||
|
if (_profileName == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ColorGenerator generator = ColorGenerator.MATERIAL;
|
||||||
|
int profileKeyColor = generator.getColor(profile.getEntry().getName());
|
||||||
|
|
||||||
|
return TextDrawable.builder().buildRound(profile.getEntry().getName().substring(0, 1).toUpperCase(), profileKeyColor);
|
||||||
|
}
|
||||||
|
}
|
|
@ -390,6 +390,8 @@ public class MainActivity extends AppCompatActivity implements KeyProfileAdapter
|
||||||
Toast.makeText(this, "An error occurred while trying to delete an entry", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "An error occurred while trying to delete an entry", Toast.LENGTH_SHORT).show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
saveDatabase();
|
||||||
|
|
||||||
_keyProfileAdapter.removeKey(profile);
|
_keyProfileAdapter.removeKey(profile);
|
||||||
})
|
})
|
||||||
.setNegativeButton(android.R.string.no, null)
|
.setNegativeButton(android.R.string.no, null)
|
||||||
|
|
|
@ -28,7 +28,7 @@ public class KeyInfo implements Serializable {
|
||||||
builder.appendQueryParameter("counter", Long.toString(_counter));
|
builder.appendQueryParameter("counter", Long.toString(_counter));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_issuer.equals("")) {
|
if (_issuer != null && !_issuer.equals("")) {
|
||||||
builder.path(String.format("%s:%s", _issuer, _accountName));
|
builder.path(String.format("%s:%s", _issuer, _accountName));
|
||||||
builder.appendQueryParameter("issuer", _issuer);
|
builder.appendQueryParameter("issuer", _issuer);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -18,7 +18,6 @@ public interface ItemTouchHelperAdapter {
|
||||||
*/
|
*/
|
||||||
void onItemMove(int fromPosition, int toPosition);
|
void onItemMove(int fromPosition, int toPosition);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when an item has been dismissed by a swipe.<br/>
|
* Called when an item has been dismissed by a swipe.<br/>
|
||||||
* <br/>
|
* <br/>
|
||||||
|
|
Loading…
Add table
Reference in a new issue