1 /*
2  * Copyright (C) 2017 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.keyguard;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.view.View;
22 import android.view.accessibility.AccessibilityEvent;
23 import android.view.accessibility.AccessibilityNodeInfo;
24 import android.widget.TextView;
25 
26 import com.android.systemui.R;
27 
28 /**
29  * Replaces fancy colons with regular colons. Only works on TextViews.
30  */
31 class KeyguardClockAccessibilityDelegate extends View.AccessibilityDelegate {
32     private final String mFancyColon;
33 
KeyguardClockAccessibilityDelegate(Context context)34     public KeyguardClockAccessibilityDelegate(Context context) {
35         mFancyColon = context.getString(R.string.keyguard_fancy_colon);
36     }
37 
38     @Override
onInitializeAccessibilityEvent(View host, AccessibilityEvent event)39     public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
40         super.onInitializeAccessibilityEvent(host, event);
41         if (TextUtils.isEmpty(mFancyColon)) {
42             return;
43         }
44         CharSequence text = event.getContentDescription();
45         if (!TextUtils.isEmpty(text)) {
46             event.setContentDescription(replaceFancyColon(text));
47         }
48     }
49 
50     @Override
onPopulateAccessibilityEvent(View host, AccessibilityEvent event)51     public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
52         if (TextUtils.isEmpty(mFancyColon)) {
53             super.onPopulateAccessibilityEvent(host, event);
54         } else {
55             CharSequence text = ((TextView) host).getText();
56             if (!TextUtils.isEmpty(text)) {
57                 event.getText().add(replaceFancyColon(text));
58             }
59         }
60     }
61 
62     @Override
onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info)63     public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
64         super.onInitializeAccessibilityNodeInfo(host, info);
65         if (TextUtils.isEmpty(mFancyColon)) {
66             return;
67         }
68         if (!TextUtils.isEmpty(info.getText())) {
69             info.setText(replaceFancyColon(info.getText()));
70         }
71         if (!TextUtils.isEmpty(info.getContentDescription())) {
72             info.setContentDescription(replaceFancyColon(info.getContentDescription()));
73         }
74     }
75 
replaceFancyColon(CharSequence text)76     private CharSequence replaceFancyColon(CharSequence text) {
77         if (TextUtils.isEmpty(mFancyColon)) {
78             return text;
79         }
80         return text.toString().replace(mFancyColon, ":");
81     }
82 
isNeeded(Context context)83     public static boolean isNeeded(Context context) {
84         return !TextUtils.isEmpty(context.getString(R.string.keyguard_fancy_colon));
85     }
86 }
87