4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 package org.fedorahosted.freeotp;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.List;
30 import org.fedorahosted.freeotp.Token.TokenUriInvalidException;
32 import android.app.AlertDialog;
33 import android.content.Context;
34 import android.content.DialogInterface;
35 import android.os.Handler;
36 import android.os.Message;
37 import android.view.View;
38 import android.view.View.OnClickListener;
39 import android.view.ViewGroup;
40 import android.widget.BaseAdapter;
41 import android.widget.ImageButton;
42 import android.widget.ProgressBar;
43 import android.widget.TextView;
45 public class TokenAdapter extends BaseAdapter {
46 private static class Ticker extends Handler {
47 private static interface OnTickListener {
48 public void tick(ProgressBar pb);
51 private final Map<ProgressBar, OnTickListener> map = new HashMap<ProgressBar, OnTickListener>();
54 public void handleMessage(Message msg) {
55 for (ProgressBar pb : map.keySet())
58 sendEmptyMessageDelayed(0, 200);
61 public void set(ProgressBar pb, OnTickListener otl) {
66 private final List<Token> tokens = new ArrayList<Token>();
67 private final Ticker ticker = new Ticker();
70 Collections.sort(tokens, new Comparator<Token>() {
72 public int compare(Token lhs, Token rhs) {
73 return lhs.getTitle().compareTo(rhs.getTitle());
78 public TokenAdapter(Context ctx) {
79 tokens.addAll(Token.getTokens(ctx));
80 ticker.sendEmptyMessageDelayed(0, 200);
85 public int getCount() {
90 public Token getItem(int position) {
91 return tokens.get(position);
95 public long getItemId(int position) {
100 public View getView(int position, View convertView, ViewGroup parent) {
101 final Context ctx = parent.getContext();
103 if (convertView == null) {
104 switch (getItem(position).getType()) {
106 convertView = View.inflate(ctx, R.layout.hotp, null);
110 convertView = View.inflate(ctx, R.layout.totp, null);
115 final Token item = getItem(position);
116 final TextView code = (TextView) convertView.findViewById(R.id.code);
117 final TextView title = (TextView) convertView.findViewById(R.id.title);
118 final ImageButton ib = (ImageButton) convertView.findViewById(R.id.button);
120 code.setText(item.getCurrentTokenValue(ctx, false));
121 title.setText(item.getTitle());
123 ib.setOnClickListener(new OnClickListener() {
125 public void onClick(View v) {
126 String delmsg = ctx.getString(R.string.delete_message);
128 AlertDialog ad = new AlertDialog.Builder(ctx)
130 .setMessage(delmsg + item.getTitle())
131 .setIcon(android.R.drawable.ic_delete)
132 .setPositiveButton(R.string.delete,
133 new DialogInterface.OnClickListener() {
135 public void onClick(DialogInterface dialog, int which) {
136 tokens.remove(tokens.indexOf(item));
138 notifyDataSetChanged();
143 .setNegativeButton(android.R.string.cancel,
144 new DialogInterface.OnClickListener() {
146 public void onClick(DialogInterface dialog, int which) {
154 switch (getItem(position).getType()) {
156 ImageButton hotp = (ImageButton) convertView.findViewById(R.id.hotpButton);
157 hotp.setOnClickListener(new OnClickListener() {
159 public void onClick(View v) {
160 code.setText(item.getCurrentTokenValue(ctx, true));
166 ProgressBar pb = (ProgressBar) convertView.findViewById(R.id.totpProgressBar);
167 ticker.set(pb, new Ticker.OnTickListener() {
169 public void tick(ProgressBar pb) {
170 int max = pb.getMax();
171 int pro = item.getProgress();
172 pb.setProgress(max - pro);
173 if (pro < max / 20 || pro > max / 20 * 19)
174 code.setText(item.getCurrentTokenValue(ctx, false));
184 public int getViewTypeCount() {
189 public int getItemViewType(int position) {
190 switch (getItem(position).getType()) {
200 public void add(Context ctx, String uri) throws TokenUriInvalidException {
201 Token t = new Token(uri);
205 notifyDataSetChanged();