]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/AddTokenDialog.java
c6f781f34303896e194323f9e38cd7653885ff51
[~andy/freeotp] / src / org / fedorahosted / freeotp / AddTokenDialog.java
1 package org.fedorahosted.freeotp;
2
3 import java.util.Locale;
4
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;
15
16 public abstract class AddTokenDialog extends AlertDialog {
17         private final int SHA1_OFFSET = 1;
18         private final int TOTP_OFFSET = 0;
19
20         public AddTokenDialog(Context ctx) {
21                 super(ctx);
22
23                 setTitle(R.string.add_token);
24                 setView(getLayoutInflater().inflate(R.layout.manual, null));
25
26                 setButton(BUTTON_NEGATIVE, ctx.getString(android.R.string.cancel), new OnClickListener() {
27                         @Override
28                         public void onClick(DialogInterface dialog, int which) {
29                         }
30                 });
31
32                 setButton(BUTTON_POSITIVE, ctx.getString(R.string.add), new OnClickListener() {
33                         @Override
34                         public void onClick(DialogInterface dialog, int which) {
35                                 // Get the fields
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;
43
44                                 // Create the URI
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));
49                                 else
50                                         uri = uri.concat(String.format("&counter=%d", interval));
51
52                                 // Add the token
53                                 addToken(uri);
54                         }
55                 });
56         }
57
58         @Override
59         public void onAttachedToWindow() {
60                 super.onAttachedToWindow();
61
62                 // Disable the Add button
63                 getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
64
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));
70
71                 // Select the default algorithm
72                 ((Spinner) findViewById(R.id.algorithm)).setSelection(SHA1_OFFSET);
73
74                 // Setup the Interval / Counter toggle
75                 ((Spinner) findViewById(R.id.type)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
76                         @Override
77                         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
78                                 if (position == 0) {
79                                         ((TextView) findViewById(R.id.interval_label)).setText(R.string.interval);
80                                         ((EditText) findViewById(R.id.interval)).setText("30");
81                                 } else {
82                                         ((TextView) findViewById(R.id.interval_label)).setText(R.string.counter);
83                                         ((EditText) findViewById(R.id.interval)).setText("0");
84                                 }
85                         }
86
87                         @Override
88                         public void onNothingSelected(AdapterView<?> parent) {
89
90                         }
91                 });
92
93         }
94
95         public abstract void addToken(String uri);
96 }