diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..dfe07704 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..aa724b77 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..6b58f438 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Andromeda \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..fb7f4a8a --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 00000000..bc944a83 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 00000000..d2ce72d1 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..860da66a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..614b52da --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 00000000..1f13976c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# andromeda + A collection of Android libraries for simplifying development diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..8e84bdf3 --- /dev/null +++ b/build.gradle @@ -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 +} \ No newline at end of file diff --git a/buzz/.gitignore b/buzz/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/buzz/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/buzz/build.gradle b/buzz/build.gradle new file mode 100644 index 00000000..9969c169 --- /dev/null +++ b/buzz/build.gradle @@ -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' +} \ No newline at end of file diff --git a/buzz/consumer-rules.pro b/buzz/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/buzz/proguard-rules.pro b/buzz/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/buzz/proguard-rules.pro @@ -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 \ No newline at end of file diff --git a/buzz/src/main/AndroidManifest.xml b/buzz/src/main/AndroidManifest.xml new file mode 100644 index 00000000..ab7520dc --- /dev/null +++ b/buzz/src/main/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/buzz/src/main/java/com/kylecorry/andromeda/buzz/Buzz.kt b/buzz/src/main/java/com/kylecorry/andromeda/buzz/Buzz.kt new file mode 100644 index 00000000..f7335853 --- /dev/null +++ b/buzz/src/main/java/com/kylecorry/andromeda/buzz/Buzz.kt @@ -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() + + private fun waveform(millis: List, 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, amplitudes: List, 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 = 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, amplitudes: List?, 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 + } +} \ No newline at end of file diff --git a/buzz/src/main/java/com/kylecorry/andromeda/buzz/HapticFeedbackType.kt b/buzz/src/main/java/com/kylecorry/andromeda/buzz/HapticFeedbackType.kt new file mode 100644 index 00000000..162ea39c --- /dev/null +++ b/buzz/src/main/java/com/kylecorry/andromeda/buzz/HapticFeedbackType.kt @@ -0,0 +1,8 @@ +package com.kylecorry.andromeda.buzz + +enum class HapticFeedbackType { + Tick, + Click, + HeavyClick, + DoubleClick +} \ No newline at end of file diff --git a/buzz/src/main/java/com/kylecorry/andromeda/buzz/IBuzz.kt b/buzz/src/main/java/com/kylecorry/andromeda/buzz/IBuzz.kt new file mode 100644 index 00000000..743dc0a1 --- /dev/null +++ b/buzz/src/main/java/com/kylecorry/andromeda/buzz/IBuzz.kt @@ -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, amplitudes: List? = null, repeat: Boolean = false) + + /** + * Determines if the device has a vibrator + * @return true if a vibrator exists, false otherwise + */ + fun isAvailable(): Boolean +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 00000000..98bed167 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,21 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app"s APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Automatically convert third-party libraries to use AndroidX +android.enableJetifier=true +# Kotlin code style for this project: "official" or "obsolete": +kotlin.code.style=official \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..e708b1c0 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..dc5d00c3 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Jul 31 08:40:14 EDT 2021 +distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100644 index 00000000..4f906e0c --- /dev/null +++ b/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 00000000..107acd32 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/notify/.gitignore b/notify/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/notify/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/notify/build.gradle b/notify/build.gradle new file mode 100644 index 00000000..77af7b8b --- /dev/null +++ b/notify/build.gradle @@ -0,0 +1,51 @@ +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 = 'notify' + version = version + } + } + } + } +} + +dependencies { + implementation 'androidx.core:core-ktx:1.6.0' + implementation 'androidx.appcompat:appcompat:1.3.1' + implementation 'com.google.android.material:material:1.4.0' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} \ No newline at end of file diff --git a/notify/consumer-rules.pro b/notify/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/notify/proguard-rules.pro b/notify/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/notify/proguard-rules.pro @@ -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 \ No newline at end of file diff --git a/notify/src/main/AndroidManifest.xml b/notify/src/main/AndroidManifest.xml new file mode 100644 index 00000000..6a208640 --- /dev/null +++ b/notify/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/notify/src/main/java/com/kylecorry/andromeda/notify/INotify.kt b/notify/src/main/java/com/kylecorry/andromeda/notify/INotify.kt new file mode 100644 index 00000000..cde84971 --- /dev/null +++ b/notify/src/main/java/com/kylecorry/andromeda/notify/INotify.kt @@ -0,0 +1,94 @@ +package com.kylecorry.andromeda.notify + +import android.app.Notification +import android.app.PendingIntent +import android.content.Context +import androidx.annotation.DrawableRes +import androidx.core.app.NotificationCompat + +interface INotify { + + // Low level + fun isActive(notificationId: Int): Boolean + + fun send(notificationId: Int, notification: Notification) + + fun cancel(notificationId: Int) + + // Channel + fun createChannel( + id: String, + name: String, + description: String, + importance: Int, + muteSound: Boolean = false + ) + + // Notification types + /** + * Used for alerts which require the user's attention + */ + fun alert( + channel: String, + title: String, + contents: String?, + @DrawableRes icon: Int, + autoCancel: Boolean = false, + alertOnlyOnce: Boolean = false, + showBigIcon: Boolean = false, + group: String? = null, + intent: PendingIntent? = null, + actions: List = listOf() + ): Notification + + /** + * Used to convey a status message + * + * Basically alerts that don't require the user's immediate attention + */ + fun status( + channel: String, + title: String, + contents: String?, + @DrawableRes icon: Int, + autoCancel: Boolean = false, + alertOnlyOnce: Boolean = false, + showBigIcon: Boolean = false, + group: String? = null, + intent: PendingIntent? = null, + actions: List = listOf() + ): Notification + + /** + * Used for notifications connected to a process which give the user useful information + */ + fun persistent( + channel: String, + title: String, + contents: String?, + @DrawableRes icon: Int, + autoCancel: Boolean = false, + alertOnlyOnce: Boolean = true, + showBigIcon: Boolean = false, + group: String? = null, + intent: PendingIntent? = null, + actions: List = listOf() + ): Notification + + /** + * Used for notifications which are connected to a process (aka required) but the user doesn't care about them + */ + fun background( + channel: String, + title: String, + contents: String?, + @DrawableRes icon: Int + ): Notification + + fun action( + name: String, + intent: PendingIntent, + @DrawableRes icon: Int? = null + ): NotificationCompat.Action + +} \ No newline at end of file diff --git a/notify/src/main/java/com/kylecorry/andromeda/notify/Notify.kt b/notify/src/main/java/com/kylecorry/andromeda/notify/Notify.kt new file mode 100644 index 00000000..b175b9a6 --- /dev/null +++ b/notify/src/main/java/com/kylecorry/andromeda/notify/Notify.kt @@ -0,0 +1,256 @@ +package com.kylecorry.andromeda.notify + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.graphics.Color +import android.graphics.drawable.Drawable +import android.os.Build +import androidx.annotation.DrawableRes +import androidx.core.app.NotificationCompat +import androidx.core.content.getSystemService +import androidx.core.content.res.ResourcesCompat +import androidx.core.graphics.drawable.toBitmap + +class Notify(private val context: Context) : INotify { + + private val manager by lazy { getNotificationManager() } + + override fun isActive(notificationId: Int): Boolean { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + manager?.activeNotifications?.any { it.id == notificationId } ?: false + } else { + // TODO: Determine if the notification exists + false + } + } + + override fun send(notificationId: Int, notification: Notification) { + manager?.notify(notificationId, notification) + } + + override fun cancel(notificationId: Int) { + manager?.cancel(notificationId) + } + + override fun createChannel( + id: String, + name: String, + description: String, + importance: Int, + muteSound: Boolean + ) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return + } + val channel = NotificationChannel(id, name, importance).apply { + this.description = description + if (muteSound) { + setSound(null, null) + enableVibration(false) + } + } + manager?.createNotificationChannel(channel) + } + + override fun alert( + channel: String, + title: String, + contents: String?, + icon: Int, + autoCancel: Boolean, + alertOnlyOnce: Boolean, + showBigIcon: Boolean, + group: String?, + intent: PendingIntent?, + actions: List + ): Notification { + val builder = NotificationCompat.Builder(context, channel) + .setContentTitle(title) + .setSmallIcon(icon) + .setAutoCancel(autoCancel) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setOnlyAlertOnce(alertOnlyOnce) + + if (contents != null) { + builder.setContentText(contents) + } + + if (showBigIcon) { + val drawable = drawable(context, icon) + val bitmap = drawable?.toBitmap() + builder.setLargeIcon(bitmap) + } + + if (group != null) { + builder.setGroup(group) + } + + if (intent != null) { + builder.setContentIntent(intent) + } + + for (action in actions) { + builder.addAction(action) + } + + val notification = builder.build() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + notification.smallIcon.setTint(Color.WHITE) + } + return notification + } + + override fun status( + channel: String, + title: String, + contents: String?, + icon: Int, + autoCancel: Boolean, + alertOnlyOnce: Boolean, + showBigIcon: Boolean, + group: String?, + intent: PendingIntent?, + actions: List + ): Notification { + val builder = NotificationCompat.Builder(context, channel) + .setContentTitle(title) + .setSmallIcon(icon) + .setAutoCancel(autoCancel) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setSilent(true) + .setOnlyAlertOnce(alertOnlyOnce) + + if (contents != null) { + builder.setContentText(contents) + } + + if (showBigIcon) { + val drawable = drawable(context, icon) + val bitmap = drawable?.toBitmap() + builder.setLargeIcon(bitmap) + } + + if (group != null) { + builder.setGroup(group) + } + + if (intent != null) { + builder.setContentIntent(intent) + } + + for (action in actions) { + builder.addAction(action) + } + + val notification = builder.build() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + notification.smallIcon.setTint(Color.WHITE) + } + return notification + } + + override fun persistent( + channel: String, + title: String, + contents: String?, + icon: Int, + autoCancel: Boolean, + alertOnlyOnce: Boolean, + showBigIcon: Boolean, + group: String?, + intent: PendingIntent?, + actions: List + ): Notification { + val builder = NotificationCompat.Builder(context, channel) + .setContentTitle(title) + .setSmallIcon(icon) + .setAutoCancel(autoCancel) + .setOngoing(true) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setSilent(true) + .setOnlyAlertOnce(alertOnlyOnce) + + if (contents != null) { + builder.setContentText(contents) + } + + if (showBigIcon) { + val drawable = drawable(context, icon) + val bitmap = drawable?.toBitmap() + builder.setLargeIcon(bitmap) + } + + if (group != null) { + builder.setGroup(group) + } + + if (intent != null) { + builder.setContentIntent(intent) + } + + for (action in actions) { + builder.addAction(action) + } + + val notification = builder.build() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + notification.smallIcon.setTint(Color.WHITE) + } + return notification + } + + override fun background( + channel: String, + title: String, + contents: String?, + icon: Int + ): Notification { + val builder = NotificationCompat.Builder(context, channel) + .setContentTitle(title) + .setSmallIcon(icon) + .setAutoCancel(false) + .setOngoing(true) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setSilent(true) + .setOnlyAlertOnce(true) + + if (contents != null) { + builder.setContentText(contents) + } + + val notification = builder.build() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + notification.smallIcon.setTint(Color.WHITE) + } + return notification + } + + override fun action( + name: String, + intent: PendingIntent, + icon: Int? + ): NotificationCompat.Action { + return NotificationCompat.Action(icon ?: 0, name, intent) + } + + private fun getNotificationManager(): NotificationManager? { + return context.getSystemService() + } + + // TODO: Extract the UI Utils from TS Core + private fun drawable(context: Context, @DrawableRes drawableId: Int): Drawable? { + return ResourcesCompat.getDrawable(context.resources, drawableId, null) + } + + companion object { + val CHANNEL_IMPORTANCE_HIGH = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) NotificationManager.IMPORTANCE_HIGH else 4 + val CHANNEL_IMPORTANCE_DEFAULT = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) NotificationManager.IMPORTANCE_DEFAULT else 3 + val CHANNEL_IMPORTANCE_LOW = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) NotificationManager.IMPORTANCE_LOW else 2 + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..199905df --- /dev/null +++ b/settings.gradle @@ -0,0 +1,3 @@ +rootProject.name = "Andromeda" +include ':buzz' +include ':notify'