1 /* <lambda>null2 * 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.OnPreferenceChangeListener 21 import androidx.preference.SwitchPreference 22 import com.android.healthconnect.controller.utils.logging.ElementName 23 import com.android.healthconnect.controller.utils.logging.ErrorPageElement 24 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger 25 import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint 26 import com.android.healthconnect.controller.utils.logging.UIAction 27 import dagger.hilt.android.EntryPointAccessors 28 29 /** A [SwitchPreference] that allows logging. */ 30 open class HealthSwitchPreference 31 @JvmOverloads 32 constructor(context: Context, attrs: AttributeSet? = null) : SwitchPreference(context, attrs) { 33 34 private var logger: HealthConnectLogger 35 var logNameActive: ElementName = ErrorPageElement.UNKNOWN_ELEMENT 36 var logNameInactive: ElementName = ErrorPageElement.UNKNOWN_ELEMENT 37 private var loggingClickListener: OnPreferenceChangeListener? = null 38 39 init { 40 val hiltEntryPoint = 41 EntryPointAccessors.fromApplication( 42 context.applicationContext, HealthConnectLoggerEntryPoint::class.java) 43 logger = hiltEntryPoint.logger() 44 } 45 46 override fun onAttached() { 47 super.onAttached() 48 if (isChecked) { 49 logger.logImpression(logNameActive) 50 } else { 51 logger.logImpression(logNameInactive) 52 } 53 } 54 55 override fun setOnPreferenceChangeListener( 56 onPreferenceChangeListener: OnPreferenceChangeListener? 57 ) { 58 loggingClickListener = OnPreferenceChangeListener { preference, newValue -> 59 if (newValue is Boolean && newValue) { 60 logger.logInteraction(logNameInactive, UIAction.ACTION_TOGGLE_ON) 61 } else if (newValue is Boolean) { 62 logger.logInteraction(logNameActive, UIAction.ACTION_TOGGLE_OFF) 63 } 64 onPreferenceChangeListener?.onPreferenceChange(preference, newValue)!! 65 } 66 super.setOnPreferenceChangeListener(loggingClickListener) 67 } 68 } 69