package com.shayu.phonelive.activity; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.ImageView; import com.adjust.sdk.Adjust; import com.adjust.sdk.AdjustEvent; import com.alibaba.android.arouter.facade.annotation.Route; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.tencent.rtmp.ITXLivePlayListener; import com.tencent.rtmp.TXLiveBase; import com.tencent.rtmp.TXLiveConstants; import com.tencent.rtmp.TXLivePlayer; import com.tencent.rtmp.ui.TXCloudVideoView; import com.tencent.ugc.TXUGCBase; import com.yunbao.common.CommonAppConfig; import com.yunbao.common.bean.AdBean; import com.yunbao.common.bean.ConfigBean; import com.yunbao.common.bean.UserBean; import com.yunbao.common.custom.CircleProgress; import com.yunbao.common.glide.ImgLoader; import com.yunbao.common.http.CommonHttpConsts; import com.yunbao.common.http.CommonHttpUtil; import com.yunbao.common.interfaces.CommonCallback; import com.yunbao.common.utils.DownloadUtil; import com.yunbao.common.utils.L; import com.yunbao.common.utils.MD5Util; import com.yunbao.common.utils.RouteUtil; import com.yunbao.common.utils.SpUtil; import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.utils.WordUtil; import com.yunbao.live.views.LauncherAdViewHolder; import com.yunbao.main.activity.EntryActivity; import com.yunbao.main.activity.MainActivity; import com.yunbao.main.http.MainHttpConsts; import com.yunbao.main.http.MainHttpUtil; import java.io.File; import java.util.ArrayList; import java.util.List; import myname.pdlive.shayu.R; import static com.yunbao.common.CommonAppContext.logger; import static com.yunbao.common.CommonAppContext.mFirebaseAnalytics; /** * Created by cxf on 2018/9/17. */ @Route(path = RouteUtil.PATH_LAUNCHER) public class LauncherActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "LauncherActivity"; private static final int WHAT_GET_CONFIG = 0; private static final int WHAT_COUNT_DOWN = 1; private Handler mHandler; protected Context mContext; private ViewGroup mRoot; private ImageView mCover; private ViewGroup mContainer; private CircleProgress mCircleProgress; private List mAdList; private List mImageViewList; private int mMaxProgressVal; private int mCurProgressVal; private int mAdIndex; private int mInterval = 2000; private View mBtnSkipImage; private View mBtnSkipVideo; private TXCloudVideoView mTXCloudVideoView; private TXLivePlayer mPlayer; private LauncherAdViewHolder mLauncherAdViewHolder; private boolean mPaused; private int mVideoLastProgress; private boolean mForward; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStatusBar(); // if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { // finish(); // return; // } setContentView(R.layout.activity_launcher); //开屏 AdjustEvent adjustEvent = new AdjustEvent("vjqk8g"); Adjust.trackEvent(adjustEvent); //谷歌激活 mFirebaseAnalytics.logEvent("FS_screen", null); //FB激活 logger.logEvent("FB_screen"); mContext = this; mRoot = findViewById(R.id.root); mCover = findViewById(R.id.cover); mCircleProgress = findViewById(R.id.progress); mContainer = findViewById(R.id.container); mBtnSkipImage = findViewById(R.id.btn_skip_img); mBtnSkipVideo = findViewById(R.id.btn_skip_video); mBtnSkipImage.setOnClickListener(this); mBtnSkipVideo.setOnClickListener(this); ImgLoader.display(mContext, R.mipmap.screen, mCover); mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case WHAT_GET_CONFIG: getConfig(); break; case WHAT_COUNT_DOWN: updateCountDown(); break; } } }; mHandler.sendEmptyMessageDelayed(WHAT_GET_CONFIG, 1000); } /** * 图片倒计时 */ private void updateCountDown() { mCurProgressVal += 100; if (mCurProgressVal > mMaxProgressVal) { return; } if (mCircleProgress != null) { mCircleProgress.setCurProgress(mCurProgressVal); } int index = mCurProgressVal / mInterval; if (index < mAdList.size() && mAdIndex != index) { View v = mImageViewList.get(mAdIndex); if (v != null && v.getVisibility() == View.VISIBLE) { v.setVisibility(View.INVISIBLE); } mAdIndex = mCurProgressVal / mInterval; } if (mCurProgressVal < mMaxProgressVal) { if (mHandler != null) { mHandler.sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 100); } } else if (mCurProgressVal == mMaxProgressVal) { checkUidAndToken(); } } /** * 获取Config信息 */ private void getConfig() { CommonHttpUtil.getConfig(mContext, new CommonCallback() { @Override public void callback(ConfigBean bean) { if (bean != null) { L.e(TAG, "----------> getPaster360Url:" + bean.getBeautyKey() + " " + bean.getPaster360Url()); CommonAppConfig.getInstance().setBeautySdkType(bean.getSproutType()); //转盘开关 CommonAppConfig.getInstance().setTurnTableEnable(bean.getTurntableEnable()); CommonAppConfig.getInstance().setBeauty360TieZhiUrl(bean.getPaster360Url()); CommonAppConfig.getInstance().setAnchorPkTime(bean.getPkTime()); if (!TextUtils.isEmpty(bean.getTxVideoUgcLicenceUrl()) && !TextUtils.isEmpty(bean.getTxVideoUgcKey())) Log.i("tvv", bean.getTxVideoUgcLicenceUrl() + "是" + bean.getTxVideoUgcKey()); TXLiveBase.getInstance().setLicence(LauncherActivity.this, bean.getTxVideoUgcLicenceUrl(), bean.getTxVideoUgcKey()); TXUGCBase.getInstance().setLicence(LauncherActivity.this, bean.getTxPlayUgcLicenceUrl(), bean.getTxVideoUgcKey()); String adInfo = bean.getAdInfo(); if (!TextUtils.isEmpty(adInfo)) { JSONObject obj = JSON.parseObject(adInfo); if (obj.getIntValue("switch") == 1) { List list = JSON.parseArray(obj.getString("list"), AdBean.class); if (list != null && list.size() > 0) { mAdList = list; mInterval = obj.getIntValue("time") * 1000; if (mContainer != null) { mContainer.setOnClickListener(LauncherActivity.this); } String[] uidAndToken = SpUtil.getInstance().getMultiStringValue( new String[]{SpUtil.UID, SpUtil.TOKEN}); final String uid = uidAndToken[0]; final String token = uidAndToken[1]; if (!TextUtils.isEmpty(uid) && !TextUtils.isEmpty(token)) { playAD(obj.getIntValue("type") == 0); } else { releaseVideo(); LauncherActivity.this.startActivity(new Intent(LauncherActivity.this, EntryActivity.class)); LauncherActivity.this.finish(); } } else { checkUidAndToken(); } } else { checkUidAndToken(); } } else { checkUidAndToken(); } } } }); } /** * 检查uid和token是否存在 */ private void checkUidAndToken() { if (mForward) { return; } mForward = true; if (mHandler != null) { mHandler.removeCallbacksAndMessages(null); mHandler = null; } String[] uidAndToken = SpUtil.getInstance().getMultiStringValue( new String[]{SpUtil.UID, SpUtil.TOKEN}); final String uid = uidAndToken[0]; final String token = uidAndToken[1]; if (!TextUtils.isEmpty(uid) && !TextUtils.isEmpty(token)) { MainHttpUtil.getBaseInfo(uid, token, new CommonCallback() { @Override public void callback(UserBean bean) { if (bean != null) { CommonAppConfig.getInstance().setLoginInfo(uid, token, false); forwardMainActivity(); } } }); } else { releaseVideo(); this.startActivity(new Intent(this, EntryActivity.class)); this.finish(); } } /** * 跳转到首页 */ private void forwardMainActivity() { releaseVideo(); MainActivity.forward(mContext); // this.startActivity(new Intent(this, TestActivity.class)); finish(); } @Override protected void onDestroy() { if (mHandler != null) { mHandler.removeCallbacksAndMessages(null); mHandler = null; } MainHttpUtil.cancel(MainHttpConsts.GET_BASE_INFO); CommonHttpUtil.cancel(CommonHttpConsts.GET_CONFIG); releaseVideo(); if (mLauncherAdViewHolder != null) { mLauncherAdViewHolder.release(); } mLauncherAdViewHolder = null; super.onDestroy(); L.e(TAG, "----------> onDestroy"); } /** * 设置透明状态栏 */ private void setStatusBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(0); } } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.btn_skip_img || i == R.id.btn_skip_video) { if (mBtnSkipImage != null) { mBtnSkipImage.setClickable(false); } if (mBtnSkipVideo != null) { mBtnSkipVideo.setClickable(false); } checkUidAndToken(); } else if (i == R.id.container) { clickAD(); } } /** * 点击广告 */ private void clickAD() { if (mAdList != null && mAdList.size() > mAdIndex) { AdBean adBean = mAdList.get(mAdIndex); if (adBean != null) { String link = adBean.getLink(); if (!TextUtils.isEmpty(link)) { if (mHandler != null) { mHandler.removeCallbacksAndMessages(null); } if (mContainer != null) { mContainer.setClickable(false); } releaseVideo(); if (mLauncherAdViewHolder == null) { mLauncherAdViewHolder = new LauncherAdViewHolder(mContext, mRoot, link + "?uid=" + CommonAppConfig.getInstance().getUid() + "&token=" + CommonAppConfig.getInstance().getToken()); mLauncherAdViewHolder.addToParent(); mLauncherAdViewHolder.loadData(); mLauncherAdViewHolder.show(); mLauncherAdViewHolder.setActionListener(new LauncherAdViewHolder.ActionListener() { @Override public void onHideClick() { checkUidAndToken(); } }); } } } } } private void releaseVideo() { if (mPlayer != null) { mPlayer.stopPlay(false); mPlayer.setPlayListener(null); } mPlayer = null; } /** * 播放广告 */ private void playAD(boolean isImage) { if (mContainer == null) { return; } if (isImage) { int imgSize = mAdList.size(); if (imgSize > 0) { mImageViewList = new ArrayList<>(); for (int i = 0; i < imgSize; i++) { ImageView imageView = new ImageView(mContext); imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setBackgroundColor(0xffffffff); mImageViewList.add(imageView); ImgLoader.display(mContext, mAdList.get(i).getUrl(), imageView); } for (int i = imgSize - 1; i >= 0; i--) { mContainer.addView(mImageViewList.get(i)); } if (mBtnSkipImage != null && mBtnSkipImage.getVisibility() != View.VISIBLE) { mBtnSkipImage.setVisibility(View.VISIBLE); } mMaxProgressVal = imgSize * mInterval; if (mCircleProgress != null) { mCircleProgress.setMaxProgress(mMaxProgressVal); } if (mHandler != null) { mHandler.sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 100); } if (mCover != null && mCover.getVisibility() == View.VISIBLE) { mCover.setVisibility(View.INVISIBLE); } } else { checkUidAndToken(); } } else { if (mAdList == null || mAdList.size() == 0) { checkUidAndToken(); return; } String videoUrl = mAdList.get(0).getUrl(); if (TextUtils.isEmpty(videoUrl)) { checkUidAndToken(); return; } String videoFileName = MD5Util.getMD5(videoUrl); if (TextUtils.isEmpty(videoFileName)) { checkUidAndToken(); return; } File file = new File(getCacheDir(), videoFileName); if (file.exists()) { playAdVideo(file); } else { downloadAdFile(videoUrl, videoFileName); } } } @Override protected void onPause() { mPaused = true; if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.setMute(true); } super.onPause(); } @Override protected void onResume() { super.onResume(); if (mPaused) { if (mPlayer != null && mPlayer.isPlaying()) { mPlayer.setMute(false); } } mPaused = false; } /** * 下载视频 */ private void downloadAdFile(String url, String fileName) { DownloadUtil downloadUtil = new DownloadUtil(); downloadUtil.download("ad_video", getCacheDir(), fileName, url, new DownloadUtil.Callback() { @Override public void onSuccess(File file) { playAdVideo(file); } @Override public void onProgress(int progress) { } @Override public void onError(Throwable e) { checkUidAndToken(); } }); } /** * 播放视频 */ private void playAdVideo(File videoFile) { if (mBtnSkipVideo != null && mBtnSkipVideo.getVisibility() != View.VISIBLE) { mBtnSkipVideo.setVisibility(View.VISIBLE); } mTXCloudVideoView = new TXCloudVideoView(mContext); mTXCloudVideoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); mTXCloudVideoView.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN); mContainer.addView(mTXCloudVideoView); mPlayer = new TXLivePlayer(mContext); mPlayer.setPlayerView(mTXCloudVideoView); mPlayer.setAutoPlay(true); mPlayer.setPlayListener(new ITXLivePlayListener() { @Override public void onPlayEvent(int e, Bundle bundle) { if (e == TXLiveConstants.PLAY_EVT_PLAY_END) {//获取到视频播放完毕的回调 checkUidAndToken(); L.e(TAG, "视频播放结束------>"); } else if (e == TXLiveConstants.PLAY_EVT_CHANGE_RESOLUTION) {////获取到视频宽高回调 float videoWidth = bundle.getInt("EVT_PARAM1", 0); float videoHeight = bundle.getInt("EVT_PARAM2", 0); if (mTXCloudVideoView != null && videoWidth > 0 && videoHeight > 0) { FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mTXCloudVideoView.getLayoutParams(); int targetH = 0; if (videoWidth >= videoHeight) {//横屏 params.gravity = Gravity.CENTER_VERTICAL; targetH = (int) (mTXCloudVideoView.getWidth() / videoWidth * videoHeight); } else { targetH = ViewGroup.LayoutParams.MATCH_PARENT; } if (targetH != params.height) { params.height = targetH; mTXCloudVideoView.requestLayout(); } } } else if (e == TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME) { if (mCover != null && mCover.getVisibility() == View.VISIBLE) { mCover.setVisibility(View.INVISIBLE); } } else if (e == TXLiveConstants.PLAY_ERR_NET_DISCONNECT || e == TXLiveConstants.PLAY_ERR_FILE_NOT_FOUND) { ToastUtil.show(WordUtil.getString(R.string.live_play_error)); checkUidAndToken(); } else if (e == TXLiveConstants.PLAY_EVT_PLAY_PROGRESS) { int progress = bundle.getInt("EVT_PLAY_PROGRESS_MS"); if (mVideoLastProgress == progress) { L.e(TAG, "视频播放结束------>"); checkUidAndToken(); } else { mVideoLastProgress = progress; } } } @Override public void onNetStatus(Bundle bundle) { } }); mPlayer.startPlay(videoFile.getAbsolutePath(), TXLivePlayer.PLAY_TYPE_LOCAL_VIDEO); } }