Refresh all codes in onResume

This commit is contained in:
Alexander Bakker 2018-02-09 19:28:31 +01:00
parent 9b960c7f34
commit d5f796ca87
5 changed files with 42 additions and 19 deletions

View file

@ -13,6 +13,7 @@ import me.impy.aegis.helpers.TextDrawableHelper;
public class KeyProfile implements Serializable { public class KeyProfile implements Serializable {
private String _code; private String _code;
private DatabaseEntry _entry; private DatabaseEntry _entry;
private Listener _listener;
public KeyProfile() { public KeyProfile() {
this(new DatabaseEntry()); this(new DatabaseEntry());
@ -22,6 +23,10 @@ public class KeyProfile implements Serializable {
_entry = entry; _entry = entry;
} }
public void setListener(Listener listener) {
_listener = listener;
}
public DatabaseEntry getEntry() { public DatabaseEntry getEntry() {
return _entry; return _entry;
} }
@ -35,10 +40,17 @@ public class KeyProfile implements Serializable {
} catch (Exception e) { } catch (Exception e) {
throw new UndeclaredThrowableException(e); throw new UndeclaredThrowableException(e);
} }
if (_listener != null) {
_listener.onRefreshCode(_code);
}
return _code; return _code;
} }
public TextDrawable getDrawable() { public TextDrawable getDrawable() {
return TextDrawableHelper.generate(getEntry().getName()); return TextDrawableHelper.generate(getEntry().getName());
} }
public interface Listener {
void onRefreshCode(String code);
}
} }

View file

