ArticleVideoManager.java 16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
package com.people.webview.util;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentTransaction;

import com.aliyun.player.IPlayer;
import com.people.musicplayer.PlayerManagerUtils;
import com.people.musicplayer.player.PlayerManager;
import com.people.webview.R;
import com.people.webview.constant.AppNotifyEventConstant;
import com.people.webview.listener.ArticleVideoListener;
import com.people.webview.listener.ChangeScreenListener;
import com.people.webview.ui.ArticleVideoPlayerFragment;
import com.people.webview.ui.NativeWebView;
import com.wd.foundation.bean.web.ArticleVideoBean;
import com.wd.foundation.wdkit.system.DeviceUtil;
import com.wd.foundation.wdkit.utils.ScreenUtils;
import com.wd.foundation.wdkit.utils.UiUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;
import com.wd.player.listener.OnPlayStateBtnClickListener;
import com.wd.player.widget.AliyunScreenMode;

/**
 * 图文详情视频管理类
 *
 * @author baozhaoxin
 * @version [V1.0.0, 2023/6/12]
 * @since V1.0.0
 */
public class ArticleVideoManager {

    /**
     * tag
     */
    private final String TAG = "ArticleVideoManager";

    /**
     * 上下文
     */
    private final Context context;

    /**
     * 上下文
     */
    private final AppCompatActivity activity;

    /**
     *视频需要悬浮窗口的容器
     */
    private final FrameLayout videoWindow;


    /**
     * 父view 里面嵌套了WebView
     */
    private final FrameLayout parentView;

    /**
     *视频播放器容器
     */
    private final FrameLayout mVideoContainer;

    /**
     * WebView
     */
    private final NativeWebView webView;

    /**
     * 图文的视频位置数据
     */
    public ArticleVideoBean articleVideoBean;

    /**
     * 是否是首次
     */
    private boolean isFrist;

    /**
     * 是否允许悬浮播放
     */
    private boolean isAllowWindow = false;

    /**
     * 视频切屏监听
     */
    private ChangeScreenListener changeScreenListener;

    /**
     * 滑动距离
     */
    private int scrollY;

    /**
     * 是否是全屏
     */
    private boolean isFull;

    public ArticleVideoManager(AppCompatActivity context, FrameLayout videoWindow, FrameLayout parentView, NativeWebView webView) {
        this.context = context;
        activity = context;
        this.videoWindow = videoWindow;
        this.videoWindow.setFocusable(true);
        this.videoWindow.setClickable(true);
        this.mVideoContainer = new FrameLayout(context);
        this.mVideoContainer.setFocusable(true);
        this.mVideoContainer.setClickable(true);
        this.parentView = parentView;
        this.webView = webView;
        this.isFrist = true;
    }

    public void onClickWebVideo(ArticleVideoBean videoBean){
//        initVideoPlayer();
        if(parentView != null){
            if(videoBean != null){
                if (isShowWindow()) {
                    videoWindow.setVisibility(View.GONE);
                }
                mVideoContainer.setVisibility(View.VISIBLE);
                articleVideoBean = videoBean;
                isFrist = false;
                //允许小窗播放
                isAllowWindow = true;
                //配置视频的宽高和坐标
                configCoordinate();
                addVideoLayout(videoBean);
            }
        }
    }

