Aegis/app/src/main/java/me/impy/aegis/ScannerActivity.java

97 lines
3.2 KiB
Java
Raw Normal View History

2016-08-15 22:31:28 +02:00
package me.impy.aegis;
2016-08-15 22:35:42 +02:00
import android.Manifest;
2016-08-15 22:31:28 +02:00
import android.app.Activity;
import android.content.Intent;
2016-08-15 22:35:42 +02:00
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
2016-08-15 23:55:03 +02:00
import android.support.v4.util.TimeUtils;
2016-08-15 22:31:28 +02:00
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
2016-08-15 22:35:42 +02:00
import android.widget.Toast;
2016-08-15 22:31:28 +02:00
2016-08-15 22:45:33 +02:00
import com.google.zxing.BarcodeFormat;
2016-08-15 22:31:28 +02:00
import com.google.zxing.Result;
import java.io.Serializable;
2016-08-15 23:55:03 +02:00
import java.sql.Time;
2016-08-15 22:45:33 +02:00
import java.util.ArrayList;
import java.util.List;
2016-08-15 22:31:28 +02:00
import me.dm7.barcodescanner.zxing.ZXingScannerView;
2016-08-15 23:31:26 +02:00
import me.impy.aegis.crypto.KeyInfo;
2016-08-15 23:55:03 +02:00
import me.impy.aegis.crypto.TOTP;
2016-08-15 22:31:28 +02:00
public class ScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
2016-08-15 22:45:33 +02:00
mScannerView.setFormats(getSupportedFormats());
2016-08-15 23:31:26 +02:00
2016-08-15 22:35:42 +02:00
ActivityCompat.requestPermissions(ScannerActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
2016-08-15 22:31:28 +02:00
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
@Override
public void handleResult(Result rawResult) {
// Do something with the result here
2016-08-15 23:31:26 +02:00
Toast.makeText(this, rawResult.getText(), Toast.LENGTH_SHORT).show();
try {
KeyInfo info = KeyInfo.FromURL(rawResult.getText());
Intent resultIntent = new Intent();
resultIntent.putExtra("Keyinfo", info);
setResult(Activity.RESULT_OK, resultIntent);
finish();
2016-08-15 23:31:26 +02:00
} catch (Exception e) {
e.printStackTrace();
}
2016-08-15 22:31:28 +02:00
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
2016-08-15 22:31:28 +02:00
}
2016-08-15 22:35:42 +02:00
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(ScannerActivity.this, "Permission denied to get access to the camera", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
2016-08-15 22:45:33 +02:00
private List<BarcodeFormat> getSupportedFormats() {
ArrayList<BarcodeFormat> supportedFormats = new ArrayList<>();
supportedFormats.add(BarcodeFormat.QR_CODE);
return supportedFormats;
}
2016-08-15 22:31:28 +02:00
}