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); 39 if (TextUtils.isEmpty(mFancyColon)) { 40 return; 41 } 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) { 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 } 57 } 58 } 59 60 @Override 61 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 62 super.onInitializeAccessibilityNodeInfo(host, info); 63 if (TextUtils.isEmpty(mFancyColon)) { 64 return; 65 } 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) { 75 if (TextUtils.isEmpty(mFancyColon)) { 76 return text; 77 } 78 return text.toString().replace(mFancyColon, ":"); 79 } 80 81 public static boolean isNeeded(Context context) { 82 return !TextUtils.isEmpty(context.getString(R.string.keyguard_fancy_colon)); 83 } 84 } 85