1 /* 2 * Copyright (C) 2023 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 * http://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 package com.android.healthconnect.controller.permissions.connectedapps 17 18 import android.content.Context 19 import android.text.TextUtils 20 import androidx.preference.Preference 21 import androidx.preference.PreferenceViewHolder 22 import com.android.healthconnect.controller.shared.app.AppMetadata 23 import com.android.healthconnect.controller.utils.logging.ElementName 24 import com.android.healthconnect.controller.utils.logging.ErrorPageElement 25 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger 26 import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint 27 import com.android.settingslib.widget.AppPreference 28 import dagger.hilt.android.EntryPointAccessors 29 30 class HealthAppPreference(context: Context, private val appMetadata: AppMetadata) : 31 AppPreference(context), ComparablePreference { 32 33 private var logger: HealthConnectLogger 34 var logName : ElementName = ErrorPageElement.UNKNOWN_ELEMENT 35 36 init { 37 title = appMetadata.appName 38 icon = appMetadata.icon 39 40 val hiltEntryPoint = 41 EntryPointAccessors.fromApplication( 42 context.applicationContext, HealthConnectLoggerEntryPoint::class.java) 43 logger = hiltEntryPoint.logger() 44 } 45 onAttachednull46 override fun onAttached() { 47 super.onAttached() 48 logger.logImpression(logName) 49 } 50 setOnPreferenceClickListenernull51 override fun setOnPreferenceClickListener( 52 onPreferenceClickListener: OnPreferenceClickListener? 53 ) { 54 val loggingClickListener = OnPreferenceClickListener { 55 logger.logInteraction(logName) 56 onPreferenceClickListener?.onPreferenceClick(it) ?: false 57 } 58 super.setOnPreferenceClickListener(loggingClickListener) 59 } 60 isSameItemnull61 override fun isSameItem(preference: Preference): Boolean { 62 return preference is HealthAppPreference && 63 TextUtils.equals(appMetadata.appName, preference.appMetadata.appName) 64 } 65 hasSameContentsnull66 override fun hasSameContents(preference: Preference): Boolean { 67 return preference is HealthAppPreference && appMetadata == preference.appMetadata 68 } 69 onBindViewHoldernull70 override fun onBindViewHolder(view: PreferenceViewHolder) { 71 super.onBindViewHolder(view) 72 } 73 } 74 75 /** Allows comparison with a [Preference] to determine if it has been changed. */ 76 internal interface ComparablePreference { 77 /** Returns true if given Preference represents an item of the same kind. */ isSameItemnull78 fun isSameItem(preference: Preference): Boolean 79 80 /** Returns true if given Preference contains the same data. */ 81 fun hasSameContents(preference: Preference): Boolean 82 } 83