The Application of Android N Multi-window: Painting in Painting

Keywords: Android Windows pip

Android N preview version supports Picture in Picture mode for video playback. Picture-in-picture is a special type of multi-window mode, which is mainly used to play video in the background. Android TV devices already support Picture-in-Picture mode. In multi-window mode, users can still see applications in pause state, and applications in pause state may still need to continue other operations. For example, a video playback application in pause mode but visible should continue to display the video. Android officials recommend that Activity, which plays video, not pause the video in onPause (), pause the video in onStop (), and resume playing in onStart (). First look at the picture in which the video is played:


In the Android N version, you can set whether Activity supports multiple windows in the manifest file. The property settings of the root activity apply to all activities of its task stack. Support multi-window settings as follows:

android:resizeableActivity="true"
If set to true, Activity will be able to split screens and start in free mode; if false, it will not support split screens. This property defaults to true. If you need to support Picture-in-Picture mode, set it as follows:

android:supportsPictureInPicture="true"

Related to multi-window layout attributes are: default height, default width, initial position, minimum height, minimum width. Specifically as follows:

<layout 
android:defaultHeight="500dp"//Set default height 
android:defaultWidth="600dp"//Set default width 
android:gravity="top|end"//Setting the initial position 
android:minimalHeight="450dp"//Set minimum height 
android:minimalWidth="300dp"//Set the minimum width
/>
If you need to query whether the current Activity is in multi-window mode, you can call the following method:
Activity.isInMultiWindowMode()
If you need to query whether the current Activity is in Picture-in-Picture mode, you can call the following method:
Activity.isInPictureInPictureMode()
When Activity enters or exits multi-window mode, the system calls the following methods. When Activity enters multi-window mode, the system passes true value to this method and false value to exit multi-window mode.
Activity.onMultiWindowModeChanged()
When Activity enters or exits Picture-in-Picture mode, the system calls the following methods. When Activity enters Picture-in-Picture (PIP) mode, the system passes the true value to this method, and when it exits PIP mode, it passes the false value.
Activity.onPictureInPictureModeChanged()
If you need to start Activity in Picture-in-Picture mode, you can call the following method:
Activity.enterPictureInPictureMode()
Start a new Activity in multi-window mode using the flag Intent.FLAG_ACTIVITY_LAUNCH_TO_ADJACENT. If the device is in split-screen mode, the system will try to create a new Activity next to the Activity that starts the system, so that the two activities will share the screen. If the device is in free-form mode, the user can specify the size and screen location of the new Activity by calling ActivityOptions.setLaunchBounds(). It's worth noting that if you start in the task stack Activity, which replaces Activeness on the screen and inherits all its multi-window properties. If you want to start a new activity in a separate window in multi-window mode, you must start it in a new task stack. When Activity is in the Picture-in-Picture mode, the life cycle should operate as follows:
@Override
protected void onStop() {
    super.onStop();
    //Go to Picture-in-Picture mode and call the onPause method instead of the onStop method
    //Suspension of Video Playing
    mMovieView.pause();
}

@Override
protected void onRestart() {
    super.onRestart();
    //Restore the playback of video
    mMovieView.showControls();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //Screen rotation, adaptive full screen
    adjustFullScreen(newConfig);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {//Windows Gets Focus, Adaptive Full Screen
        adjustFullScreen(getResources().getConfiguration());
    }
}
        Activity When entering or drawing in a picture, it needs to be rewritten. onPitctureInPictureModeChanged Method:
@Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode);
        if (isInPictureInPictureMode) {
            // Start accepting click response events from pictures in pictures
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent == null || !ACTION_MEDIA_CONTROL.equals(intent.getAction())) {
                        return;
                    }

                    // Callback after clicking
                    final int controlType = intent.getIntExtra(EXTRA_CONTROL_TYPE, 0);
                    switch (controlType) {
                        case CONTROL_TYPE_PLAY:
                            mMovieView.play();
                            break;
                        case CONTROL_TYPE_PAUSE:
                            mMovieView.pause();
                            break;
                    }
                }
            };
            registerReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
        } else {
            // Exit Picture-in-Picture mode, cancel broadcasting, no longer receive click response events
            unregisterReceiver(mReceiver);
            mReceiver = null;
            if (mMovieView != null && !mMovieView.isPlaying()) {
                mMovieView.showControls();
            }
        }
    }
When the video status changes, call the following method buttons and text status:
/**
 * In Picture-in-Picture mode, update playback/pause status
 *
 * @param iconId      id of Icon
 * @param title       Title
 * @param controlType Control type: playback/pause
 * @param requestCode Request Code Using Pending Intent
 */
void updatePictureInPictureActions(@DrawableRes int iconId, String title, int controlType,
        int requestCode) {
    final ArrayList<RemoteAction> actions = new ArrayList<>();

    //Pending Intent is called when the user clicks the control button
    final PendingIntent intent = PendingIntent.getBroadcast(MainActivity.this,
            requestCode, new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType), 0);
    final Icon icon = Icon.createWithResource(MainActivity.this, iconId);
    actions.add(new RemoteAction(icon, title, title, intent));

    actions.add(new RemoteAction(
            Icon.createWithResource(MainActivity.this, R.drawable.ic_info_24dp),
            getString(R.string.info), getString(R.string.info_description),
            PendingIntent.getActivity(MainActivity.this, REQUEST_INFO,
                    new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.info_uri))),
                    0)));

    mPictureInPictureArgs.setActions(actions);

    //Setting Picture-in-Picture Parameters
    setPictureInPictureArgs(mPictureInPictureArgs);
}
Video playback callbacks, including playback, pause and minimal meeting windows, each interface calls the methods described above to update the status:
/**
 * Callback of Video Play
 */
private MovieView.MovieListener mMovieListener = new MovieView.MovieListener() {

    @Override
    public void onMovieStarted() {
        // In Picture-in-Picture mode, display pause button when playing video
        updatePictureInPictureActions(R.drawable.ic_pause_24dp, mPause, CONTROL_TYPE_PAUSE,
                REQUEST_PAUSE);
    }

    @Override
    public void onMovieStopped() {
        // In Picture-in-Picture mode, when pausing the video, display the play button
        updatePictureInPictureActions(R.drawable.ic_play_arrow_24dp, mPlay, CONTROL_TYPE_PLAY,
                REQUEST_PLAY);
    }

    @Override
    public void onMovieMinimized() {
        // Enter Picture-in-Picture mode and minimize the playback window
        minimize();
    }

};
All right, here's an introduction to Android N's multi-window and picture-in-picture mode. If this article is helpful to you, please give me a compliment.

Posted by mospeed on Sat, 13 Jul 2019 14:51:32 -0700