• 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.content.Intent;
21 
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.core.BasePreferenceController;
25 import com.android.settingslib.HelpUtils;
26 
27 /**
28  * Preference controller that controls the help link and customizes the preference title in {@link
29  * AccessibilityFooterPreference}.
30  */
31 public class AccessibilityFooterPreferenceController extends BasePreferenceController {
32 
33     private int mHelpResource;
34     private String mLearnMoreText;
35     private String mIntroductionTitle;
36 
AccessibilityFooterPreferenceController(Context context, String key)37     public AccessibilityFooterPreferenceController(Context context, String key) {
38         super(context, key);
39     }
40 
41     @Override
getAvailabilityStatus()42     public int getAvailabilityStatus() {
43         return AVAILABLE;
44     }
45 
46     @Override
displayPreference(PreferenceScreen screen)47     public void displayPreference(PreferenceScreen screen) {
48         super.displayPreference(screen);
49 
50         final AccessibilityFooterPreference footerPreference =
51                 screen.findPreference(getPreferenceKey());
52         updateFooterPreferences(footerPreference);
53     }
54 
55     /**
56      * Setups a help item in the {@link AccessibilityFooterPreference} with specific content
57      * description.
58      */
setupHelpLink(int helpResource, String learnMoreText)59     public void setupHelpLink(int helpResource, String learnMoreText) {
60         mHelpResource = helpResource;
61         mLearnMoreText = learnMoreText;
62     }
63 
64     /**
65      * Overrides this if showing a help item in the {@link AccessibilityFooterPreference}, by
66      * returning the resource id.
67      *
68      * @return the resource id for the help url
69      */
getHelpResource()70     protected int getHelpResource() {
71         return mHelpResource;
72     }
73 
74     /**
75      * Overrides this if showing a help item in the {@link AccessibilityFooterPreference} with
76      * specific learn more title.
77      *
78      * @return learn more title for the help url
79      */
getLearnMoreText()80     protected String getLearnMoreText() {
81         return mLearnMoreText;
82     }
83 
84     /**
85      * Sets the announcement the specific features introduction in the {@link
86      * AccessibilityFooterPreference}.
87      */
setIntroductionTitle(String introductionTitle)88     public void setIntroductionTitle(String introductionTitle) {
89         mIntroductionTitle = introductionTitle;
90     }
91 
92     /**
93      * Overrides this if announcement the specific features introduction in the {@link
94      * AccessibilityFooterPreference}.
95      *
96      * @return the extended content description for specific features introduction
97      */
getIntroductionTitle()98     protected String getIntroductionTitle() {
99         return mIntroductionTitle;
100     }
101 
updateFooterPreferences(AccessibilityFooterPreference footerPreference)102     private void updateFooterPreferences(AccessibilityFooterPreference footerPreference) {
103         final StringBuffer sb = new StringBuffer();
104         sb.append(getIntroductionTitle()).append("\n\n").append(footerPreference.getTitle());
105         footerPreference.setContentDescription(sb);
106 
107         final Intent helpIntent;
108         if (getHelpResource() != 0) {
109             // Returns may be null if content is wrong or empty.
110             helpIntent = HelpUtils.getHelpIntent(mContext, mContext.getString(getHelpResource()),
111                     mContext.getClass().getName());
112         } else {
113             helpIntent = null;
114         }
115 
116         if (helpIntent != null) {
117             footerPreference.setLearnMoreAction(view -> {
118                 view.startActivityForResult(helpIntent, 0);
119             });
120             footerPreference.setLearnMoreText(getLearnMoreText());
121             footerPreference.setLinkEnabled(true);
122         } else {
123             footerPreference.setLinkEnabled(false);
124         }
125 
126         // Grouping subcomponents to make more accessible.
127         footerPreference.setSelectable(false);
128     }
129 }
130