Make Phonegap Vibrate Plugin respect Audio Mute Setting [Android]

613 Views Asked by At

I searched for a solution to make my Android Phonegap application respect the mute audio setting, when using the Vibration plugin (org.apache.cordova.vibration 0.3.11)

https://github.com/apache/cordova-plugin-vibration

But there seems to be no way to avoid vibration in mute state of an Android device. Or does anyone now a setting?

So I applied the following patch, which seems to work for me.

Edit Vibration.java file and check for AudioManager.RINGER_MODE_SILENT before trigger a vibration:

import android.media.AudioManager; // new

public void vibrate(long time) {
    // Start the vibration, 0 defaults to half a second.
    if (time == 0) {
        time = 500;
    }
    // OLD Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
    // OLD vibrator.vibrate(time);

    AudioManager manager = (AudioManager) this.cordova.getActivity().getSystemService(Context.AUDIO_SERVICE); 
    // respect silent rintone mode
    if (manager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
      Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
      vibrator.vibrate(time);
    }
}

public void vibrateWithPattern(long[] pattern, int repeat) {
    // OLD Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
    // OLD vibrator.vibrate(pattern, repeat);

    AudioManager manager = (AudioManager) this.cordova.getActivity().getSystemService(Context.AUDIO_SERVICE); 
    // respect silent rintone mode
    if (manager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
      Vibrator vibrator = (Vibrator) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
      vibrator.vibrate(pattern, repeat);  
    }
}    

Does anyone know a smarter solution without any patch?

0

There are 0 best solutions below