Skip to content

Commit e3f7ea1

Browse files
Okuro3499dogi
authored andcommitted
actions: foreground bluetooth service (fixes #1919) (#1925)
1 parent 46f4a06 commit e3f7ea1

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

.github/workflows/android-release.yml

+10-10
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ jobs:
7272
sha256sum output/remote.aab > output/remote.aab.sha256
7373
ls -alR output
7474
75-
- name: publish AAB to playstore
76-
if: github.ref == 'refs/heads/master'
77-
uses: dogi/[email protected]
78-
with:
79-
serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
80-
packageName: io.treehouses.remote
81-
releaseFiles: output/remote.aab
82-
track: internal
83-
releaseName: "${{ env.VERSION }}"
84-
status: completed
75+
# - name: publish AAB to playstore
76+
# if: github.ref == 'refs/heads/master'
77+
# uses: dogi/[email protected]
78+
# with:
79+
# serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
80+
# packageName: io.treehouses.remote
81+
# releaseFiles: output/remote.aab
82+
# track: internal
83+
# releaseName: "${{ env.VERSION }}"
84+
# status: completed
8585

8686
# - name: mobile security framework
8787
# run: |

app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
tools:ignore="ProtectedPermissions" />
2020
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2121
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
22+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>
23+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
2224

2325
<application
2426
android:name=".MainApplication"
@@ -68,6 +70,7 @@
6870
android:name=".network.BluetoothChatService"
6971
android:enabled="true"
7072
android:exported="false"
73+
android:foregroundServiceType="connectedDevice"
7174
android:label="Bluetooth Connection" />
7275
</application>
7376
</manifest>

app/src/main/kotlin/io/treehouses/remote/bases/BaseBluetoothChatService.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import android.bluetooth.BluetoothDevice
99
import android.bluetooth.BluetoothManager
1010
import android.content.Context
1111
import android.content.Intent
12+
import android.content.pm.ServiceInfo
13+
import android.os.Build
1214
import android.os.Bundle
1315
import android.os.Handler
1416
import android.os.IBinder
1517
import android.os.Message
1618
import android.util.Log
1719
import androidx.core.app.NotificationCompat
20+
import androidx.core.app.ServiceCompat
1821
import io.treehouses.remote.Constants
1922
import io.treehouses.remote.InitialActivity
2023
import io.treehouses.remote.R
@@ -90,7 +93,14 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler?
9093
.setContentIntent(pendingClickIntent)
9194
.addAction(R.drawable.bluetooth, "Disconnect", disconnectPendingIntent)
9295
.build()
93-
startForeground(2, notification)
96+
ServiceCompat.startForeground(this,2,
97+
notification,
98+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
99+
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
100+
} else {
101+
0
102+
}
103+
)
94104
}
95105

96106
/**

app/src/main/kotlin/io/treehouses/remote/bases/PermissionActivity.kt

+20-24
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import androidx.core.content.ContextCompat
1515
import io.treehouses.remote.R
1616

1717
abstract class PermissionActivity : AppCompatActivity() {
18-
private fun checkPermission(strPermission: String?): Boolean {
19-
val result = ContextCompat.checkSelfPermission(this, strPermission!!)
18+
private fun checkPermission(strPermission: String): Boolean {
19+
val result = ContextCompat.checkSelfPermission(this, strPermission)
2020
return result == PackageManager.PERMISSION_GRANTED
2121
}
2222

@@ -30,31 +30,28 @@ abstract class PermissionActivity : AppCompatActivity() {
3030
private fun buildAlertMessageNoGps() {
3131
val builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.CustomAlertDialogStyle))
3232
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
33-
.setCancelable(false)
34-
.setPositiveButton("Yes") { _: DialogInterface?, _: Int -> startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
35-
.setNegativeButton("No") { dialog: DialogInterface, _: Int -> dialog.cancel() }
33+
.setCancelable(false)
34+
.setPositiveButton("Yes") { _, _ -> startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
35+
.setNegativeButton("No") { dialog, _ -> dialog.cancel() }
3636
val alert = builder.create()
37-
alert.window!!.setBackgroundDrawableResource(android.R.color.transparent)
37+
alert.window?.setBackgroundDrawableResource(android.R.color.transparent)
3838
alert.show()
3939
}
4040

4141
fun requestPermission() {
42-
if (!checkPermission(Manifest.permission.ACCESS_FINE_LOCATION) ||
43-
!checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION) ||
44-
!checkPermission(Manifest.permission.CHANGE_WIFI_STATE) ||
45-
!checkPermission(Manifest.permission.BLUETOOTH) ||
46-
!checkPermission(Manifest.permission.BLUETOOTH_ADMIN) ||
47-
!checkPermission(Manifest.permission.BLUETOOTH_CONNECT) ||
48-
!checkPermission(Manifest.permission.BLUETOOTH_CONNECT)) {
49-
ActivityCompat.requestPermissions(this, arrayOf(
50-
Manifest.permission.CHANGE_WIFI_STATE,
51-
Manifest.permission.ACCESS_FINE_LOCATION,
52-
Manifest.permission.ACCESS_COARSE_LOCATION,
53-
Manifest.permission.BLUETOOTH,
54-
Manifest.permission.BLUETOOTH_ADMIN,
55-
Manifest.permission.BLUETOOTH_CONNECT,
56-
Manifest.permission.BLUETOOTH_SCAN,
57-
), PERMISSION_REQUEST_WIFI)
42+
val permissionsToRequest = mutableListOf(
43+
Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_FINE_LOCATION,
44+
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH,
45+
Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH_CONNECT,
46+
Manifest.permission.BLUETOOTH_SCAN
47+
)
48+
49+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU && !checkPermission(Manifest.permission.POST_NOTIFICATIONS)) {
50+
permissionsToRequest.add(Manifest.permission.POST_NOTIFICATIONS)
51+
}
52+
53+
if (permissionsToRequest.any { !checkPermission(it) }) {
54+
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), PERMISSION_REQUEST_WIFI)
5855
} else {
5956
statusCheck()
6057
}
@@ -63,8 +60,7 @@ abstract class PermissionActivity : AppCompatActivity() {
6360
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
6461
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
6562
if (requestCode == PERMISSION_REQUEST_WIFI) {
66-
if (grantResults.isNotEmpty()
67-
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
63+
if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
6864
statusCheck()
6965
}
7066
}

0 commit comments

Comments
 (0)