blob: 65734349b00e000d849ddfda843c81a1f02a7d32 [file] [log] [blame]
csitari315e6ee2022-12-04 01:45:13 +00001/*
2 * Copyright (C) 2022 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
17package com.android.healthconnect.controller.shared.inactiveapp
18
19import android.content.Context
20import android.view.View.OnClickListener
21import android.view.ViewGroup
csitari315e6ee2022-12-04 01:45:13 +000022import androidx.preference.PreferenceViewHolder
23import com.android.healthconnect.controller.R
Teo Georgescufc252b72023-02-27 13:58:23 +000024import com.android.healthconnect.controller.utils.logging.ElementName
25import com.android.healthconnect.controller.utils.logging.ErrorPageElement
26import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
27import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint
Magdie87b64d2022-12-12 23:04:55 +000028import com.android.settingslib.widget.AppPreference
Teo Georgescufc252b72023-02-27 13:58:23 +000029import dagger.hilt.android.EntryPointAccessors
csitari315e6ee2022-12-04 01:45:13 +000030
31/** Custom preference for displaying an inactive app. */
Magdie87b64d2022-12-12 23:04:55 +000032class InactiveAppPreference constructor(context: Context) : AppPreference(context) {
csitari315e6ee2022-12-04 01:45:13 +000033 private var deleteButtonListener: OnClickListener? = null
34
Teo Georgescufc252b72023-02-27 13:58:23 +000035 private var logger: HealthConnectLogger
36 var logName : ElementName = ErrorPageElement.UNKNOWN_ELEMENT
37
csitari315e6ee2022-12-04 01:45:13 +000038 init {
39 widgetLayoutResource = R.layout.widget_delete_inactive_app
40 isSelectable = false
Teo Georgescufc252b72023-02-27 13:58:23 +000041 val hiltEntryPoint =
42 EntryPointAccessors.fromApplication(
43 context.applicationContext, HealthConnectLoggerEntryPoint::class.java)
44 logger = hiltEntryPoint.logger()
45 }
46
47 override fun onAttached() {
48 super.onAttached()
49 logger.logImpression(logName)
csitari315e6ee2022-12-04 01:45:13 +000050 }
51
Magdib0b2f5f2023-07-21 09:03:30 +000052 override fun onBindViewHolder(holder: PreferenceViewHolder) {
csitari315e6ee2022-12-04 01:45:13 +000053 super.onBindViewHolder(holder)
54
Magdib0b2f5f2023-07-21 09:03:30 +000055 val widgetFrame: ViewGroup? = holder.findViewById(android.R.id.widget_frame) as ViewGroup?
csitari315e6ee2022-12-04 01:45:13 +000056 widgetFrame?.setOnClickListener(deleteButtonListener)
57
58 val widgetFrameParent: ViewGroup? = widgetFrame?.parent as ViewGroup?
59 widgetFrameParent?.setPaddingRelative(
60 widgetFrameParent.paddingStart,
61 widgetFrameParent.paddingTop,
62 /* end = */ 0,
63 widgetFrameParent.paddingBottom)
64 }
65
66 /** Sets the listener for delete button click. */
67 fun setOnDeleteButtonClickListener(listener: OnClickListener) {
Teo Georgescufc252b72023-02-27 13:58:23 +000068 val loggingClickListener = OnClickListener {
69 logger.logInteraction(logName)
70 listener.onClick(it)
71 }
72 deleteButtonListener = loggingClickListener
csitari315e6ee2022-12-04 01:45:13 +000073 notifyChanged()
74 }
75}