/TalkBack/src/com/google/android/marvin/talkback/speechrules/RuleImageView.java
Java | 64 lines | 33 code | 7 blank | 24 comment | 7 complexity | d0fa9d9576da6c7e3000e0847e1e300c MD5 | raw file
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.google.android.marvin.talkback.speechrules; 18 19import android.content.Context; 20import android.text.TextUtils; 21import android.util.Log; 22import android.view.accessibility.AccessibilityEvent; 23import android.view.accessibility.AccessibilityNodeInfo; 24 25import com.google.android.marvin.talkback.AccessibilityNodeInfoUtils; 26import com.google.android.marvin.talkback.R; 27 28/** 29 * Processes images that are not image buttons. 30 * 31 * @author alanv@google.com (Alan Viverette) 32 */ 33public class RuleImageView extends RuleDefault { 34 @Override 35 public boolean accept(AccessibilityNodeInfo node) { 36 return AccessibilityNodeInfoUtils.nodeMatchesClassByType(node, 37 android.widget.ImageView.class) 38 && !AccessibilityNodeInfoUtils.nodeMatchesClassByType(node, 39 android.widget.ImageButton.class); 40 } 41 42 @Override 43 public CharSequence 44 format(Context context, AccessibilityNodeInfo node, AccessibilityEvent event) { 45 final CharSequence text = super.format(context, node, event); 46 final boolean isActionable = AccessibilityNodeInfoUtils.isActionable(node); 47 final boolean hasLabel = !TextUtils.isEmpty(text); 48 49 if (isActionable && !hasLabel) { 50 // Log an error and announce a number for actionable unlabeled 51 // images. 52 Log.e("TalkBack", "Unlabeled image in " + node.getPackageName()); 53 final int nodeInt = (node.hashCode() % 100); 54 return context.getString(R.string.template_unlabeled_image_view, nodeInt); 55 } else if (!isActionable && hasLabel) { 56 // Append "image" to non-actionable labeled images. 57 return context.getString(R.string.template_image_view, text); 58 } else { 59 // Otherwise, just return the label. 60 return text; 61 } 62 } 63 64}