Scanner now passes result correctly to MainActivity

This commit is contained in:
Michael Schättgen 2016-08-16 00:08:01 +02:00
parent 745e5c13b6
commit b14eb42b12
4 changed files with 49 additions and 5 deletions

View file

@ -5,22 +5,48 @@ import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.Serializable;
import me.impy.aegis.crypto.KeyInfo;
import me.impy.aegis.crypto.TOTP;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
static final int GET_KEYINFO = 1;
Button btnScan; Button btnScan;
TextView tvTotp;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.button); btnScan = (Button) findViewById(R.id.button);
tvTotp = (TextView) findViewById(R.id.textView2);
btnScan.setOnClickListener(new View.OnClickListener() { btnScan.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Intent scannerActivity = new Intent(getApplicationContext(), ScannerActivity.class); Intent scannerActivity = new Intent(getApplicationContext(), ScannerActivity.class);
startActivity(scannerActivity); startActivityForResult(scannerActivity, GET_KEYINFO);
} }
}); });
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == GET_KEYINFO) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
KeyInfo info = (KeyInfo)data.getSerializableExtra("Keyinfo");
String nowTimeString = (Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod()));
String totp = TOTP.generateTOTP(info.getSecret(), nowTimeString, info.getDigits(), info.getAlgo());
tvTotp.setText(totp);
}
}
}
} }

View file

@ -2,6 +2,7 @@ package me.impy.aegis;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.util.TimeUtils; import android.support.v4.util.TimeUtils;
@ -13,6 +14,7 @@ import android.widget.Toast;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result; import com.google.zxing.Result;
import java.io.Serializable;
import java.sql.Time; import java.sql.Time;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -53,14 +55,19 @@ public class ScannerActivity extends Activity implements ZXingScannerView.Result
try { try {
KeyInfo info = KeyInfo.FromURL(rawResult.getText()); KeyInfo info = KeyInfo.FromURL(rawResult.getText());
String nowTimeString = (Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod())); //String nowTimeString = (Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod()));
Toast.makeText(this, TOTP.generateTOTP(info.getSecret(), nowTimeString, info.getDigits(), info.getAlgo()), Toast.LENGTH_LONG).show(); //Toast.makeText(this, TOTP.generateTOTP(info.getSecret(), nowTimeString, info.getDigits(), info.getAlgo()), Toast.LENGTH_LONG).show();
Intent resultIntent = new Intent();
resultIntent.putExtra("Keyinfo", info);
setResult(Activity.RESULT_OK, resultIntent);
finish();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// If you would like to resume scanning, call this method below: // If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this); //mScannerView.resumeCameraPreview(this);
} }
@Override @Override

View file

@ -2,9 +2,11 @@ package me.impy.aegis.crypto;
import android.net.Uri; import android.net.Uri;
import java.io.Serializable;
import me.impy.aegis.encoding.Base32; import me.impy.aegis.encoding.Base32;
public class KeyInfo { public class KeyInfo implements Serializable {
private String type; private String type;
private String label; private String label;
private byte[] secret; private byte[] secret;

View file

@ -17,4 +17,13 @@
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/> android:layout_alignParentStart="true"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout> </RelativeLayout>