PageRenderTime 21ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/SoundBack/src/com/google/android/marvin/soundback/SoundBackService.java

http://eyes-free.googlecode.com/
Java | 105 lines | 69 code | 11 blank | 25 comment | 5 complexity | 1fcac70128bfe61a2b66e9e8ee19a3c9 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 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.google.android.marvin.soundback;
 18
 19import android.accessibilityservice.AccessibilityService;
 20import android.accessibilityservice.AccessibilityServiceInfo;
 21import android.media.SoundPool;
 22import android.util.Log;
 23import android.util.SparseArray;
 24import android.view.accessibility.AccessibilityEvent;
 25
 26/**
 27 * Accessibility service that provides audible feedback.
 28 *
 29 * @author svetoslavganov@google.com (Svetoslav R. Ganov)
 30 */
 31public class SoundBackService extends AccessibilityService {
 32
 33    private static final String LOG_TAG = "SoundBackService";
 34
 35    private static final int NUMBER_OF_CHANNELS = 4;
 36    private static final int STREAM_MUSIC = 3;
 37    private static final float SOUND_EFFECT_VOLUME = 1000.0f;
 38
 39    private static final int NOTIFICATION_TIMEOUT_MILLIS = 80;
 40
 41    private SoundPool mSoundPool = new SoundPool(NUMBER_OF_CHANNELS, STREAM_MUSIC, 0);
 42    private SparseArray<Integer> mResourceIdToSoundIdMap = new SparseArray<Integer>();
 43    private int currentSoundId;
 44
 45    @Override
 46    public void onServiceConnected() {
 47        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
 48        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
 49        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_AUDIBLE;
 50        info.notificationTimeout = NOTIFICATION_TIMEOUT_MILLIS;
 51        info.flags = AccessibilityServiceInfo.DEFAULT;
 52        setServiceInfo(info);
 53    }
 54
 55    @Override
 56    public synchronized void onAccessibilityEvent(AccessibilityEvent event) {
 57        if (event == null){
 58            Log.e(LOG_TAG, "Received null accessibility event.");
 59            return;
 60        }
 61        int eventType = event.getEventType();
 62        switch (eventType) {
 63            case AccessibilityEvent.TYPE_VIEW_FOCUSED : 
 64                playEarcon(R.raw.open);
 65                return;
 66            case AccessibilityEvent.TYPE_VIEW_SELECTED : 
 67                playEarcon(R.raw.select);
 68                return;
 69            case AccessibilityEvent.TYPE_VIEW_CLICKED : 
 70                playEarcon(R.raw.button);
 71                return;
 72            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED :
 73                playEarcon(R.raw.open);
 74                return;
 75            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED :
 76                playEarcon(R.raw.item);
 77                return;
 78            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED :
 79                playEarcon(R.raw.working);
 80                return;
 81            default :
 82                Log.w(LOG_TAG, "Unknown accessibility event type " + eventType);
 83        }
 84    }
 85
 86    @Override
 87    public void onInterrupt() {
 88        mSoundPool.stop(currentSoundId);
 89    }
 90
 91    /**
 92     * Plays an earcon given its resource id.
 93     * 
 94     * @param resourceId The resource id of the earcon to be played.
 95     */
 96    private void playEarcon(int resourceId) {
 97        Integer soundId = mResourceIdToSoundIdMap.get(resourceId);
 98        if (soundId == null) {
 99            soundId = mSoundPool.load(this, resourceId, 1234567);
100            mResourceIdToSoundIdMap.put(resourceId, soundId);
101        }
102        currentSoundId = soundId;
103        mSoundPool.play(soundId, SOUND_EFFECT_VOLUME, SOUND_EFFECT_VOLUME, 0, 0, 1.0f);
104    }
105}