    private void configWindowLayout(){
        if(videoWindow == null){
            return;
        }
        if(articleVideoBean == null){
            return;
        }
        videoWindow.setBackgroundColor(context.getResources().getColor(R.color.res_color_common_C12));
        String landscape = articleVideoBean.getVideoLandscape();
        try {
            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) videoWindow.getLayoutParams();
            int width;
            int height;
            int top = UiUtils.dp2px(context,55);
            int left;
            if(StringUtils.isEqual("1",landscape)){
                //横屏
                width = UiUtils.dp2px(context,208);
                height = UiUtils.dp2px(context,117);
                left = ScreenUtils.getScreenWidth() * 158 / 375;
            }else {
                //竖屏
                width = UiUtils.dp2px(context,117);
                height = UiUtils.dp2px(context,208);
                left = ScreenUtils.getScreenWidth() * 250 / 375;
            }
            /*layoutParams.leftMargin = left;
            layoutParams.topMargin = top;*/
            layoutParams.width = width;
            layoutParams.height = height;
            videoWindow.setLayoutParams(layoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 配置视频坐标信息
     */
    private void configCoordinate() {
        if(articleVideoBean == null){
            return;
        }
        FrameLayout.LayoutParams layoutParams = null;
        try {
            int width = Integer.parseInt(articleVideoBean.getPositionWidth()) + 4;
            int height = Integer.parseInt(articleVideoBean.getPositionHeight()) + 4;
            int positionLeft = Integer.parseInt(articleVideoBean.getPositionLeft());
            int positionTop = Integer.parseInt(articleVideoBean.getPositionTop());
            layoutParams = new FrameLayout.LayoutParams(width, height);
            layoutParams.leftMargin = positionLeft;
        /*if (scrollY >= 0 && scrollY <= statusBarHeight) {
            top = (bean.getTop() - scrollY);
        } else {
            top = (bean.getTop() - statusBarHeight);
        }*/
//        Logger.t(TAG).d(TAG, "configCoordinate: 计算过后的 top --> " + top + " scrollY:" + scrollY + " statusBarHeight:" + statusBarHeight);
            layoutParams.topMargin = positionTop;
        } catch (Exception e) {
            e.printStackTrace();
        }
        mVideoContainer.removeAllViews();
        parentView.removeView(mVideoContainer);
        parentView.addView(mVideoContainer, layoutParams);
        configWindowLayout();
    }

    private ArticleVideoPlayerFragment articleVideoPlayerFragment;
    private void addVideoLayout(ArticleVideoBean articleVideoBean){
        if(articleVideoPlayerFragment != null){
            articleVideoPlayerFragment.onDestroy();
        }
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        articleVideoPlayerFragment = ArticleVideoPlayerFragment.newInstance(articleVideoBean);
        articleVideoPlayerFragment.setScreenModeChangeListener(new MyScreenModeChangeListener());
        articleVideoPlayerFragment.setArticleVideoListener(new ArticleVideoListener() {
            @Override
            public void onCloseVideo() {
                closeVideoWindow();
            }
        });
        mVideoContainer.setId(R.id.article_video_layout);
        transaction.add(R.id.article_video_layout, articleVideoPlayerFragment);
        transaction.commit();
        articleVideoPlayerFragment.setPlayStateBtnClickListener(new OnPlayStateBtnClickListener() {
            @Override
            public void onPlayBtnClick(int mPlayerState) {
                if(mPlayerState == IPlayer.paused || mPlayerState == IPlayer.prepared ||
                        mPlayerState == IPlayer.stopped){
                    //开始播放视频
                    if (PlayerManager.getInstance().isPlaying()) {
                        PlayerManagerUtils.updateInformation(
                                PlayerManagerUtils.BUTTON_CLOSE);
                    }
                    if(webView == null){
                        return;
                    }
                    //App开始播放音、视频(处理音视频互斥问题)
                    JSBridgeUtils.jsCall_appNotifyEvent(webView, AppNotifyEventConstant.EVENT_FIVE);
                }
            }
        });
    }

    /**
     * 判断窗体是否显示
     *
     * @return boolean
     */
    public boolean isShowWindow() {
        return videoWindow != null && videoWindow.getVisibility() == View.VISIBLE;
    }

    public boolean isAllowWindow() {
        return isAllowWindow;
    }

    public FrameLayout getVideoContainer(){
        return mVideoContainer;
    }

    public void setChangeScreenListener(ChangeScreenListener changeScreenListener) {
        this.changeScreenListener = changeScreenListener;
    }

    /**
     * 横竖屏操作回调
     */
    private class MyScreenModeChangeListener implements ChangeScreenListener {
        FrameLayout.LayoutParams mediaAreaLayoutParams = (FrameLayout.LayoutParams) mVideoContainer.getLayoutParams();
        WindowManager.LayoutParams lp =  activity.getWindow().getAttributes();
        Window mWindow =  activity.getWindow();

        int width = Integer.parseInt(articleVideoBean.getPositionWidth()) + 4;
        int height = Integer.parseInt(articleVideoBean.getPositionHeight()) + 4;
        int positionLeft = Integer.parseInt(articleVideoBean.getPositionLeft());
        int positionTop = Integer.parseInt(articleVideoBean.getPositionTop());

        @Override
        public void onChangeToScreenMode(AliyunScreenMode currentMode) {// 切换后的模式
            if (currentMode == AliyunScreenMode.Full) {
                // 小屏变全屏
                mediaAreaLayoutParams.topMargin = 0;
                mediaAreaLayoutParams.leftMargin = 0;
                mediaAreaLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                mediaAreaLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                isFull = true;
            } else if(currentMode == AliyunScreenMode.Small){
                // 全屏变小屏
                //设置上下左右边距
                mediaAreaLayoutParams.topMargin = positionTop;
                mediaAreaLayoutParams.leftMargin = positionLeft;
                mediaAreaLayoutParams.width = width;
                mediaAreaLayoutParams.height = height;
                isFull = false;
            }else if(currentMode == AliyunScreenMode.AMPLIFY){
                // 缩小屏变放大屏
                isFull = true;
                mediaAreaLayoutParams.topMargin = 0;
                mediaAreaLayoutParams.leftMargin = 0;
                mediaAreaLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                mediaAreaLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;

                // 隐藏状态栏
                if (!DeviceUtil.isStrangePhone()) {
                    //获得 WindowManager.LayoutParams 属性对象
                    //直接对它flags变量操作   LayoutParams.FLAG_FULLSCREEN 表示设置全屏
                    lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    //设置属性
                    mWindow.setAttributes(lp);
                    //意思大致就是  允许窗口扩展到屏幕之外
                    mWindow.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                }
            }else {
                // 放大屏变缩小屏
                isFull = false;
                //设置上下左右边距
                mediaAreaLayoutParams.topMargin = positionTop;
                mediaAreaLayoutParams.leftMargin = positionLeft;
                mediaAreaLayoutParams.width = width;
                mediaAreaLayoutParams.height = height;

                //LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
                lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
                //设置属性
                mWindow.setAttributes(lp);
                //不允许窗口扩展到屏幕之外  clear掉了
                mWindow.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            }

            mVideoContainer.setLayoutParams(mediaAreaLayoutParams);
            if (changeScreenListener!=null) {
                changeScreenListener.onChangeToScreenMode(currentMode);
            }
        }
    }

    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if (scrollY >= 0 && articleVideoBean != null && mVideoContainer.getVisibility() == View.VISIBLE) {
            this.scrollY = scrollY;
            /*FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVideoContainer.getLayoutParams();
            if ((coordinateBean.getTop() - top) == statusBarHeight) {
                layoutParams.topMargin = top + statusBarHeight - scrollY;
            } else if ((coordinateBean.getTop() - top) == 0) {
                layoutParams.topMargin = top - scrollY;
            } else {
                layoutParams.topMargin = top + statusBarHeight - scrollY;
            }
            mVideoContainer.setLayoutParams(layoutParams);*/
        }
    }

    /**
     * h5页面滑动处理
     * @param scrollY
     * @param oldScrollY
     */
    public void webVideoScroll(int scrollY, int oldScrollY) {
        if (articleVideoBean == null){
            return;
        }
        if(isFull){
            return;
        }
        boolean isUp = (scrollY - oldScrollY) < 0;
        int top = Integer.parseInt(articleVideoBean.getPositionTop());
        int height = Integer.parseInt(articleVideoBean.getPositionHeight());
        int ScreenHeight = DeviceUtil.getScreenHeight();
        int contentHeight = ScreenHeight - UiUtils.dp2px(111);
        int webHeight = articleVideoBean.getWebViewHeight() ;
        if (scrollY >= (top + height)) {
            //小窗播放
            scrollWindow();
        }else if (scrollY + contentHeight <= top) {
            //小窗播放
            scrollWindow();
        }else {
            resumePlayer();
        }
    }

    private void scrollWindow() {
        if (isAllowWindow() && !isShowWindow()) {
            attachSuspensionWindow();
        }
    }

    private void resumePlayer() {
        if (isShowWindow()) {
            attachResumePosition();
        }
    }

    /**
     * 悬浮小窗播放视频
     */
    public void attachSuspensionWindow() {
        if(videoWindow == null){
            return;
        }
        if(articleVideoPlayerFragment == null || !articleVideoPlayerFragment.isPlaying()){
            return;
        }
        if (isAllowWindow) {
            videoWindow.setVisibility(View.VISIBLE);
            articleVideoPlayerFragment.setWindowMode();
            mVideoContainer.setVisibility(View.GONE);
            View vv = articleVideoPlayerFragment.getView();
            ViewGroup parent = (ViewGroup)vv.getParent();
            parent.removeView(vv);
            videoWindow.addView(vv);
        }
    }

    /**
     * 从悬浮小窗回复到原来到位置
     */
    public void attachResumePosition() {
        if (isShowWindow()) {
            videoWindow.setVisibility(View.GONE);
            if (mVideoContainer.getVisibility() == View.GONE) {
                mVideoContainer.setVisibility(View.VISIBLE);
            }

            if(articleVideoPlayerFragment != null){
                articleVideoPlayerFragment.setResumePositionMode();
                View vv = articleVideoPlayerFragment.getView();
                ViewGroup parent = (ViewGroup)vv.getParent();
                parent.removeView(vv);
                mVideoContainer.addView(vv);
            }
        }
    }

    /**
     * 销毁视频窗口
     */
    public void closeVideoWindow() {
        if(articleVideoPlayerFragment != null){
            articleVideoPlayerFragment.onDestroy();
            articleVideoPlayerFragment = null;
        }
        videoWindow.setVisibility(View.GONE);
        isAllowWindow = false;
        mVideoContainer.setVisibility(View.GONE);
    }

    /**
     * 设置视频暂停
     */
    public void setVideoPause(){
        if(articleVideoPlayerFragment != null){
            articleVideoPlayerFragment.onPause();
        }
    }

    /**
     * 视频返回
     */
    public void setVideoBack(){
        if(articleVideoPlayerFragment != null){
            articleVideoPlayerFragment.onBack();
        }
    }
}