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