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 KeyguardClockAccessibilityDelegate(Context context)32 public KeyguardClockAccessibilityDelegate(Context context) { 33 mFancyColon = context.getString(R.string.keyguard_fancy_colon); 34 } 35 36 @Override onInitializeAccessibilityEvent(View host, AccessibilityEvent event)37 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { 38 super.onInitializeAccessibilityEvent(host, event); 39 CharSequence text = event.getContentDescription(); 40 if (!TextUtils.isEmpty(text)) { 41 event.setContentDescription(replaceFancyColon(text)); 42 } 43 } 44 45 @Override onPopulateAccessibilityEvent(View host, AccessibilityEvent event)46 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) { 47 CharSequence text = ((TextView) host).getText(); 48 if (!TextUtils.isEmpty(text)) { 49 event.getText().add(replaceFancyColon(text)); 50 } 51 } 52 53 @Override onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info)54 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 55 super.onInitializeAccessibilityNodeInfo(host, info); 56 if (!TextUtils.isEmpty(info.getText())) { 57 info.setText(replaceFancyColon(info.getText())); 58 } 59 if (!TextUtils.isEmpty(info.getContentDescription())) { 60 info.setContentDescription(replaceFancyColon(info.getContentDescription())); 61 } 62 } 63 replaceFancyColon(CharSequence text)64 private CharSequence replaceFancyColon(CharSequence text) { 65 return text.toString().replace(mFancyColon, ":"); 66 } 67 } 68