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.shared.preference 17 18 import android.content.Context 19 import android.util.AttributeSet 20 import androidx.preference.Preference 21 import androidx.preference.Preference.OnPreferenceClickListener 22 import com.android.healthconnect.controller.permissions.connectedapps.ComparablePreference 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 dagger.hilt.android.EntryPointAccessors 28 29 /** A [Preference] that allows logging. */ 30 open class HealthPreference 31 @JvmOverloads 32 constructor(context: Context, attrs: AttributeSet? = null) : 33 Preference(context, attrs), ComparablePreference { 34 35 private var logger: HealthConnectLogger 36 var logName: ElementName = ErrorPageElement.UNKNOWN_ELEMENT 37 38 init { 39 val hiltEntryPoint = 40 EntryPointAccessors.fromApplication( 41 context.applicationContext, HealthConnectLoggerEntryPoint::class.java) 42 logger = hiltEntryPoint.logger() 43 } 44 onAttachednull45 override fun onAttached() { 46 super.onAttached() 47 logger.logImpression(logName) 48 } 49 50 // TODO (b/270944053) - This does not currently work for preferences defined in XML 51 // because they don't have the log name when this method is called 52 // override fun onAttachedToHierarchy(preferenceManager: PreferenceManager?) { 53 // super.onAttachedToHierarchy(preferenceManager) 54 // logger.logImpression(logName) 55 // } 56 setOnPreferenceClickListenernull57 override fun setOnPreferenceClickListener( 58 onPreferenceClickListener: OnPreferenceClickListener? 59 ) { 60 val loggingClickListener = OnPreferenceClickListener { 61 logger.logInteraction(logName) 62 onPreferenceClickListener?.onPreferenceClick(it) ?: false 63 } 64 super.setOnPreferenceClickListener(loggingClickListener) 65 } 66 isSameItemnull67 override fun isSameItem(preference: Preference): Boolean { 68 return preference == this 69 } 70 hasSameContentsnull71 override fun hasSameContents(preference: Preference): Boolean { 72 return preference is HealthPreference && 73 this.title == preference.title && 74 this.summary == preference.summary && 75 this.icon == preference.icon && 76 this.isEnabled == preference.isEnabled 77 } 78 } 79