[O] Don't use application context

This commit is contained in:
Azalea Gui
2023-01-23 15:32:05 -05:00
parent 694e850077
commit cdba873abb
2 changed files with 6 additions and 12 deletions
@@ -46,9 +46,7 @@ class ActivityScan : AppCompatActivity() {
} }
} }
private val bluetoothManager by lazy { private val bluetoothManager by lazy { getSystemService(BLUETOOTH_SERVICE) as BluetoothManager }
applicationContext.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
}
private lateinit var bluetoothHandler: BluetoothHandler private lateinit var bluetoothHandler: BluetoothHandler
@@ -86,7 +84,7 @@ class ActivityScan : AppCompatActivity() {
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private fun initBluetoothHandler() { private fun initBluetoothHandler() {
if (this::bluetoothHandler.isInitialized) return if (this::bluetoothHandler.isInitialized) return
bluetoothHandler = BluetoothHandler.getInstance(applicationContext) bluetoothHandler = BluetoothHandler.ble(this)
println("OnCreate called, Initializing...") println("OnCreate called, Initializing...")
@@ -248,7 +246,7 @@ class ActivityScan : AppCompatActivity() {
private fun getMissingPermissions(requiredPermissions: Array<String>): Array<String> { private fun getMissingPermissions(requiredPermissions: Array<String>): Array<String> {
val missingPermissions: MutableList<String> = ArrayList() val missingPermissions: MutableList<String> = ArrayList()
for (requiredPermission in requiredPermissions) { for (requiredPermission in requiredPermissions) {
if (applicationContext.checkSelfPermission(requiredPermission) != PackageManager.PERMISSION_GRANTED) { if (checkSelfPermission(requiredPermission) != PackageManager.PERMISSION_GRANTED) {
missingPermissions.add(requiredPermission) missingPermissions.add(requiredPermission)
} }
} }
@@ -278,8 +276,7 @@ class ActivityScan : AppCompatActivity() {
} }
private fun areLocationServicesEnabled(): Boolean { private fun areLocationServicesEnabled(): Boolean {
val locationManager = val locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
applicationContext.getSystemService(LOCATION_SERVICE) as LocationManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
locationManager.isLocationEnabled locationManager.isLocationEnabled
} else { } else {
@@ -1,6 +1,5 @@
package org.hydev.wearsync.bles package org.hydev.wearsync.bles
import android.bluetooth.BluetoothAdapter
import android.content.Context import android.content.Context
import com.welie.blessed.* import com.welie.blessed.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
@@ -278,14 +277,12 @@ internal class BluetoothHandler private constructor(context: Context) {
private val CONTOUR_CLOCK = UUID.fromString("00001026-0002-11E2-9E96-0800200C9A66") private val CONTOUR_CLOCK = UUID.fromString("00001026-0002-11E2-9E96-0800200C9A66")
private var instance: BluetoothHandler? = null private var instance: BluetoothHandler? = null
@JvmStatic
@Synchronized @Synchronized
fun getInstance(context: Context): BluetoothHandler fun ble(context: Context): BluetoothHandler {
{
if (instance == null) { if (instance == null) {
instance = BluetoothHandler(context.applicationContext) instance = BluetoothHandler(context.applicationContext)
} }
return requireNotNull(instance) return instance!!
} }
} }