1 /*
2  * Copyright (C) 2020 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.permissioncontroller.permission.ui.handheld.v34
18 
19 import android.content.Context
20 import android.text.SpannableString
21 import android.text.style.ClickableSpan
22 import android.util.AttributeSet
23 import android.view.View
24 import android.widget.TextView
25 import androidx.core.text.method.LinkMovementMethodCompat
26 import androidx.preference.Preference
27 import androidx.preference.PreferenceViewHolder
28 import com.android.permissioncontroller.R
29 
30 /** A preference for a footer with an icon and a link. */
31 class AppDataSharingUpdatesFooterPreference : Preference {
32     constructor(c: Context) : super(c)
33     constructor(c: Context, a: AttributeSet) : super(c, a)
34     constructor(c: Context, a: AttributeSet, attr: Int) : super(c, a, attr)
35     constructor(c: Context, a: AttributeSet, attr: Int, res: Int) : super(c, a, attr, res)
36 
37     private var footerMessageView: TextView? = null
38     private var footerLinkView: TextView? = null
39 
40     init {
41         layoutResource = R.layout.app_data_sharing_updates_footer_preference
42     }
43 
44     /** Message for the footer. */
45     var footerMessage: CharSequence = ""
46         set(value) {
47             field = value
48             notifyChanged()
49         }
50 
51     /** Clickable link for the footer. */
52     var footerLink: CharSequence = ""
53         set(value) {
54             field = value
55             notifyChanged()
56         }
57 
58     /** [View.OnClickListener] for the footer link. */
59     var onFooterLinkClick: View.OnClickListener? = null
60         set(value) {
61             field = value
62             notifyChanged()
63         }
64 
onBindViewHoldernull65     override fun onBindViewHolder(holder: PreferenceViewHolder) {
66         footerMessageView = holder.findViewById(R.id.footer_message) as TextView?
67         footerMessageView?.text = footerMessage
68 
69         footerLinkView = holder.findViewById(R.id.footer_link) as TextView?
70         val footerLinkText = SpannableString(footerLink)
71         footerLinkText.setSpan(
72             object : ClickableSpan() {
73                 override fun onClick(v: View) {
74                     onFooterLinkClick?.onClick(v)
75                 }
76             },
77             0,
78             footerLink.length,
79             0
80         )
81         footerLinkView?.let {
82             it.visibility = if (onFooterLinkClick == null) View.GONE else View.VISIBLE
83             it.text = footerLinkText
84             it.movementMethod = LinkMovementMethodCompat.getInstance()
85         }
86         super.onBindViewHolder(holder)
87     }
88 }
89