csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 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 |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 22 | import androidx.preference.PreferenceViewHolder |
| 23 | import com.android.healthconnect.controller.R |
Teo Georgescu | fc252b7 | 2023-02-27 13:58:23 +0000 | [diff] [blame] | 24 | import com.android.healthconnect.controller.utils.logging.ElementName |
| 25 | import com.android.healthconnect.controller.utils.logging.ErrorPageElement |
| 26 | import com.android.healthconnect.controller.utils.logging.HealthConnectLogger |
| 27 | import com.android.healthconnect.controller.utils.logging.HealthConnectLoggerEntryPoint |
Magdi | e87b64d | 2022-12-12 23:04:55 +0000 | [diff] [blame] | 28 | import com.android.settingslib.widget.AppPreference |
Teo Georgescu | fc252b7 | 2023-02-27 13:58:23 +0000 | [diff] [blame] | 29 | import dagger.hilt.android.EntryPointAccessors |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 30 | |
| 31 | /** Custom preference for displaying an inactive app. */ |
Magdi | e87b64d | 2022-12-12 23:04:55 +0000 | [diff] [blame] | 32 | class InactiveAppPreference constructor(context: Context) : AppPreference(context) { |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 33 | private var deleteButtonListener: OnClickListener? = null |
| 34 | |
Teo Georgescu | fc252b7 | 2023-02-27 13:58:23 +0000 | [diff] [blame] | 35 | private var logger: HealthConnectLogger |
| 36 | var logName : ElementName = ErrorPageElement.UNKNOWN_ELEMENT |
| 37 | |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 38 | init { |
| 39 | widgetLayoutResource = R.layout.widget_delete_inactive_app |
| 40 | isSelectable = false |
Teo Georgescu | fc252b7 | 2023-02-27 13:58:23 +0000 | [diff] [blame] | 41 | 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) |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Magdi | b0b2f5f | 2023-07-21 09:03:30 +0000 | [diff] [blame] | 52 | override fun onBindViewHolder(holder: PreferenceViewHolder) { |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 53 | super.onBindViewHolder(holder) |
| 54 | |
Magdi | b0b2f5f | 2023-07-21 09:03:30 +0000 | [diff] [blame] | 55 | val widgetFrame: ViewGroup? = holder.findViewById(android.R.id.widget_frame) as ViewGroup? |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 56 | 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 Georgescu | fc252b7 | 2023-02-27 13:58:23 +0000 | [diff] [blame] | 68 | val loggingClickListener = OnClickListener { |
| 69 | logger.logInteraction(logName) |
| 70 | listener.onClick(it) |
| 71 | } |
| 72 | deleteButtonListener = loggingClickListener |
csitari | 315e6ee | 2022-12-04 01:45:13 +0000 | [diff] [blame] | 73 | notifyChanged() |
| 74 | } |
| 75 | } |