• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constructor(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, HealthConnectLoggerEntryPoint::class.java)
45         logger = hiltEntryPoint.logger()
46     }
47 
onAttachednull48     override fun onAttached() {
49         super.onAttached()
50         logger.logImpression(logName)
51         logger.logImpression(AppPermissionsElement.INACTIVE_APP_DELETE_BUTTON)
52     }
53 
onBindViewHoldernull54     override fun onBindViewHolder(holder: PreferenceViewHolder) {
55         super.onBindViewHolder(holder)
56 
57         val widgetFrame: ViewGroup? = holder.findViewById(android.R.id.widget_frame) as ViewGroup?
58         widgetFrame?.setOnClickListener(deleteButtonListener)
59         widgetFrame?.tag = "Delete button inactive app"
60 
61         val widgetFrameParent: ViewGroup? = widgetFrame?.parent as ViewGroup?
62         widgetFrameParent?.setPaddingRelative(
63             widgetFrameParent.paddingStart,
64             widgetFrameParent.paddingTop,
65             /* end = */ 0,
66             widgetFrameParent.paddingBottom)
67     }
68 
69     /** Sets the listener for delete button click. */
setOnDeleteButtonClickListenernull70     fun setOnDeleteButtonClickListener(listener: OnClickListener) {
71         val loggingClickListener = OnClickListener {
72             logger.logInteraction(AppPermissionsElement.INACTIVE_APP_DELETE_BUTTON)
73             listener.onClick(it)
74         }
75         deleteButtonListener = loggingClickListener
76         notifyChanged()
77     }
78 }
79