1/*
2 * Copyright (C) 2011 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
17package com.example.android.supportv4.accessibility;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.util.AttributeSet;
23import android.view.View;
24import android.view.accessibility.AccessibilityEvent;
25
26import androidx.core.view.AccessibilityDelegateCompat;
27import androidx.core.view.ViewCompat;
28import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
29
30import com.example.android.supportv4.R;
31
32/**
33 * This class demonstrates how to use the support library to register
34 * a View.AccessibilityDelegate that customizes the accessibility
35 * behavior of a View. Aiming to maximize simplicity this example
36 * tweaks the text reported to accessibility services but using
37 * these APIs a client can inject any accessibility functionality into
38 * a View.
39 */
40public class AccessibilityDelegateSupportActivity extends Activity {
41
42    /**
43     * {@inheritDoc}
44     */
45    @Override
46    public void onCreate(Bundle savedInstanceState) {
47        super.onCreate(savedInstanceState);
48        setContentView(R.layout.accessibility_delegate);
49    }
50
51    /**
52     * This class represents a View that is customized via an AccessibilityDelegate
53     * as opposed to inheritance. An accessibility delegate can be used for adding
54     * accessibility to custom Views, i.e. ones that extend classes from android.view,
55     * in a backwards compatible fashion. Note that overriding a method whose return
56     * type or arguments are not part of a target platform APIs makes your application
57     * not backwards compatible with that platform version.
58     */
59    public static class AccessibilityDelegateSupportView extends View {
60
61        public AccessibilityDelegateSupportView(Context context, AttributeSet attrs) {
62            super(context, attrs);
63            installAccessibilityDelegate();
64        }
65
66        private void installAccessibilityDelegate() {
67            // The accessibility delegate enables customizing accessibility behavior
68            // via composition as opposed as inheritance. The main benefit is that
69            // one can write a backwards compatible application by setting the delegate
70            // only if the API level is high enough i.e. the delegate is part of the APIs.
71            // The easiest way to achieve that is by using the support library which
72            // takes the burden of checking API version and knowing which API version
73            // introduced the delegate off the developer.
74            ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegateCompat() {
75                @Override
76                public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
77                    super.onPopulateAccessibilityEvent(host, event);
78                    // Note that View.onPopulateAccessibilityEvent was introduced in
79                    // ICS and we would like to tweak a bit the text that is reported to
80                    // accessibility services via the AccessibilityEvent.
81                    event.getText().add(getContext().getString(
82                            R.string.accessibility_delegate_custom_text_added));
83                }
84
85                @Override
86                public void onInitializeAccessibilityNodeInfo(View host,
87                        AccessibilityNodeInfoCompat info) {
88                    super.onInitializeAccessibilityNodeInfo(host, info);
89                    // Note that View.onInitializeAccessibilityNodeInfo was introduced in
90                    // ICS and we would like to tweak a bit the text that is reported to
91                    // accessibility services via the AccessibilityNodeInfo.
92                    info.setText(getContext().getString(
93                            R.string.accessibility_delegate_custom_text_added));
94                }
95            });
96        }
97    }
98}
99