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 | |
| 26 | /** |
| 27 | * Replaces fancy colons with regular colons. Only works on TextViews. |
| 28 | */ |
| 29 | class KeyguardClockAccessibilityDelegate extends View.AccessibilityDelegate { |
| 30 | private final String mFancyColon; |
| 31 | |
| 32 | public KeyguardClockAccessibilityDelegate(Context context) { |
| 33 | mFancyColon = context.getString(R.string.keyguard_fancy_colon); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { |
| 38 | super.onInitializeAccessibilityEvent(host, event); |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 39 | if (TextUtils.isEmpty(mFancyColon)) { |
| 40 | return; |
| 41 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 42 | CharSequence text = event.getContentDescription(); |
| 43 | if (!TextUtils.isEmpty(text)) { |
| 44 | event.setContentDescription(replaceFancyColon(text)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) { |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 50 | if (TextUtils.isEmpty(mFancyColon)) { |
| 51 | super.onPopulateAccessibilityEvent(host, event); |
| 52 | } else { |
| 53 | CharSequence text = ((TextView) host).getText(); |
| 54 | if (!TextUtils.isEmpty(text)) { |
| 55 | event.getText().add(replaceFancyColon(text)); |
| 56 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { |
| 62 | super.onInitializeAccessibilityNodeInfo(host, info); |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 63 | if (TextUtils.isEmpty(mFancyColon)) { |
| 64 | return; |
| 65 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 66 | if (!TextUtils.isEmpty(info.getText())) { |
| 67 | info.setText(replaceFancyColon(info.getText())); |
| 68 | } |
| 69 | if (!TextUtils.isEmpty(info.getContentDescription())) { |
| 70 | info.setContentDescription(replaceFancyColon(info.getContentDescription())); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private CharSequence replaceFancyColon(CharSequence text) { |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 75 | if (TextUtils.isEmpty(mFancyColon)) { |
| 76 | return text; |
| 77 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 78 | return text.toString().replace(mFancyColon, ":"); |
| 79 | } |
Adrian Roos | b670f4b | 2017-07-20 20:06:54 +0200 | [diff] [blame] | 80 | |
| 81 | public static boolean isNeeded(Context context) { |
| 82 | return !TextUtils.isEmpty(context.getString(R.string.keyguard_fancy_colon)); |
| 83 | } |
Adrian Roos | 2edb63b | 2017-04-26 10:43:21 -0700 | [diff] [blame] | 84 | } |