Skip to content

Commit

Permalink
updating for android 12 and Continuous integration
Browse files Browse the repository at this point in the history
  • Loading branch information
romellfudi committed Apr 21, 2022
2 parents a997c5e + 38c87f5 commit bec1948
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 240 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Android CI

on:
pull_request:
branches:
- main
paths-ignore:
- '**.md'
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build App with Gradle
run: ./gradlew assembleDebug
- name: Testing fudi-nfc
run: ./gradlew assembleDebug lint -p fudi-nfc
- name: Upload fudi-nfc artifact
uses: actions/upload-artifact@v1
with:
name: fudi-nfc-aar
path: fudi-nfc/build/outputs/aar/fudi-nfc-debug.aar
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ repositories {
}
dependencies {
implementation 'com.github.romellfudi:FudiNFC:1.1.0'
// support for Android Nougat (API 31)
implementation 'com.github.romellfudi:FudiNFC:android-12-1.1.0'
}
```

Expand Down
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ android {
outputFileName = "fudiNFC-app-${flavor}.apk"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


Expand All @@ -45,7 +49,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.core:core-ktx:1.7.0"
implementation "androidx.core:core-ktx:1.6.0"
implementation 'com.airbnb.android:lottie:3.7.0'

implementation "org.koin:koin-android:2.0.0"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/romellfudi/fudinfc/app/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ val moduleNFC = module {
override fun onReturn(result: Boolean?) {
Timber.d("Received our result : $result")
if (mProgressDialog.isShowing) mProgressDialog.dismiss()
if (result!!) Toast.makeText(mainActivity, "Write has been done!", Toast.LENGTH_SHORT).show()
result?.run {Toast.makeText(mainActivity, "Write has been done!", Toast.LENGTH_SHORT).show()}
}

override fun onProgressUpdate(vararg values: Boolean?) {
Timber.i("Trying to write!")
if (values.isNotEmpty() && values[0]!!) {
if (values.isNotEmpty() && values[0] != null) {
mProgressDialog.setMessage("Writing")
Timber.i("Writing!!!")
}
Expand Down
61 changes: 40 additions & 21 deletions app/src/main/java/com/romellfudi/fudinfc/app/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ class MainActivity : NfcAct(), KoinComponent {
if (emailText != null) {
val text = edit_emailText.text.toString()
mOpCallback = object : OpCallback {
override fun performWrite(it: NfcWriteUtility?): Boolean {
return it!!.writeEmailToTagFromIntent(text, null, null, intent)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility?.run {
writeEmailToTagFromIntent(
text,
null,
null,
intent
)
}
?: false
}
}
showDialog()
Expand All @@ -56,8 +64,9 @@ class MainActivity : NfcAct(), KoinComponent {
if (smsText != null) {
val text = edit_smsText.text.toString()
mOpCallback = object : OpCallback {
override fun performWrite(it: NfcWriteUtility?): Boolean {
return it!!.writeSmsToTagFromIntent(text, null, intent)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility?.run { writeSmsToTagFromIntent(text, null, intent) }
?: false
}
}
showDialog()
Expand All @@ -68,8 +77,14 @@ class MainActivity : NfcAct(), KoinComponent {
val longitude = edit_latitudeText.text.toString().toDouble()
val latitude = edit_longitudeText.text.toString().toDouble()
mOpCallback = object : OpCallback {
override fun performWrite(it: NfcWriteUtility?): Boolean {
return it!!.writeGeolocationToTagFromIntent(latitude, longitude, intent)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility?.run {
writeGeolocationToTagFromIntent(
latitude,
longitude,
intent
)
} ?: false
}
}
showDialog()
Expand All @@ -78,26 +93,27 @@ class MainActivity : NfcAct(), KoinComponent {
uriButton?.setOnClickListener {
val uriText = edit_input_text_uri_target.text.toString()
mOpCallback = object : OpCallback {
override fun performWrite(it: NfcWriteUtility?): Boolean {
return it!!.writeUriToTagFromIntent(uriText, intent)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility?.run { writeUriToTagFromIntent(uriText, intent) } ?: false
}
}
showDialog()
}
telButton.setOnClickListener {
val telText = edit_input_text_tel_target.text.toString()
mOpCallback = object : OpCallback {
override fun performWrite(it: NfcWriteUtility?): Boolean {
return it!!.writeTelToTagFromIntent(telText, intent)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility?.run { writeTelToTagFromIntent(telText, intent) } ?: false
}
}
showDialog()
}
bluetoothButton.setOnClickListener {
val text = edit_bluetoothInput.text.toString()
mOpCallback = object : OpCallback {
override fun performWrite(it: NfcWriteUtility?): Boolean {
return it!!.writeBluetoothAddressToTagFromIntent(text, intent)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility?.run { writeBluetoothAddressToTagFromIntent(text, intent) }
?: false
}
}
showDialog()
Expand All @@ -112,14 +128,14 @@ class MainActivity : NfcAct(), KoinComponent {

public override fun onNewIntent(paramIntent: Intent) {
super.onNewIntent(paramIntent)
if (mOpCallback != null && mProgressDialog.isShowing) {
WriteCallbackNfc(mTaskCallback, mOpCallback!!).executeWriteOperation()
if (mProgressDialog.isShowing) {
mOpCallback?.let { WriteCallbackNfc(mTaskCallback, it).executeWriteOperation() }
mOpCallback = null
} else {
var dataFull = "my mac: " +
getMAC(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG) as Tag)
mNfcReadUtility.readFromTagWithMap(paramIntent)!!.values
.fold(dataFull) { full, st -> full + "\n${st}" }
val dataFull =
"my mac: " + getMAC(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG) as? Tag)
mNfcReadUtility.readFromTagWithMap(paramIntent)?.values
?.fold(dataFull) { full, st -> full + "\n${st}" }
.also { Toast.makeText(this, it, Toast.LENGTH_SHORT).show() }
}
}
Expand All @@ -143,8 +159,11 @@ class MainActivity : NfcAct(), KoinComponent {
progressbar.visibility = View.INVISIBLE
}

private fun getMAC(tag: Tag): String =
private fun getMAC(tag: Tag?): String =
Regex("(.{2})").replace(
String.format("%0" + (tag.id.size * 2).toString() + "X",
BigInteger(1, tag.id)),"$1:").dropLast(1)
String.format(
"%0" + ((tag?.id?.size ?: 0) * 2).toString() + "X",
BigInteger(1, tag?.id ?: byteArrayOf())
), "$1:"
).dropLast(1)
}
30 changes: 11 additions & 19 deletions fudi-nfc/src/main/java/com/romellfudi/fudinfc/gear/GenericTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,42 @@ class GenericTask : AsyncTask<Void?, Boolean?, Boolean> {
* @param params
* @return OpCallback.performWrite() == true
*/
protected override fun doInBackground(vararg params: Void?): Boolean {
Log.d(TAG, "Writing ..")
override fun doInBackground(vararg params: Void?): Boolean {
var res = false
try {
Log.d(TAG, "Writing ..")
publishProgress(true)
if (mOpCallback != null) {
res = mOpCallback!!.performWrite(mNfcWriteUtility)
} else {
mOpCallback?.let {
res = it.performWrite(mNfcWriteUtility)
} ?: run {
error = NullPointerException("OperationCallback is null")
}
} catch (e: Exception) {
Log.w(TAG, e)
mTaskCallback!!.onError(e)
mTaskCallback?.onError(e)
error = e
}

// Remove tag from intent in order to prevent writing to a not present tag
return res
}

override fun onPreExecute() {
super.onPreExecute()
}

/**
* Fire the onReturn.
* If the error != null then onError is executed as well.
* @param result
*/
override fun onPostExecute(result: Boolean) {
super.onPostExecute(result)
if (mOpCallback != null && mTaskCallback != null) {
if (error != null) {
mTaskCallback!!.onError(error)
}
mTaskCallback!!.onReturn(result)
mOpCallback?.run {
error?.let { mTaskCallback?.onError(it) }
mTaskCallback?.onReturn(result)
}
}

protected override fun onProgressUpdate(vararg values: Boolean?) {
override fun onProgressUpdate(vararg values: Boolean?) {
super.onProgressUpdate(*values)
if (mTaskCallback != null) {
mTaskCallback!!.onProgressUpdate(*values)
}
mTaskCallback?.onProgressUpdate(*values)
}

companion object {
Expand Down
26 changes: 9 additions & 17 deletions fudi-nfc/src/main/java/com/romellfudi/fudinfc/gear/NfcAct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.os.Bundle
import android.util.Log
import android.util.SparseArray
import androidx.appcompat.app.AppCompatActivity
import androidx.core.util.forEach
import com.romellfudi.fudinfc.util.interfaces.NfcMessageUtility
import com.romellfudi.fudinfc.util.interfaces.NfcReadUtility
import com.romellfudi.fudinfc.util.sync.NfcMessageUtilityImpl
Expand Down Expand Up @@ -53,7 +54,7 @@ abstract class NfcAct : AppCompatActivity(), CreateNdefMessageCallback {
super.onResume()
initAdapter()
if (nfcAdapter != null) {
nfcAdapter!!.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists)
nfcAdapter?.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists)
Log.d(TAG, "FGD enabled")
}
}
Expand All @@ -78,13 +79,13 @@ abstract class NfcAct : AppCompatActivity(), CreateNdefMessageCallback {
override fun onPause() {
super.onPause()
if (nfcAdapter != null) {
nfcAdapter!!.disableForegroundDispatch(this)
nfcAdapter?.disableForegroundDispatch(this)
Log.d(TAG, "FGD disabled")
}
}

override fun createNdefMessage(event: NfcEvent): NdefMessage {
return NfcMessageUtilityImpl().createText("You're seeing this message because you have not overridden the createNdefMessage(NfcEvent event) in your activity.")!!
return NfcMessageUtilityImpl().createText("You're seeing this message because you have not overridden the createNdefMessage(NfcEvent event) in your activity.")
}

private fun initFields() {
Expand All @@ -111,33 +112,24 @@ abstract class NfcAct : AppCompatActivity(), CreateNdefMessageCallback {

protected fun enableBeam() {
if (nfcAdapter != null) {
nfcAdapter!!.setNdefPushMessageCallback(this, this)
nfcAdapter?.setNdefPushMessageCallback(this, this)
mBeamEnabled = true
Log.d(TAG, "Beam enabled")
}
}

protected fun beamEnabled(): Boolean {
return mBeamEnabled
}

protected fun disableBeam() {
if (nfcAdapter != null) {
nfcAdapter!!.setNdefPushMessageCallback(null, this)
nfcAdapter?.setNdefPushMessageCallback(null, this)
mBeamEnabled = false
Log.d(TAG, "Beam disabled")
}
}

protected fun transformSparseArrayToArrayList(sparseArray: SparseArray<String?>?): List<String?> {
val list: MutableList<String?> = ArrayList(
sparseArray!!.size()
)
for (i in 0 until sparseArray.size()) {
list.add(sparseArray.valueAt(i))
protected fun transformSparseArrayToArrayList(sparseArray: SparseArray<String?>?) =
ArrayList<String?>(sparseArray?.size() ?: 0).apply {
sparseArray?.forEach { _, value -> add(value) }
}
return list
}

companion object {
private val TAG = NfcAct::class.java.name
Expand Down
16 changes: 10 additions & 6 deletions fudi-nfc/src/main/java/com/romellfudi/fudinfc/util/async/Nfc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ abstract class Nfc : NfcToOperation {
}

override fun executeWriteOperation() {
if (nfcWriteUtility != null) {
GenericTask(asyncUiCallback, asyncOperationCallback!!, nfcWriteUtility!!)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
} else {
GenericTask(asyncUiCallback, asyncOperationCallback!!)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
nfcWriteUtility?.let {
asyncOperationCallback?.let { callback ->
GenericTask(asyncUiCallback, callback, it)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
} ?: run {
asyncOperationCallback?.let { callback ->
GenericTask(asyncUiCallback, callback)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ class WriteBluetoothNfc : Nfc {
TagNotPresentException::class,
FormatException::class
)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility!!.writeSmsToTagFromIntent(
(args[0] as String),
args[1] as String,
intent
)
}
override fun performWrite(writeUtility: NfcWriteUtility?) = (writeUtility?.apply {
writeSmsToTagFromIntent(args[0] as String, args[1] as String, intent)
} ?: false) as Boolean
}
executeWriteOperation()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class WriteEmailNfc : Nfc {
}

override fun executeWriteOperation(intent: Intent?, vararg args: Any?) {
if (!checkStringArguments(args.javaClass) || args.size == 0) {
if (!checkStringArguments(args.javaClass) || args.isEmpty() || intent == null) {
throw UnsupportedOperationException("Incorrect arguments")
}
val recipient = args[0] as String
Expand All @@ -69,14 +69,9 @@ class WriteEmailNfc : Nfc {
TagNotPresentException::class,
FormatException::class
)
override fun performWrite(writeUtility: NfcWriteUtility?): Boolean {
return writeUtility!!.writeEmailToTagFromIntent(
recipient,
subject,
message,
intent!!
)
}
override fun performWrite(writeUtility: NfcWriteUtility?) = (writeUtility?.apply {
writeEmailToTagFromIntent(recipient, subject, message, intent)
} ?: false) as Boolean
}
super.executeWriteOperation()
}
Expand Down
Loading

0 comments on commit bec1948

Please sign in to comment.