1 /*
2  * Copyright (C) 2021 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.settings.accessibility;
18 
19 import android.content.Context;
20 import android.text.method.LinkMovementMethod;
21 import android.util.AttributeSet;
22 import android.widget.TextView;
23 
24 import androidx.preference.PreferenceViewHolder;
25 
26 import com.android.settingslib.widget.FooterPreference;
27 
28 /** A custom preference acting as footer of a page. Disables the movement method by default. */
29 public final class AccessibilityFooterPreference extends FooterPreference {
30 
31     private boolean mLinkEnabled;
32 
AccessibilityFooterPreference(Context context, AttributeSet attrs)33     public AccessibilityFooterPreference(Context context, AttributeSet attrs) {
34         super(context, attrs);
35     }
36 
AccessibilityFooterPreference(Context context)37     public AccessibilityFooterPreference(Context context) {
38         super(context);
39     }
40 
41     @Override
onBindViewHolder(PreferenceViewHolder holder)42     public void onBindViewHolder(PreferenceViewHolder holder) {
43         super.onBindViewHolder(holder);
44 
45         final TextView title = holder.itemView.findViewById(android.R.id.title);
46         if (mLinkEnabled) {
47             // When a TextView has a movement method, it will set the view to focusable and
48             // clickable. This makes View.onTouchEvent always return true and consumes the touch
49             // event, essentially nullifying any return values of MovementMethod.onTouchEvent.
50             // To still allow propagating touch events to the parent when this view doesn't have
51             // links, we only set the movement method here if the text contains links.
52             title.setMovementMethod(LinkMovementMethod.getInstance());
53 
54             // Groups of related title and link content by making the container focusable,
55             // then make all the children inside not focusable.
56             title.setFocusable(false);
57         } else {
58             title.setMovementMethod(/* movement= */ null);
59         }
60     }
61 
62     /**
63      * Sets the title field supports movement method.
64      */
setLinkEnabled(boolean enabled)65     public void setLinkEnabled(boolean enabled) {
66         if (mLinkEnabled != enabled) {
67             mLinkEnabled = enabled;
68             notifyChanged();
69         }
70     }
71 
72     /**
73      * Returns true if the title field supports movement method.
74      */
isLinkEnabled()75     public boolean isLinkEnabled() {
76         return mLinkEnabled;
77     }
78 }
79