Add support for standard Android haptic feedback, which does not require the VIBRATE permission.

This commit is contained in:
Henrik Rydgård 2013-10-10 15:59:57 +02:00
parent ac1620137f
commit 9aa30a69f6
2 changed files with 33 additions and 4 deletions

View File

@ -37,6 +37,7 @@ import android.view.Gravity;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyEvent;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
@ -644,15 +645,32 @@ public class NativeActivity extends Activity {
inputBox(params, "", "OK");
} else if (command.equals("vibrate")) {
if (vibrator != null) {
int milliseconds = 50;
int milliseconds = -1;
if (params != "") {
try {
milliseconds = Integer.parseInt(params);
} catch (NumberFormatException e) {
milliseconds = 50;
}
}
vibrator.vibrate(milliseconds);
// Special parameters to perform standard haptic feedback operations
// -1 = Standard keyboard press feedback
// -2 = Virtual key press
// -3 = Long press feedback
// Note that these three do not require the VIBRATE Android permission.
switch (milliseconds) {
case -1:
mGLSurfaceView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
break;
case -2:
mGLSurfaceView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
break;
case -3:
mGLSurfaceView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
break;
default:
vibrator.vibrate(milliseconds);
break;
}
}
} else {
Log.e(TAG, "Unsupported command " + command + " , param: " + params);

View File

@ -92,6 +92,17 @@ void NativeSaveState(); // onDestroy
void SystemToast(const char *text);
void ShowKeyboard();
void ShowAd(int x, int y, bool center_x);
// Vibrate either takes a number of milliseconds to vibrate unconditionally,
// or you can specify these constants for "standard" feedback. On Android,
// these will only be performed if haptic feedback is enabled globally.
// Also, on Android, these will work even if you don't have the VIBRATE permission,
// while generic vibration will not if you don't have it.
enum {
HAPTIC_SOFT_KEYBOARD = -1,
HAPTIC_VIRTUAL_KEY = -2,
HAPTIC_LONG_PRESS_ACTIVATED = -3,
};
void Vibrate(int length_ms);
void LaunchBrowser(const char *url);
void LaunchMarket(const char *url);
@ -104,4 +115,4 @@ enum SystemProperty {
SYSPROP_CPUINFO,
};
std::string System_GetProperty(SystemProperty prop);
std::string System_GetProperty(SystemProperty prop);