Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/add_backgr_loc_option_hi…
Browse files Browse the repository at this point in the history
…nt' into hotfix/v0.3.1
  • Loading branch information
deniszpua committed Mar 3, 2021
2 parents 3546d49 + ca9bde0 commit 2db695d
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import com.hypertrack.android.utils.MyApplication
import com.judemanutd.autostarter.AutoStartPermissionHelper

interface PermissionsInteractor {
fun checkPermissionState(activity: Activity): PermissionState
fun isWhitelistingGranted(): Boolean
fun checkPermissionsState(activity: Activity): PermissionsState
fun requestWhitelisting(activity: Activity)
fun requestRequiredPermissions(activity: Activity)
fun requestBackgroundLocationPermission(activity: Activity)
fun isWhitelistingGranted(): Boolean
fun isBackgroundLocationGranted(): Boolean
fun isBasePermissionsGranted(): Boolean
}

class PermissionsInteractorImpl(
Expand All @@ -23,20 +25,11 @@ class PermissionsInteractorImpl(

private val autostarter = AutoStartPermissionHelper.getInstance()

override fun checkPermissionState(activity: Activity): PermissionState {
return PermissionState(
activityTrackingGranted = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
hasPermission(Manifest.permission.ACTIVITY_RECOGNITION)
} else {
true
},
override fun checkPermissionsState(activity: Activity): PermissionsState {
return PermissionsState(
activityTrackingGranted = isActivityGranted(),
foregroundLocationGranted = hasPermission(Manifest.permission.ACCESS_FINE_LOCATION),
backgroundLocationGranted = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
hasPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
true
},
whitelistingGranted = isWhitelistingGranted(),
backgroundLocationGranted = isBackgroundLocationGranted(),
)
}

Expand All @@ -45,6 +38,27 @@ class PermissionsInteractorImpl(
return !applicable || (applicable && accountRepository.wasWhitelisted)
}

override fun isBasePermissionsGranted(): Boolean {
return isActivityGranted() && hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)
}

private fun isActivityGranted(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
hasPermission(Manifest.permission.ACTIVITY_RECOGNITION)
} else {
true
}
}

override fun isBackgroundLocationGranted(): Boolean {
//we don't need ACCESS_BACKGROUND_LOCATION for Q
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
hasPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
true
}
}

