/KickBack/src/com/google/android/marvin/kickback/KickBackService.java

http://eyes-free.googlecode.com/ · Java · 89 lines · 57 code · 11 blank · 21 comment · 1 complexity · 83690554ab35f72a44112f952146cff7 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.kickback;
  17. import android.accessibilityservice.AccessibilityService;
  18. import android.accessibilityservice.AccessibilityServiceInfo;
  19. import android.app.Service;
  20. import android.os.Vibrator;
  21. import android.util.Log;
  22. import android.view.accessibility.AccessibilityEvent;
  23. /**
  24. * Accessibility service that provides haptic feedback.
  25. *
  26. * @author svetoslavganov@google.com (Svetoslav R. Ganov)
  27. *
  28. */
  29. public class KickBackService extends AccessibilityService {
  30. private static final String LOG_TAG = "KickBackService";
  31. private static final int NOTIFICATION_TIMEOUT_MILLIS = 80;
  32. private static final long[] VIEW_CLICKED_PATTERN = new long[] {0, 100};
  33. private static final long[] VIEW_FOCUSED_OR_SELECTED_PATTERN = new long[] {0, 15, 10, 15};
  34. private static final long[] NOTIFICATION_OR_WINDOW_STATE_CHANGED_PATTERN =
  35. new long[] {0, 25, 50, 25, 50, 25};
  36. private Vibrator mVibrator;
  37. @Override
  38. public void onCreate() {
  39. super.onCreate();
  40. mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
  41. }
  42. @Override
  43. public void onServiceConnected() {
  44. AccessibilityServiceInfo info = new AccessibilityServiceInfo();
  45. info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
  46. info.feedbackType = AccessibilityServiceInfo.FEEDBACK_HAPTIC;
  47. info.notificationTimeout = NOTIFICATION_TIMEOUT_MILLIS;
  48. info.flags = AccessibilityServiceInfo.DEFAULT;
  49. setServiceInfo(info);
  50. }
  51. @Override
  52. public void onAccessibilityEvent(AccessibilityEvent event) {
  53. int eventType = event.getEventType();
  54. switch (eventType) {
  55. case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED :
  56. mVibrator.vibrate(NOTIFICATION_OR_WINDOW_STATE_CHANGED_PATTERN, -1);
  57. return;
  58. case AccessibilityEvent.TYPE_VIEW_CLICKED :
  59. mVibrator.vibrate(VIEW_CLICKED_PATTERN, -1);
  60. break;
  61. case AccessibilityEvent.TYPE_VIEW_FOCUSED :
  62. mVibrator.vibrate(VIEW_FOCUSED_OR_SELECTED_PATTERN, -1);
  63. break;
  64. case AccessibilityEvent.TYPE_VIEW_SELECTED :
  65. mVibrator.vibrate(VIEW_FOCUSED_OR_SELECTED_PATTERN, -1);
  66. break;
  67. case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED :
  68. mVibrator.vibrate(NOTIFICATION_OR_WINDOW_STATE_CHANGED_PATTERN, -1);
  69. break;
  70. default :
  71. Log.w(LOG_TAG, "Unknown accessibility event type " + eventType);
  72. }
  73. }
  74. @Override
  75. public void onInterrupt() {
  76. mVibrator.cancel();
  77. }
  78. }