Skip to content

Commit

Permalink
Merge pull request #351 from thgoebel/fully-remove-activity-ref
Browse files Browse the repository at this point in the history
Fully remove activity ref
  • Loading branch information
simondankelmann authored Jan 30, 2025
2 parents 5b25258 + 915e632 commit cb5adae
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package de.simon.dankelmann.bluetoothlespam.AppContext

import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.util.Log
import de.simon.dankelmann.bluetoothlespam.Handlers.AdvertisementSetQueueHandler
import de.simon.dankelmann.bluetoothlespam.Interfaces.Services.IAdvertisementService
import de.simon.dankelmann.bluetoothlespam.Interfaces.Services.IBluetoothLeScanService
import de.simon.dankelmann.bluetoothlespam.Services.BluetoothLeScanService

abstract class AppContext {
companion object {

private lateinit var _context: Context
private lateinit var _activity: Activity
private lateinit var _advertisementService: IAdvertisementService
private lateinit var _bluetoothLeScanService: IBluetoothLeScanService
private lateinit var _advertisementSetQueueHandler: AdvertisementSetQueueHandler
Expand All @@ -41,14 +37,6 @@ abstract class AppContext {
return _context
}

fun setActivity(activity: Activity) {
_activity = activity
}

fun getActivity(): Activity {
return _activity
}

fun setAdvertisementService(advertisementService: IAdvertisementService) {
_advertisementService = advertisementService
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class BluetoothHelpers {
}

return when (useLegacyAdvertisementService) {
true -> LegacyAdvertisementService()
true -> LegacyAdvertisementService(context)
else -> {
ModernAdvertisementService()
ModernAdvertisementService(context)
}
}
}

fun getBluetoothLeScanService(): IBluetoothLeScanService {
return BluetoothLeScanService()
fun getBluetoothLeScanService(context: Context): IBluetoothLeScanService {
return BluetoothLeScanService(context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class MainActivity : AppCompatActivity() {

// Initialize AppContext, Activity, Advertisement Service and QueHandler
AppContext.setContext(applicationContext)
AppContext.setActivity(this)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ open class BluetoothLeScanResult {
model.address = scanResult.device.address

// get device data
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_CONNECT, AppContext.getActivity())){
if(scanResult.device != null && scanResult.device.name != null){
if (PermissionCheck.checkPermission(
Manifest.permission.BLUETOOTH_CONNECT, AppContext.getContext()
)
) {
if (scanResult.device != null && scanResult.device.name != null) {
model.deviceName = scanResult.device.name
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ package de.simon.dankelmann.bluetoothlespam.PermissionCheck

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.checkSelfPermission
import de.simon.dankelmann.bluetoothlespam.AppContext.AppContext
import de.simon.dankelmann.bluetoothlespam.Constants.Constants

class PermissionCheck (){
companion object {

private val _logTag = "PermissionCheck"
fun checkPermission(permission:String, activity: Activity, requestIfNotGranted:Boolean = true):Boolean{
if (permission == "android.permission.BLUETOOTH_ADVERTISE" && Build.VERSION.SDK_INT < Build.VERSION_CODES.S)
{

fun checkPermissionAndRequest(permission: String, activity: Activity): Boolean {
val isGranted = checkPermission(permission, activity)
if (!isGranted) {
ActivityCompat.requestPermissions(
activity, arrayOf(permission), Constants.REQUEST_CODE_SINGLE_PERMISSION
)
}
return isGranted
}

fun checkPermission(permission: String, context: Context): Boolean {
if (permission == "android.permission.BLUETOOTH_ADVERTISE" && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
// android.permission.BLUETOOTH_ADVERTISE was first introduced in api level 31
return true
}
Expand Down Expand Up @@ -57,16 +68,7 @@ class PermissionCheck (){
return true
}

if(ContextCompat.checkSelfPermission(AppContext.getContext(), permission) == PackageManager.PERMISSION_GRANTED){
//Log.d(_logTag, "Permission granted: $permission")
return true
} else {
if(requestIfNotGranted){
ActivityCompat.requestPermissions(activity, arrayOf(permission), Constants.REQUEST_CODE_SINGLE_PERMISSION)
}
}
Log.d(_logTag, "Permission not granted: $permission")
return false
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
}

fun requireAllPermissions(activity: Activity, permissions: Array<String>){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ class BluetoothLeAdvertisementService(

private fun startAdvertising(advertisementSet: AdvertisementSet){
if(_advertiser != null){
if(advertisementSet.validate()){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
if (advertisementSet.validate()) {
if (PermissionCheck.checkPermission(
Manifest.permission.BLUETOOTH_ADVERTISE, context
)
) {
val preparedAdvertisementSet = prepareAdvertisementSet(advertisementSet)
_advertiser!!.startAdvertising(preparedAdvertisementSet.advertiseSettings.build(), preparedAdvertisementSet.advertiseData.build(), preparedAdvertisementSet.advertisingCallback)
_bleAdvertisementServiceCallback.map {
Expand All @@ -154,9 +157,9 @@ class BluetoothLeAdvertisementService(
}
}

private fun stopAdvertising(advertisementSet: AdvertisementSet){
if(_advertiser != null){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
private fun stopAdvertising(advertisementSet: AdvertisementSet) {
if (_advertiser != null) {
if (PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, context)) {
_advertiser!!.stopAdvertising(advertisementSet.advertisingCallback)
} else {
Log.d(_logTag, "Missing permission to stop advertisement")
Expand All @@ -168,8 +171,11 @@ class BluetoothLeAdvertisementService(

private fun startAdvertisingSet(advertisementSet: AdvertisementSet){
if(_advertiser != null){
if(advertisementSet.validate()){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
if (advertisementSet.validate()) {
if (PermissionCheck.checkPermission(
Manifest.permission.BLUETOOTH_ADVERTISE, context
)
) {
val preparedAdvertisementSet = prepareAdvertisementSet(advertisementSet)
_advertiser!!.startAdvertisingSet(preparedAdvertisementSet.advertisingSetParameters.build(), preparedAdvertisementSet.advertiseData.build(), null, null, null, preparedAdvertisementSet.advertisingSetCallback)
_bleAdvertisementServiceCallback.map {
Expand All @@ -186,9 +192,9 @@ class BluetoothLeAdvertisementService(
}
}

private fun stopAdvertisingSet(advertisementSet: AdvertisementSet){
if(_advertiser != null){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
private fun stopAdvertisingSet(advertisementSet: AdvertisementSet) {
if (_advertiser != null) {
if (PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, context)) {
_advertiser!!.stopAdvertisingSet(advertisementSet.advertisingSetCallback)
_bleAdvertisementServiceCallback.map {
it.onAdvertisementStopped()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanFilter
import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.ParcelUuid
Expand All @@ -30,7 +31,9 @@ import java.time.Duration
import java.time.LocalDate
import java.time.LocalDateTime

class BluetoothLeScanService () : IBluetoothLeScanService, ScanCallback() {
class BluetoothLeScanService(
private val context: Context,
) : IBluetoothLeScanService, ScanCallback() {

private val _logTag = "BluetoothLeScanService"
private var _bluetoothAdapter:BluetoothAdapter? = null
Expand Down Expand Up @@ -159,8 +162,8 @@ class BluetoothLeScanService () : IBluetoothLeScanService, ScanCallback() {
}

override fun startScanning(){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_SCAN, AppContext.getActivity())){
if(_bluetoothLeScanner != null){
if (PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_SCAN, context)) {
if (_bluetoothLeScanner != null) {
// SET THE FILTERS AND SETTINGS
val filterList:List<ScanFilter> = mutableListOf(ScanFilter.Builder().build())

Expand All @@ -173,9 +176,9 @@ class BluetoothLeScanService () : IBluetoothLeScanService, ScanCallback() {
}
}

override fun stopScanning(){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_SCAN, AppContext.getActivity())){
if(_bluetoothLeScanner != null) {
override fun stopScanning() {
if (PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_SCAN, context)) {
if (_bluetoothLeScanner != null) {
_bluetoothLeScanner!!.stopScan(this)
Log.d(_logTag, "Stopped BLE Scan")
_scanning = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothAdapter
import android.bluetooth.le.AdvertiseCallback
import android.bluetooth.le.AdvertiseSettings
import android.bluetooth.le.BluetoothLeAdvertiser
import android.content.Context
import android.util.Log
import de.simon.dankelmann.bluetoothlespam.AppContext.AppContext
import de.simon.dankelmann.bluetoothlespam.AppContext.AppContext.Companion.bluetoothAdapter
Expand All @@ -16,7 +17,9 @@ import de.simon.dankelmann.bluetoothlespam.Interfaces.Services.IAdvertisementSer
import de.simon.dankelmann.bluetoothlespam.Models.AdvertisementSet
import de.simon.dankelmann.bluetoothlespam.PermissionCheck.PermissionCheck

class LegacyAdvertisementService: IAdvertisementService {
class LegacyAdvertisementService(
private val context: Context,
): IAdvertisementService {

// private
private val _logTag = "AdvertisementService"
Expand All @@ -34,11 +37,14 @@ class LegacyAdvertisementService: IAdvertisementService {
}

override fun startAdvertisement(advertisementSet:AdvertisementSet){
if(_advertiser != null){
if(advertisementSet.validate()){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
if(_advertiser != null) {
if (advertisementSet.validate()) {
if (PermissionCheck.checkPermission(
Manifest.permission.BLUETOOTH_ADVERTISE, context
)
) {
val preparedAdvertisementSet = prepareAdvertisementSet(advertisementSet)
if(preparedAdvertisementSet.scanResponse != null){
if (preparedAdvertisementSet.scanResponse != null) {
_advertiser!!.startAdvertising(preparedAdvertisementSet.advertiseSettings.build(), preparedAdvertisementSet.advertiseData.build(), preparedAdvertisementSet.scanResponse!!.build(), preparedAdvertisementSet.advertisingCallback)
} else {
_advertiser!!.startAdvertising(preparedAdvertisementSet.advertiseSettings.build(), preparedAdvertisementSet.advertiseData.build(), preparedAdvertisementSet.advertisingCallback)
Expand All @@ -61,8 +67,11 @@ class LegacyAdvertisementService: IAdvertisementService {

override fun stopAdvertisement(){
if(_advertiser != null){
if(_currentAdvertisementSet != null){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
if(_currentAdvertisementSet != null) {
if (PermissionCheck.checkPermission(
Manifest.permission.BLUETOOTH_ADVERTISE, context
)
) {
_advertiser!!.stopAdvertising(_currentAdvertisementSet!!.advertisingCallback)

_advertisementServiceCallbacks.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.bluetooth.le.AdvertiseSettings
import android.bluetooth.le.AdvertisingSet
import android.bluetooth.le.AdvertisingSetCallback
import android.bluetooth.le.BluetoothLeAdvertiser
import android.content.Context
import android.content.pm.PackageManager
import android.os.Handler
import android.os.Looper
Expand All @@ -21,7 +22,9 @@ import de.simon.dankelmann.bluetoothlespam.Interfaces.Services.IAdvertisementSer
import de.simon.dankelmann.bluetoothlespam.Models.AdvertisementSet
import de.simon.dankelmann.bluetoothlespam.PermissionCheck.PermissionCheck

class ModernAdvertisementService: IAdvertisementService{
class ModernAdvertisementService(
private val context: Context,
): IAdvertisementService{

// private
private val _logTag = "AdvertisementService"
Expand Down Expand Up @@ -53,7 +56,7 @@ class ModernAdvertisementService: IAdvertisementService{
override fun startAdvertisement(advertisementSet: AdvertisementSet) {
if(_advertiser != null){
if(advertisementSet.validate()){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, context)){
val preparedAdvertisementSet = prepareAdvertisementSet(advertisementSet)
if(preparedAdvertisementSet.scanResponse != null){
_advertiser!!.startAdvertisingSet(preparedAdvertisementSet.advertisingSetParameters.build(), preparedAdvertisementSet.advertiseData.build(), preparedAdvertisementSet.scanResponse!!.build(), null, null, preparedAdvertisementSet.advertisingSetCallback)
Expand All @@ -79,8 +82,11 @@ class ModernAdvertisementService: IAdvertisementService{

override fun stopAdvertisement() {
if(_advertiser != null){
if(_currentAdvertisementSet != null){
if(PermissionCheck.checkPermission(Manifest.permission.BLUETOOTH_ADVERTISE, AppContext.getActivity())){
if (_currentAdvertisementSet != null) {
if (PermissionCheck.checkPermission(
Manifest.permission.BLUETOOTH_ADVERTISE, context
)
) {
_advertiser!!.stopAdvertisingSet(_currentAdvertisementSet!!.advertisingSetCallback)
_currentAdvertisementSet = null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class StartFragment : Fragment() {
} else {
addMissingRequirement("Bluetooth is disabled")
if (promptIfAdapterIsDisabled) {
if (PermissionCheck.checkPermission(
if (PermissionCheck.checkPermissionAndRequest(
Manifest.permission.BLUETOOTH_CONNECT, activity
)
) {
Expand Down Expand Up @@ -317,7 +317,7 @@ class StartFragment : Fragment() {
var missingRequirementString =
"Permission " + permission.replace("android.permission.", "") + " not granted"
val activity = requireActivity()
val isGranted = PermissionCheck.checkPermission(permission, activity, false)
val isGranted = PermissionCheck.checkPermissionAndRequest(permission, activity)

if (isGranted) {
removeMissingRequirement(missingRequirementString)
Expand Down Expand Up @@ -348,9 +348,8 @@ class StartFragment : Fragment() {
}

fun checkBackgroundLocationAccessPermission(promptForNotGranted:Boolean = false):Boolean{
val activity = requireActivity()
val isGranted = PermissionCheck.checkPermission(
Manifest.permission.ACCESS_BACKGROUND_LOCATION, activity, false
Manifest.permission.ACCESS_BACKGROUND_LOCATION, requireContext()
)
if (promptForNotGranted) {
//PermissionCheck.requireAllPermissions(AppContext.getActivity(), notGrantedPermissions.toTypedArray())
Expand Down Expand Up @@ -379,9 +378,9 @@ class StartFragment : Fragment() {
}


if(!AppContext.bluetoothLeScanServiceIsInitialized()){
if (!AppContext.bluetoothLeScanServiceIsInitialized()) {
try {
val bluetoothLeScanService = BluetoothHelpers.getBluetoothLeScanService()
val bluetoothLeScanService = BluetoothHelpers.getBluetoothLeScanService(context)
AppContext.setBluetoothLeScanService(bluetoothLeScanService)
//BluetoothLeScanForegroundService.startService(AppContext.getContext(), "Bluetooth LE Scan Foreground Service is running...")
} catch (e:Exception){
Expand Down

0 comments on commit cb5adae

Please sign in to comment.