diff --git a/app/src/main/java/me/impy/aegis/KeyProfile.java b/app/src/main/java/me/impy/aegis/KeyProfile.java index aff6839f..e89af775 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfile.java +++ b/app/src/main/java/me/impy/aegis/KeyProfile.java @@ -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); + } } diff --git a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java index ce730cd4..904f5039 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileAdapter.java @@ -54,6 +54,12 @@ public class KeyProfileAdapter extends RecyclerView.Adapter 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 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) { diff --git a/app/src/main/java/me/impy/aegis/KeyProfileHolder.java b/app/src/main/java/me/impy/aegis/KeyProfileHolder.java index 1a8f6c65..54c04b43 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileHolder.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileHolder.java @@ -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; } } diff --git a/app/src/main/java/me/impy/aegis/KeyProfileView.java b/app/src/main/java/me/impy/aegis/KeyProfileView.java index 214834f3..c5bbbb10 100644 --- a/app/src/main/java/me/impy/aegis/KeyProfileView.java +++ b/app/src/main/java/me/impy/aegis/KeyProfileView.java @@ -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); diff --git a/app/src/main/java/me/impy/aegis/MainActivity.java b/app/src/main/java/me/impy/aegis/MainActivity.java index e990a751..1c3537f5 100644 --- a/app/src/main/java/me/impy/aegis/MainActivity.java +++ b/app/src/main/java/me/impy/aegis/MainActivity.java @@ -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) {