• 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 package com.android.healthconnect.controller.shared.preference
17 
18 import android.content.Context
19 import android.view.View
20 import android.view.View.OnClickListener
21 import android.widget.TextView
22 import androidx.preference.Preference
23 import androidx.preference.PreferenceViewHolder
24 import com.android.healthconnect.controller.R
25 import com.android.healthconnect.controller.utils.convertTextViewIntoLink
26 
27 /** Custom preference for the headers containing a link on U and below. From V, TopIntroPreference supports link, see [topIntroPreference]. */
28 internal class LegacyTopIntroPreference constructor(context: Context) : Preference(context) {
29 
30     private lateinit var headerTitle: TextView
31     private lateinit var headerLink: TextView
32 
33     private var headerText: String? = null
34     private var headerLinkText: String? = null
35     private var linkAction: OnClickListener? = null
36 
37     init {
38         layoutResource = R.layout.widget_header_preference
39         isSelectable = false
40     }
41 
setTitlenull42     fun setTitle(headerText: String) {
43         this.headerText = headerText
44     }
45 
setLearnMoreTextnull46     fun setLearnMoreText(headerLinkText: String) {
47         this.headerLinkText = headerLinkText
48     }
49 
setLearnMoreActionnull50     fun setLearnMoreAction(onClickListener: OnClickListener) {
51         this.linkAction = onClickListener
52     }
53 
onBindViewHoldernull54     override fun onBindViewHolder(holder: PreferenceViewHolder) {
55         super.onBindViewHolder(holder)
56 
57         headerTitle = holder.findViewById(R.id.header_title) as TextView
58         headerTitle.text = headerText
59 
60         headerLink = holder.findViewById(R.id.header_link) as TextView
61         if (headerLinkText != null) {
62             headerLink.visibility = View.VISIBLE
63             convertTextViewIntoLink(
64                 headerLink,
65                 headerLinkText,
66                 0,
67                 headerLinkText!!.length,
68                 linkAction,
69             )
70         } else {
71             headerLink.visibility = View.GONE
72         }
73     }
74 }
75