Skip to content

Commit

Permalink
Merge pull request frankmorgner#187 from ph4r05/pr/002-upgrade-android
Browse files Browse the repository at this point in the history
Improve remote-reader
  • Loading branch information
frankmorgner committed Feb 25, 2021
2 parents 0540c45 + dc314e7 commit 1eaf5a1
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 52 deletions.
24 changes: 12 additions & 12 deletions remote-reader/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 29
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.vsmartcard.remotesmartcardreader.app"
// NFC reader mode was added in KitKat
minSdkVersion 19
targetSdkVersion 23
targetSdkVersion 29
versionCode 6
versionName "2.2"
}
Expand Down Expand Up @@ -38,7 +38,7 @@ android.applicationVariants.all { variant ->
} else {
newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk"
}
output.outputFile = new File(output.outputFile.parent, newApkName)
//output.outputFile = new File(output.outputFile.parent, newApkName)
}
}

Expand All @@ -47,12 +47,12 @@ repositories {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-v4:23.2.1'

compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
implementation 'com.google.zxing:core:3.2.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
Expand All @@ -42,6 +41,8 @@
import android.view.ViewGroup;
import android.widget.ScrollView;

import androidx.fragment.app.Fragment;

/**
* Simple fraggment which contains a LogView and uses is to output log data it receives
* through the LogNode interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;

/**
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
* to be used with AppCompat.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.android.common.logger.Log;
import com.example.android.common.logger.LogFragment;
import com.example.android.common.logger.LogWrapper;
import com.example.android.common.logger.MessageOnlyLogFilter;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.vsmartcard.remotesmartcardreader.app.screaders.*;

@TargetApi(Build.VERSION_CODES.KITKAT)
Expand Down Expand Up @@ -197,11 +198,12 @@ public void onTagDiscovered(Tag tag) {
}

private void vpcdConnect(SCReader scReader) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(this);
int port = Integer.parseInt(SP.getString("port", Integer.toString(VPCDWorker.DEFAULT_PORT)));
String hostname = SP.getString("hostname", VPCDWorker.DEFAULT_HOSTNAME);
final SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(this);
final int port = Integer.parseInt(SP.getString("port", Integer.toString(VPCDWorker.DEFAULT_PORT)));
final String hostname = SP.getString("hostname", VPCDWorker.DEFAULT_HOSTNAME);
final boolean listen = SP.getBoolean("listen", VPCDWorker.DEFAULT_LISTEN);
vpcdTest = new VPCDWorker();
vpcdTest.execute(new VPCDWorker.VPCDWorkerParams(hostname, port, scReader));
vpcdTest.execute(new VPCDWorker.VPCDWorkerParams(hostname, port, scReader, listen));
}

private void vpcdDisconnect() {
Expand Down Expand Up @@ -231,6 +233,7 @@ public void onResume() {
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
super.onNewIntent(intent);
setIntent(intent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.example.android.common.logger.LogFragment;
import com.google.android.material.snackbar.Snackbar;

public class MyLogFragment extends LogFragment {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.view.MenuItem;

import androidx.appcompat.app.ActionBar;

import com.google.android.material.snackbar.Snackbar;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,49 @@
package com.vsmartcard.remotesmartcardreader.app;

import android.os.AsyncTask;
import android.support.annotation.Nullable;

import androidx.annotation.Nullable;

import com.example.android.common.logger.Log;
import com.vsmartcard.remotesmartcardreader.app.screaders.SCReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

class VPCDWorker extends AsyncTask<VPCDWorker.VPCDWorkerParams, Void, Void> {

public static class VPCDWorkerParams {
final String hostname;
final int port;
final SCReader reader;
VPCDWorkerParams(String hostname, int port, SCReader reader) {
final boolean listen;
VPCDWorkerParams(String hostname, int port, SCReader reader, boolean listen) {
this.hostname = hostname;
this.port = port;
this.reader = reader;
this.listen = listen;
}
}

public static final int DEFAULT_PORT = 35963;
// default URI when used in emulator
public static final String DEFAULT_HOSTNAME = "10.0.2.2";
public static final boolean DEFAULT_LISTEN = false;

private SCReader reader;
private ServerSocket listenSocket;
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
Expand All @@ -74,15 +88,22 @@ protected void onCancelled () {
public Void doInBackground(VPCDWorkerParams... params) {
try {
reader = params[0].reader;
Log.i(this.getClass().getName(), "Connecting to " + params[0].hostname + ":" + Integer.toString(params[0].port) + "...");
vpcdConnect(params[0].hostname, params[0].port);
Log.i(this.getClass().getName(), "Connected to VPCD");
vpcdConnection(params[0]);

while (!isCancelled()) {
vpcdAccept();
byte[] out = null;
byte[] in = receiveFromVPCD();
if (in == null)
break;
if (in == null) {
if (listenSocket == null) {
Log.i(this.getClass().getName(), "End of stream, finishing");
break;
} else {
Log.i(this.getClass().getName(), "End of stream, closing connection");
vpcdCloseClient();
continue; // back to accept
}
}

if (in.length == VPCD_CTRL_LEN) {
switch (in[0]) {
Expand Down Expand Up @@ -168,7 +189,79 @@ private void sendToVPCD(byte[] data) throws IOException {
outputStream.flush();
}

private void vpcdConnection(VPCDWorkerParams params) throws IOException {
if (params.listen){
vpcdListen(params.port);
return;
}

Log.i(this.getClass().getName(), "Connecting to " + params.hostname + ":" + Integer.toString(params.port) + "...");
vpcdConnect(params.hostname, params.port);
Log.i(this.getClass().getName(), "Connected to VPCD");
}

private void vpcdListen(int port) throws IOException {
listenSocket = new ServerSocket(port);

final List<String> ifaceAddresses = new LinkedList<>();
final Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while(ifaces.hasMoreElements()){
final NetworkInterface iface = ifaces.nextElement();
if (!iface.isUp() || iface.isLoopback() || iface.isVirtual()) {
continue;
}
for(InterfaceAddress addr : iface.getInterfaceAddresses()){
final InetAddress inetAddr = addr.getAddress();
ifaceAddresses.add(inetAddr.getHostAddress());
}
}

Log.i(this.getClass().getName(), "Listening on port " + port + ". Local addresses: " + join(", ", ifaceAddresses));
}

private void vpcdAccept() throws IOException {
if(listenSocket == null){
return;
}

if (socket != null){
return; // Already accepted, only one client allowed
}

Log.i(this.getClass().getName(),"Waiting for connections...");
while(!isCancelled()) {
listenSocket.setSoTimeout(1000);
try {
socket = listenSocket.accept();
} catch (SocketTimeoutException ignored){}
if (socket != null){
break;
}
}

Log.i(this.getClass().getName(),"Connected, " + socket.getInetAddress());
listenSocket.setSoTimeout(0);
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
}

private void vpcdCloseClient(){
try {
outputStream.close();
} catch (IOException ignored) { }
try {
inputStream.close();
} catch (IOException ignored) { }
try {
socket.close();
} catch (IOException ignored) { }
outputStream = null;
inputStream = null;
socket = null;
}

private void vpcdConnect(String hostname, int port) throws IOException {
listenSocket = null;
socket = new Socket(InetAddress.getByName(hostname), port);
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
Expand All @@ -182,5 +275,25 @@ private void vpcdDisconnect() throws IOException {
socket.close();
Log.i(this.getClass().getName(), "Disconnected from VPCD");
}
if (listenSocket != null) {
Log.i(this.getClass().getName(), "Closing listening socket");
listenSocket.close();
}
}

/**
* Usage of API level 24+ would allow streams(), join can be removed.
*/
private static String join(String separator, List<String> input) {
if (input == null || input.size() <= 0) return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.size(); i++) {
sb.append(input.get(i));
if (i != input.size() - 1) {
sb.append(separator);
}
}
return sb.toString();

}
}
Loading

0 comments on commit 1eaf5a1

Please sign in to comment.