Unverified Commit a51439ac authored by Mara Habets's avatar Mara Habets
Browse files

Add relative volume support

parent 5b8fc908
Loading
Loading
Loading
Loading
+17 −6
Original line number Diff line number Diff line
@@ -53,14 +53,29 @@ class AudioService : Service() {
        audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
    }
    private var previousVolume = -1
    private var previousValue = -1

    private fun changeVolume(value: Int): Int {
        if (value == -1) return -1
        val newVolume = map(value, 0, 15, 0, maxVolume)

        val newVolume = if (Settings.isRelative(this)) {
            val currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
            when {
                value > previousValue -> currentVolume + 2
                value < previousValue -> currentVolume - 2
                else -> currentVolume
            }
        } else {
            map(value, 0, 15, 0, maxVolume)
        }


        if (previousVolume != newVolume) {
            previousVolume = newVolume
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, if (BuildConfig.DEBUG) AudioManager.FLAG_SHOW_UI else 0)
        }

        previousValue = value
        previousVolume = newVolume
        return previousVolume
    }

@@ -70,10 +85,6 @@ class AudioService : Service() {

    override fun onBind(intent: Intent): IBinder? = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onCreate() {
        super.onCreate()
        createNotificationChannel(this)
+3 −2
Original line number Diff line number Diff line
@@ -7,8 +7,6 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
@@ -26,6 +24,9 @@ class MainActivity : AppCompatActivity() {
            startService(Intent(this, AudioService::class.java))
        }
        buttonDisable.setOnClickListener { stopService(Intent(this, AudioService::class.java)) }
        checkBox.setOnClickListener {
            checkBox.isChecked = Settings.toggleRelative(this)
        }
        volumeControlStream = AudioManager.STREAM_MUSIC
    }

+28 −0
Original line number Diff line number Diff line
package io.habets.jellyfish

import android.content.Context

object Settings {

    private const val KEY = "settings"
    private const val KEY_RELATIVE = "relative"

    fun isRelative(context: Context): Boolean {
        return context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
                .getBoolean(KEY_RELATIVE, false)
    }

    fun setRelative(context: Context, isRelative: Boolean) {
        context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
                .edit()
                .putBoolean(KEY_RELATIVE, isRelative)
                .apply()
    }

    fun toggleRelative(context: Context): Boolean {
        val prefs = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
        val new = !prefs.getBoolean(KEY_RELATIVE, false)
        prefs.edit().putBoolean(KEY_RELATIVE, new).apply()
        return new
    }
}
 No newline at end of file
+23 −12
Original line number Diff line number Diff line
@@ -6,35 +6,35 @@
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="io.habets.jellyfish.MainActivity"
    tools:showIn="@layout/activity_main"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/buttonEnable"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_marginTop="16dp"
        android:text="Enable"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="19dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintEnd_toStartOf="@+id/buttonDisable"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp" />
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="19dp" />

    <Button
        android:id="@+id/buttonDisable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Disable"
        tools:layout_editor_absoluteX="280dp"
        tools:layout_editor_absoluteY="16dp"
        app:layout_constraintStart_toEndOf="@+id/buttonEnable"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/buttonEnable"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp" />
        tools:layout_editor_absoluteX="280dp"
        tools:layout_editor_absoluteY="16dp" />

    <TextView
        android:id="@+id/textView"
@@ -49,4 +49,15 @@
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="162dp"
        tools:layout_editor_absoluteY="245dp" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Relative mode"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>