]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/AddTokenDialog.java
Relicense to Apache 2.0
[~andy/freeotp] / src / org / fedorahosted / freeotp / AddTokenDialog.java
1 /*
2  * FreeOTP
3  *
4  * Authors: Nathaniel McCallum <npmccallum@redhat.com>
5  *
6  * Copyright (C) 2013  Nathaniel McCallum, Red Hat
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 package org.fedorahosted.freeotp;
22
23 import java.util.Locale;
24
25 import android.app.AlertDialog;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.net.Uri;
29 import android.view.View;
30 import android.widget.AdapterView;
31 import android.widget.EditText;
32 import android.widget.RadioButton;
33 import android.widget.Spinner;
34 import android.widget.TextView;
35
36 public abstract class AddTokenDialog extends AlertDialog {
37         private final int SHA1_OFFSET = 1;
38         private final int TOTP_OFFSET = 0;
39
40         public AddTokenDialog(Context ctx) {
41                 super(ctx);
42
43                 setTitle(R.string.add_token);
44                 setView(getLayoutInflater().inflate(R.layout.manual, null));
45
46                 setButton(BUTTON_NEGATIVE, ctx.getString(android.R.string.cancel), new OnClickListener() {
47                         @Override
48                         public void onClick(DialogInterface dialog, int which) {
49                         }
50                 });
51
52                 setButton(BUTTON_POSITIVE, ctx.getString(R.string.add), new OnClickListener() {
53                         @Override
54                         public void onClick(DialogInterface dialog, int which) {
55                                 // Get the fields
56                                 String issuer = Uri.encode(((EditText) findViewById(R.id.issuer)).getText().toString());
57                                 String id = Uri.encode(((EditText) findViewById(R.id.id)).getText().toString());
58                                 String secret = Uri.encode(((EditText) findViewById(R.id.secret)).getText().toString());
59                                 String type = ((Spinner) findViewById(R.id.type)).getSelectedItemId() == TOTP_OFFSET ? "totp" : "hotp";
60                                 String algorithm = ((Spinner) findViewById(R.id.algorithm)).getSelectedItem().toString().toLowerCase(Locale.US);
61                                 int interval = Integer.parseInt(((EditText) findViewById(R.id.interval)).getText().toString());
62                                 int digits = ((RadioButton) findViewById(R.id.digits6)).isChecked() ? 6 : 8;
63
64                                 // Create the URI
65                                 String uri = String.format(Locale.US, "otpauth://%s/%s:%s?secret=%s&algorithm=%s&digits=%d",
66                                            type, issuer, id, secret, algorithm, digits);
67                                 if (type.equals("totp"))
68                                         uri = uri.concat(String.format("&period=%d", interval));
69                                 else
70                                         uri = uri.concat(String.format("&counter=%d", interval));
71
72                                 // Add the token
73                                 addToken(uri);
74                         }
75                 });
76         }
77
78         @Override
79         public void onAttachedToWindow() {
80                 super.onAttachedToWindow();
81
82                 // Disable the Add button
83                 getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
84
85                 // Set constraints on when the Add button is enabled
86                 ((EditText) findViewById(R.id.issuer)).addTextChangedListener(new AddTokenTextWatcher(this));
87                 ((EditText) findViewById(R.id.id)).addTextChangedListener(new AddTokenTextWatcher(this));
88                 ((EditText) findViewById(R.id.secret)).addTextChangedListener(new AddTokenSecretTextWatcher(this));
89                 ((EditText) findViewById(R.id.interval)).addTextChangedListener(new AddTokenTextWatcher(this));
90
91                 // Select the default algorithm
92                 ((Spinner) findViewById(R.id.algorithm)).setSelection(SHA1_OFFSET);
93
94                 // Setup the Interval / Counter toggle
95                 ((Spinner) findViewById(R.id.type)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
96                         @Override
97                         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
98                                 if (position == 0) {
99                                         ((TextView) findViewById(R.id.interval_label)).setText(R.string.interval);
100                                         ((EditText) findViewById(R.id.interval)).setText("30");
101                                 } else {
102                                         ((TextView) findViewById(R.id.interval_label)).setText(R.string.counter);
103                                         ((EditText) findViewById(R.id.interval)).setText("0");
104                                 }
105                         }
106
107                         @Override
108                         public void onNothingSelected(AdapterView<?> parent) {
109
110                         }
111                 });
112
113         }
114
115         public abstract void addToken(String uri);
116 }