1 /*
<lambda>null2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *
17  */
18 
19 package com.android.healthconnect.controller.permissions.additionalaccess
20 
21 import android.app.Dialog
22 import android.content.Intent.EXTRA_PACKAGE_NAME
23 import android.health.connect.HealthPermissions.READ_EXERCISE
24 import android.os.Bundle
25 import android.util.Log
26 import androidx.core.os.bundleOf
27 import androidx.fragment.app.DialogFragment
28 import androidx.fragment.app.activityViewModels
29 import com.android.healthconnect.controller.R
30 import com.android.healthconnect.controller.permissions.app.AppPermissionViewModel
31 import com.android.healthconnect.controller.permissions.data.HealthPermission.FitnessPermission.Companion.fromPermissionString
32 import com.android.healthconnect.controller.shared.Constants.EXTRA_APP_NAME
33 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder
34 import com.android.healthconnect.controller.utils.logging.AdditionalAccessElement.DISABLE_EXERCISE_PERMISSION_DIALOG_CONTAINER
35 import com.android.healthconnect.controller.utils.logging.AdditionalAccessElement.DISABLE_EXERCISE_PERMISSION_DIALOG_NEGATIVE_BUTTON
36 import com.android.healthconnect.controller.utils.logging.AdditionalAccessElement.DISABLE_EXERCISE_PERMISSION_DIALOG_POSITIVE_BUTTON
37 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
38 import dagger.hilt.android.AndroidEntryPoint
39 import javax.inject.Inject
40 
41 /**
42  * Dialog to ask user to disable [READ_EXERCISE_ROUTES] permission when disabling [READ_EXERCISE]
43  */
44 @AndroidEntryPoint(DialogFragment::class)
45 class DisableExerciseRoutePermissionDialog : Hilt_DisableExerciseRoutePermissionDialog() {
46 
47     @Inject lateinit var logger: HealthConnectLogger
48 
49     private lateinit var packageName: String
50     private lateinit var appName: String
51 
52     private val viewModel: AppPermissionViewModel by activityViewModels()
53 
54     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
55 
56         val packageNameExtra = requireArguments().getString(EXTRA_PACKAGE_NAME)
57         if (packageNameExtra.isNullOrEmpty()) {
58             Log.e(TAG, "Invalid Intent Extras, finishing.")
59             return super.onCreateDialog(savedInstanceState)
60         }
61         packageName = packageNameExtra
62 
63         val appNameExtra = requireArguments().getString(EXTRA_APP_NAME)
64         if (appNameExtra == null) {
65             Log.e(TAG, "Invalid Intent Extras, finishing.")
66             return super.onCreateDialog(savedInstanceState)
67         }
68         appName = appNameExtra
69 
70         val dialog =
71             AlertDialogBuilder(this, DISABLE_EXERCISE_PERMISSION_DIALOG_CONTAINER)
72                 .setTitle(R.string.exercise_permission_dialog_disable_title)
73                 .setMessage(getString(R.string.exercise_permission_dialog_disable_summary, appName))
74                 .setPositiveButton(
75                     R.string.exercise_permission_dialog_positive_button,
76                     DISABLE_EXERCISE_PERMISSION_DIALOG_POSITIVE_BUTTON) { _, _ ->
77                         viewModel.disableExerciseRoutePermission(packageName)
78                         dismiss()
79                     }
80                 .setNegativeButton(
81                     R.string.exercise_permission_dialog_negative_button,
82                     DISABLE_EXERCISE_PERMISSION_DIALOG_NEGATIVE_BUTTON) { _, _ ->
83                         viewModel.updatePermission(
84                             packageName, fromPermissionString(READ_EXERCISE), grant = true)
85                         dismiss()
86                     }
87                 .create()
88         dialog.setCanceledOnTouchOutside(false)
89         setCancelable(false)
90         return dialog
91     }
92 
93     override fun dismiss() {
94         viewModel.hideExerciseRoutePermissionDialog()
95         super.dismiss()
96     }
97 
98     companion object {
99         private const val TAG = "DisableExercisePermDialog"
100 
101         fun createDialog(
102             packageName: String,
103             appName: String
104         ): DisableExerciseRoutePermissionDialog {
105             return DisableExerciseRoutePermissionDialog().apply {
106                 arguments = bundleOf(EXTRA_PACKAGE_NAME to packageName, EXTRA_APP_NAME to appName)
107             }
108         }
109     }
110 }
111