10888a09821a98ac0680fad765217302858e70fa4Paul Duffin/*
20888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Copyright (C) 2011 The Android Open Source Project
30888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
40888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
50888a09821a98ac0680fad765217302858e70fa4Paul Duffin * you may not use this file except in compliance with the License.
60888a09821a98ac0680fad765217302858e70fa4Paul Duffin * You may obtain a copy of the License at
70888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
80888a09821a98ac0680fad765217302858e70fa4Paul Duffin *      http://www.apache.org/licenses/LICENSE-2.0
90888a09821a98ac0680fad765217302858e70fa4Paul Duffin *
100888a09821a98ac0680fad765217302858e70fa4Paul Duffin * Unless required by applicable law or agreed to in writing, software
110888a09821a98ac0680fad765217302858e70fa4Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
120888a09821a98ac0680fad765217302858e70fa4Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130888a09821a98ac0680fad765217302858e70fa4Paul Duffin * See the License for the specific language governing permissions and
140888a09821a98ac0680fad765217302858e70fa4Paul Duffin * limitations under the License.
150888a09821a98ac0680fad765217302858e70fa4Paul Duffin */
160888a09821a98ac0680fad765217302858e70fa4Paul Duffin
170888a09821a98ac0680fad765217302858e70fa4Paul Duffinpackage com.example.android.supportv4.accessibility;
180888a09821a98ac0680fad765217302858e70fa4Paul Duffin
193ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffinimport android.app.Activity;
203ecfa412eddc4b084663f38d562537b86b9734d5Paul Duffinimport android.content.Context;
210888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport android.os.Bundle;
220888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport android.util.AttributeSet;
230888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport android.view.View;
240888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport android.view.accessibility.AccessibilityEvent;
250888a09821a98ac0680fad765217302858e70fa4Paul Duffin
260888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport androidx.core.view.AccessibilityDelegateCompat;
270888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport androidx.core.view.ViewCompat;
280888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
290888a09821a98ac0680fad765217302858e70fa4Paul Duffin
300888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.example.android.supportv4.R;
310888a09821a98ac0680fad765217302858e70fa4Paul Duffin
320888a09821a98ac0680fad765217302858e70fa4Paul Duffin/**
330888a09821a98ac0680fad765217302858e70fa4Paul Duffin * This class demonstrates how to use the support library to register
340888a09821a98ac0680fad765217302858e70fa4Paul Duffin * a View.AccessibilityDelegate that customizes the accessibility
350888a09821a98ac0680fad765217302858e70fa4Paul Duffin * behavior of a View. Aiming to maximize simplicity this example
360888a09821a98ac0680fad765217302858e70fa4Paul Duffin * tweaks the text reported to accessibility services but using
370888a09821a98ac0680fad765217302858e70fa4Paul Duffin * these APIs a client can inject any accessibility functionality into
380888a09821a98ac0680fad765217302858e70fa4Paul Duffin * a View.
390888a09821a98ac0680fad765217302858e70fa4Paul Duffin */
400888a09821a98ac0680fad765217302858e70fa4Paul Duffinpublic class AccessibilityDelegateSupportActivity extends Activity {
410888a09821a98ac0680fad765217302858e70fa4Paul Duffin
420888a09821a98ac0680fad765217302858e70fa4Paul Duffin    /**
430888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * {@inheritDoc}
440888a09821a98ac0680fad765217302858e70fa4Paul Duffin     */
450888a09821a98ac0680fad765217302858e70fa4Paul Duffin    @Override
460888a09821a98ac0680fad765217302858e70fa4Paul Duffin    public void onCreate(Bundle savedInstanceState) {
470888a09821a98ac0680fad765217302858e70fa4Paul Duffin        super.onCreate(savedInstanceState);
480888a09821a98ac0680fad765217302858e70fa4Paul Duffin        setContentView(R.layout.accessibility_delegate);
490888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
500888a09821a98ac0680fad765217302858e70fa4Paul Duffin
510888a09821a98ac0680fad765217302858e70fa4Paul Duffin    /**
520888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * This class represents a View that is customized via an AccessibilityDelegate
530888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * as opposed to inheritance. An accessibility delegate can be used for adding
540888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * accessibility to custom Views, i.e. ones that extend classes from android.view,
550888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * in a backwards compatible fashion. Note that overriding a method whose return
560888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * type or arguments are not part of a target platform APIs makes your application
570888a09821a98ac0680fad765217302858e70fa4Paul Duffin     * not backwards compatible with that platform version.
580888a09821a98ac0680fad765217302858e70fa4Paul Duffin     */
590888a09821a98ac0680fad765217302858e70fa4Paul Duffin    public static class AccessibilityDelegateSupportView extends View {
600888a09821a98ac0680fad765217302858e70fa4Paul Duffin
610888a09821a98ac0680fad765217302858e70fa4Paul Duffin        public AccessibilityDelegateSupportView(Context context, AttributeSet attrs) {
620888a09821a98ac0680fad765217302858e70fa4Paul Duffin            super(context, attrs);
630888a09821a98ac0680fad765217302858e70fa4Paul Duffin            installAccessibilityDelegate();
640888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
650888a09821a98ac0680fad765217302858e70fa4Paul Duffin
660888a09821a98ac0680fad765217302858e70fa4Paul Duffin        private void installAccessibilityDelegate() {
670888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // The accessibility delegate enables customizing accessibility behavior
680888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // via composition as opposed as inheritance. The main benefit is that
690888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // one can write a backwards compatible application by setting the delegate
700888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // only if the API level is high enough i.e. the delegate is part of the APIs.
710888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // The easiest way to achieve that is by using the support library which
720888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // takes the burden of checking API version and knowing which API version
730888a09821a98ac0680fad765217302858e70fa4Paul Duffin            // introduced the delegate off the developer.
740888a09821a98ac0680fad765217302858e70fa4Paul Duffin            ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegateCompat() {
750888a09821a98ac0680fad765217302858e70fa4Paul Duffin                @Override
760888a09821a98ac0680fad765217302858e70fa4Paul Duffin                public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
770888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    super.onPopulateAccessibilityEvent(host, event);
780888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    // Note that View.onPopulateAccessibilityEvent was introduced in
790888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    // ICS and we would like to tweak a bit the text that is reported to
800888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    // accessibility services via the AccessibilityEvent.
810888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    event.getText().add(getContext().getString(
820888a09821a98ac0680fad765217302858e70fa4Paul Duffin                            R.string.accessibility_delegate_custom_text_added));
830888a09821a98ac0680fad765217302858e70fa4Paul Duffin                }
840888a09821a98ac0680fad765217302858e70fa4Paul Duffin
850888a09821a98ac0680fad765217302858e70fa4Paul Duffin                @Override
860888a09821a98ac0680fad765217302858e70fa4Paul Duffin                public void onInitializeAccessibilityNodeInfo(View host,
870888a09821a98ac0680fad765217302858e70fa4Paul Duffin                        AccessibilityNodeInfoCompat info) {
880888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    super.onInitializeAccessibilityNodeInfo(host, info);
890888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    // Note that View.onInitializeAccessibilityNodeInfo was introduced in
900888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    // ICS and we would like to tweak a bit the text that is reported to
910888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    // accessibility services via the AccessibilityNodeInfo.
920888a09821a98ac0680fad765217302858e70fa4Paul Duffin                    info.setText(getContext().getString(
930888a09821a98ac0680fad765217302858e70fa4Paul Duffin                            R.string.accessibility_delegate_custom_text_added));
940888a09821a98ac0680fad765217302858e70fa4Paul Duffin                }
950888a09821a98ac0680fad765217302858e70fa4Paul Duffin            });
960888a09821a98ac0680fad765217302858e70fa4Paul Duffin        }
970888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
980888a09821a98ac0680fad765217302858e70fa4Paul Duffin}
990888a09821a98ac0680fad765217302858e70fa4Paul Duffin