Added timers to update

pull/41/head
Michael Schättgen 9 years ago
parent 2cd0e2660e
commit a8179865d6

@ -1,5 +1,6 @@
package me.impy.aegis;
import android.os.Handler;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
@ -7,27 +8,79 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> {
import me.impy.aegis.crypto.OTP;
public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.KeyProfileHolder> {
private ArrayList<KeyProfile> mKeyProfiles;
private final List<KeyProfileHolder> lstHolders;
private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
for (KeyProfileHolder holder : lstHolders) {
holder.updateCode();
}
}
}
};
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
TextView profileName;
TextView profileCode;
KeyProfile keyProfile;
KeyProfileHolder(View itemView) {
super(itemView);
profileName = (TextView) itemView.findViewById(R.id.profile_name);
profileCode = (TextView) itemView.findViewById(R.id.profile_code);
}
public void setData(KeyProfile profile) {
this.keyProfile = profile;
profileName.setText(profile.Name);
profileCode.setText(profile.Code);
}
public void updateCode() {
if (this.keyProfile == null) {
return;
}
String otp = "";
try {
otp = OTP.generateOTP(this.keyProfile.KeyInfo);
} catch (Exception e) {
e.printStackTrace();
}
profileCode.setText(otp.substring(0, 3) + " " + otp.substring(3));
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public KeyProfileAdapter(ArrayList<KeyProfile> keyProfiles) {
mKeyProfiles = keyProfiles;
lstHolders = new ArrayList<>();
startUpdateTimer();
}
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)
@ -44,8 +97,11 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(KeyProfileHolder holder, int position) {
holder.profileName.setText(mKeyProfiles.get(position).Name);
holder.profileCode.setText(mKeyProfiles.get(position).Code);
holder.setData(mKeyProfiles.get(position));
synchronized (lstHolders) {
lstHolders.add(holder);
}
holder.updateCode();
}
// Return the size of your dataset (invoked by the layout manager)

@ -18,27 +18,30 @@
android:layout_alignWithParentIfMissing="false"
android:gravity="bottom"
android:id="@+id/relativeLayout"
android:layout_alignParentStart="false"
android:layout_alignEnd="@+id/imageView">
android:layout_alignParentStart="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/profile_name"
android:text="Post title"
android:textSize="20sp"
android:layout_alignStart="@+id/profile_code" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/profile_code"
android:layout_below="@+id/profile_name"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
android:layout_alignParentLeft="true"
android:textSize="36sp"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/profile_name"
android:text="Post title"
android:layout_below="@+id/profile_code"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</LinearLayout>

Loading…
Cancel
Save