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