mirror of
https://codeberg.org/timelimit/timelimit-android.git
synced 2025-10-03 09:49:25 +02:00
Mute audio if possible
This commit is contained in:
parent
9c2341863e
commit
26b01440b3
5 changed files with 39 additions and 1 deletions
|
@ -42,6 +42,7 @@ abstract class PlatformIntegration(
|
|||
abstract fun showOverlayMessage(text: String)
|
||||
|
||||
abstract fun showAppLockScreen(currentPackageName: String, currentActivityName: String?)
|
||||
abstract fun muteAudioIfPossible(packageName: String)
|
||||
abstract fun setShowBlockingOverlay(show: Boolean)
|
||||
// this should throw an SecurityException if the permission is missing
|
||||
abstract suspend fun getForegroundApp(result: ForegroundAppSpec, queryInterval: Long)
|
||||
|
|
|
@ -28,11 +28,13 @@ import android.content.Intent
|
|||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.media.session.MediaSessionManager
|
||||
import android.os.Build
|
||||
import android.os.PowerManager
|
||||
import android.os.UserManager
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import android.view.KeyEvent
|
||||
import android.widget.Toast
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
|
@ -223,6 +225,26 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
|
|||
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) {
|
||||
if (show) {
|
||||
overlay.show()
|
||||
|
|
|
@ -65,6 +65,8 @@ class NotificationListener: NotificationListenerService() {
|
|||
lastOngoingNotificationHidden.remove(sbn.packageName)
|
||||
}
|
||||
} else {
|
||||
appLogic.platformIntegration.muteAudioIfPossible(sbn.packageName)
|
||||
|
||||
val success = try {
|
||||
if (sbn.isOngoing && SUPPORTS_HIDING_ONGOING_NOTIFICATIONS) {
|
||||
// only snooze for 5 seconds to show it again soon
|
||||
|
|
|
@ -87,6 +87,10 @@ class DummyIntegration(
|
|||
launchLockScreenForPackage = currentPackageName
|
||||
}
|
||||
|
||||
override fun muteAudioIfPossible(packageName: String) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
override fun setShowBlockingOverlay(show: Boolean) {
|
||||
// ignore
|
||||
}
|
||||
|
|
|
@ -75,6 +75,10 @@ class LockActivity : AppCompatActivity(), ActivityViewModelHolder {
|
|||
.commitNow()
|
||||
}
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
stopMediaPlayback()
|
||||
}
|
||||
|
||||
val syncModel = ViewModelProviders.of(this).get(SyncStatusModel::class.java)
|
||||
|
||||
syncModel.statusText.observe(this, Observer {
|
||||
|
@ -118,7 +122,7 @@ class LockActivity : AppCompatActivity(), ActivityViewModelHolder {
|
|||
IsAppInForeground.reportStop()
|
||||
}
|
||||
|
||||
fun lockTaskModeWorkaround() {
|
||||
private fun lockTaskModeWorkaround() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
val platformIntegration = DefaultAppLogic.with(this).platformIntegration
|
||||
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() {
|
||||
// do nothing because going back would open the blocked app again
|
||||
// super.onBackPressed()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue