Skip to content

Commit

Permalink
Refact: Demo app, step to kotlin and new features
Browse files Browse the repository at this point in the history
  • Loading branch information
ocfmem committed Feb 3, 2020
1 parent df97fff commit c516d93
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 213 deletions.
40 changes: 24 additions & 16 deletions java/example/calypso/android/omapi/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

project.group 'org.eclipse.keyple'

buildscript {
ext.kotlin_version = '1.3.61'
repositories {
Expand All @@ -12,15 +18,10 @@ buildscript {
}
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

project.group 'org.eclipse.keyple'

//Loading Keystore specified for SE Access
Properties properties = new Properties()
if(project.rootProject.file("local.properties").exists()){
if (project.rootProject.file("local.properties").exists()) {
properties.load(project.rootProject.file("local.properties").newDataInputStream())
}

Expand All @@ -36,8 +37,8 @@ android {
}

signingConfigs {
if(properties.containsKey("path_to_se_registered_keystore")){
keypleDebug{
if (properties.containsKey("path_to_se_registered_keystore")) {
keypleDebug {
storeFile file(properties["path_to_se_registered_keystore"])
storePassword properties["keystore_password"]
keyAlias properties["key_alias"]
Expand All @@ -48,8 +49,8 @@ android {


buildTypes {
if(properties.containsKey("path_to_se_registered_keystore")){
debug{
if (properties.containsKey("path_to_se_registered_keystore")) {
debug {
signingConfig signingConfigs.keypleDebug
}
}
Expand All @@ -70,6 +71,11 @@ android {
exclude 'META-INF/NOTICE.md'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}

}

repositories {
Expand Down Expand Up @@ -98,11 +104,7 @@ dependencies {
implementation group: 'org.eclipse.keyple', name: 'keyple-android-plugin-omapi', version: '+'
//implementation project(':android-omapi')

/*
OMAPI dependencies
*/
compileOnly files('libs/org.simalliance.openmobileapi.jar')


/*
Android components
*/
Expand All @@ -122,10 +124,16 @@ dependencies {
implementation 'androidx.core:core-ktx:1.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

/*
Coroutines
*/
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'

/*
Test
*/
testImplementation "junit:junit:${junit_version}"
testImplementation "junit:junit:4.12"
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Expand Down
21 changes: 0 additions & 21 deletions java/example/calypso/android/omapi/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
org.gradle.jvmargs=-Xmx1536M

# uncomment keyple_version to force using a specific version of keyple artifacts
# by default, the last version available is used, value '+'
#keyple_version=0.8.0


#keyple components
slf4japi_version = 1.7.25
slf4jsimple_version = 1.7.25
slf4jext_version = 1.7.25
junit_version = 4.12

#android log
android_logback_version = 1.1.1-12

#android support
android_support_version = 26.1.0
android_support_constraint_version = 1.0.2
android_support_test = 1.0.1
android_support_espresso = 3.0.1

android.useAndroidX=true
android.enableJetifier=true
13 changes: 6 additions & 7 deletions java/example/calypso/android/omapi/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.eclipse.keyple.example.calypso.android.omapi">

<!-- Required for Open Mobile API-->
<!-- Required for Open Mobile API -->
<uses-permission android:name="org.simalliance.openmobileapi.SMARTCARD" />


<application
android:name=".ExampleApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<!-- Required for Open Mobile API-->
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<!-- Required for Open Mobile API -->
<uses-library
android:name="org.simalliance.openmobileapi"
android:required="true" />
Expand All @@ -25,9 +24,9 @@
android:label="Keyple Android OMAPI">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
</application>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package org.eclipse.keyple.example.calypso.android.omapi
import android.app.Application
import timber.log.Timber

class ExampleApplication : Application() {

class ExampleApplication: Application() {
override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.eclipse.keyple.example.calypso.android.omapi

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import kotlinx.coroutines.*
import org.eclipse.keyple.core.seproxy.SeProxyService
import org.eclipse.keyple.core.seproxy.SeReader
import org.eclipse.keyple.core.seproxy.exception.KeyplePluginInstantiationException
import org.eclipse.keyple.core.seproxy.exception.KeyplePluginNotFoundException
import org.eclipse.keyple.plugin.android.omapi.AndroidOmapiPlugin
import org.eclipse.keyple.plugin.android.omapi.AndroidOmapiPluginFactory
import timber.log.Timber
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

companion object{
private const val MAX_TRIES = 10
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initActionBar()

/* Get the instance of the SeProxyService (Singleton pattern) */
val seProxyService = SeProxyService.getInstance()

/* register Omapi Plugin to the SeProxyService */
try {
seProxyService.registerPlugin(AndroidOmapiPluginFactory(this))
} catch (e: KeyplePluginInstantiationException) {
e.printStackTrace()
}

}

override fun onResume() {
super.onResume()
//Retrieve OMAPI Readers, can't observed but may take time.
//So we retry every second 10 times
progressBar.visibility = View.VISIBLE

CoroutineScope(Dispatchers.Main).launch{
try {
val readers = connectOmapi()
//readers are initialized, we can show the menu
connectOmapiTV.visibility = View.GONE
} catch (e: KeyplePluginNotFoundException) {
Timber.e(e)
showAlertDialog(e)
} catch (e: KeyplePluginInstantiationException){
Timber.e(e)
showAlertDialog(e)
}finally {
progressBar.visibility = View.INVISIBLE
}
}
}

private fun initActionBar(){
setSupportActionBar(toolbar)
val actionBar = supportActionBar
actionBar?.title = "Hello APP"
actionBar?.subtitle = "App subtitle"
actionBar?.setDisplayShowHomeEnabled(true)
actionBar?.setLogo(R.mipmap.ic_launcher)
actionBar?.setDisplayUseLogoEnabled(true)
}


@Throws(KeyplePluginNotFoundException::class, KeyplePluginInstantiationException::class)
private suspend fun connectOmapi(): SortedSet<SeReader> = withContext(Dispatchers.IO) {
var readers : SortedSet<SeReader>? = null
for(x in 1..MAX_TRIES){
readers = SeProxyService.getInstance()
.getPlugin(AndroidOmapiPlugin.PLUGIN_NAME).readers
if(readers == null|| readers.size < 1) {
Timber.d("No readers found in OMAPI Keyple Plugin")
Timber.d("Retrying in 1 second")
delay(1000)
}else{
Timber.d("Readers Found")
break
}
}

readers ?: throw KeyplePluginInstantiationException(getString(R.string.error_no_reader_found, MAX_TRIES))
}

private fun showAlertDialog(t: Throwable){
val builder = AlertDialog.Builder(this@MainActivity)
builder.setTitle(R.string.alert_dialog_title)
builder.setMessage(getString(R.string.alert_dialog_message, t.message))
val dialog = builder.create()
dialog.show()

}
}

0 comments on commit c516d93

Please sign in to comment.