1 package org.fedorahosted.freeotp;
3 import java.util.Locale;
5 import android.app.AlertDialog;
6 import android.content.Context;
7 import android.content.DialogInterface;
8 import android.net.Uri;
9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.EditText;
12 import android.widget.RadioButton;
13 import android.widget.Spinner;
14 import android.widget.TextView;
16 public abstract class AddTokenDialog extends AlertDialog {
17 private final int SHA1_OFFSET = 1;
18 private final int TOTP_OFFSET = 0;
20 public AddTokenDialog(Context ctx) {
23 setTitle(R.string.add_token);
24 setView(getLayoutInflater().inflate(R.layout.manual, null));
26 setButton(BUTTON_NEGATIVE, ctx.getString(android.R.string.cancel), new OnClickListener() {
28 public void onClick(DialogInterface dialog, int which) {
32 setButton(BUTTON_POSITIVE, ctx.getString(R.string.add), new OnClickListener() {
34 public void onClick(DialogInterface dialog, int which) {
36 String issuer = Uri.encode(((EditText) findViewById(R.id.issuer)).getText().toString());
37 String id = Uri.encode(((EditText) findViewById(R.id.id)).getText().toString());
38 String secret = Uri.encode(((EditText) findViewById(R.id.secret)).getText().toString());
39 String type = ((Spinner) findViewById(R.id.type)).getSelectedItemId() == TOTP_OFFSET ? "totp" : "hotp";
40 String algorithm = ((Spinner) findViewById(R.id.algorithm)).getSelectedItem().toString().toLowerCase(Locale.US);
41 int interval = Integer.parseInt(((EditText) findViewById(R.id.interval)).getText().toString());
42 int digits = ((RadioButton) findViewById(R.id.digits6)).isChecked() ? 6 : 8;
45 String uri = String.format(Locale.US, "otpauth://%s/%s:%s?secret=%s&algorithm=%s&digits=%d",
46 type, issuer, id, secret, algorithm, digits);
47 if (type.equals("totp"))
48 uri = uri.concat(String.format("&period=%d", interval));
50 uri = uri.concat(String.format("&counter=%d", interval));
59 public void onAttachedToWindow() {
60 super.onAttachedToWindow();
62 // Disable the Add button
63 getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
65 // Set constraints on when the Add button is enabled
66 ((EditText) findViewById(R.id.issuer)).addTextChangedListener(new AddTokenTextWatcher(this));
67 ((EditText) findViewById(R.id.id)).addTextChangedListener(new AddTokenTextWatcher(this));
68 ((EditText) findViewById(R.id.secret)).addTextChangedListener(new AddTokenSecretTextWatcher(this));
69 ((EditText) findViewById(R.id.interval)).addTextChangedListener(new AddTokenTextWatcher(this));
71 // Select the default algorithm
72 ((Spinner) findViewById(R.id.algorithm)).setSelection(SHA1_OFFSET);
74 // Setup the Interval / Counter toggle
75 ((Spinner) findViewById(R.id.type)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
77 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
79 ((TextView) findViewById(R.id.interval_label)).setText(R.string.interval);
80 ((EditText) findViewById(R.id.interval)).setText("30");
82 ((TextView) findViewById(R.id.interval_label)).setText(R.string.counter);
83 ((EditText) findViewById(R.id.interval)).setText("0");
88 public void onNothingSelected(AdapterView<?> parent) {
95 public abstract void addToken(String uri);