/* * Copyright (C) 2023 The Android Open Source Project * * 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 com.android.healthconnect.controller.permissions.request import android.app.Activity.RESULT_OK import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import androidx.preference.PreferenceFragmentCompat import com.android.healthconnect.controller.R import com.android.healthconnect.controller.permissions.data.HealthPermission import com.android.healthconnect.controller.permissions.data.PermissionState import com.android.healthconnect.controller.utils.increaseViewTouchTargetSize /** Base fragment class for permission request screens. */ abstract class PermissionsFragment : PreferenceFragmentCompat() { private lateinit var preferenceContainer: ViewGroup private lateinit var prefView: ViewGroup private lateinit var allowButton: Button private lateinit var dontAllowButton: Button // Places the preference fragment inside the preference container and allows us // to have the action buttons in the same fragment override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { val rootView = inflater.inflate(R.layout.fragment_permissions_request, container, false) prefView = rootView.findViewById(R.id.preference_container) preferenceContainer = super.onCreateView(inflater, container, savedInstanceState) as ViewGroup prefView.addView(preferenceContainer) allowButton = rootView.findViewById(R.id.allow) dontAllowButton = rootView.findViewById(R.id.dont_allow) val allowParentView = allowButton.parent.parent as View increaseViewTouchTargetSize(requireContext(), allowButton, allowParentView) val dontAllowParentView = dontAllowButton.parent as View increaseViewTouchTargetSize(requireContext(), dontAllowButton, dontAllowParentView) return rootView } fun getPackageNameExtra(): String { return requireActivity().intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME).orEmpty() } fun getAllowButton(): Button = allowButton fun getDontAllowButton(): Button = dontAllowButton fun handlePermissionResults(results: MutableMap) { val grants = mutableListOf() val permissionStrings = mutableListOf() for ((permission, state) in results) { if (state == PermissionState.GRANTED) { grants.add(PackageManager.PERMISSION_GRANTED) } else { grants.add(PackageManager.PERMISSION_DENIED) } permissionStrings.add(permission.toString()) } val result = Intent() result.putExtra( PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES, permissionStrings.toTypedArray()) result.putExtra(PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS, grants.toIntArray()) requireActivity().setResult(RESULT_OK, result) requireActivity().finish() } private fun getPermissionStrings(): Array { return requireActivity() .intent .getStringArrayExtra(PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) .orEmpty() } }