Skip to content

Commit

Permalink
add post notification permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Feb 16, 2024
1 parent 2f7f821 commit 5283723
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:name=".MainApplication"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import io.treehouses.remote.Constants
import io.treehouses.remote.InitialActivity
import io.treehouses.remote.R
Expand Down Expand Up @@ -90,7 +93,14 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler?
.setContentIntent(pendingClickIntent)
.addAction(R.drawable.bluetooth, "Disconnect", disconnectPendingIntent)
.build()
startForeground(2, notification)
ServiceCompat.startForeground(this,2,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
} else {
0
}
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import androidx.core.content.ContextCompat
import io.treehouses.remote.R

abstract class PermissionActivity : AppCompatActivity() {
private fun checkPermission(strPermission: String?): Boolean {
val result = ContextCompat.checkSelfPermission(this, strPermission!!)
private fun checkPermission(strPermission: String): Boolean {
val result = ContextCompat.checkSelfPermission(this, strPermission)
return result == PackageManager.PERMISSION_GRANTED
}

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

fun requestPermission() {
if (!checkPermission(Manifest.permission.ACCESS_FINE_LOCATION) ||
!checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION) ||
!checkPermission(Manifest.permission.CHANGE_WIFI_STATE) ||
!checkPermission(Manifest.permission.BLUETOOTH) ||
!checkPermission(Manifest.permission.BLUETOOTH_ADMIN) ||
!checkPermission(Manifest.permission.BLUETOOTH_CONNECT) ||
!checkPermission(Manifest.permission.BLUETOOTH_CONNECT)) {
ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN,
), PERMISSION_REQUEST_WIFI)
val permissionsToRequest = mutableListOf(
Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
)

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU && !checkPermission(Manifest.permission.POST_NOTIFICATIONS)) {
permissionsToRequest.add(Manifest.permission.POST_NOTIFICATIONS)
}

if (permissionsToRequest.any { !checkPermission(it) }) {
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), PERMISSION_REQUEST_WIFI)
} else {
statusCheck()
}
Expand All @@ -63,8 +60,7 @@ abstract class PermissionActivity : AppCompatActivity() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_WIFI) {
if (grantResults.isNotEmpty()
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
statusCheck()
}
}
Expand Down

0 comments on commit 5283723

Please sign in to comment.