PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/widgets/access-shim/src/com/googlecode/eyesfree/widget/AccessibilityShim.java

http://eyes-free.googlecode.com/
Java | 145 lines | 53 code | 18 blank | 74 comment | 7 complexity | b576c15b732ce0613156cdc2bfb90d39 MD5 | raw file
  1/*
  2 * Copyright (C) 2011 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.googlecode.eyesfree.widget;
 18
 19import android.app.Activity;
 20import android.app.Dialog;
 21import android.content.Context;
 22import android.content.pm.ApplicationInfo;
 23import android.content.pm.PackageManager;
 24import android.provider.Settings;
 25import android.view.View;
 26import android.view.ViewGroup;
 27import android.view.accessibility.AccessibilityManager;
 28
 29/**
 30 * <p>
 31 * The AccessibilityShim class inserts itself into an {@link Activity} or
 32 * {@link Dialog}'s view heirarchy and adds Touch Exploration. It is only
 33 * enabled when {@link AccessibilityManager} is enabled.
 34 * </p>
 35 * <p>
 36 * To add Touch Exploration to your activity, add the following line to your
 37 * onCreate() method after any calls to setContentView():
 38 * </p>
 39 * <p>
 40 * <code>AccessiblityShim.attachToActivity(this);</code>
 41 * <p>
 42 * If your activity uses dialogs, also add the following line at the end of your
 43 * onCreateDialog() method:
 44 * </p>
 45 * <p>
 46 * <code>AccessiblityShim.attachToDialog(dialog);</code>
 47 * </p>
 48 *
 49 * @author alanv@google.com (Alan Viverette)
 50 */
 51public class AccessibilityShim {
 52    private static final String KEY_DISABLE_EXPLORATION = "disableTouchExploration";
 53
 54    /**
 55     * If Accessibility is enabled, adds touch exploration to a view heirarchy.
 56     *
 57     * @param decorView The view to which touch exploration will be added.
 58     */
 59    public static void attachToView(View decorView) {
 60        final Context context = decorView.getContext();
 61
 62        if (!isAccessibilityEnabled(context)) {
 63            return;
 64        }
 65
 66        if (isTouchExplorationDisabled(context)) {
 67            return;
 68        }
 69
 70        final AccessibleFrameLayout frame = new AccessibleFrameLayout(decorView.getContext());
 71
 72        if (decorView instanceof ViewGroup) {
 73            frame.inject((ViewGroup) decorView);
 74        }
 75    }
 76
 77    /**
 78     * Returns <code>true</code> if the user has enabled Accessibility within
 79     * the system's settings.
 80     *
 81     * @param context The application's {@link Context}.
 82     * @return Returns <code>true</code> if Accessibility is enabled.
 83     */
 84    private static boolean isAccessibilityEnabled(Context context) {
 85        final int enabled = Settings.Secure.getInt(context.getContentResolver(),
 86                Settings.Secure.ACCESSIBILITY_ENABLED, -1);
 87
 88        return (enabled == 1);
 89    }
 90
 91    /**
 92     * Returns <code>true</code> if the application has explicitly disabled
 93     * touch exploration using a meta tag.
 94     * <p>
 95     * <code><meta-data android:name="disableTouchExploration" android:value="true" /> * </code>
 96     * .
 97     * </p>
 98     *
 99     * @param context The application's {@link Context}.
100     * @return Returns <code>true</code> if the application has explicitly
101     *         disabled touch exploration.
102     */
103    private static boolean isTouchExplorationDisabled(Context context) {
104        try {
105            final ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
106                    context.getPackageName(), PackageManager.GET_META_DATA);
107
108            if (appInfo.metaData != null && appInfo.metaData.containsKey(KEY_DISABLE_EXPLORATION)) {
109                return true;
110            }
111        } catch (PackageManager.NameNotFoundException e) {
112            e.printStackTrace();
113        }
114
115        return false;
116    }
117
118    /**
119     * Enables touch exploration within an activity's view heirarchy. If your
120     * application uses dialogs, you must also use attachToDialog inside of your
121     * activity's onCreateDialog method.
122     *
123     * @param activity The activity to which touch exploration will be added.
124     */
125    public static void attachToActivity(Activity activity) {
126        final View decorView = activity.getWindow().getDecorView();
127
128        attachToView(decorView);
129    }
130
131    /**
132     * Enables touch exploration within a dialog's view heirarchy.
133     *
134     * @param dialog The dialog to which touch exploration will be added.
135     */
136    public static void attachToDialog(Dialog dialog) {
137        final View decorView = dialog.getWindow().getDecorView();
138
139        attachToView(decorView);
140    }
141
142    private AccessibilityShim() {
143        // This class is non-instantiable.
144    }
145}