From 6f6bb3dbfea2f94a72efb9793d5deae5fb36b13e Mon Sep 17 00:00:00 2001
From: Maxime Naturel <maxime.naturel@niji.fr>
Date: Mon, 14 Mar 2022 17:20:15 +0100
Subject: [PATCH] Adding method to show dialog when permission is missing

---
 .../location/ILocationSharingNavigator.kt     | 22 +++++++++++++
 .../location/LocationSharingFragment.kt       | 18 +++++++++--
 .../location/LocationSharingNavigator.kt      | 31 +++++++++++++++++++
 vector/src/main/res/values/strings.xml        |  2 ++
 4 files changed, 71 insertions(+), 2 deletions(-)
 create mode 100644 vector/src/main/java/im/vector/app/features/location/ILocationSharingNavigator.kt
 create mode 100644 vector/src/main/java/im/vector/app/features/location/LocationSharingNavigator.kt

diff --git a/vector/src/main/java/im/vector/app/features/location/ILocationSharingNavigator.kt b/vector/src/main/java/im/vector/app/features/location/ILocationSharingNavigator.kt
new file mode 100644
index 0000000000..f86f622712
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/features/location/ILocationSharingNavigator.kt
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2022 New Vector Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package im.vector.app.features.location
+
+interface ILocationSharingNavigator {
+    fun quit()
+    fun goToAppSettings()
+}
diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt
index 399f8e4419..7e7303c1f3 100644
--- a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt
@@ -50,6 +50,8 @@ class LocationSharingFragment @Inject constructor(
 
     private val viewModel: LocationSharingViewModel by fragmentViewModel()
 
+    private val viewNavigator: ILocationSharingNavigator by lazy { LocationSharingNavigator(activity) }
+
     // Keep a ref to handle properly the onDestroy callback
     private var mapView: WeakReference<MapView>? = null
 
@@ -78,7 +80,7 @@ class LocationSharingFragment @Inject constructor(
         viewModel.observeViewEvents {
             when (it) {
                 LocationSharingViewEvents.LocationNotAvailableError -> handleLocationNotAvailableError()
-                LocationSharingViewEvents.Close                     -> activity?.finish()
+                LocationSharingViewEvents.Close                     -> viewNavigator.quit()
                 is LocationSharingViewEvents.ZoomToUserLocation     -> handleZoomToUserLocationEvent(it)
             }.exhaustive
         }
@@ -138,12 +140,24 @@ class LocationSharingFragment @Inject constructor(
                 .setTitle(R.string.location_not_available_dialog_title)
                 .setMessage(R.string.location_not_available_dialog_content)
                 .setPositiveButton(R.string.ok) { _, _ ->
-                    activity?.finish()
+                    viewNavigator.quit()
                 }
                 .setCancelable(false)
                 .show()
     }
 
+    private fun handleMissingBackgroundLocationPermission() {
+        MaterialAlertDialogBuilder(requireActivity())
+                .setTitle(R.string.location_in_background_missing_permission_dialog_title)
+                .setMessage(R.string.location_in_background_missing_permission_dialog_content)
+                .setPositiveButton(R.string.settings) { _, _ ->
+                    viewNavigator.goToAppSettings()
+                }
+                .setNegativeButton(R.string.action_not_now, null)
+                .setCancelable(false)
+                .show()
+    }
+
     private fun initLocateButton() {
         views.mapView.locateButton.setOnClickListener {
             viewModel.handle(LocationSharingAction.ZoomToUserLocation)
diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingNavigator.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingNavigator.kt
new file mode 100644
index 0000000000..c474cd6b58
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingNavigator.kt
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2022 New Vector Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package im.vector.app.features.location
+
+import android.app.Activity
+import im.vector.app.core.utils.openAppSettingsPage
+
+class LocationSharingNavigator constructor(val activity: Activity?): ILocationSharingNavigator {
+
+    override fun quit() {
+        activity?.finish()
+    }
+
+    override fun goToAppSettings() {
+        activity?.let { openAppSettingsPage(it) }
+    }
+}
diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml
index 162ab3e119..9ec2cda85f 100644
--- a/vector/src/main/res/values/strings.xml
+++ b/vector/src/main/res/values/strings.xml
@@ -2937,6 +2937,8 @@
     <string name="a11y_location_share_option_user_live_icon">Share live location</string>
     <string name="location_share_option_pinned">Share this location</string>
     <string name="a11y_location_share_option_pinned_icon">Share this location</string>
+    <string name="location_in_background_missing_permission_dialog_title">Allow access</string>
+    <string name="location_in_background_missing_permission_dialog_content">If you’d like to share your Live location, ${app_name} needs location access when the app is in the background.\nWe will only access your location for the duration that you choose.</string>
     <string name="location_not_available_dialog_title">${app_name} could not access your location</string>
     <string name="location_not_available_dialog_content">${app_name} could not access your location. Please try again later.</string>
     <string name="location_share_external">Open with</string>