Skip to content

Commit

Permalink
Adaptions in OpenMobileApiSample
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jul 5, 2012
1 parent 4d53b72 commit f6e81a4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 149 deletions.
13 changes: 7 additions & 6 deletions samples/OpenMobileApiSample/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" package="org.simalliance.openmobileapi.test">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="org.simalliance.openmobileapi.test">

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
<uses-permission android:name="org.simalliance.openmobileapi.SMARTCARD"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="org.simalliance.openmobileapi" android:required="true"/>
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
Expand All @@ -11,6 +14,4 @@
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="org.simalliance.openmobileapi.SMARTCARD"></uses-permission>
</manifest>
Binary file not shown.
Binary file not shown.
4 changes: 1 addition & 3 deletions samples/OpenMobileApiSample/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
android:layout_height="fill_parent">
</LinearLayout>
3 changes: 1 addition & 2 deletions samples/OpenMobileApiSample/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">OpenMobileAPISample</string>
<string name="app_name">Open Mobile API Sample</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,38 @@
import org.simalliance.openmobileapi.Channel;
import org.simalliance.openmobileapi.Reader;
import org.simalliance.openmobileapi.SEService;
import org.simalliance.openmobileapi.Session;
import org.simalliance.openmobileapi.SEService.CallBack;
import org.simalliance.openmobileapi.Session;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
* This sample demonstrates how a Secure Element (SE) based android application
* can be realized via the G&D Smart Card API .<br>
*
* Smart Card API implements the simalliance Open Mobile API ({@link http://www.simalliance.org}).
* Open Mobile API sample to demonstrate how a Secure Element based
* Android application can be realized with the G&D SmartCard API .<br>
*
* SmartCard API implements the SIMalliance Open Mobile API ({@link http://www.simalliance.org}).
*/
public class MainActivity extends Activity {

/** Open Mobile API service. */
SEService _service = null;
/** GUI elements on the screen. */
Session _session = null;

TextView _textview = null;
ScrollView _scrollview = null;
/** AID of the issuer security domain. */
private static final byte[] ISD_AID = new byte[] { (byte) 0xA0, 0x00, 0x00,
0x00, 0x03, 0x00, 0x00, 0x00 };

private static final byte[] ISD_AID = new byte[] { (byte) 0xA0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 };


private static String bytesToString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
for (byte b : bytes)
sb.append(String.format("%02x ", b & 0xFF));
}

return sb.toString();
}

Expand All @@ -54,157 +52,103 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);

layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
_scrollview = new ScrollView(this);

_textview = new TextView(this);
_textview.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
_textview.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
_scrollview.addView(_textview);

layout.addView(_scrollview);

setContentView(layout);


SEServiceCallback callback = new SEServiceCallback();
new SEService(this, callback);
}

/**
* Callback interface if informs that this SEService is connected to the
* backend system and it's resources.
* Callback interface if informs that this SEService is connected to the SmartCardService
*/
public class SEServiceCallback implements CallBack {

/**
* This method will be called if this SEService is connected
*/
public void serviceConnected(SEService service) {
try {
performsOperations(service);
} catch (Exception e) {
printException(e);
}
_service = service;
performTest();
}
}

void performsOperations(SEService service) throws Exception {

_service = service;
Reader[] readers = service.getReaders();
logText("All available readers: \n");
private void performTest() {
Reader[] readers = _service.getReaders();
logText("Available readers: \n");
for (Reader reader : readers)
logText(" " + reader.getName() + "\n");
logText(" " + reader.getName() + " - " + ((reader.isSecureElementPresent()) ? "present" : "absent") + "\n");

if (readers.length == 0) {
logText("No reader available \n");
service.shutdown();
return;
}

for (Reader reader : readers) {
logText("Selected Reader:" + reader.getName() + "\n");

logText("SecureElement:");
boolean isPresent = reader.isSecureElementPresent();
logText(isPresent ? " present\n" : " absent\n");
logText("\n");
if (!isPresent)
if (!reader.isSecureElementPresent())
continue;

logText("\n--------------------------------\nSelected reader: \"" + reader.getName() + "\"\n");

Session session = reader.openSession();
try {
_session = reader.openSession();
} catch (Exception e) {
logText(e.getMessage());
}
if (_session == null)
continue;

byte[] atr = session.getATR();
if(atr != null)
{
logText("ATR: " + bytesToString(atr));
logText("\n");
try {
byte[] atr = _session.getATR();
logText("ATR: " + ((atr == null) ? "unavailable" : bytesToString(atr)) + "\n\n");
} catch (Exception e) {
logText("Exception on getATR(): " + e.getMessage() + "\n\n");
}

logText("************************\n");
logText("basic channel test\n");
logText("************************\n");

logText("\n");
logText("OpenBasicChannel\n");
Channel basicChannel = session.openBasicChannel(null);
logText("\n");

logText("basicChannel.transmit() \n");
byte[] cmd = new byte[] { (byte) 0x80, (byte) 0xCA, (byte) 0x9F,
0x7F, 0x00 };
logText(" Command: " + bytesToString(cmd) + "\n");
byte[] response = basicChannel.transmit(cmd);
logText(" Response: " + bytesToString(response) + "\n");
logText("\n");

logText("************************\n");
logText("logical channel test\n");
logText("************************\n");

logText("\n");
logText("OpenLogicalChannel\n");
Channel logicalChannel = session.openLogicalChannel(null);
logText("\n");

logText("logicalChannel.transmit() \n");
cmd = new byte[] { (byte) 0x80, (byte) 0xCA, (byte) 0x9F, 0x7F,
0x00 };
logText(" Command: " + bytesToString(cmd) + "\n");
response = logicalChannel.transmit(cmd);
logText(" Response: " + bytesToString(response) + "\n");

logText("\n");
logText("basicChannel.close()\n");
basicChannel.close();
logText("logicalChannel.close()\n");
logicalChannel.close();
logText("\n");

logText("************************\n");
logText("basic channel test (AID)\n");
logText("************************\n");

logText("\n");
logText("OpenBasicChannel(AID=" + bytesToString(ISD_AID) + ")\n");
basicChannel = session.openBasicChannel(ISD_AID);
logText("\n");

logText("basicChannel.transmit() \n");
cmd = new byte[] { (byte) 0x80, (byte) 0xCA, (byte) 0x9F, 0x7F,
0x00 };
logText(" Command: " + bytesToString(cmd) + "\n");
response = basicChannel.transmit(cmd);
logText(" Response: " + bytesToString(response) + "\n");

logText("************************\n");
logText("logical channel test (AID)\n");
logText("************************\n");

logText("\n");
logText("OpenLogicalChannel(AID=" + bytesToString(ISD_AID) + ")\n");
logicalChannel = session.openLogicalChannel(ISD_AID);
logText("\n");

logText("logicalChannel.transmit() \n");
cmd = new byte[] { (byte) 0x80, (byte) 0xCA, (byte) 0x9F, 0x7F,
0x00 };
logText(" Command: " + bytesToString(cmd) + "\n");
response = logicalChannel.transmit(cmd);
logText(" Response: " + bytesToString(response) + "\n");

logText("\n");
logText("basicChannel.close()\n");
basicChannel.close();
logText("logicalChannel.close()\n");
logicalChannel.close();
logText("\n");

testBasicChannel(null);
testBasicChannel(ISD_AID);

testLogicalChannel(null);
testLogicalChannel(ISD_AID);

_session.close();
}
}

void testBasicChannel(byte[] aid) {
try {
logText("BasicChannel test: " + ((aid == null) ? "default applet" : bytesToString(aid)) + "\n");
Channel channel = _session.openBasicChannel(aid);

byte[] cmd = new byte[] { (byte) 0x80, (byte) 0xCA, (byte) 0x9F, 0x7F, 0x00 };
logText(" -> " + bytesToString(cmd) + "\n");
byte[] rsp = channel.transmit(cmd);
logText(" <- " + bytesToString(rsp) + "\n\n");

channel.close();
} catch (Exception e) {
logText("Exception on BasicChannel: " + e.getMessage() + "\n\n");
}
}

void testLogicalChannel(byte[] aid) {
try {
logText("LogicalChannel test: " + ((aid == null) ? "default applet" : bytesToString(aid)) + "\n");
Channel channel = _session.openLogicalChannel(aid);

byte[] cmd = new byte[] { (byte) 0x80, (byte) 0xCA, (byte) 0x9F, 0x7F, 0x00 };
logText(" -> " + bytesToString(cmd) + "\n");
byte[] rsp = channel.transmit(cmd);
logText(" <- " + bytesToString(rsp) + "\n\n");

channel.close();
} catch (Exception e) {
logText("Exception on LogicalChannel: " + e.getMessage() + "\n\n");
}

}

@Override
Expand All @@ -214,11 +158,4 @@ protected void onDestroy() {
}
super.onDestroy();
}

public void printException(Exception e) {
String msg = e.toString();
logText("Exception: " + msg);
Log.e("OpenMobileApiSample", msg);
}

}

0 comments on commit f6e81a4

Please sign in to comment.