Skip to content

Commit

Permalink
individual preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
jerleo committed Aug 25, 2019
1 parent 9d200a6 commit de00e93
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 71 deletions.
134 changes: 109 additions & 25 deletions .idea/codeStyles/Project.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "de.jerleo"
applicationId "de.jerleo.autoplay"
minSdkVersion 26
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -22,12 +22,12 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.1.0-alpha04'
implementation 'androidx.core:core-ktx:1.2.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
implementation 'androidx.preference:preference:1.0.0'
implementation 'com.android.support:design:28.1.0'
implementation 'com.google.android.material:material:1.0.0'
}
16 changes: 8 additions & 8 deletions app/src/main/java/de/jerleo/autoplay/Audio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ class Audio(
return manager
}

fun startPlayback(): Boolean {
fun startPlayback(device: String): Boolean {

if (manager()?.isMusicActive == true) {
Log.i(Main.TAG, "Music is active")
return false
}

// Get delay in milliseconds from preference
val delay = settings.delay() * 1000L
// Get delay in milliseconds from device preference
val delay = settings.delay(device) * 1000L

// Start playback with delay
Handler().postDelayed({ launchAudio() }, delay)
Handler().postDelayed({ launchAudio(device) }, delay)

return true
}

private fun launchAudio() {
private fun launchAudio(device: String) {

Log.i(Main.TAG, "Sending media key event")
manager()?.let {
Expand All @@ -45,15 +45,15 @@ class Audio(
KeyEvent.KEYCODE_MEDIA_PLAY
)
)
adjustVolume()
adjustVolume(device)
}
}

private fun adjustVolume() {
private fun adjustVolume(device: String) {

Log.i(Main.TAG, "Adjusting volume")
manager()?.let {
val percentage = settings.volume()
val percentage = settings.volume(device)
val maxVolume = it.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
val currentVolume = it.getStreamVolume(AudioManager.STREAM_MUSIC)
val targetVolume = percentage * maxVolume / 100
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/de/jerleo/autoplay/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.media.app.NotificationCompat as MediaNotificationCompat
import androidx.preference.PreferenceManager

class Main : AppCompatActivity() {

Expand All @@ -33,9 +33,9 @@ class Main : AppCompatActivity() {
fun startPlayback(device: BluetoothDevice) {

if (settings.isChecked(device))
if (audio.startPlayback()) {
if (audio.startPlayback(device.address)) {

Log.i(Main.TAG, "Launching playback")
Log.i(TAG, "Launching playback")
val text: String = getString(R.string.launching) +
": " + device.name
notification.show(getString(R.string.playback), text)
Expand All @@ -53,7 +53,11 @@ class Main : AppCompatActivity() {
.replace(R.id.settings, settings)
.commit()

Log.i(Main.TAG, "Listening for bluetooth events")
// Monitor preference changes
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(settings)

Log.i(TAG, "Listening for bluetooth events")
registerReceiver(
Bluetooth,
IntentFilter().apply {
Expand All @@ -72,7 +76,7 @@ class Main : AppCompatActivity() {

override fun onDestroy() {

Log.i(Main.TAG, "Stopped listening for bluetooth events")
Log.i(TAG, "Stopped listening for bluetooth events")
unregisterReceiver(Bluetooth)
notification.cancel()
super.onDestroy()
Expand Down
Loading

0 comments on commit de00e93

Please sign in to comment.