BlePreCheck -> core

This commit is contained in:
Milos Kozak 2020-05-04 23:40:39 +02:00
parent 47fd4bd1fe
commit d8f27c609c
5 changed files with 97 additions and 6 deletions

View file

@ -1549,10 +1549,6 @@
<string name="rileylink_scanner_scan_stop">STOP</string>
<string name="rileylink_scanner_selected_device">Selected</string>
<string name="rileylink_scanner_title">RileyLink Scan</string>
<string name="rileylink_scanner_ble_not_supported">Bluetooth Low Energy not supported.</string>
<string name="rileylink_scanner_ble_not_enabled">Bluetooth not enabled.</string>
<string name="location_not_found_title">Location Is Not Enabled</string>
<string name="location_not_found_message">For Bluetooth discovery to work on newer devices, location must be enabled. AAPS does not track your location and it can be disabled after pairing is successful.</string>
<string name="location_yes">Enable</string>
<string name="location_no">No</string>
<string name="rileylink_scanner_scanning">Scanning</string>

View file

@ -42,7 +42,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation "androidx.preference:preference-ktx:1.1.1"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"

View file

@ -1,2 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.nightscout.androidaps.core" />
package="info.nightscout.androidaps.core" >
<uses-permission android:name="android.permission.BLUETOOTH" />
</manifest>

View file

@ -0,0 +1,87 @@
package info.nightscout.androidaps.plugins.pump.common.ble
import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.resources.ResourceHelper
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class BlePreCheck @Inject constructor(
val resourceHelper: ResourceHelper
) {
companion object {
private const val PERMISSION_REQUEST_COARSE_LOCATION = 30241 // arbitrary.
}
fun prerequisitesCheck(activity: AppCompatActivity): Boolean {
if (!activity.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
OKDialog.show(activity, resourceHelper.gs(R.string.message), resourceHelper.gs(R.string.rileylink_scanner_ble_not_supported))
return false
} else {
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// your code that requires permission
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_COARSE_LOCATION)
}
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled) {
OKDialog.show(activity, resourceHelper.gs(R.string.message), resourceHelper.gs(R.string.rileylink_scanner_ble_not_enabled))
return false
} else {
// Will request that GPS be enabled for devices running Marshmallow or newer.
if (!isLocationEnabled(activity)) {
requestLocation(activity)
return false
}
}
}
return true
}
/**
* Determine if GPS is currently enabled.
*
*
* On Android 6 (Marshmallow), location needs to be enabled for Bluetooth discovery to work.
*
* @param context The current app context.
* @return true if location is enabled, false otherwise.
*/
private fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
/**
* Prompt the user to enable GPS location if it isn't already on.
*
* @param activity The currently visible activity.
*/
private fun requestLocation(activity: AppCompatActivity) {
if (isLocationEnabled(activity)) {
return
}
// Shamelessly borrowed from http://stackoverflow.com/a/10311877/868533
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.location_not_found_title), resourceHelper.gs(R.string.location_not_found_message), Runnable {
activity.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
})
}
}

View file

@ -7,4 +7,9 @@
<string name="cancel">Cancel</string>
<string name="dismiss">DISMISS</string>
<string name="rileylink_scanner_ble_not_supported">Bluetooth Low Energy not supported.</string>
<string name="rileylink_scanner_ble_not_enabled">Bluetooth not enabled.</string>
<string name="location_not_found_title">Location Is Not Enabled</string>
<string name="location_not_found_message">For Bluetooth discovery to work on newer devices, location must be enabled. AAPS does not track your location and it can be disabled after pairing is successful.</string>
</resources>