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