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.Locale;
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;
36 public abstract class AddTokenDialog extends AlertDialog {
37 private final int SHA1_OFFSET = 1;
38 private final int TOTP_OFFSET = 0;
40 public AddTokenDialog(Context ctx) {
43 setTitle(R.string.add_token);
44 setView(getLayoutInflater().inflate(R.layout.manual, null));
46 setButton(BUTTON_NEGATIVE, ctx.getString(android.R.string.cancel), new OnClickListener() {
48 public void onClick(DialogInterface dialog, int which) {
52 setButton(BUTTON_POSITIVE, ctx.getString(R.string.add), new OnClickListener() {
54 public void onClick(DialogInterface dialog, int which) {
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;
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));
70 uri = uri.concat(String.format("&counter=%d", interval));
79 public void onAttachedToWindow() {
80 super.onAttachedToWindow();
82 // Disable the Add button
83 getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
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));
91 // Select the default algorithm
92 ((Spinner) findViewById(R.id.algorithm)).setSelection(SHA1_OFFSET);
94 // Setup the Interval / Counter toggle
95 ((Spinner) findViewById(R.id.type)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
97 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
99 ((TextView) findViewById(R.id.interval_label)).setText(R.string.interval);
100 ((EditText) findViewById(R.id.interval)).setText("30");
102 ((TextView) findViewById(R.id.interval_label)).setText(R.string.counter);
103 ((EditText) findViewById(R.id.interval)).setText("0");
108 public void onNothingSelected(AdapterView<?> parent) {
115 public abstract void addToken(String uri);