Skip to content

Commit

Permalink
Soundboard: support multi-display devices
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 committed Jul 24, 2023
1 parent d2535e2 commit 9719c62
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package com.google.oboe.samples.soundboard

import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.Point
import android.graphics.Rect
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.min

Expand Down Expand Up @@ -52,10 +54,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onResume() {
setDefaultStreamValues(this)
calculateAndSetRectangles(this)
mEngineHandle = startEngine(mNumRows * mNumColumns)
createMusicTiles(this)
setup()
super.onResume()
}

Expand All @@ -64,6 +63,13 @@ class MainActivity : AppCompatActivity() {
super.onPause()
}

private fun setup() {
setDefaultStreamValues(this)
calculateAndSetRectangles(this)
mEngineHandle = startEngine(mNumRows * mNumColumns)
createMusicTiles(this)
}

private fun setDefaultStreamValues(context: Context) {
val myAudioMgr = context.getSystemService(AUDIO_SERVICE) as AudioManager
val sampleRateStr = myAudioMgr.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)
Expand All @@ -75,14 +81,19 @@ class MainActivity : AppCompatActivity() {
}

private fun calculateAndSetRectangles(context: Context) {
val size = Point()
val width: Int
val height: Int

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
display?.getRealSize(size)
width = windowManager.currentWindowMetrics.bounds.width()
height = windowManager.currentWindowMetrics.bounds.height()
} else {
val size = Point()
windowManager.defaultDisplay.getRealSize(size)
height = size.y
width = size.x
}
val height = size.y
val width = size.x

if (height > width) {
mNumColumns = DIMENSION_MIN_SIZE
mNumRows = min(DIMENSION_MIN_SIZE * height / width, DIMENSION_MAX_SIZE)
Expand All @@ -108,18 +119,14 @@ class MainActivity : AppCompatActivity() {
}

private fun createMusicTiles(context: Context) {
setContentView(MusicTileView(this, mRectangles, NoteListener(mEngineHandle)))
setContentView(MusicTileView(this, mRectangles, NoteListener(mEngineHandle),
ScreenChangeListener({ setup() })))
}

fun Activity.getRealScreenSize(): Pair<Int, Int> { //<width, height>
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val size = Point()
display?.getRealSize(size)
Pair(size.x, size.y)
} else {
val size = Point()
windowManager.defaultDisplay.getRealSize(size)
Pair(size.x, size.y)
class ScreenChangeListener(private var mFunc: () -> Unit) : MusicTileView.ConfigChangeListener {
override fun onConfigurationChanged() {
mFunc()
}
}

}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.oboe.samples.soundboard

import android.content.Context
import android.content.res.Configuration
import android.graphics.*
import android.util.SparseArray
import android.view.MotionEvent
Expand All @@ -25,12 +26,18 @@ import android.view.View
class MusicTileView(
context: Context?,
private val mRectangles: ArrayList<Rect>,
tileListener: TileListener
tileListener: TileListener,
configChangeListener: ConfigChangeListener
) : View(context) {
private val mIsPressedPerRectangle: BooleanArray = BooleanArray(mRectangles.size)
private val mPaint: Paint = Paint()
private val mLocationsOfFingers: SparseArray<PointF> = SparseArray()
private val mTileListener: TileListener
private val mConfigChangeListener : ConfigChangeListener

interface ConfigChangeListener {
fun onConfigurationChanged()
}

interface TileListener {
fun onTileOn(index: Int)
Expand Down Expand Up @@ -145,7 +152,13 @@ class MusicTileView(
return true
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
mConfigChangeListener.onConfigurationChanged()
}

init {
mTileListener = tileListener
mConfigChangeListener = configChangeListener
}
}

0 comments on commit 9719c62

Please sign in to comment.