diff --git a/app/src/main/java/com/hypertrack/android/interactors/PermissionsInteractor.kt b/app/src/main/java/com/hypertrack/android/interactors/PermissionsInteractor.kt index 02c6db14..20c56464 100644 --- a/app/src/main/java/com/hypertrack/android/interactors/PermissionsInteractor.kt +++ b/app/src/main/java/com/hypertrack/android/interactors/PermissionsInteractor.kt @@ -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( @@ -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(), ) } @@ -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") @@ -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 } \ No newline at end of file diff --git a/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsFragment.kt b/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsFragment.kt index b1184254..24ad0540 100644 --- a/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsFragment.kt +++ b/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsFragment.kt @@ -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(R.layout.fragment_background_permission) { +class BackgroundPermissionsFragment : + BaseFragment(R.layout.fragment_background_permission) { private val vm: BackgroundPermissionsViewModel by viewModels { MyApplication.injector.provideViewModelFactory(MyApplication.context) @@ -19,6 +27,22 @@ class BackgroundPermissionsFragment : BaseFragment(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) }) @@ -29,9 +53,9 @@ class BackgroundPermissionsFragment : BaseFragment(R.layout.fragme } override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResults: IntArray + requestCode: Int, + permissions: Array, + grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) vm.onPermissionResult(mainActivity()) diff --git a/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsViewModel.kt b/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsViewModel.kt index a7dc153f..98fae41f 100644 --- a/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsViewModel.kt +++ b/app/src/main/java/com/hypertrack/android/ui/screens/background_permissions/BackgroundPermissionsViewModel.kt @@ -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 @@ -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()) } } diff --git a/app/src/main/java/com/hypertrack/android/ui/screens/driver_id_input/DriverLoginViewModel.kt b/app/src/main/java/com/hypertrack/android/ui/screens/driver_id_input/DriverLoginViewModel.kt index 59f90680..4ab86177 100644 --- a/app/src/main/java/com/hypertrack/android/ui/screens/driver_id_input/DriverLoginViewModel.kt +++ b/app/src/main/java/com/hypertrack/android/ui/screens/driver_id_input/DriverLoginViewModel.kt @@ -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()) } } diff --git a/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestFragment.kt b/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestFragment.kt index a2599220..5ac29de0 100644 --- a/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestFragment.kt +++ b/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestFragment.kt @@ -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 @@ -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(btnWhitelisting, whitelistingMessage) .forEach { it.setGoneState(!visible) } } vm.showPermissionsButton.observe(viewLifecycleOwner) { show -> - listOf(btnContinue, permissionRationalMessage) - .forEach { it.setGoneState(!show) } + listOf(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()) diff --git a/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestViewModel.kt b/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestViewModel.kt index 0c20c4cb..bb2ace35 100644 --- a/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestViewModel.kt +++ b/app/src/main/java/com/hypertrack/android/ui/screens/permission_request/PermissionRequestViewModel.kt @@ -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 @@ -14,36 +13,49 @@ class PermissionRequestViewModel( private val hyperTrackService: HyperTrackService ) : ViewModel() { - val whitelistingRequired = MutableLiveData(!permissionsInteractor.isWhitelistingGranted()) + val showWhitelistingButton = + MutableLiveData(!permissionsInteractor.isWhitelistingGranted()) val showPermissionsButton = MutableLiveData(true) + val showSkipButton = MutableLiveData(false) val destination = MutableLiveData() 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()) + } } diff --git a/app/src/main/java/com/hypertrack/android/ui/screens/splash_screen/SplashScreenViewModel.kt b/app/src/main/java/com/hypertrack/android/ui/screens/splash_screen/SplashScreenViewModel.kt index a3e7eb53..bae1f026 100644 --- a/app/src/main/java/com/hypertrack/android/ui/screens/splash_screen/SplashScreenViewModel.kt +++ b/app/src/main/java/com/hypertrack/android/ui/screens/splash_screen/SplashScreenViewModel.kt @@ -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()) } } } diff --git a/app/src/main/res/layout/fragment_background_permission.xml b/app/src/main/res/layout/fragment_background_permission.xml index df8d8ee4..162fe8a8 100644 --- a/app/src/main/res/layout/fragment_background_permission.xml +++ b/app/src/main/res/layout/fragment_background_permission.xml @@ -6,12 +6,12 @@ android:layout_height="match_parent" android:background="@color/colorWhite"> + - + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 407aa822..d37fd709 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,7 +18,8 @@ Please use the link to login or contact your manager to get one Visits We need your permission to access location and activity data - We need your permission to access device location data while app is running in the background \n (to turn on tracking when new order is assigned to you) + We need your permission to access device location data while app is running in the background. It is used to automatically start tracking location based on your work schedule (without you having to open app) + To grant the permission press Allow Go to Settings PermissionRequestActivity @@ -90,4 +91,5 @@ Retry Unknown error during uploading photo Error when loading history + Skip diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 3781a34b..1dff935d 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -28,6 +28,11 @@ @drawable/bg_light_button + +