@ -54,6 +54,12 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
notifyItemChanged(position); notifyItemChanged(position);
} }
public void refresh() {
for (KeyProfile profile : _keyProfiles) {
profile.refreshCode();
}
}
private KeyProfile getKeyByID(long id) { private KeyProfile getKeyByID(long id) {
for (KeyProfile profile : _keyProfiles) { for (KeyProfile profile : _keyProfiles) {
if (profile.getEntry().getID() == id) { if (profile.getEntry().getID() == id) {
@ -99,7 +105,7 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileHolder> im
public void onBindViewHolder(final KeyProfileHolder holder, int position) { public void onBindViewHolder(final KeyProfileHolder holder, int position) {
final KeyProfile profile = _keyProfiles.get(position); final KeyProfile profile = _keyProfiles.get(position);
holder.setData(profile, _showIssuer); holder.setData(profile, _showIssuer);
holder.startUpdateLoop(); holder.startRefreshLoop();
holder.itemView.setOnClickListener(new View.OnClickListener() { holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {

View file

@ -10,14 +10,13 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable; import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
public class KeyProfileHolder extends RecyclerView.ViewHolder { public class KeyProfileHolder extends RecyclerView.ViewHolder implements KeyProfile.Listener {
private TextView _profileName; private TextView _profileName;
private TextView _profileCode; private TextView _profileCode;
private TextView _profileIssuer; private TextView _profileIssuer;
private ImageView _profileDrawable; private ImageView _profileDrawable;
private KeyProfile _keyProfile; private KeyProfile _profile;
private ProgressBar _progressBar; private ProgressBar _progressBar;
private Handler _uiHandler; private Handler _uiHandler;
@ -34,11 +33,15 @@ public class KeyProfileHolder extends RecyclerView.ViewHolder {
} }
public void setData(KeyProfile profile, boolean showIssuer) { public void setData(KeyProfile profile, boolean showIssuer) {
if ((_keyProfile = profile) == null) { if (profile == null) {
_profile.setListener(null);
_profile = null;
_running = false; _running = false;
return; return;
} }
_profile = profile;
profile.setListener(this);
_profileName.setText(profile.getEntry().getName()); _profileName.setText(profile.getEntry().getName());
_profileCode.setText(profile.getCode()); _profileCode.setText(profile.getCode());
_profileIssuer.setText(""); _profileIssuer.setText("");
@ -50,36 +53,34 @@ public class KeyProfileHolder extends RecyclerView.ViewHolder {
_profileDrawable.setImageDrawable(drawable); _profileDrawable.setImageDrawable(drawable);
} }
public void startUpdateLoop() { public void startRefreshLoop() {
if (_running) { if (_running) {
return; return;
} }
_running = true; _running = true;
updateCode(); _profile.refreshCode();
_uiHandler.postDelayed(new Runnable() { _uiHandler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
if (_running) { if (_running) {
updateCode(); _profile.refreshCode();
_uiHandler.postDelayed(this, _keyProfile.getEntry().getInfo().getMillisTillNextRotation()); _uiHandler.postDelayed(this, _profile.getEntry().getInfo().getMillisTillNextRotation());
} }
} }
}, _keyProfile.getEntry().getInfo().getMillisTillNextRotation()); }, _profile.getEntry().getInfo().getMillisTillNextRotation());
} }
private boolean updateCode() { @Override
public void onRefreshCode(String otp) {
// reset the progress bar // reset the progress bar
int maxProgress = _progressBar.getMax(); int maxProgress = _progressBar.getMax();
_progressBar.setProgress(maxProgress); _progressBar.setProgress(maxProgress);
// refresh the code
String otp = _keyProfile.refreshCode();
_profileCode.setText(otp.substring(0, otp.length() / 2) + " " + otp.substring(otp.length() / 2)); _profileCode.setText(otp.substring(0, otp.length() / 2) + " " + otp.substring(otp.length() / 2));
// calculate the progress the bar should start at // calculate the progress the bar should start at
long millisTillRotation = _keyProfile.getEntry().getInfo().getMillisTillNextRotation(); long millisTillRotation = _profile.getEntry().getInfo().getMillisTillNextRotation();
long period = _keyProfile.getEntry().getInfo().getPeriod() * maxProgress; long period = _profile.getEntry().getInfo().getPeriod() * maxProgress;
int currentProgress = maxProgress - (int) ((((double) period - millisTillRotation) / period) * maxProgress); int currentProgress = maxProgress - (int) ((((double) period - millisTillRotation) / period) * maxProgress);
// start progress animation // start progress animation
@ -87,6 +88,5 @@ public class KeyProfileHolder extends RecyclerView.ViewHolder {
animation.setDuration(millisTillRotation); animation.setDuration(millisTillRotation);
animation.setInterpolator(new LinearInterpolator()); animation.setInterpolator(new LinearInterpolator());
animation.start(); animation.start();
return true;
} }
} }

View file

@ -83,6 +83,10 @@ public class KeyProfileView extends Fragment implements KeyProfileAdapter.Listen
_adapter.replaceKey(profile); _adapter.replaceKey(profile);
} }
public void refresh() {
_adapter.refresh();
}
public interface Listener { public interface Listener {
void onEntryClick(KeyProfile profile); void onEntryClick(KeyProfile profile);
void onEntryMove(DatabaseEntry entry1, DatabaseEntry entry2); void onEntryMove(DatabaseEntry entry1, DatabaseEntry entry2);

View file

@ -392,8 +392,6 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
} }
private void addKey(KeyProfile profile) { private void addKey(KeyProfile profile) {
profile.refreshCode();
DatabaseEntry entry = profile.getEntry(); DatabaseEntry entry = profile.getEntry();
entry.setName(entry.getInfo().getAccountName()); entry.setName(entry.getInfo().getAccountName());
try { try {
@ -485,6 +483,9 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
setPreferredTheme(nightMode); setPreferredTheme(nightMode);
recreate(); recreate();
} }
// refresh all codes to prevent showing old ones
_keyProfileView.refresh();
} }
private BottomSheetDialog createBottomSheet(final KeyProfile profile) { private BottomSheetDialog createBottomSheet(final KeyProfile profile) {