blob: a78c293a65ac0b0b61c63872cc2286562b15498e [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
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
17package com.android.keyguard;
18
19import android.content.Context;
20import android.text.TextUtils;
21import android.view.View;
22import android.view.accessibility.AccessibilityEvent;
23import android.view.accessibility.AccessibilityNodeInfo;
24import android.widget.TextView;
25
26import com.android.systemui.R;
27
28/**
29 * Replaces fancy colons with regular colons. Only works on TextViews.
30 */
31class KeyguardClockAccessibilityDelegate extends View.AccessibilityDelegate {
32 private final String mFancyColon;
33
34 public KeyguardClockAccessibilityDelegate(Context context) {
35 mFancyColon = context.getString(R.string.keyguard_fancy_colon);
36 }
37
38 @Override
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
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
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
76 private CharSequence replaceFancyColon(CharSequence text) {
77 if (TextUtils.isEmpty(mFancyColon)) {
78 return text;
79 }
80 return text.toString().replace(mFancyColon, ":");
81 }
82
83 public static boolean isNeeded(Context context) {
84 return !TextUtils.isEmpty(context.getString(R.string.keyguard_fancy_colon));
85 }
86}