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.support.v4.view.AccessibilityDelegateCompat;
231212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.support.v4.view.ViewCompat;
241212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
251212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.util.AttributeSet;
261212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.view.View;
271212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport android.view.accessibility.AccessibilityEvent;
281212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
291212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovimport com.example.android.supportv4.R;
301212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
311212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov/**
321212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * This class demonstrates how to use the support library to register
331212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * a View.AccessibilityDelegate that customizes the accessibility
341212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * behavior of a View. Aiming to maximize simplicity this example
351212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * tweaks the text reported to accessibility services but using
361212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * these APIs a client can inject any accessibility functionality into
371212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov * a View.
381212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov */
391212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganovpublic class AccessibilityDelegateSupportActivity extends Activity {
401212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
411212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    /**
421212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * {@inheritDoc}
431212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     */
441212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    @Override
451212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    public void onCreate(Bundle savedInstanceState) {
461212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov        super.onCreate(savedInstanceState);
471212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov        setContentView(R.layout.accessibility_delegate);
481212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    }
491212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
501212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    /**
511212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * This class represents a View that is customized via an AccessibilityDelegate
521212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * as opposed to inheritance. An accessibility delegate can be used for adding
531212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * accessibility to custom Views, i.e. ones that extend classes from android.view,
541212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * in a backwards compatible fashion. Note that overriding a method whose return
551212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * type or arguments are not part of a target platform APIs makes your application
561212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     * not backwards compatible with that platform version.
571212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov     */
581212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    public static class AccessibilityDelegateSupportView extends View {
591212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
601212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov        public AccessibilityDelegateSupportView(Context context, AttributeSet attrs) {
611212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            super(context, attrs);
621212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            installAccessibilityDelegate();
631212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov        }
641212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
651212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov        private void installAccessibilityDelegate() {
661212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // The accessibility delegate enables customizing accessibility behavior
671212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // via composition as opposed as inheritance. The main benefit is that
681212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // one can write a backwards compatible application by setting the delegate
691212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // only if the API level is high enough i.e. the delegate is part of the APIs.
701212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // The easiest way to achieve that is by using the support library which
711212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // takes the burden of checking API version and knowing which API version
721212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            // introduced the delegate off the developer.
731212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegateCompat() {
741212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                @Override
751212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
761212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    super.onPopulateAccessibilityEvent(host, event);
771212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    // Note that View.onPopulateAccessibilityEvent was introduced in
781212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    // ICS and we would like to tweak a bit the text that is reported to
791212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    // accessibility services via the AccessibilityEvent.
801212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    event.getText().add(getContext().getString(
811212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                            R.string.accessibility_delegate_custom_text_added));
821212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                }
831212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov
841212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                @Override
851212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                public void onInitializeAccessibilityNodeInfo(View host,
861212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                        AccessibilityNodeInfoCompat info) {
871212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    super.onInitializeAccessibilityNodeInfo(host, info);
881212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    // Note that View.onInitializeAccessibilityNodeInfo was introduced in
891212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    // ICS and we would like to tweak a bit the text that is reported to
901212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    // accessibility services via the AccessibilityNodeInfo.
911212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                    info.setText(getContext().getString(
921212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                            R.string.accessibility_delegate_custom_text_added));
931212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov                }
941212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov            });
951212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov        }
961212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov    }
971212111afe952e9e5f423f777414067dbda74f24Svetoslav Ganov}
98