• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
29  * A custom preference acting as footer of a page. Disables the movement method by default.
30  */
31 public final class AccessibilityFooterPreference extends FooterPreference {
32 
33     private boolean mLinkEnabled;
34 
AccessibilityFooterPreference(Context context, AttributeSet attrs)35     public AccessibilityFooterPreference(Context context, AttributeSet attrs) {
36         super(context, attrs);
37     }
38 
AccessibilityFooterPreference(Context context)39     public AccessibilityFooterPreference(Context context) {
40         super(context);
41     }
42 
43     @Override
onBindViewHolder(PreferenceViewHolder holder)44     public void onBindViewHolder(PreferenceViewHolder holder) {
45         super.onBindViewHolder(holder);
46 
47         final TextView title = holder.itemView.findViewById(android.R.id.title);
48         if (mLinkEnabled) {
49             // When a TextView has a movement method, it will set the view to clickable. This makes
50             // View.onTouchEvent always return true and consumes the touch event, essentially
51             // nullifying any return values of MovementMethod.onTouchEvent.
52             // To still allow propagating touch events to the parent when this view doesn't have
53             // links, we only set the movement method here if the text contains links.
54             title.setMovementMethod(LinkMovementMethod.getInstance());
55         } else {
56             title.setMovementMethod(/* movement= */ null);
57         }
58     }
59 
60     /**
61      * Sets the title field supports movement method.
62      */
setLinkEnabled(boolean enabled)63     public void setLinkEnabled(boolean enabled) {
64         if (mLinkEnabled != enabled) {
65             mLinkEnabled = enabled;
66             notifyChanged();
67         }
68     }
69 
70     /**
71      * Returns true if the title field supports movement method.
72      */
isLinkEnabled()73     public boolean isLinkEnabled() {
74         return mLinkEnabled;
75     }
76 }
77