Skip to content

Commit

Permalink
NFC Library Update
Browse files Browse the repository at this point in the history
- 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
OganBelema committed May 7, 2019
1 parent 60342a5 commit 5476e3f
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.view.ViewGroup
import com.google.android.material.snackbar.Snackbar

import com.oganbelema.nfcforecommerce.R
import com.oganbelema.nfcforecommerce.base.BaseNfcListenerFragment
import com.oganbelema.basicnfclibrary.BaseNfcListenerFragment
import com.oganbelema.nfcforecommerce.util.Util
import kotlinx.android.synthetic.main.add_customer_fragment.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.oganbelema.nfcforecommerce.base

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.oganbelema.basicnfclibrary.BaseActivityWithNfcListeners
import com.oganbelema.basicnfclibrary.NFCRegistration
import com.oganbelema.nfcforecommerce.R
import kotlinx.android.synthetic.main.activity_base.*
Expand All @@ -16,7 +15,7 @@ import kotlinx.android.synthetic.main.activity_base.*
/**
* This base activity for the application that will serve as the entry point to the app
*/
class BaseActivity : AppCompatActivity() {
class BaseActivity : BaseActivityWithNfcListeners() {

/**
* This is the device's default nfc adapter
Expand All @@ -33,11 +32,7 @@ class BaseActivity : AppCompatActivity() {
*/
private lateinit var navController: NavController

/**
* Stores the listeners for when an nfc tag is discovered by the device and the
* BaseActivity is called
*/
val nfcTagDiscoveredListeners: MutableList<NfcTagDiscoveredListener> = arrayListOf()


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -70,13 +65,5 @@ class BaseActivity : AppCompatActivity() {
nfcRegistration.disableForegroundDispatch()
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)

for (nfcTagDiscoveredListener in nfcTagDiscoveredListeners){
nfcTagDiscoveredListener.onNfcTagDiscovered(intent)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.View
import android.view.ViewGroup

import com.oganbelema.nfcforecommerce.R
import com.oganbelema.nfcforecommerce.base.BaseNfcListenerFragment
import com.oganbelema.basicnfclibrary.BaseNfcListenerFragment

class CashierFragment : BaseNfcListenerFragment() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.view.ViewGroup
import com.google.android.material.snackbar.Snackbar

import com.oganbelema.nfcforecommerce.R
import com.oganbelema.nfcforecommerce.base.BaseNfcListenerFragment
import com.oganbelema.basicnfclibrary.BaseNfcListenerFragment
import com.oganbelema.nfcforecommerce.util.Util
import kotlinx.android.synthetic.main.add_customer_fragment.scanNfcTagHintViews
import kotlinx.android.synthetic.main.customer_support_fragment.*
Expand Down
11 changes: 9 additions & 2 deletions basicnfclibrary/src/main/AndroidManifest.xml
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>
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.oganbelema.nfcforecommerce.base
package com.oganbelema.basicnfclibrary

import android.content.Intent
import android.nfc.NfcAdapter
import android.nfc.Tag
import androidx.fragment.app.Fragment
import com.oganbelema.basicnfclibrary.BaseActivityWithNfcListeners
import com.oganbelema.basicnfclibrary.NfcTagDiscoveredListener


/**
* A base fragment that listens for nfc tag discovered notifications from the base
* activity.
* A base fragment that listens for nfc tag discovered notifications from a BaseActivityWithNfcListeners.
* Contains helper methods that are required by an nfc listener fragment
* Extend this to listen for nfc tag discovered notifications from the base activity in a fragment
* Extend this to listen for nfc tag discovered notifications from a BaseActivityWithNfcListeners activity in a fragment
*/
abstract class BaseNfcListenerFragment : Fragment(), NfcTagDiscoveredListener {

Expand Down Expand Up @@ -45,31 +46,30 @@ abstract class BaseNfcListenerFragment : Fragment(), NfcTagDiscoveredListener {
abstract fun onExtractTagId(tagId: ByteArray?)

/**
* Registers the fragment to receive the detected tag from the activity
* Registers the fragment as a listener for dispatched nfc tag discovered intent
*/
private fun registerTagDiscoveredListener() {
if (activity != null){
(activity as BaseActivity).nfcTagDiscoveredListeners.add(this)
private fun registerAsListener() {
if (activity != null) {
(activity as BaseActivityWithNfcListeners).registerTagDiscoveredListener(this)
}
}

/**
* Unregisters the fragment from receiving the detected tag from the activity
* Unregisters the fragment as a listener for dispatched nfc tag discovered intent
*/
private fun unregisterTagDiscoveredListener(){
if (activity != null){
(activity as BaseActivity).nfcTagDiscoveredListeners.remove(this)
private fun unregisterAsListener() {
if (activity != null) {
(activity as BaseActivityWithNfcListeners).unregisterTagDiscoveredListener(this)
}
}

override fun onStart() {
super.onStart()
registerTagDiscoveredListener()
registerAsListener()
}

override fun onStop() {
unregisterTagDiscoveredListener()
unregisterAsListener()
super.onStop()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.nfc.NfcAdapter
/**
* Use this class to register to detect nfc tags in an activity
* @param activity to be notified when nfc tag is discovered
* @author Belema Ogan
*/
class NFCRegistration(private val activity: Activity) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.oganbelema.nfcforecommerce.base
package com.oganbelema.basicnfclibrary

import android.content.Intent

/**
* Implement this to be able to receive nfc tag intent from the BaseActivity's onNewIntent
* @author Belema Ogan
*/
interface NfcTagDiscoveredListener {

Expand Down

0 comments on commit 5476e3f

Please sign in to comment.