Mute audio if possible

This commit is contained in:
Jonas L 2019-05-27 00:00:00 +00:00
parent 9c2341863e
commit 26b01440b3
5 changed files with 39 additions and 1 deletions

View file

@ -42,6 +42,7 @@ abstract class PlatformIntegration(
abstract fun showOverlayMessage(text: String) abstract fun showOverlayMessage(text: String)
abstract fun showAppLockScreen(currentPackageName: String, currentActivityName: String?) abstract fun showAppLockScreen(currentPackageName: String, currentActivityName: String?)
abstract fun muteAudioIfPossible(packageName: String)
abstract fun setShowBlockingOverlay(show: Boolean) abstract fun setShowBlockingOverlay(show: Boolean)
// this should throw an SecurityException if the permission is missing // this should throw an SecurityException if the permission is missing
abstract suspend fun getForegroundApp(result: ForegroundAppSpec, queryInterval: Long) abstract suspend fun getForegroundApp(result: ForegroundAppSpec, queryInterval: Long)

View file

@ -28,11 +28,13 @@ import android.content.Intent
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.media.session.MediaSessionManager
import android.os.Build import android.os.Build
import android.os.PowerManager import android.os.PowerManager
import android.os.UserManager import android.os.UserManager
import android.provider.Settings import android.provider.Settings
import android.util.Log import android.util.Log
import android.view.KeyEvent
import android.widget.Toast import android.widget.Toast
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
@ -223,6 +225,26 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
LockActivity.start(context, currentPackageName, currentActivityName) LockActivity.start(context, currentPackageName, currentActivityName)
} }
override fun muteAudioIfPossible(packageName: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (getNotificationAccessPermissionStatus() == NewPermissionStatus.Granted) {
val manager = context.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager
val sessions = manager.getActiveSessions(ComponentName(context, NotificationListener::class.java))
val sessionsOfTheApp = sessions.filter { it.packageName == packageName }
sessionsOfTheApp.forEach { session ->
session.dispatchMediaButtonEvent(KeyEvent(
KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_STOP
))
session.dispatchMediaButtonEvent(KeyEvent(
KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_MEDIA_STOP
))
}
}
}
}
override fun setShowBlockingOverlay(show: Boolean) { override fun setShowBlockingOverlay(show: Boolean) {
if (show) { if (show) {
overlay.show() overlay.show()

View file

@ -65,6 +65,8 @@ class NotificationListener: NotificationListenerService() {
lastOngoingNotificationHidden.remove(sbn.packageName) lastOngoingNotificationHidden.remove(sbn.packageName)
} }
} else { } else {
appLogic.platformIntegration.muteAudioIfPossible(sbn.packageName)
val success = try { val success = try {
if (sbn.isOngoing && SUPPORTS_HIDING_ONGOING_NOTIFICATIONS) { if (sbn.isOngoing && SUPPORTS_HIDING_ONGOING_NOTIFICATIONS) {
// only snooze for 5 seconds to show it again soon // only snooze for 5 seconds to show it again soon

View file

@ -87,6 +87,10 @@ class DummyIntegration(
launchLockScreenForPackage = currentPackageName launchLockScreenForPackage = currentPackageName
} }
override fun muteAudioIfPossible(packageName: String) {
// ignore
}
override fun setShowBlockingOverlay(show: Boolean) { override fun setShowBlockingOverlay(show: Boolean) {
// ignore // ignore
} }

View file

@ -75,6 +75,10 @@ class LockActivity : AppCompatActivity(), ActivityViewModelHolder {
.commitNow() .commitNow()
} }
if (savedInstanceState == null) {
stopMediaPlayback()
}
val syncModel = ViewModelProviders.of(this).get(SyncStatusModel::class.java) val syncModel = ViewModelProviders.of(this).get(SyncStatusModel::class.java)
syncModel.statusText.observe(this, Observer { syncModel.statusText.observe(this, Observer {
@ -118,7 +122,7 @@ class LockActivity : AppCompatActivity(), ActivityViewModelHolder {
IsAppInForeground.reportStop() IsAppInForeground.reportStop()
} }
fun lockTaskModeWorkaround() { private fun lockTaskModeWorkaround() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val platformIntegration = DefaultAppLogic.with(this).platformIntegration val platformIntegration = DefaultAppLogic.with(this).platformIntegration
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@ -135,6 +139,11 @@ class LockActivity : AppCompatActivity(), ActivityViewModelHolder {
} }
} }
private fun stopMediaPlayback() {
val platformIntegration = DefaultAppLogic.with(this).platformIntegration
platformIntegration.muteAudioIfPossible(blockedPackageName)
}
override fun onBackPressed() { override fun onBackPressed() {
// do nothing because going back would open the blocked app again // do nothing because going back would open the blocked app again
// super.onBackPressed() // super.onBackPressed()