Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 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 | |
Sunny Goyal | 87fccf0 | 2019-08-13 17:39:10 -0700 | [diff] [blame] | 26 | import com.android.systemui.R; |
| 27 | |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 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 | |
| 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); |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 41 | if (TextUtils.isEmpty(mFancyColon)) { |
| 42 | return; |
| 43 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 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) { |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 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 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 64 | super.onInitializeAccessibilityNodeInfo(host, info); |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 65 | if (TextUtils.isEmpty(mFancyColon)) { |
| 66 | return; |
| 67 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 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) { |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 77 | if (TextUtils.isEmpty(mFancyColon)) { |
| 78 | return text; |
| 79 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 80 | return text.toString().replace(mFancyColon, ":"); |
| 81 | } |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 82 | |
| 83 | public static boolean isNeeded(Context context) { |
| 84 | return !TextUtils.isEmpty(context.getString(R.string.keyguard_fancy_colon)); |
| 85 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 86 | } |