1 /*
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 
17 package com.android.healthconnect.controller.shared.inactiveapp
18 
19 import android.content.Context
20 import android.view.View.OnClickListener
21 import android.view.ViewGroup
22 import androidx.preference.PreferenceViewHolder
23 import com.android.healthconnect.controller.R
24 import com.android.healthconnect.controller.utils.logging.AppPermissionsElement
25 import com.android.healthconnect.controller.utils.logging.ElementName
26 import com.android.healthconnect.controller.utils.logging.ErrorPageElement
27 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
28 import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint
29 import com.android.settingslib.widget.AppPreference
30 import dagger.hilt.android.EntryPointAccessors
31 
32 /** Custom preference for displaying an inactive app. */
33 class InactiveAppPreference(context: Context) : AppPreference(context) {
34     private var deleteButtonListener: OnClickListener? = null
35 
36     private var logger: HealthConnectLogger
37     var logName: ElementName = ErrorPageElement.UNKNOWN_ELEMENT
38 
39     init {
40         widgetLayoutResource = R.layout.widget_delete_inactive_app
41         isSelectable = false
42         val hiltEntryPoint =
43             EntryPointAccessors.fromApplication(
44                 context.applicationContext,
45                 HealthConnectLoggerEntryPoint::class.java,
46             )
47         logger = hiltEntryPoint.logger()
48     }
49 
onAttachednull50     override fun onAttached() {
51         super.onAttached()
52         logger.logImpression(logName)
53         logger.logImpression(AppPermissionsElement.INACTIVE_APP_DELETE_BUTTON)
54     }
55 
onBindViewHoldernull56     override fun onBindViewHolder(holder: PreferenceViewHolder) {
57         super.onBindViewHolder(holder)
58 
59         val widgetFrame: ViewGroup? = holder.findViewById(android.R.id.widget_frame) as ViewGroup?
60         widgetFrame?.setOnClickListener(deleteButtonListener)
61         widgetFrame?.tag = "Delete button inactive app"
62 
63         val widgetFrameParent: ViewGroup? = widgetFrame?.parent as ViewGroup?
64         widgetFrameParent?.setPaddingRelative(
65             widgetFrameParent.paddingStart,
66             widgetFrameParent.paddingTop,
67             /* end = */ 0,
68             widgetFrameParent.paddingBottom,
69         )
70     }
71 
72     /** Sets the listener for delete button click. */
setOnDeleteButtonClickListenernull73     fun setOnDeleteButtonClickListener(listener: OnClickListener) {
74         val loggingClickListener = OnClickListener {
75             logger.logInteraction(AppPermissionsElement.INACTIVE_APP_DELETE_BUTTON)
76             listener.onClick(it)
77         }
78         deleteButtonListener = loggingClickListener
79         notifyChanged()
80     }
81 }
82