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.os.Bundle 24 import android.util.Log 25 import androidx.core.os.bundleOf 26 import androidx.fragment.app.DialogFragment 27 import androidx.fragment.app.activityViewModels 28 import com.android.healthconnect.controller.R 29 import com.android.healthconnect.controller.shared.Constants.EXTRA_APP_NAME 30 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder 31 import com.android.healthconnect.controller.utils.logging.AdditionalAccessElement.ENABLE_EXERCISE_PERMISSION_DIALOG_CONTAINER 32 import com.android.healthconnect.controller.utils.logging.AdditionalAccessElement.ENABLE_EXERCISE_PERMISSION_DIALOG_NEGATIVE_BUTTON 33 import com.android.healthconnect.controller.utils.logging.AdditionalAccessElement.ENABLE_EXERCISE_PERMISSION_DIALOG_POSITIVE_BUTTON 34 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger 35 import dagger.hilt.android.AndroidEntryPoint 36 import javax.inject.Inject 37 38 /** Dialog to ask user to enable [READ_EXERCISE] permission when enabling [READ_EXERCISE_ROUTES] */ 39 @AndroidEntryPoint(DialogFragment::class) 40 class EnableExercisePermissionDialog : Hilt_EnableExercisePermissionDialog() { 41 42 @Inject lateinit var logger: HealthConnectLogger 43 44 private lateinit var packageName: String 45 private lateinit var appName: String 46 47 private val viewModel: AdditionalAccessViewModel by activityViewModels() 48 49 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 50 51 val packageNameExtra = requireArguments().getString(EXTRA_PACKAGE_NAME) 52 if (packageNameExtra.isNullOrEmpty()) { 53 Log.e(TAG, "Invalid Intent Extras, finishing.") 54 return super.onCreateDialog(savedInstanceState) 55 } 56 packageName = packageNameExtra 57 58 val appNameExtra = requireArguments().getString(EXTRA_APP_NAME) 59 if (appNameExtra == null) { 60 Log.e(TAG, "Invalid Intent Extras, finishing.") 61 return super.onCreateDialog(savedInstanceState) 62 } 63 appName = appNameExtra 64 65 val dialog = 66 AlertDialogBuilder(this, ENABLE_EXERCISE_PERMISSION_DIALOG_CONTAINER) 67 .setTitle(R.string.exercise_permission_dialog_enable_title) 68 .setMessage(getString(R.string.exercise_permission_dialog_enabled_summary, appName)) 69 .setPositiveButton( 70 R.string.exercise_permission_dialog_positive_button, 71 ENABLE_EXERCISE_PERMISSION_DIALOG_POSITIVE_BUTTON) { _, _ -> 72 viewModel.enableExercisePermission(packageName) 73 dismiss() 74 } 75 .setNegativeButton( 76 R.string.exercise_permission_dialog_negative_button, 77 ENABLE_EXERCISE_PERMISSION_DIALOG_NEGATIVE_BUTTON) { _, _ -> 78 dismiss() 79 } 80 .create() 81 setCancelable(false) 82 dialog.setCanceledOnTouchOutside(false) 83 return dialog 84 } 85 86 override fun dismiss() { 87 viewModel.hideExercisePermissionRequestDialog() 88 super.dismiss() 89 } 90 91 companion object { 92 private const val TAG = "EnableExercisePDialog" 93 94 fun createDialog(packageName: String, appName: String): EnableExercisePermissionDialog { 95 return EnableExercisePermissionDialog().apply { 96 arguments = bundleOf(EXTRA_PACKAGE_NAME to packageName, EXTRA_APP_NAME to appName) 97 } 98 } 99 } 100 } 101