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 {
private String _code;
private DatabaseEntry _entry;
private Listener _listener;
public KeyProfile() {
this(new DatabaseEntry());
@ -22,6 +23,10 @@ public class KeyProfile implements Serializable {
_entry = entry;
}
public void setListener(Listener listener) {
_listener = listener;
}
public DatabaseEntry getEntry() {
return _entry;
}
@ -35,10 +40,17 @@ public class KeyProfile implements Serializable {
} catch (Exception e) {
throw new UndeclaredThrowableException(e);
}
if (_listener != null) {
_listener.onRefreshCode(_code);
}
return _code;
}
public TextDrawable getDrawable() {
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);
}
public void refresh() {
for (KeyProfile profile : _keyProfiles) {
profile.refreshCode();
}
}
private KeyProfile getKeyByID(long id) {
for (KeyProfile profile : _keyProfiles) {
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) {
final KeyProfile profile = _keyProfiles.get(position);
holder.setData(profile, _showIssuer);
holder.startUpdateLoop();
holder.startRefreshLoop();
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View file

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

View file

@ -83,6 +83,10 @@ public class KeyProfileView extends Fragment implements KeyProfileAdapter.Listen
_adapter.replaceKey(profile);
}
public void refresh() {
_adapter.refresh();
}
public interface Listener {
void onEntryClick(KeyProfile profile);
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) {
profile.refreshCode();
DatabaseEntry entry = profile.getEntry();
entry.setName(entry.getInfo().getAccountName());
try {
@ -485,6 +483,9 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
setPreferredTheme(nightMode);
recreate();
}
// refresh all codes to prevent showing old ones
_keyProfileView.refresh();
}
private BottomSheetDialog createBottomSheet(final KeyProfile profile) {