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