Skip to content

Instantly share code, notes, and snippets.

View nieldeokar's full-sized avatar

Nilesh Deokar nieldeokar

View GitHub Profile
@nieldeokar
nieldeokar / DemoVibrateAndroid
Created November 13, 2017 11:18
Using Vibrate in android demo snippet
<uses-permission android:name="android.permission.VIBRATE"/>
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator.hasVibrator()) {
vibrator.vibrate(500); // for 500 ms
}
@nieldeokar
nieldeokar / OneShotVibration.java
Last active November 13, 2017 11:24
This method would create a one shot vibration using VibrationEffects
@RequiresApi(api = Build.VERSION_CODES.O)
private void createOneShotVibrationUsingVibrationEffect() {
// 1000 : Vibrate for 1 sec
// VibrationEffect.DEFAULT_AMPLITUDE - would perform vibration at full strength
VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
}
@nieldeokar
nieldeokar / WaveFormVibration.java
Last active November 13, 2017 11:33
This method would create wave form vibration using vibration effect
@RequiresApi(api = Build.VERSION_CODES.O)
private void createWaveFormVibrationUsingVibrationEffectAndAmplitude() {
long[] mVibratePattern = new long[]{0, 400, 800, 600, 800, 800, 800, 1000};
int[] mAmplitudes = new int[]{0, 255, 0, 255, 0, 255, 0, 255};
// -1 : Play exactly once
if (vibrator.hasAmplitudeControl()) {
VibrationEffect effect = VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, -1);
vibrator.vibrate(effect);
}
@nieldeokar
nieldeokar / VibratePattern.java
Last active November 13, 2017 11:37
This would create a custom vibrate pattern.
private void customVibratePatternNoRepeat() {
// 0 : Start without a delay
// 400 : Vibrate for 400 milliseconds
// 200 : Pause for 200 milliseconds
// 400 : Vibrate for 400 milliseconds
long[] mVibratePattern = new long[]{0, 400, 200, 400};
// -1 : Do not repeat this pattern
// pass 0 if you want to repeat this pattern from 0th index
vibrator.vibrate(mVibratePattern, -1);
@nieldeokar
nieldeokar / CreateOneshotVibration.java
Last active November 13, 2017 11:41
It would create one shot vibration using VibrationEffect
@RequiresApi(api = Build.VERSION_CODES.O)
private void createOneShotVibrationUsingVibrationEffect() {
// 1000 : Vibrate for 1 sec
// VibrationEffect.DEFAULT_AMPLITUDE - would perform vibration at full strength
VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
}
@nieldeokar
nieldeokar / SetAppUpdateAvailable.java
Last active May 30, 2018 13:30
Used for storing multiple boolean values into one int variable.
public class SetAppUpdateAvailable {
private int mUpdateValue = 0;
private static final int UPDATE_AVAILABLE = 1;
private static final int UPDATE_COMPULSORY = 2;
public void setUpdateAvailable(){
mUpdateValue = mUpdateValue | (1 << UPDATE_AVAILABLE);
@nieldeokar
nieldeokar / GetAppUpdateAvailable.java
Last active May 30, 2018 13:31
Used for storing multiple boolean values into one int variable. This snippet checks the bit values set in SetAppUpdateAvailable.java
package com.nileshdeokar.healthapp;
public class GetAppUpdateAvailable {
private int updateValue = 0b0110;
private static final int UPDATE_AVAILABLE = 1;
private static final int UPDATE_COMPULSORY = 2;
public void getUpdateAvailable(){
@nieldeokar
nieldeokar / GetAppUpdateAvailable.java
Last active May 30, 2018 13:36
Used for storing multiple boolean values into one int variable. This snippet checks the bit values set in SetAppUpdateAvailable.java
public class GetAppUpdateAvailable {
private int mUpdateValue = 0b0010;
private static final int UPDATE_AVAILABLE = 1;
private static final int UPDATE_COMPULSORY = 2;
public void getUpdateAvailable(){
boolean result = false;
result = (mUpdateValue & (1 << UPDATE_AVAILABLE)) != 0;
@nieldeokar
nieldeokar / AppUpdateCheckerUsingBitwiseShiftOperation.java
Last active May 31, 2018 17:39
Demonstration of how to use 1 Integer object to store 32 boolean values
public class AppUpdateCheckerUsingBitwiseShiftOperation {
private int mUpdateValue = 0;
private static final int UPDATE_AVAILABLE = 1;
private static final int UPDATE_COMPULSORY = 2;
public void setValue(int position){
mUpdateValue = mUpdateValue | (1 << position);
@nieldeokar
nieldeokar / SetBitPosition.java
Last active June 2, 2018 06:53
Demonstration of how to set specific bit to 1
int myOriginalValue = 0b0010;
// STEP 1 : 0b0001 << 3 = 0b1000;
// STEP 2 : OR 0b0010; originalValue
// --------------
// RESULT : 0b1010; // 3rd bit is set to true
// Formula for setting a bit :
// myOriginalValue = myOriginalValue | (1 << position);