-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move the BaseNfcListenerFragment to the basicnfclibrary - Create BaseActivityWithNfcListeners class for activities that need to dispatch nfc tags discovered to listeners - Create function to register and unregister nfc tag discovered listeners in the activity
- Loading branch information
1 parent
60342a5
commit 5476e3f
Showing
9 changed files
with
79 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.oganbelema.basicnfclibrary"> | ||
<uses-permission android:name="android.permission.NFC"/> | ||
</manifest> | ||
|
||
<uses-permission android:name="android.permission.NFC" /> | ||
|
||
<application> | ||
<activity android:name=".BaseActivityWithNfcListeners"/> | ||
</application> | ||
|
||
</manifest> |
46 changes: 46 additions & 0 deletions
46
basicnfclibrary/src/main/java/com/oganbelema/basicnfclibrary/BaseActivityWithNfcListeners.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.oganbelema.basicnfclibrary | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
|
||
open class BaseActivityWithNfcListeners : AppCompatActivity() { | ||
|
||
/** | ||
* Stores the listeners for when an nfc tag is discovered by the device and the | ||
* BaseActivity is called | ||
*/ | ||
private val nfcTagDiscoveredListeners: MutableList<NfcTagDiscoveredListener> = arrayListOf() | ||
|
||
/** | ||
* Registers the listener to receive the detected tag from the activity | ||
* @param nfcTagDiscoveredListener a listener for discovered nfc tag | ||
*/ | ||
fun registerTagDiscoveredListener(nfcTagDiscoveredListener: NfcTagDiscoveredListener) { | ||
nfcTagDiscoveredListeners.add(nfcTagDiscoveredListener) | ||
} | ||
|
||
/** | ||
* Unregisters the listener from receiving the detected tag from the activity | ||
* @param nfcTagDiscoveredListener a listener for discovered nfc tag | ||
*/ | ||
fun unregisterTagDiscoveredListener(nfcTagDiscoveredListener: NfcTagDiscoveredListener){ | ||
nfcTagDiscoveredListeners.remove(nfcTagDiscoveredListener) | ||
} | ||
|
||
override fun onNewIntent(intent: Intent?) { | ||
super.onNewIntent(intent) | ||
|
||
notifyListenersOfDetectedTag(intent) | ||
} | ||
|
||
/** | ||
* Notifies the listeners that the tag was detected | ||
* @param intent containing the discovered tag | ||
*/ | ||
private fun notifyListenersOfDetectedTag(intent: Intent?) { | ||
for (nfcTagDiscoveredListener in nfcTagDiscoveredListeners) { | ||
nfcTagDiscoveredListener.onNfcTagDiscovered(intent) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...commerce/base/NfcTagDiscoveredListener.kt → ...sicnfclibrary/NfcTagDiscoveredListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters