1 /* 2 * Copyright (C) 2011 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 17 package com.android.inputmethod.compat; 18 19 import android.content.Context; 20 import android.os.Vibrator; 21 22 import java.lang.reflect.Method; 23 24 public class VibratorCompatWrapper { 25 private static final Method METHOD_hasVibrator = CompatUtils.getMethod(Vibrator.class, 26 "hasVibrator"); 27 28 private static final VibratorCompatWrapper sInstance = new VibratorCompatWrapper(); 29 private Vibrator mVibrator; 30 31 private VibratorCompatWrapper() { 32 } 33 34 public static VibratorCompatWrapper getInstance(Context context) { 35 if (sInstance.mVibrator == null) { 36 sInstance.mVibrator = 37 (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 38 } 39 return sInstance; 40 } 41 42 public boolean hasVibrator() { 43 if (mVibrator == null) 44 return false; 45 return (Boolean) CompatUtils.invoke(mVibrator, true, METHOD_hasVibrator); 46 } 47 48 public void vibrate(long milliseconds) { 49 mVibrator.vibrate(milliseconds); 50 } 51 } 52