@regulaforensics/cordova-plugin-document-reader-api 6.9.1 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,11 @@
1
+ //
2
+ // BluetoothUtil.kt
3
+ // DocumentReader
4
+ //
5
+ // Created by Pavel Masiuk on 21.09.2023.
6
+ // Copyright © 2023 Regula. All rights reserved.
7
+ //
8
+
1
9
  package cordova.plugin.documentreader
2
10
 
3
11
  import android.Manifest.permission.*
@@ -21,88 +29,84 @@ import com.regula.documentreader.api.internal.permission.BluetoothPermissionHelp
21
29
  import com.regula.documentreader.api.internal.permission.BluetoothSettingsHelper.isBluetoothEnabled
22
30
  import com.regula.documentreader.api.internal.permission.BluetoothSettingsHelper.isLocationServiceEnabled
23
31
 
24
- class BluetoothUtil {
25
- companion object {
26
- private const val REQUEST_ENABLE_LOCATION = 196
27
- private const val REQUEST_ENABLE_BT = 197
32
+ const val REQUEST_ENABLE_LOCATION = 196
33
+ const val REQUEST_ENABLE_BT = 197
28
34
 
29
- @SuppressLint("StaticFieldLeak")
30
- var bleManager: BLEWrapper? = null
35
+ @SuppressLint("StaticFieldLeak")
36
+ var bleManager: BLEWrapper? = null
31
37
 
32
- @RequiresPermission("android.permission.BLUETOOTH_CONNECT")
33
- private fun requestEnableBle(activity: Activity) {
34
- val enableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
35
- activity.startActivityForResult(enableIntent, REQUEST_ENABLE_BT)
36
- }
38
+ @RequiresPermission("android.permission.BLUETOOTH_CONNECT")
39
+ fun requestEnableBle(activity: Activity) {
40
+ val enableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
41
+ activity.startActivityForResult(enableIntent, REQUEST_ENABLE_BT)
42
+ }
37
43
 
38
- private fun requestEnableLocationService(activity: Activity) {
39
- val myIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
40
- activity.startActivityForResult(myIntent, REQUEST_ENABLE_LOCATION)
41
- }
44
+ fun requestEnableLocationService(activity: Activity) {
45
+ val myIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
46
+ activity.startActivityForResult(myIntent, REQUEST_ENABLE_LOCATION)
47
+ }
42
48
 
43
- // requestEnableBle() is called after a check for permission
44
- @SuppressLint("MissingPermission")
45
- fun isBlePermissionsGranted(activity: Activity): Boolean {
46
- if (!isLocationServiceEnabled(activity)) {
47
- requestEnableLocationService(activity)
48
- return false
49
- }
50
- deniedBluetoothPermissions(activity)?.let {
51
- requestPermissions(activity, it, BLE_ACCESS_PERMISSION)
52
- return false
53
- }
54
- if (!isBluetoothEnabled(activity)) {
55
- requestEnableBle(activity)
56
- return false
57
- }
58
- return true
59
- }
49
+ // requestEnableBle() is called after a check for permission
50
+ @SuppressLint("MissingPermission")
51
+ fun isBlePermissionsGranted(activity: Activity): Boolean {
52
+ if (!isLocationServiceEnabled(activity)) {
53
+ requestEnableLocationService(activity)
54
+ return false
55
+ }
56
+ deniedBluetoothPermissions(activity)?.let {
57
+ requestPermissions(activity, it, BLE_ACCESS_PERMISSION)
58
+ return false
59
+ }
60
+ if (!isBluetoothEnabled(activity)) {
61
+ requestEnableBle(activity)
62
+ return false
63
+ }
64
+ return true
65
+ }
60
66
 
61
- private fun deniedBluetoothPermissions(activity: Activity): Array<String>? {
62
- val result = mutableListOf<String>()
63
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
64
- result.addAll(deniedBluetoothPermission(activity, BLUETOOTH_SCAN))
65
- result.addAll(deniedBluetoothPermission(activity, BLUETOOTH_CONNECT))
66
- } else
67
- result.addAll(deniedBluetoothPermission(activity, ACCESS_FINE_LOCATION))
68
- return result.let { if (it.size > 0) it.toTypedArray() else null }
69
- }
67
+ fun deniedBluetoothPermissions(activity: Activity): Array<String>? {
68
+ val result = mutableListOf<String>()
69
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
70
+ result.addAll(deniedBluetoothPermission(activity, BLUETOOTH_SCAN))
71
+ result.addAll(deniedBluetoothPermission(activity, BLUETOOTH_CONNECT))
72
+ } else
73
+ result.addAll(deniedBluetoothPermission(activity, ACCESS_FINE_LOCATION))
74
+ return result.let { if (it.size > 0) it.toTypedArray() else null }
75
+ }
70
76
 
71
- private fun deniedBluetoothPermission(
72
- activity: Activity,
73
- permission: String
74
- ): Array<String> {
75
- if (checkSelfPermission(activity, permission) != PERMISSION_GRANTED)
76
- return arrayOf(permission)
77
- return arrayOf()
78
- }
77
+ fun deniedBluetoothPermission(
78
+ activity: Activity,
79
+ permission: String
80
+ ): Array<String> {
81
+ if (checkSelfPermission(activity, permission) != PERMISSION_GRANTED)
82
+ return arrayOf(permission)
83
+ return arrayOf()
84
+ }
79
85
 
80
- fun startBluetoothService(
81
- activity: Activity,
82
- onConnected: (Boolean) -> Unit,
83
- onDisconnected: () -> Unit,
84
- onReady: () -> Unit
85
- ) {
86
- val bleIntent = Intent(activity, RegulaBleService::class.java)
87
- activity.startService(bleIntent)
86
+ fun startBluetoothService(
87
+ activity: Activity,
88
+ onConnected: (Boolean) -> Unit,
89
+ onDisconnected: () -> Unit,
90
+ onReady: () -> Unit
91
+ ) {
92
+ val bleIntent = Intent(activity, RegulaBleService::class.java)
93
+ activity.startService(bleIntent)
88
94
 
89
- activity.bindService(bleIntent, object : ServiceConnection {
90
- override fun onServiceConnected(name: ComponentName, service: IBinder) {
91
- bleManager = (service as RegulaBleService.LocalBinder).service.bleManager
92
- val isBleManagerConnected = bleManager?.isConnected == true
93
- onConnected(isBleManagerConnected)
94
- if (!isBleManagerConnected) {
95
- bleManager?.addCallback(object : BleWrapperCallback() {
96
- override fun onDeviceReady() {
97
- bleManager!!.removeCallback(this)
98
- onReady()
99
- }
100
- })
95
+ activity.bindService(bleIntent, object : ServiceConnection {
96
+ override fun onServiceConnected(name: ComponentName, service: IBinder) {
97
+ bleManager = (service as RegulaBleService.LocalBinder).service.bleManager
98
+ val isBleManagerConnected = bleManager?.isConnected == true
99
+ onConnected(isBleManagerConnected)
100
+ if (!isBleManagerConnected) {
101
+ bleManager?.addCallback(object : BleWrapperCallback() {
102
+ override fun onDeviceReady() {
103
+ bleManager!!.removeCallback(this)
104
+ onReady()
101
105
  }
102
- }
103
-
104
- override fun onServiceDisconnected(name: ComponentName) = onDisconnected()
105
- }, 0)
106
+ })
107
+ }
106
108
  }
107
- }
109
+
110
+ override fun onServiceDisconnected(name: ComponentName) = onDisconnected()
111
+ }, 0)
108
112
  }