Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.app.PictureInPictureParams;
import android.util.Log;
import android.util.Rational;
import android.app.AppOpsManager;
import android.content.Context;

import androidx.annotation.NonNull;

Expand All @@ -17,6 +19,7 @@
public class PipAndroidModule extends ReactContextBaseJavaModule {
public static final String NAME = "PipAndroid";
public static final String PIP_MODE_CHANGE = "PIP_MODE_CHANGE";
public static final String PIP_CLOSED = "PIP_CLOSED";
private static DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter = null;

ReactApplicationContext reactApplicationContext;
Expand All @@ -25,17 +28,21 @@ public static void pipModeChanged(Boolean isInPictureInPictureMode) {
eventEmitter.emit(PIP_MODE_CHANGE, isInPictureInPictureMode);
}

public static void pipClosed() {
eventEmitter.emit(PIP_CLOSED, true);
}

public PipAndroidModule(ReactApplicationContext reactContext) {
super(reactContext);
Log.d("PIP", "Got the context");
Log.d("PIP", "Got the context");
this.reactApplicationContext = reactContext;
}

@Override
@NonNull
public String getName() {
return NAME;
}
@Override
@NonNull
public String getName() {
return NAME;
}

@Override
public void initialize() {
Expand All @@ -44,9 +51,14 @@ public void initialize() {
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
}

private boolean hasPermission() {
AppOpsManager appOps = (AppOpsManager) getReactApplicationContext().getSystemService(Context.APP_OPS_SERVICE);
return appOps.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), getReactApplicationContext().getPackageName()) == AppOpsManager.MODE_ALLOWED;
}

@ReactMethod
public void enterPipMode(int width, int height) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && hasPermission()) {
int ratWidth = width > 0 ? width : 380;
int ratHeight = height > 0 ? height : 214;

Expand Down
4 changes: 4 additions & 0 deletions src/PipHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class PipHandler {
return this?.EventEmitter?.addListener('PIP_MODE_CHANGE', listener);
}

onPipClosed(listener: () => void) {
return this?.EventEmitter?.addListener('PIP_CLOSED', listener);
}

/**
* Call this method from any component to enter the pip mode.
* This method accepts two integers, width and height, which have default values,
Expand Down