-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Kyle Corry | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# andromeda | ||
A collection of Android libraries for simplifying development |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
group = 'com.kylecorry.andromeda' | ||
version = '1.0.0' | ||
|
||
buildscript { | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath "com.android.tools.build:gradle:4.2.2" | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21" | ||
} | ||
} | ||
|
||
allprojects { | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
} | ||
|
||
task clean(type: Delete) { | ||
delete rootProject.buildDir | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'maven-publish' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion 30 | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 30 | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
coreLibraryDesugaringEnabled true | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
useIR = true | ||
} | ||
afterEvaluate { | ||
publishing { | ||
publications { | ||
release(MavenPublication) { | ||
from components.release | ||
groupId = group | ||
artifactId = 'buzz' | ||
version = version | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'androidx.core:core-ktx:1.6.0' | ||
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5' | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.kylecorry.andromeda.buzz"> | ||
|
||
<uses-permission android:name="android.permission.VIBRATE" /> | ||
<application /> | ||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.kylecorry.andromeda.buzz | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import android.os.VibrationEffect | ||
import android.os.Vibrator | ||
import androidx.core.content.getSystemService | ||
import java.time.Duration | ||
|
||
class Buzz(context: Context) : IBuzz { | ||
private val vibrator = context.getSystemService<Vibrator>() | ||
|
||
private fun waveform(millis: List<Long>, repeatPosition: Int = -1) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
vibrator?.vibrate(VibrationEffect.createWaveform(millis.toLongArray(), repeatPosition)) | ||
} else { | ||
vibrator?.vibrate(millis.toLongArray(), repeatPosition) | ||
} | ||
} | ||
|
||
private fun waveform(millis: List<Long>, amplitudes: List<Int>, repeatPosition: Int = -1) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
vibrator?.vibrate( | ||
VibrationEffect.createWaveform( | ||
millis.toLongArray(), | ||
amplitudes.toIntArray(), | ||
repeatPosition | ||
) | ||
) | ||
} else { | ||
vibrator?.vibrate(millis.toLongArray(), repeatPosition) | ||
} | ||
} | ||
|
||
private fun oneShot(millis: Long, amplitude: Int) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
vibrator?.vibrate(VibrationEffect.createOneShot(millis, amplitude)) | ||
} else { | ||
vibrator?.vibrate(millis) | ||
} | ||
} | ||
|
||
override fun on(amplitude: Int) { | ||
oneShot(Duration.ofDays(1).toMillis(), amplitude) | ||
} | ||
|
||
override fun feedback(feedbackType: HapticFeedbackType) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
val effectId = when (feedbackType) { | ||
HapticFeedbackType.Tick -> VibrationEffect.EFFECT_TICK | ||
HapticFeedbackType.Click -> VibrationEffect.EFFECT_CLICK | ||
HapticFeedbackType.HeavyClick -> VibrationEffect.EFFECT_HEAVY_CLICK | ||
HapticFeedbackType.DoubleClick -> VibrationEffect.EFFECT_DOUBLE_CLICK | ||
} | ||
vibrator?.vibrate(VibrationEffect.createPredefined(effectId)) | ||
} else { | ||
val vibrationPattern: List<Long> = when (feedbackType) { | ||
HapticFeedbackType.Tick -> listOf(125, 30) | ||
HapticFeedbackType.Click -> listOf(0, 10, 20, 30) | ||
HapticFeedbackType.HeavyClick -> listOf(0, 1, 20, 21) | ||
HapticFeedbackType.DoubleClick -> listOf(0, 30, 100, 30) | ||
} | ||
pattern(vibrationPattern.map { Duration.ofMillis(it) }) | ||
} | ||
} | ||
|
||
override fun off() { | ||
vibrator?.cancel() | ||
} | ||
|
||
override fun once(onDuration: Duration, amplitude: Int) { | ||
oneShot(onDuration.toMillis(), amplitude) | ||
} | ||
|
||
override fun interval(onDuration: Duration, offDuration: Duration, amplitude: Int) { | ||
pattern(listOf(onDuration, offDuration), listOf(amplitude, 0), true) | ||
} | ||
|
||
override fun pattern(durations: List<Duration>, amplitudes: List<Int>?, repeat: Boolean) { | ||
if (amplitudes != null) { | ||
waveform(durations.map { it.toMillis() }, amplitudes, if (repeat) 0 else -1) | ||
} else { | ||
waveform(durations.map { it.toMillis() }, if (repeat) 0 else -1) | ||
} | ||
} | ||
|
||
override fun isAvailable(): Boolean { | ||
return vibrator?.hasVibrator() == true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.kylecorry.andromeda.buzz | ||
|
||
enum class HapticFeedbackType { | ||
Tick, | ||
Click, | ||
HeavyClick, | ||
DoubleClick | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.kylecorry.andromeda.buzz | ||
|
||
import com.kylecorry.andromeda.buzz.HapticFeedbackType | ||
import java.time.Duration | ||
|
||
/** | ||
* An interface for the vibrator on Android | ||
*/ | ||
interface IBuzz { | ||
/** | ||
* Turns the vibrator on (note: maximum 1 day) | ||
* @param amplitude the amplitude between 0 and 255, or -1 for the default amplitude | ||
*/ | ||
fun on(amplitude: Int = -1) | ||
|
||
/** | ||
* Performs the specified haptic feedback | ||
*/ | ||
fun feedback(feedbackType: HapticFeedbackType) | ||
|
||
/** | ||
* Turns the vibrator off | ||
*/ | ||
fun off() | ||
|
||
/** | ||
* Turns the vibrator on for a duration | ||
* @param onDuration the duration to turn the vibrator on for | ||
* @param amplitude the amplitude between 0 and 255, or -1 for the default amplitude | ||
*/ | ||
fun once(onDuration: Duration, amplitude: Int = -1) | ||
|
||
/** | ||
* Turns the vibrator on and off indefinitely (starts on) | ||
* @param onDuration the duration to turn the vibrator on for | ||
* @param offDuration the duration to turn the vibrator off for | ||
* @param amplitude the amplitude between 0 and 255, or -1 for the default amplitude | ||
*/ | ||
fun interval(onDuration: Duration, offDuration: Duration, amplitude: Int = -1) | ||
|
||
/** | ||
* Turns the vibrator on and off based on a pattern (starts off by default, override with amplitudes) | ||
* @param durations the duration to turn the vibrator on or off for | ||
* @param amplitudes the amplitudes between 0 and 255, or -1 for the default amplitude. Must correspond with the durations array. | ||
* @param repeat true to repeat indefinitely, defaults to false | ||
*/ | ||
fun pattern(durations: List<Duration>, amplitudes: List<Int>? = null, repeat: Boolean = false) | ||
|
||
/** | ||
* Determines if the device has a vibrator | ||
* @return true if a vibrator exists, false otherwise | ||
*/ | ||
fun isAvailable(): Boolean | ||
} |