Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Aug 9, 2021
0 parents commit c315802
Show file tree
Hide file tree
Showing 32 changed files with 1,093 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
15 changes: 15 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE
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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# andromeda
A collection of Android libraries for simplifying development
24 changes: 24 additions & 0 deletions build.gradle
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
}
1 change: 1 addition & 0 deletions buzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
50 changes: 50 additions & 0 deletions buzz/build.gradle
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'
}
Empty file added buzz/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions buzz/proguard-rules.pro
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
7 changes: 7 additions & 0 deletions buzz/src/main/AndroidManifest.xml
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>
90 changes: 90 additions & 0 deletions buzz/src/main/java/com/kylecorry/andromeda/buzz/Buzz.kt
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
}
54 changes: 54 additions & 0 deletions buzz/src/main/java/com/kylecorry/andromeda/buzz/IBuzz.kt
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
}
Loading

0 comments on commit c315802

Please sign in to comment.