1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * ``` 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * ``` 10 * 11 * Unless required by applicable law or agreed to in writing, software distributed under the License 12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 * or implied. See the License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.healthconnect.controller.permissions.request 17 18 import android.content.Context 19 import android.util.AttributeSet 20 import android.widget.TextView 21 import androidx.preference.Preference 22 import androidx.preference.PreferenceViewHolder 23 import com.android.healthconnect.controller.R 24 import com.android.healthconnect.controller.utils.boldAppName 25 import com.android.healthconnect.controller.utils.convertTextViewIntoLink 26 27 internal class RequestPermissionHeaderPreference 28 @JvmOverloads 29 constructor( 30 context: Context, 31 attrs: AttributeSet? = null, 32 defStyleAttr: Int = 0, 33 defStyleRes: Int = 0, 34 ) : Preference(context, attrs, defStyleAttr, defStyleRes) { 35 36 private lateinit var title: TextView 37 private lateinit var historyAccess: TextView 38 private lateinit var privacyPolicy: TextView 39 private var appName: String? = null 40 private var onRationaleLinkClicked: (() -> Unit)? = null 41 private var historyAccessGranted: Boolean = false 42 43 init { 44 layoutResource = R.layout.widget_request_permission_header 45 isSelectable = false 46 } 47 onBindViewHoldernull48 override fun onBindViewHolder(holder: PreferenceViewHolder) { 49 super.onBindViewHolder(holder) 50 title = holder.findViewById(R.id.title) as TextView 51 updateTitle() 52 historyAccess = holder.findViewById(R.id.history_access) as TextView 53 updateHistoryAccess() 54 privacyPolicy = holder.findViewById(R.id.privacy_policy) as TextView 55 updatePrivacyString() 56 } 57 bindnull58 fun bind(appName: String, historyAccessGranted: Boolean, onRationaleLinkClicked: () -> Unit) { 59 this.appName = appName 60 this.historyAccessGranted = historyAccessGranted 61 this.onRationaleLinkClicked = onRationaleLinkClicked 62 notifyChanged() 63 } 64 updateTitlenull65 private fun updateTitle() { 66 val text = context.getString(R.string.request_permissions_header_title, appName) 67 title.text = boldAppName(appName, text) 68 } 69 updateHistoryAccessnull70 private fun updateHistoryAccess() { 71 if (historyAccessGranted) { 72 historyAccess.text = 73 context.getString(R.string.request_permissions_header_time_frame_history_desc) 74 } else { 75 historyAccess.text = 76 context.getString(R.string.request_permissions_header_time_frame_desc) 77 } 78 } 79 updatePrivacyStringnull80 private fun updatePrivacyString() { 81 val policyString = context.getString(R.string.request_permissions_privacy_policy) 82 val rationaleText = 83 context.resources.getString( 84 R.string.request_permissions_rationale, appName, policyString) 85 convertTextViewIntoLink( 86 privacyPolicy, 87 rationaleText, 88 rationaleText.indexOf(policyString), 89 rationaleText.indexOf(policyString) + policyString.length) { 90 onRationaleLinkClicked?.invoke() 91 } 92 } 93 } 94