11212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov/* 21212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * Copyright (C) 2011 The Android Open Source Project 31212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * 41212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License"); 51212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * you may not use this file except in compliance with the License. 61212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * You may obtain a copy of the License at 71212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * 81212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * http://www.apache.org/licenses/LICENSE-2.0 91212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * 101212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software 111212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS, 121212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * See the License for the specific language governing permissions and 141212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * limitations under the License. 151212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov */ 161212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 171212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovpackage com.example.android.supportv4.accessibility; 181212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 191212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.app.Activity; 201212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.content.Context; 211212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.os.Bundle; 221212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.util.AttributeSet; 231212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.view.View; 241212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.view.accessibility.AccessibilityEvent; 251212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 26def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.view.AccessibilityDelegateCompat; 27def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.view.ViewCompat; 28def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.view.accessibility.AccessibilityNodeInfoCompat; 29def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas 301212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport com.example.android.supportv4.R; 311212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 321212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov/** 331212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * This class demonstrates how to use the support library to register 341212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * a View.AccessibilityDelegate that customizes the accessibility 351212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * behavior of a View. Aiming to maximize simplicity this example 361212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * tweaks the text reported to accessibility services but using 371212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * these APIs a client can inject any accessibility functionality into 381212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * a View. 391212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov */ 401212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovpublic class AccessibilityDelegateSupportActivity extends Activity { 411212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 421212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov /** 431212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * {@inheritDoc} 441212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov */ 451212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov @Override 461212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov public void onCreate(Bundle savedInstanceState) { 471212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov super.onCreate(savedInstanceState); 481212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov setContentView(R.layout.accessibility_delegate); 491212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov } 501212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 511212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov /** 521212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * This class represents a View that is customized via an AccessibilityDelegate 531212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * as opposed to inheritance. An accessibility delegate can be used for adding 541212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * accessibility to custom Views, i.e. ones that extend classes from android.view, 551212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * in a backwards compatible fashion. Note that overriding a method whose return 561212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * type or arguments are not part of a target platform APIs makes your application 571212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * not backwards compatible with that platform version. 581212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov */ 591212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov public static class AccessibilityDelegateSupportView extends View { 601212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 611212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov public AccessibilityDelegateSupportView(Context context, AttributeSet attrs) { 621212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov super(context, attrs); 631212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov installAccessibilityDelegate(); 641212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov } 651212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 661212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov private void installAccessibilityDelegate() { 671212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // The accessibility delegate enables customizing accessibility behavior 681212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // via composition as opposed as inheritance. The main benefit is that 691212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // one can write a backwards compatible application by setting the delegate 701212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // only if the API level is high enough i.e. the delegate is part of the APIs. 711212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // The easiest way to achieve that is by using the support library which 721212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // takes the burden of checking API version and knowing which API version 731212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // introduced the delegate off the developer. 741212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegateCompat() { 751212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov @Override 761212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) { 771212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov super.onPopulateAccessibilityEvent(host, event); 781212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // Note that View.onPopulateAccessibilityEvent was introduced in 791212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // ICS and we would like to tweak a bit the text that is reported to 801212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // accessibility services via the AccessibilityEvent. 811212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov event.getText().add(getContext().getString( 821212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov R.string.accessibility_delegate_custom_text_added)); 831212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov } 841212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov 851212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov @Override 861212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov public void onInitializeAccessibilityNodeInfo(View host, 871212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov AccessibilityNodeInfoCompat info) { 881212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov super.onInitializeAccessibilityNodeInfo(host, info); 891212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // Note that View.onInitializeAccessibilityNodeInfo was introduced in 901212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // ICS and we would like to tweak a bit the text that is reported to 911212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov // accessibility services via the AccessibilityNodeInfo. 921212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov info.setText(getContext().getString( 931212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov R.string.accessibility_delegate_custom_text_added)); 941212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov } 951212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov }); 961212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov } 971212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov } 981212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov} 99