Skip to content

Commit

Permalink
added universal pip controls
Browse files Browse the repository at this point in the history
  • Loading branch information
legendsayantan committed Jun 24, 2024
1 parent 5df671e commit 1216c4c
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 66 deletions.
Binary file modified app/release/app-release.apk
Binary file not shown.
9 changes: 4 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@
<receiver
android:name=".receivers.PipReceiver"
android:enabled="true"
android:exported="true"></receiver>
android:exported="true" />

<activity
android:name=".ForcePipActivity"
android:exported="false" />
<activity
android:name=".PipStarterActivity"
android:excludeFromRecents="true"
android:exported="true" />
android:exported="true"
android:label=""
android:theme="@style/DialogActivityTheme" />

<service
android:name=".services.SoundMasterService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class LookbackActivity : AppCompatActivity() {
}

try {
if(!outputFile.exists()){
outputFile.parentFile?.mkdirs()
outputFile.createNewFile()
}
val outputStream = FileOutputStream(outputFile)
inputStream.use { input ->
outputStream.use { output ->
Expand Down
210 changes: 150 additions & 60 deletions app/src/main/java/com/legendsayantan/adbtools/PipStarterActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.card.MaterialCardView
import com.legendsayantan.adbtools.lib.ShizukuRunner
import java.util.Timer
import kotlin.concurrent.timerTask
Expand All @@ -13,95 +15,183 @@ class PipStarterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
intent.getStringExtra("package")
?.let {
startActivity(packageManager.getLaunchIntentForPackage(it))
intent.getStringExtra("package").let { pkg ->
if (pkg == null) {
showing = true
setContentView(R.layout.activity_pip_starter)
val controls = listOf<MaterialCardView>(
findViewById(R.id.skipPrev),
findViewById(R.id.rewind),
findViewById(R.id.playPause),
findViewById(R.id.forward),
findViewById(R.id.skipNext)
)
val keys = listOf(
arrayOf(KeyEvent.KEYCODE_MEDIA_PREVIOUS),
arrayOf(KeyEvent.KEYCODE_MEDIA_REWIND,KeyEvent.KEYCODE_DPAD_LEFT),
arrayOf(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE),
arrayOf(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD,KeyEvent.KEYCODE_DPAD_RIGHT),
arrayOf(KeyEvent.KEYCODE_MEDIA_NEXT)
)
controls.forEachIndexed { index, materialCardView ->
materialCardView.setOnClickListener {
keys[index].forEach { key ->
getExternalWindowId { window-> ShizukuRunner.runAdbCommand("input -d $window keyevent $key",
object : ShizukuRunner.CommandResultListener {}) }
interacted()
}
}
}
val extraBtns = listOf<MaterialCardView>(
findViewById(R.id.adSkipButton),
findViewById(R.id.fullScreenButton)
)
extraBtns[0].setOnClickListener {
interacted()
val metrics = getWindowParams()
getExternalWindowId {
ShizukuRunner.runAdbCommand("input -d $it tap ${(metrics.first * 0.95).toInt()} ${(metrics.second*0.86).toInt()}",
object : ShizukuRunner.CommandResultListener {})
}
}
extraBtns[1].setOnClickListener {
disablePip()
interacted()
}

//outside touch
findViewById<ConstraintLayout>(R.id.main).setOnClickListener {
finish()
}

setupAutoHide()
} else {
startActivity(packageManager.getLaunchIntentForPackage(pkg))
playVideo()
finish()
}
finish()
}
}

private fun setupAutoHide() {
Timer().schedule(timerTask {
if (lastInteractionAt >= 0
&& lastInteractionAt + hideTimerInterval < System.currentTimeMillis()
) {
finish()
cancel()
}
}, hideTimerInterval, hideTimerInterval)
}

companion object {
var showing = false
private const val hideTimerInterval = 3000L
var lastInteractionAt = 0L
var interacted = {
lastInteractionAt = System.currentTimeMillis()
}
fun Context.getWindowParams():Pair<Int,Int>{
val metrics = resources.displayMetrics
return Pair(metrics.widthPixels,((metrics.widthPixels / (metrics.heightPixels.toFloat() / metrics.widthPixels)) + 100).toInt())
}
fun getExternalWindowId(callback:(Int)->Unit){
ShizukuRunner.runAdbCommand("dumpsys display | grep 'Display [0-9][0-9]*'",
object :
ShizukuRunner.CommandResultListener {
override fun onCommandResult(
output: String,
done: Boolean
) {
if (done) {
callback("\\d+".toRegex()
.findAll(output)
.filter { it.value != "0" }
.map { it.value.toInt() }
.maxOrNull() ?: 0)
}
}

override fun onCommandError(error: String) {
println(error)
disablePip()
}
})
}


fun Context.handlePip() {
ShizukuRunner.runAdbCommand("settings get global overlay_display_devices",object : ShizukuRunner.CommandResultListener{
override fun onCommandResult(output: String, done: Boolean) {
if(done){
if(output.trim().contains("null",true)){
enablePip()
}else{
disablePip()
ShizukuRunner.runAdbCommand("settings get global overlay_display_devices",
object : ShizukuRunner.CommandResultListener {
override fun onCommandResult(output: String, done: Boolean) {
if (done) {
if (output.trim().contains("null", true)) {
enablePip()
} else {
ShizukuRunner.runAdbCommand("am start -n $packageName/${PipStarterActivity::class.java.canonicalName} --display 0",
object : ShizukuRunner.CommandResultListener {
override fun onCommandError(
error: String
) {
disablePip()
println(error)
}
})
}
}
}
}
})
})
}

fun Context.enablePip(){
fun Context.enablePip() {
Timer().schedule(timerTask {
ShizukuRunner.runAdbCommand(
"dumpsys window displays | grep -E 'mCurrentFocus'",
object : ShizukuRunner.CommandResultListener {
override fun onCommandResult(output: String, done: Boolean) {
if (done) {
val pipPackage = output.split(" ")[4].split("/")[0]
val metrics = resources.displayMetrics
val height = (metrics.widthPixels / (metrics.heightPixels.toFloat() / metrics.widthPixels))+100
ShizukuRunner.runAdbCommand("settings put global overlay_display_devices ${metrics.widthPixels}x${height.toInt()}/240",
val metrics = getWindowParams()
ShizukuRunner.runAdbCommand("settings put global overlay_display_devices ${metrics.first}x${metrics.second}/240",
object : ShizukuRunner.CommandResultListener {
override fun onCommandResult(
output: String,
done: Boolean
) {
if (done) {
Timer().schedule(timerTask {
ShizukuRunner.runAdbCommand("dumpsys display | grep 'Display [0-9][0-9]*'",
object : ShizukuRunner.CommandResultListener {
override fun onCommandResult(
output: String,
done: Boolean
) {
if (done) {
val newDisplayId =
"\\d+".toRegex()
.findAll(output)
.filter { it.value != "0" }
.map { it.value.toInt() }
.maxOrNull() ?: 0
println(output)
println(newDisplayId)
val command =
"am start -n $packageName/${PipStarterActivity::class.java.canonicalName} --es package $pipPackage --display $newDisplayId"
ShizukuRunner.runAdbCommand(
command,
object :
ShizukuRunner.CommandResultListener {
override fun onCommandResult(
output: String,
done: Boolean
) {
if (done) {
println("PIP started")
}
}

override fun onCommandError(
error: String
) {
disablePip()
println(error)
}
})
getExternalWindowId { newDisplayId->
val command =
"am start -n $packageName/${PipStarterActivity::class.java.canonicalName} --es package $pipPackage --display $newDisplayId"
ShizukuRunner.runAdbCommand(
command,
object :
ShizukuRunner.CommandResultListener {
override fun onCommandResult(
output: String,
done: Boolean
) {
if (done) {
println("PIP started")
}
}
}

override fun onCommandError(error: String) {
disablePip()
println(error)
}
})
override fun onCommandError(
error: String
) {
disablePip()
println(error)
}
})
}
}, 500)
}
}

override fun onCommandError(error: String) {
disablePip()
println(error)
}
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_fast_forward_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M4,18l8.5,-6L4,6v12zM13,6v12l8.5,-6L13,6z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_fast_rewind_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M11,18L11,6l-8.5,6 8.5,6zM11.5,12l8.5,6L20,6l-8.5,6z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_fullscreen_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M7,14L5,14v5h5v-2L7,17v-3zM5,10h2L7,7h3L10,5L5,5v5zM17,17h-3v2h5v-5h-2v3zM14,5v2h3v3h2L19,5h-5z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_skip_next_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M6,18l8.5,-6L6,6v12zM16,6v12h2V6h-2z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_skip_previous_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M6,6h2v12L6,18zM9.5,12l8.5,6L18,6z"/>

</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/rounded_next_plan_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="#000000"
android:viewportWidth="960"
android:viewportHeight="960">

<path
android:fillColor="@android:color/white"
android:pathData="M 584 480 L 560 480 Q 543 480 531.5 491.5 Q 520 503 520 520 Q 520 537 531.5 548.5 Q 543 560 560 560 L 680 560 Q 697 560 708.5 548.5 Q 720 537 720 520 L 720 400 Q 720 383 708.5 371.5 Q 697 360 680 360 Q 663 360 651.5 371.5 Q 640 383 640 400 L 640 422 Q 608 384 563.5 362 Q 519 340 466 340 Q 383 340 322.5 389.5 Q 262 439 245 514 Q 241 532 252 546 Q 263 560 280 560 Q 297 560 309.5 547.5 Q 322 535 327 518 Q 341 475 379 447.5 Q 417 420 466 420 Q 502 420 533 436.5 Q 564 453 584 480" />

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/rounded_play_pause_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M200,648L200,312L440,480L200,648ZM520,640L520,320L600,320L600,640L520,640ZM680,640L680,320L760,320L760,640L680,640Z"/>

</vector>
Loading

0 comments on commit 1216c4c

Please sign in to comment.