fix picture in picture

This commit is contained in:
Page Asgardius 2022-10-09 20:24:03 -07:00
parent 19b20185f4
commit 7c7f05c760
2 changed files with 74 additions and 29 deletions

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/4.7_WXGA_API_24.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-10-10T02:57:43.359125Z" />
</component>
</project>

View file

@ -2,6 +2,7 @@ package asgardius.page.s3manager;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.app.AppOpsManager;
import android.app.PictureInPictureParams; import android.app.PictureInPictureParams;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -58,11 +59,13 @@ public class VideoPlayer extends AppCompatActivity {
MediaSessionCompat mediaSession; MediaSessionCompat mediaSession;
MediaSessionConnector mediaSessionConnector; MediaSessionConnector mediaSessionConnector;
StyledPlayerView.ControllerVisibilityListener control; StyledPlayerView.ControllerVisibilityListener control;
AppOpsManager appOpsManager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player); setContentView(R.layout.activity_video_player);
appOpsManager = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
mediaSession = new MediaSessionCompat(this, getPackageName()); mediaSession = new MediaSessionCompat(this, getPackageName());
mediaSessionConnector = new MediaSessionConnector(mediaSession); mediaSessionConnector = new MediaSessionConnector(mediaSession);
hideSystemBars(); hideSystemBars();
@ -216,18 +219,26 @@ public class VideoPlayer extends AppCompatActivity {
protected void enterPIPMode() { protected void enterPIPMode() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N try {
&& this.getPackageManager() if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
.hasSystemFeature( && this.getPackageManager()
PackageManager.FEATURE_PICTURE_IN_PICTURE)) { .hasSystemFeature(
videoPosition = player.getCurrentPosition(); PackageManager.FEATURE_PICTURE_IN_PICTURE) && appOpsManager.checkOpNoThrow(
playerView.setUseController(false); AppOpsManager.OPSTR_PICTURE_IN_PICTURE,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { this.getPackageManager().getApplicationInfo(this.getPackageName(),
PictureInPictureParams params = new PictureInPictureParams.Builder().build(); PackageManager.GET_META_DATA).uid, this.getPackageName())
this.enterPictureInPictureMode(params); == AppOpsManager.MODE_ALLOWED) {
}else { videoPosition = player.getCurrentPosition();
this.enterPictureInPictureMode(); playerView.setUseController(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PictureInPictureParams params = new PictureInPictureParams.Builder().build();
this.enterPictureInPictureMode(params);
}else {
this.enterPictureInPictureMode();
}
} }
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} }
} }
@ -256,20 +267,28 @@ public class VideoPlayer extends AppCompatActivity {
super.onDestroy(); super.onDestroy();
} }
public void onStop() { /*public void onStop() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N try {
&& this.getPackageManager() if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
.hasSystemFeature( && this.getPackageManager()
PackageManager.FEATURE_PICTURE_IN_PICTURE)) { .hasSystemFeature(
mediaSessionConnector.setPlayer(null); PackageManager.FEATURE_PICTURE_IN_PICTURE) && appOpsManager.checkOpNoThrow(
mediaSession.setActive(false); AppOpsManager.OPSTR_PICTURE_IN_PICTURE,
player.release(); this.getPackageManager().getApplicationInfo(this.getPackageName(),
playerView.setPlayer(null); PackageManager.GET_META_DATA).uid, this.getPackageName())
simpleCache.release(); == AppOpsManager.MODE_ALLOWED) {
finish(); mediaSessionConnector.setPlayer(null);
mediaSession.setActive(false);
player.release();
playerView.setPlayer(null);
simpleCache.release();
finish();
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} }
super.onStop(); super.onStop();
} }*/
public void onResume(){ public void onResume(){
super.onResume(); super.onResume();
@ -285,12 +304,21 @@ public class VideoPlayer extends AppCompatActivity {
} }
public void onBackPressed() { public void onBackPressed() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N try {
&& this.getPackageManager() if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
.hasSystemFeature( && this.getPackageManager()
PackageManager.FEATURE_PICTURE_IN_PICTURE)) { .hasSystemFeature(
enterPIPMode(); PackageManager.FEATURE_PICTURE_IN_PICTURE) && appOpsManager.checkOpNoThrow(
} else { AppOpsManager.OPSTR_PICTURE_IN_PICTURE,
this.getPackageManager().getApplicationInfo(this.getPackageName(),
PackageManager.GET_META_DATA).uid, this.getPackageName())
== AppOpsManager.MODE_ALLOWED) {
enterPIPMode();
} else {
super.onBackPressed();
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
super.onBackPressed(); super.onBackPressed();
} }
} }