override fun requestWhitelisting(activity: Activity) {
val granted = AutoStartPermissionHelper.getInstance().getAutoStartPermission(activity)
// Log.d(PermissionRequestViewModel.TAG, "AutoStart granted value is $granted")
Expand Down Expand Up @@ -78,22 +92,20 @@ class PermissionsInteractorImpl(

}

class PermissionState(
class PermissionsState(
val activityTrackingGranted: Boolean,
val foregroundLocationGranted: Boolean,
val backgroundLocationGranted: Boolean,
val whitelistingGranted: Boolean
) {
fun getDestination(): PermissionDestination {
fun getNextPermissionRequest(): PermissionDestination {
return when {
!foregroundLocationGranted || !activityTrackingGranted -> PermissionDestination.FOREGROUND_AND_TRACKING
!whitelistingGranted -> PermissionDestination.WHITELISTING
// !backgroundLocationGranted -> PermissionDestination.BACKGROUND todo
!backgroundLocationGranted -> PermissionDestination.BACKGROUND
else -> PermissionDestination.PASS
}
}
}

enum class PermissionDestination {
PASS, FOREGROUND_AND_TRACKING, BACKGROUND, WHITELISTING
PASS, FOREGROUND_AND_TRACKING, BACKGROUND
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.hypertrack.android.ui.screens.background_permissions

import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.StyleSpan
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.hypertrack.android.ui.MainActivity
import com.hypertrack.android.ui.base.BaseFragment
import com.hypertrack.android.ui.common.hide
import com.hypertrack.android.utils.MyApplication
import com.hypertrack.logistics.android.github.R
import kotlinx.android.synthetic.main.fragment_background_permission.*

class BackgroundPermissionsFragment : BaseFragment<MainActivity>(R.layout.fragment_background_permission) {
class BackgroundPermissionsFragment :
BaseFragment<MainActivity>(R.layout.fragment_background_permission) {

private val vm: BackgroundPermissionsViewModel by viewModels {
MyApplication.injector.provideViewModelFactory(MyApplication.context)
Expand All @@ -19,6 +27,22 @@ class BackgroundPermissionsFragment : BaseFragment<MainActivity>(R.layout.fragme
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val hint = getString(R.string.background_location_permission_option_hint)
val sb =
SpannableStringBuilder(hint + "\n" + MyApplication.context.packageManager.backgroundPermissionOptionLabel)

sb.setSpan(
StyleSpan(Typeface.BOLD),
hint.length,
sb.length,
Spannable.SPAN_INCLUSIVE_INCLUSIVE
); // make first 4 characters Bold
tvOptionHint.text = sb
} else {
tvOptionHint.hide()
}

vm.destination.observe(viewLifecycleOwner, {
findNavController().navigate(it)
})
Expand All @@ -29,9 +53,9 @@ class BackgroundPermissionsFragment : BaseFragment<MainActivity>(R.layout.fragme
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
vm.onPermissionResult(mainActivity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.lifecycle.ViewModel
import androidx.navigation.NavDirections
import com.hypertrack.android.interactors.PermissionDestination
import com.hypertrack.android.interactors.PermissionsInteractor
import com.hypertrack.android.ui.MainActivity

class BackgroundPermissionsViewModel(
private val permissionsInteractor: PermissionsInteractor
Expand All @@ -19,18 +18,15 @@ class BackgroundPermissionsViewModel(
}

fun onPermissionResult(activity: Activity) {
when (permissionsInteractor.checkPermissionState(activity).getDestination()) {
when (permissionsInteractor.checkPermissionsState(activity).getNextPermissionRequest()) {
PermissionDestination.PASS -> {
destination.postValue(BackgroundPermissionsFragmentDirections.actionBackgroundPermissionsFragmentToPermissionRequestFragment())
destination.postValue(BackgroundPermissionsFragmentDirections.actionBackgroundPermissionsFragmentToVisitManagementFragment())
}
PermissionDestination.FOREGROUND_AND_TRACKING -> {
destination.postValue(BackgroundPermissionsFragmentDirections.actionBackgroundPermissionsFragmentToVisitManagementFragment())
destination.postValue(BackgroundPermissionsFragmentDirections.actionBackgroundPermissionsFragmentToPermissionRequestFragment())
}
PermissionDestination.BACKGROUND -> {
//todo should we let user proceed?
}
PermissionDestination.WHITELISTING -> {
destination.postValue(BackgroundPermissionsFragmentDirections.actionBackgroundPermissionsFragmentToPermissionRequestFragment())
destination.postValue(BackgroundPermissionsFragmentDirections.actionBackgroundPermissionsFragmentToVisitManagementFragment())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ class DriverLoginViewModel(
// Log.d(TAG, "Proceeding with Driver Id $driverId")
hyperTrackService.driverId = driverId
driverRepository.driverId = driverId
when (permissionsInteractor.checkPermissionState(activity).getDestination()) {
when (permissionsInteractor.checkPermissionsState(activity)
.getNextPermissionRequest()) {
PermissionDestination.PASS -> {
destination.postValue(DriverIdInputFragmentDirections.actionDriverIdInputFragmentToVisitManagementFragment())
}
PermissionDestination.FOREGROUND_AND_TRACKING,
PermissionDestination.WHITELISTING -> {
destination.postValue(DriverIdInputFragmentDirections.actionDriverIdInputFragmentToPermissionRequestFragment())
}
PermissionDestination.BACKGROUND -> {
destination.postValue(DriverIdInputFragmentDirections.actionDriverIdInputFragmentToBackgroundPermissionsFragment())
destination.postValue(DriverIdInputFragmentDirections.actionDriverIdInputFragmentToPermissionRequestFragment())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.hypertrack.android.ui.screens.permission_request

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
Expand All @@ -25,17 +23,24 @@ class PermissionRequestFragment : ProgressDialogFragment(R.layout.fragment_permi
findNavController().navigate(it)
})

vm.whitelistingRequired.observe(viewLifecycleOwner) { visible ->
vm.showSkipButton.observe(viewLifecycleOwner) { visible ->
btnSkip.setGoneState(!visible)
}


vm.showWhitelistingButton.observe(viewLifecycleOwner) { visible ->
listOf<View>(btnWhitelisting, whitelistingMessage)
.forEach { it.setGoneState(!visible) }
}

vm.showPermissionsButton.observe(viewLifecycleOwner) { show ->
listOf<View>(btnContinue, permissionRationalMessage)
.forEach { it.setGoneState(!show) }
listOf<View>(btnAllow, permissionRationalMessage)
.forEach { it.setGoneState(!show) }
}

btnContinue.setOnClickListener { vm.requestPermissions(mainActivity()) }
btnSkip.setOnClickListener { vm.onSkipClicked() }

btnAllow.setOnClickListener { vm.requestPermissions(mainActivity()) }

btnWhitelisting.setOnClickListener {
vm.requestWhitelisting(mainActivity())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hypertrack.android.ui.screens.permission_request

import android.app.Activity
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.NavDirections
Expand All @@ -14,36 +13,49 @@ class PermissionRequestViewModel(
private val hyperTrackService: HyperTrackService
) : ViewModel() {

val whitelistingRequired = MutableLiveData<Boolean>(!permissionsInteractor.isWhitelistingGranted())
val showWhitelistingButton =
MutableLiveData<Boolean>(!permissionsInteractor.isWhitelistingGranted())
val showPermissionsButton = MutableLiveData<Boolean>(true)
val showSkipButton = MutableLiveData<Boolean>(false)

val destination = MutableLiveData<NavDirections>()

fun requestWhitelisting(activity: Activity) {
permissionsInteractor.requestWhitelisting(activity)
whitelistingRequired.postValue(!permissionsInteractor.isWhitelistingGranted())
showWhitelistingButton.postValue(!permissionsInteractor.isWhitelistingGranted())
}

private fun onPermissionResult(activity: Activity) {
when (permissionsInteractor.checkPermissionState(activity).getDestination()) {
PermissionDestination.PASS -> {
syncDeviceSettings()
destination.postValue(PermissionRequestFragmentDirections.actionPermissionRequestFragmentToVisitManagementFragment())
}
PermissionDestination.FOREGROUND_AND_TRACKING -> {
showPermissionsButton.postValue(true)
}
PermissionDestination.BACKGROUND -> {
syncDeviceSettings()
destination.postValue(PermissionRequestFragmentDirections.actionPermissionRequestFragmentToBackgroundPermissionsFragment())
}
PermissionDestination.WHITELISTING -> {
syncDeviceSettings()
showPermissionsButton.postValue(false)
permissionsInteractor.checkPermissionsState(activity).let {
when (permissionsInteractor.checkPermissionsState(activity)
.getNextPermissionRequest()) {
PermissionDestination.FOREGROUND_AND_TRACKING -> {
showPermissionsButton.postValue(true)
}
PermissionDestination.BACKGROUND -> {
syncDeviceSettings()
destination.postValue(PermissionRequestFragmentDirections.actionPermissionRequestFragmentToBackgroundPermissionsFragment())
}
PermissionDestination.PASS -> {
syncDeviceSettings()
if (permissionsInteractor.isWhitelistingGranted()) {
destination.postValue(PermissionRequestFragmentDirections.actionPermissionRequestFragmentToVisitManagementFragment())
}
}
}

showPermissionsButton.postValue(!permissionsInteractor.isBasePermissionsGranted())
showWhitelistingButton.postValue(!permissionsInteractor.isWhitelistingGranted())
showSkipButton.postValue(it.foregroundLocationGranted)
}
}

whitelistingRequired.postValue(!permissionsInteractor.isWhitelistingGranted())
fun onSkipClicked() {
if (permissionsInteractor.isBackgroundLocationGranted()) {
destination.postValue(PermissionRequestFragmentDirections.actionGlobalVisitManagementFragment())
} else {
destination.postValue(PermissionRequestFragmentDirections.actionPermissionRequestFragmentToBackgroundPermissionsFragment())
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ class SplashScreenViewModel(
driverRepository.hasDriverId -> {
// already logged in
crashReportsProvider.setUserIdentifier(driverRepository.driverId)
when (permissionsInteractor.checkPermissionState(activity).getDestination()) {
when (permissionsInteractor.checkPermissionsState(activity)
.getNextPermissionRequest()) {
PermissionDestination.PASS -> {
destination.postValue(SplashScreenFragmentDirections.actionGlobalVisitManagementFragment())
}
PermissionDestination.FOREGROUND_AND_TRACKING,
PermissionDestination.WHITELISTING -> {
destination.postValue(SplashScreenFragmentDirections.actionSplashScreenFragmentToPermissionRequestFragment())
}
PermissionDestination.BACKGROUND -> {
destination.postValue(SplashScreenFragmentDirections.actionSplashScreenFragmentToBackgroundPermissionsFragment())
destination.postValue(SplashScreenFragmentDirections.actionSplashScreenFragmentToPermissionRequestFragment())
}
}
}
Expand Down
Loading

0 comments on commit 2db695d

Please sign in to comment.