Skip to content

Commit b1b6d94

Browse files
committed
Add Picture-in-Picture support to keep the camera alive in the background
1 parent 201dccf commit b1b6d94

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/main/AndroidManifest.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@
6464
android:screenOrientation="portrait" />
6565
<activity
6666
android:name=".MonitorActivity"
67-
android:configChanges="orientation|screenSize"
6867
android:label="@string/app_name"
6968
android:screenOrientation="portrait"
70-
android:launchMode="singleTop" />
69+
android:launchMode="singleTop"
70+
android:resizeableActivity="true"
71+
android:supportsPictureInPicture="true"
72+
android:configChanges=
73+
"screenSize|smallestScreenSize|screenLayout|orientation"
74+
/>
7175
<activity
7276
android:name=".ui.VideoPlayerActivity"
7377
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"

src/main/java/org/havenapp/main/MonitorActivity.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.Manifest;
2020
import android.content.Intent;
2121
import android.content.pm.PackageManager;
22+
import android.content.res.Configuration;
23+
import android.os.Build;
2224
import android.os.Bundle;
2325
import android.os.CountDownTimer;
2426
import android.os.Environment;
@@ -209,6 +211,18 @@ private void doCancel() {
209211

210212
}
211213

214+
@Override
215+
public void onPictureInPictureModeChanged (boolean isInPictureInPictureMode, Configuration newConfig) {
216+
if (isInPictureInPictureMode) {
217+
// Hide the full-screen UI (controls, etc.) while in picture-in-picture mode.
218+
findViewById(R.id.buttonBar).setVisibility(View.GONE);
219+
} else {
220+
// Restore the full-screen UI.
221+
findViewById(R.id.buttonBar).setVisibility(View.VISIBLE);
222+
223+
}
224+
}
225+
212226
private void showSettings() {
213227

214228
Intent i = new Intent(this, SettingsActivity.class);
@@ -288,21 +302,36 @@ private void initMonitor() {
288302

289303
}
290304

291-
/**
292-
* Closes the monitor activity and unset session properties
293-
*/
294-
private void close() {
295-
296-
finish();
297305

306+
@Override
307+
public void onUserLeaveHint () {
308+
if (mIsMonitoring) {
309+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
310+
enterPictureInPictureMode();
311+
}
312+
}
298313
}
299-
300314
/**
301315
* When user closes the activity
302316
*/
303317
@Override
304318
public void onBackPressed() {
305-
close();
319+
320+
if (mIsMonitoring) {
321+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
322+
enterPictureInPictureMode();
323+
}
324+
else
325+
{
326+
finish();
327+
}
328+
}
329+
else
330+
{
331+
finish();
332+
}
333+
334+
306335
}
307336

308337
private void showTimeDelayDialog() {

src/main/res/layout/activity_monitor.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
android:layout_height="match_parent"
7373
android:gravity="center_horizontal|bottom"
7474
android:orientation="horizontal"
75+
android:id="@+id/buttonBar"
7576
android:padding="10dp">
7677

7778
<ImageView

0 commit comments

Comments
 (0)