AusweisApp2/src/aidl/AidlBinder.java

164 lines
3.4 KiB
Java
Raw Normal View History

2017-12-20 14:54:05 +01:00
/*
2018-03-28 15:10:51 +02:00
* \copyright Copyright (c) 2016-2018 Governikus GmbH & Co. KG, Germany
2017-12-20 14:54:05 +01:00
*/
2017-07-03 09:30:10 +02:00
package com.governikus.ausweisapp2;
2018-03-28 15:10:51 +02:00
import org.qtproject.qt5.android.QtNative;
2017-12-20 14:54:05 +01:00
import android.content.Intent;
import android.nfc.NfcAdapter;
2017-07-03 09:30:10 +02:00
import android.nfc.Tag;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.util.Log;
2017-12-20 14:54:05 +01:00
2017-07-03 09:30:10 +02:00
class AidlBinder extends IAusweisApp2Sdk.Stub
{
2018-03-28 15:10:51 +02:00
private static final String LOG_TAG = AusweisApp2Service.LOG_TAG;
private IAusweisApp2SdkCallback mCallback;
private String mCallbackSessionId;
2017-07-03 09:30:10 +02:00
private void cleanUpDeadCallback()
{
if (mCallback == null)
{
return;
}
IBinder binder = mCallback.asBinder();
if (!binder.isBinderAlive() || !binder.pingBinder())
{
Log.i(LOG_TAG, "Android service: Removing dead callback.");
mCallback = null;
}
}
2017-09-15 10:23:30 +02:00
private void handleClientException(Throwable pException)
2017-07-03 09:30:10 +02:00
{
2017-09-15 10:23:30 +02:00
Log.w(LOG_TAG, "Android service: Connected client sent an exception. Dropping client.", pException);
2017-07-03 09:30:10 +02:00
mCallback = null;
}
public synchronized boolean connectSdk(IAusweisApp2SdkCallback pCallback)
{
if (pCallback == null)
{
Log.w(LOG_TAG, "Android service: Supplied callback is null.");
return false;
}
cleanUpDeadCallback();
if (mCallback != null)
{
2017-09-15 10:23:30 +02:00
Log.i(LOG_TAG, "Android service: A client is already connected. Dropping previous callback.");
try
{
mCallbackSessionId = null;
mCallback.sdkDisconnected();
}
catch (Throwable t)
{
handleClientException(t);
}
2017-07-03 09:30:10 +02:00
}
2017-09-15 10:23:30 +02:00
mCallbackSessionId = resetValidSessionID();
if (mCallbackSessionId.isEmpty())
{
return false;
}
2017-07-03 09:30:10 +02:00
mCallback = pCallback;
2017-09-15 10:23:30 +02:00
final boolean sessionIdIsSecure = isSecureRandomPsk();
2017-07-03 09:30:10 +02:00
Log.i(LOG_TAG, "Android service: Callback connected.");
try
{
2017-09-15 10:23:30 +02:00
mCallback.sessionIdGenerated(sessionIdIsSecure ? mCallbackSessionId : null, sessionIdIsSecure);
2017-07-03 09:30:10 +02:00
}
2017-09-15 10:23:30 +02:00
catch (Throwable t)
2017-07-03 09:30:10 +02:00
{
2017-09-15 10:23:30 +02:00
handleClientException(t);
2017-07-03 09:30:10 +02:00
}
2017-09-15 10:23:30 +02:00
return sessionIdIsSecure;
2017-07-03 09:30:10 +02:00
}
private boolean isValidSessionId(String pSessionId)
{
if (mCallback == null || pSessionId.compareTo(mCallbackSessionId) != 0)
{
Log.w(LOG_TAG, "Android service: Invalid sessiond ID!");
return false;
}
return true;
}
public synchronized boolean send(String pSessionId, String pMessageFromClient)
{
Log.d(LOG_TAG, "Android service: Received JSON from client");
2018-03-28 15:10:51 +02:00
if (!isValidSessionId(pSessionId))
2017-07-03 09:30:10 +02:00
{
return false;
}
aidlSend(pMessageFromClient);
return true;
}
public synchronized boolean updateNfcTag(String pSessionId, Tag pTag)
{
2017-12-20 14:54:05 +01:00
Log.d(LOG_TAG, "Android service: Received nfc tag from client");
2018-03-28 15:10:51 +02:00
if (!isValidSessionId(pSessionId))
2017-07-03 09:30:10 +02:00
{
return false;
}
2017-12-20 14:54:05 +01:00
Intent newIntent = new Intent();
newIntent.putExtra(NfcAdapter.EXTRA_TAG, pTag);
QtNative.onNewIntent(newIntent);
2017-07-03 09:30:10 +02:00
return true;
}
2017-07-03 09:33:28 +02:00
public synchronized void aidlReceive(String pMessageToClient)
2017-07-03 09:30:10 +02:00
{
Log.d(LOG_TAG, "Android service: Passing JSON to client");
if (mCallback == null)
{
Log.d(LOG_TAG, "Android service: Callback not connected.");
2017-07-03 09:33:28 +02:00
return;
2017-07-03 09:30:10 +02:00
}
try
{
mCallback.receive(pMessageToClient);
}
catch (DeadObjectException e)
{
Log.w(LOG_TAG, "Android service: Connected client is already dead.");
mCallback = null;
}
2017-09-15 10:23:30 +02:00
catch (Throwable t)
2017-07-03 09:30:10 +02:00
{
2017-09-15 10:23:30 +02:00
handleClientException(t);
2017-07-03 09:30:10 +02:00
}
}
2017-09-15 10:23:30 +02:00
private native String resetValidSessionID();
2017-07-03 09:30:10 +02:00
private native boolean isSecureRandomPsk();
private native void aidlSend(String pMessageFromClient);
}