mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-04-22 06:49:12 +00:00
Fire off the updateCode tasks at exactly the right time
This commit is contained in:
parent
59402d30ff
commit
9aa4e35203
2 changed files with 26 additions and 32 deletions
|
@ -1,16 +1,12 @@
|
||||||
package me.impy.aegis;
|
package me.impy.aegis;
|
||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.support.v7.widget.CardView;
|
|
||||||
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.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import java.security.Key;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
|
@ -21,18 +17,8 @@ import me.impy.aegis.crypto.OTP;
|
||||||
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> {
|
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> {
|
||||||
private ArrayList<KeyProfile> mKeyProfiles;
|
private ArrayList<KeyProfile> mKeyProfiles;
|
||||||
private final List<KeyProfileHolder> lstHolders;
|
private final List<KeyProfileHolder> lstHolders;
|
||||||
|
private Timer timer;
|
||||||
private Handler mHandler = new Handler();
|
private Handler uiHandler;
|
||||||
private Runnable updateRemainingTimeRunnable = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
synchronized (lstHolders) {
|
|
||||||
for (KeyProfileHolder holder : lstHolders) {
|
|
||||||
holder.updateCode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
|
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
|
||||||
TextView profileName;
|
TextView profileName;
|
||||||
|
@ -69,18 +55,8 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
|
||||||
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
|
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
|
||||||
mKeyProfiles = keyProfiles;
|
mKeyProfiles = keyProfiles;
|
||||||
lstHolders = new ArrayList<>();
|
lstHolders = new ArrayList<>();
|
||||||
startUpdateTimer();
|
timer = new Timer();
|
||||||
}
|
uiHandler = new Handler();
|
||||||
|
|
||||||
private void startUpdateTimer() {
|
|
||||||
Timer timer = new Timer();
|
|
||||||
timer.schedule(new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mHandler.post(updateRemainingTimeRunnable);
|
|
||||||
}
|
|
||||||
//TODO: Replace delay with seconds that are left
|
|
||||||
}, 0, 5000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new views (invoked by the layout manager)
|
// Create new views (invoked by the layout manager)
|
||||||
|
@ -96,12 +72,25 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
|
||||||
|
|
||||||
// Replace the contents of a view (invoked by the layout manager)
|
// Replace the contents of a view (invoked by the layout manager)
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(KeyProfileHolder holder, int position) {
|
public void onBindViewHolder(final KeyProfileHolder holder, int position) {
|
||||||
holder.setData(mKeyProfiles.get(position));
|
holder.setData(mKeyProfiles.get(position));
|
||||||
synchronized (lstHolders) {
|
|
||||||
lstHolders.add(holder);
|
|
||||||
}
|
|
||||||
holder.updateCode();
|
holder.updateCode();
|
||||||
|
lstHolders.add(holder);
|
||||||
|
|
||||||
|
timer.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
uiHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// check if this key profile still exists
|
||||||
|
if (lstHolders.contains(holder)) {
|
||||||
|
holder.updateCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, holder.keyProfile.KeyInfo.getMillisTillNextRotation(), holder.keyProfile.KeyInfo.getPeriod() * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the size of your dataset (invoked by the layout manager)
|
// Return the size of your dataset (invoked by the layout manager)
|
||||||
|
|
|
@ -43,6 +43,11 @@ public class KeyInfo implements Serializable {
|
||||||
|
|
||||||
private KeyInfo() { }
|
private KeyInfo() { }
|
||||||
|
|
||||||
|
public long getMillisTillNextRotation() {
|
||||||
|
long p = period * 1000;
|
||||||
|
return p - (System.currentTimeMillis() % p);
|
||||||
|
}
|
||||||
|
|
||||||
public static KeyInfo FromURL(String s) throws Exception {
|
public static KeyInfo FromURL(String s) throws Exception {
|
||||||
final Uri url = Uri.parse(s);
|
final Uri url = Uri.parse(s);
|
||||||
if (!url.getScheme().equals("otpauth")) {
|
if (!url.getScheme().equals("otpauth")) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue