Skip to content

Instantly share code, notes, and snippets.

@WindSekirun
Created September 26, 2016 05:33
Show Gist options
  • Save WindSekirun/1ec8349325f9e62612aeaec4b34a02f0 to your computer and use it in GitHub Desktop.
Save WindSekirun/1ec8349325f9e62612aeaec4b34a02f0 to your computer and use it in GitHub Desktop.
package com.company;
public class VibrateIntensityUtil {
/**
* get Intensity (Desired Strength) of Vibrate
* NOTE: Some device's default value may 0.7f (Tested in Galaxy S6),
* So you need to give choice to user in some way (RadioButton in Dialog?)
* <p/>
* But.. for Palette2? I don't know.
* <p/>
* Usage: Just load your Vibrator using getSystemService(Context.VIBRATOR_SERVICE), and vibrate(getVibratorPattern(NORMAL, 50), -1);
* Warning: This long[] array may take a lot of memory, I recommend using Field Variable or Singleton.
* <p/>
* VERY SOFT = 0.1f (40 1), SOFT: 0.3f (20 1), NORMAL: 0.5f(1 1), HARD: 0.7f(1 20), VERY HARD: 0.9f(1 40)
*
* @param intensity Desired strength of vibration. There are 5 Options.
* @param duration Time to vibrate.
* @return long[] array of Pattern of Vibrate.
*/
public static long[] getVibratorPattern(Intensity intensity, long duration) {
float intensityToLong;
switch (intensity) {
case VERYSOFT:
intensityToLong = 0.1f;
break;
case SOFT:
intensityToLong = 0.3f;
break;
case NORMAL:
default:
intensityToLong = 0.5f;
break;
case HARD:
intensityToLong = 0.7f;
break;
case VERYHARD:
intensityToLong = 0.9f;
break;
}
return getVibratorPattern(intensityToLong, duration);
}
/**
* get Intensity (Desired Strength) of Vibrate
* Normal Way, just use getVibratorPattern(Intensity, long) for easy work.
* if you need deliberate control, use this methods.
* <p/>
* Usage: Just load your Vibrator using getSystemService(Context.VIBRATOR_SERVICE), and vibrate(getVibratorPattern(NORMAL, 50), -1);
* Warning: This long[] array may take a lot of memory, I recommend using Field Variable or Singleton.
* <p/>
*
* @param intensity Float value. Range is 0.0f ~ 1.0f
* @param duration Time to vibrate
* @return long[] array of Pattern of Vibrate
*/
public static long[] getVibratorPattern(float intensity, long duration) {
long[] pattern;
if ((intensity >= 0.0f) && (intensity <= 1.0f)) { // intensity 가 지정된 값을 벗어났는지
float dutyCycle = Math.abs((intensity * 2.0f) - 1.0f); // Math.abs => 절대값을 구함
long hWidth = (long) (dutyCycle * (duration - 1)) + 1; // 시간에 따라 진동을 울릴 곳을 표시
long lWidth = dutyCycle == 1.0f ? 0 : 1; // dutyCycle 가 1이면 0, 나머지의 경우 1
int pulseCount = (int) (2.0f * (float) (duration / (hWidth + lWidth))); // 구한 값을 기초로 패턴 수 정함
pattern = new long[pulseCount]; // 배열 생성
for (int i = 0; i < pulseCount; i++) { // 배열 대입
pattern[i] = intensity < 0.5f ? (i % 2 == 0 ? hWidth : lWidth) : (i % 2 == 0 ? lWidth : hWidth);
// 다항 연산자
// intensity -> 0.5 밑이면, i가 짝수일 경우 hWidth 삽입, 홀수의 경우 lWidth 삽입
// intensity -> 0.5 이상이면, i가 짝수일 경우 lWidth 삽입, 홀수의 경우 hWidth 삽입
}
return pattern; // 리턴
} else {
throw new RuntimeException("Abnormal intensity value! check your method"); // 잘못된 값을 던질 경우 오류를 던짐
}
}
public enum Intensity {
VERYSOFT, SOFT, NORMAL, HARD, VERYHARD
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment