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.fragment.app.FragmentActivity
23 import androidx.preference.Preference
24 import androidx.preference.PreferenceViewHolder
25 import com.android.healthconnect.controller.R
26 import com.android.healthconnect.controller.utils.convertTextViewIntoLink
27 
28 /** Custom preference for the headers containing a link. */
29 class HeaderPreference constructor(context: Context) :
30     Preference(context) {
31 
32     private lateinit var headerTitle: TextView
33     private lateinit var headerLink: TextView
34 
35     private var headerText: String? = null
36     private var headerLinkText: String? = null
37     private var linkAction: OnClickListener? = null
38 
39     init {
40         layoutResource = R.layout.widget_header_preference
41         isSelectable = false
42     }
43 
setHeaderTextnull44     fun setHeaderText(headerText: String) {
45         this.headerText = headerText
46     }
47 
setHeaderLinkTextnull48     fun setHeaderLinkText(headerLinkText: String) {
49         this.headerLinkText = headerLinkText
50     }
51 
setHeaderLinkActionnull52     fun setHeaderLinkAction(onClickListener: OnClickListener) {
53         this.linkAction = onClickListener
54     }
55 
onBindViewHoldernull56     override fun onBindViewHolder(holder: PreferenceViewHolder) {
57         super.onBindViewHolder(holder)
58 
59         headerTitle = holder.findViewById(R.id.header_title) as TextView
60         headerTitle.text = headerText
61 
62         headerLink = holder.findViewById(R.id.header_link) as TextView
63         if (headerLinkText != null) {
64             headerLink.visibility = View.VISIBLE
65             convertTextViewIntoLink(headerLink, headerLinkText, 0, headerLinkText!!.length, linkAction)
66         } else {
67             headerLink.visibility = View.GONE
68         }
69     }
70 }
71