Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLEの権限周り実装 #4

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 51 additions & 31 deletions app/src/main/java/jp/co/tracecovid19/screen/home/BLEActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import jp.co.tracecovid19.data.repository.session.SessionRepository
import jp.co.tracecovid19.data.repository.trase.TraceRepository
import jp.co.tracecovid19.extension.setUpToolBar
import jp.co.tracecovid19.idmanager.TempIdManager
import jp.co.tracecovid19.screen.permission.BLEPermissionSettingFragment
import jp.co.tracecovid19.util.BLEUtil
import kotlinx.android.synthetic.main.activity_b_l_e.*
import kotlinx.android.synthetic.main.ble_contact_row_item.view.*
Expand All @@ -34,13 +35,17 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions
import kotlin.coroutines.CoroutineContext

class BLEActivity : AppCompatActivity(), CoroutineScope {

companion object {
const val TAG = "BLEActivity"
const val KEY = "jp.co.tracecovid19.screen.home.BLEActivity"
private const val PERMISSION_REQUEST_ACCESS_LOCATION = 222
private const val REQUEST_ENABLE_BT = 333
}

private val sessionRepository: SessionRepository by inject()
Expand All @@ -49,6 +54,12 @@ class BLEActivity : AppCompatActivity(), CoroutineScope {

private val contactList: MutableList<ContactModel> = mutableListOf()

private val bluetoothAdapter: BluetoothAdapter? by lazy(LazyThreadSafetyMode.NONE) {
val bluetoothManager =
this.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.adapter
}

private var job: Job = Job()

override val coroutineContext: CoroutineContext
Expand All @@ -72,8 +83,8 @@ class BLEActivity : AppCompatActivity(), CoroutineScope {
private fun initialize() {
setContentView(R.layout.activity_b_l_e)

if (initBluetoothAdapter() && initLocationManager()) {
BLEUtil.startBluetoothMonitoringService(this)
if (enableBluetooth()) {
setupPermissionsAndSettings()
} else {
finish()
}
Expand Down Expand Up @@ -128,46 +139,55 @@ class BLEActivity : AppCompatActivity(), CoroutineScope {
})
}

private fun initBluetoothAdapter(): Boolean {
val adapter = (getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
if (adapter == null) {
return false
} else {
if (!BluetoothAdapter.getDefaultAdapter().isEnabled) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
PERMISSION_REQUEST_ACCESS_LOCATION -> {
BLEUtil.startBluetoothMonitoringService(this)
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT) {
// BLEを許可しようがしまいが、先に進む
setupPermissionsAndSettings()
}
super.onActivityResult(requestCode, resultCode, data)
}

private fun enableBluetooth(): Boolean {
bluetoothAdapter?.let {
if (!it.isEnabled) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, 1)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
return false
}
return true
} ?: run {
// TODO: ここどうする?そもそもBLEが使えない機種の可能性がある
return false
}
}

@RequiresApi(api = Build.VERSION_CODES.M)
private fun initLocationManager(): Boolean {
val permissionCheck = ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
)
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(
this,
"BLE用の位置情報を有効にして下さい。",
Toast.LENGTH_SHORT
).show()
return false
@AfterPermissionGranted(PERMISSION_REQUEST_ACCESS_LOCATION)
private fun setupPermissionsAndSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val perms = BLEUtil.getRequiredPermissions()
if (EasyPermissions.hasPermissions(this, *perms)) {
BLEUtil.startBluetoothMonitoringService(this)
} else {
requestPermissions(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
), 1
)
return false
EasyPermissions.requestPermissions(
this, getString(R.string.permission_location_rationale), PERMISSION_REQUEST_ACCESS_LOCATION, *perms)
}
}
return true
}

}

class ContactViewAdapter(private val context: Context, private val contactList: List<ContactModel>) : RecyclerView.Adapter<ContactViewAdapter.ContactViewHolder>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
package jp.co.tracecovid19.screen.permission

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.Fragment
import jp.co.tracecovid19.R
import jp.co.tracecovid19.util.BLEUtil
import kotlinx.android.synthetic.main.fragment_tutorial_1.nextButton
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions

class BLEPermissionSettingFragment(private val navigator: PermissionSettingNavigator): Fragment() {

companion object {
const val KEY = "jp.co.tracecovid19.screen.setting.BLEPermissionSettingFragment"
private const val PERMISSION_REQUEST_ACCESS_LOCATION = 456
private const val REQUEST_ENABLE_BT = 123
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -30,12 +47,78 @@ class BLEPermissionSettingFragment(private val navigator: PermissionSettingNavig
setupViews()
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
PERMISSION_REQUEST_ACCESS_LOCATION -> {
goNext()
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT) {
// BLEを許可しようがしまいが、先に進む
setupPermissionsAndSettings()
}
super.onActivityResult(requestCode, resultCode, data)
}

private fun initialize() {
}

private fun setupViews() {
nextButton.setOnClickListener {
navigator.goToNext(PermissionSettingNavigator.PermissionSettingPageType.BLE)
if (enableBluetooth()) {
setupPermissionsAndSettings()
}
}
}

private val bluetoothAdapter: BluetoothAdapter? by lazy(LazyThreadSafetyMode.NONE) {
val bluetoothManager =
activity?.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.adapter
}

private fun enableBluetooth(): Boolean {
bluetoothAdapter?.let {
if (!it.isEnabled) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
return false
}
return true
} ?: run {
// TODO: ここどうする?そもそもBLEが使えない機種の可能性がある
return false
}
}

@AfterPermissionGranted(PERMISSION_REQUEST_ACCESS_LOCATION)
private fun setupPermissionsAndSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context?.let {
val perms = BLEUtil.getRequiredPermissions()
if (EasyPermissions.hasPermissions(it, *perms)) {
goNext()
} else {
EasyPermissions.requestPermissions(
this, getString(R.string.permission_location_rationale),
PERMISSION_REQUEST_ACCESS_LOCATION, *perms
)
}
}
}
}

private fun goNext() {
navigator.goToNext(PermissionSettingNavigator.PermissionSettingPageType.BLE)
}
}

1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
<string name="service_not_ok_title">Oh no! :(</string>
<string name="service_not_ok_body">J-Trace is not scanning.</string>
<string name="service_not_ok_action">Check app now</string>
<string name="permission_location_rationale">BLE 位置情報取得の許可をONにしてください.</string>

</resources>