package it.labconsulenze.beta.urbano.activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import at.nineyards.anyline.core.LicenseException;
import io.anyline.AnylineSDK;
import io.anyline.plugin.ScanResult;
import io.anyline.plugin.ScanResultListener;
import io.anyline.plugin.id.ID;
import io.anyline.plugin.id.IdScanViewPlugin;
import io.anyline.plugin.id.Identification;
import io.anyline.view.ScanView;
import it.labconsulenze.beta.urbano.R;
import it.labconsulenze.beta.urbano.core.objects.Setting;
public class ScanPatenteActivity extends AppCompatActivity implements ScanResultListener<ScanResult<ID>> {
protected TextView resultText;
protected ScanView drivingLicenseScanView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set the flag to keep the screen on (otherwise the screen may go dark during scanning)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_scan_patente);
// Get the view from the layout
drivingLicenseScanView = findViewById(R.id.scan_view);
resultText = findViewById(R.id.result_text);
SharedPreferences versionSetting = ScanPatenteActivity.this.getSharedPreferences(Setting.SETTING_DEFAULT, Context.MODE_PRIVATE);
boolean anyLineEnabled = versionSetting.getBoolean(Setting.ANYLINE_ENABLED, false);
if (!anyLineEnabled) {
Intent dataIntent = new Intent();
dataIntent.putExtra("caller", "ScanPatente");
dataIntent.putExtra("resultDataKeys", "");
dataIntent.putExtra("resultDataValues", "");
setResult(RESULT_OK, dataIntent);
finish();
return;
} else {
try {
String license = versionSetting.getString(Setting.ANYLINE_CODE, "");
AnylineSDK.init(license, this);
} catch (LicenseException e) {
e.printStackTrace();
}
}
drivingLicenseScanView = findViewById(R.id.scan_view);
drivingLicenseScanView.init("universal_id_driver_view_config.json");
IdScanViewPlugin scanViewPlugin = (IdScanViewPlugin) drivingLicenseScanView.getScanViewPlugin();
scanViewPlugin.addScanResultListener(this);
drivingLicenseScanView.setScanViewPlugin(scanViewPlugin);
drivingLicenseScanView.start();
}
@Override
protected void onResume() {
super.onResume();
//TODO
}
@Override
protected void onPause() {
super.onPause();
drivingLicenseScanView.stop();
drivingLicenseScanView.releaseCameraInBackground();
}
@Override
public void onResult(ScanResult<ID> idScanResult) {
Identification identification = (Identification) idScanResult.getResult();
HashMap<String, String> data = (HashMap<String, String>) identification.getResultData();
Set<String> setKeys = data.keySet();
String[] arrayKeys = setKeys.toArray(new String[setKeys.size()]);
Collection<String> values = data.values();
String[] arrayValues = values.toArray(new String[values.size()]);
Intent dataIntent = new Intent();
dataIntent.putExtra("caller", "ScanPatente");
dataIntent.putExtra("resultDataKeys", arrayKeys);
dataIntent.putExtra("resultDataValues", arrayValues);
setResult(RESULT_OK, dataIntent);
finish();
}
}