feat: 【V5.16.0】启动广告图—客户端 https://jira.shanqu.cc/browse/GHZS-16

This commit is contained in:
张玉久
2022-11-08 15:03:10 +08:00
parent 9ee743c14f
commit 0e16aa7dd4
16 changed files with 327 additions and 87 deletions

View File

@ -1,7 +1,7 @@
package com.gh.gamecenter;
import static com.gh.gamecenter.common.constant.Constants.LOGIN_TAG;
import static com.gh.gamecenter.common.constant.Constants.LOGOUT_TAG;
import static com.gh.gamecenter.login.utils.LoginHelper.WEIBO_SCOPE;
import static com.gh.gamecenter.common.constant.EntranceConsts.ENTRANCE_BROWSER;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_LAUNCH_SIMULATOR_GAME;
import static com.gh.gamecenter.common.constant.EntranceConsts.HOST_QQ;
@ -14,7 +14,7 @@ import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_TO;
import static com.gh.gamecenter.common.constant.EntranceConsts.KEY_TYPE;
import static com.gh.gamecenter.common.utils.ExtensionsKt.observableToMain;
import static com.gh.gamecenter.fragment.MainWrapperFragment.INDEX_PERSONAL;
import static com.gh.gamecenter.common.constant.Constants.LOGIN_TAG;
import static com.gh.gamecenter.login.utils.LoginHelper.WEIBO_SCOPE;
import android.annotation.SuppressLint;
import android.app.Activity;
@ -26,6 +26,7 @@ import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.Html;
@ -40,8 +41,13 @@ import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.common.DefaultUrlHandler;
import com.gh.common.constant.Config;
import com.gh.common.exposure.ExposureEvent;
import com.gh.common.exposure.ExposureManager;
import com.gh.common.exposure.ExposureSource;
import com.gh.common.exposure.ExposureType;
import com.gh.common.history.HistoryDatabase;
import com.gh.common.history.HistoryHelper;
import com.gh.common.repository.ReservationRepository;
@ -58,8 +64,7 @@ import com.gh.common.util.ErrorHelper;
import com.gh.common.util.HomePluggableHelper;
import com.gh.common.util.LogUtils;
import com.gh.common.util.LunchType;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.common.utils.NotificationHelper;
import com.gh.common.util.NewFlatLogUtils;
import com.gh.common.util.PackageInstaller;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
@ -72,12 +77,17 @@ import com.gh.gamecenter.common.base.fragment.BaseFragment_ViewPager;
import com.gh.gamecenter.common.base.fragment.ToolbarFragment;
import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.entity.LinkEntity;
import com.gh.gamecenter.common.entity.NotificationUgc;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.common.exposure.meta.MetaUtil;
import com.gh.gamecenter.common.retrofit.BiResponse;
import com.gh.gamecenter.common.retrofit.Response;
import com.gh.gamecenter.common.utils.DeviceUtils;
import com.gh.gamecenter.common.utils.DialogHelper;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.common.utils.NotificationHelper;
import com.gh.gamecenter.common.utils.ShareUtils;
import com.gh.gamecenter.core.AppExecutor;
import com.gh.gamecenter.core.utils.ClassUtils;
@ -86,21 +96,21 @@ import com.gh.gamecenter.core.utils.GsonUtils;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.core.utils.SPUtils;
import com.gh.gamecenter.core.utils.SentryHelper;
import com.gh.gamecenter.core.utils.TimeUtils;
import com.gh.gamecenter.core.utils.ToastUtils;
import com.gh.gamecenter.core.utils.UrlFilterUtils;
import com.gh.gamecenter.download.DownloadFragment;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.InnerMetaInfoEntity;
import com.gh.gamecenter.common.entity.NotificationUgc;
import com.gh.gamecenter.entity.StartupAdEntity;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.fragment.MainWrapperFragment;
import com.gh.gamecenter.home.skip.PackageSkipActivity;
import com.gh.gamecenter.login.user.UserManager;
import com.gh.gamecenter.login.utils.QuickLoginHelper;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.UpdateManager;
import com.gh.gamecenter.login.user.UserManager;
import com.gh.gamecenter.packagehelper.PackageViewModel;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.room.AppDatabase;
@ -136,6 +146,7 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
@ -159,6 +170,9 @@ public class MainActivity extends BaseActivity {
public final static String SWITCH_TO_COMMUNITY = "switch_to_community";
public final static String SWITCH_TO_VIDEO = "switch_to_video";
public final static String SHOW_AD = "show_ad";
private final static int COUNTDOWN_AD = 100;
private final static int countdownMaxCount = 3;
private int countdownCount = 0;
private final static String CURRENT_PAGE = "current_page";
@ -475,37 +489,75 @@ public class MainActivity extends BaseActivity {
}
private void observeAd() {
AdHelper.startupAd.observe(this, startupAdEntity -> {
if (showAd) {
if (startupAdEntity == null) {
hideAd(true);
} else {
showAd(startupAdEntity);
AppExecutor.getUiExecutor().execute(() -> {
AppExecutor.getUiExecutor().executeWithDelay(() -> hideAd(true), 2000);
});
}
final StartupAdEntity startUpAd = AdHelper.getStartUpAd();
if (showAd) {
if (startUpAd == null) {
hideAd();
return;
}
});
AppExecutor.getUiExecutor().executeWithDelay(() -> hideAd(false), 950);
if (!startUpAd.getImg().isEmpty()) {
final String showedTodayTimestamp = SPUtils.getString(Constants.SP_STARTUP_AD_TIMESTAMP, "");
final String rule = startUpAd.getRule();
switch (rule) {
case "each":
showAd(startUpAd);
break;
case "once":
if (TextUtils.isEmpty(showedTodayTimestamp)) {
showAd(startUpAd);
}
break;
case "everyday":
final String today = TimeUtils.getToday();
if (!TextUtils.equals(today, showedTodayTimestamp)) {
showAd(startUpAd);
}
break;
default:
// Do nothing
break;
}
SPUtils.setString(Constants.SP_STARTUP_AD_TIMESTAMP, TimeUtils.getToday());
} else {
showAd(startUpAd);
AppExecutor.getUiExecutor().executeWithDelay(() -> hideAd(), 2000);
}
}
}
private void hideAd(boolean forceToHide) {
if (forceToHide || AdHelper.startupAd.getValue() == null) {
showAd = false;
getIntent().putExtra(SHOW_AD, false);
View view = findViewById(R.id.maskContainer);
if (view != null) {
view.setVisibility(View.GONE);
ExtensionsKt.removeFromParent(view);
@Override
protected void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == COUNTDOWN_AD) {
countdownCount++;
TextView jumpBtn = findViewById(R.id.jumpBtn);
jumpBtn.setText(String.format(Locale.CHINA, "跳过 %d", countdownMaxCount - countdownCount));
if (countdownMaxCount <= countdownCount) {
hideAd();
} else {
mBaseHandler.sendEmptyMessageDelayed(COUNTDOWN_AD, 1000);
}
checkDialog();
}
}
private void hideAd() {
showAd = false;
getIntent().putExtra(SHOW_AD, false);
View maskContainer = findViewById(R.id.maskContainer);
View startAdContainer = findViewById(R.id.startAdContainer);
if (maskContainer != null) {
maskContainer.setVisibility(View.GONE);
ExtensionsKt.removeFromParent(maskContainer);
}
if (startAdContainer != null) {
startAdContainer.setVisibility(View.GONE);
ExtensionsKt.removeFromParent(startAdContainer);
}
checkDialog();
}
private void checkDialog() {
// 检查通知权限
checkNotificationPermission();
@ -518,26 +570,65 @@ public class MainActivity extends BaseActivity {
}
private void showAd(StartupAdEntity ad) {
TextView adContentTv = findViewById(R.id.adContentTv);
View containerView = findViewById(R.id.maskContainer);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
containerView.setElevation(500F);
}
containerView.setOnClickListener(v -> {
// do nothing 只是为了点击拦截事件,避免传递到下面的页面
});
adContentTv.setText(ad.getDesc());
adContentTv.setVisibility(View.VISIBLE);
if (ad.getButton()) {
View btn = findViewById(R.id.adBtn);
btn.setOnClickListener((v) -> {
DirectUtils.directToLinkPage(this, ad.getJump(), "(启动广告)", "");
LogUtils.logStartAd("click_watch_start_ads", ad);
if (!ad.getImg().isEmpty()) {
View startAdContainer = findViewById(R.id.startAdContainer);
View jumpBtn = findViewById(R.id.jumpBtn);
TextView jumpDetailBtn = findViewById(R.id.jumpDetailBtn);
SimpleDraweeView adImage = findViewById(R.id.adImage);
startAdContainer.setVisibility(View.VISIBLE);
jumpDetailBtn.setText(ad.getDesc());
ImageUtils.display(adImage, ad.getImg());
startAdContainer.setOnClickListener(v -> {
// do nothing 只是为了点击拦截事件,避免传递到下面的页面
});
btn.setVisibility(View.VISIBLE);
LogUtils.logStartAd("watch_start_ads", ad);
jumpBtn.setOnClickListener(v -> {
mBaseHandler.removeMessages(COUNTDOWN_AD);
hideAd();
LinkEntity linkEntity = ad.getJump();
NewFlatLogUtils.logOpenScreenAdSkip(
ad.getId(),
linkEntity.getText() != null ? linkEntity.getText() : "",
linkEntity.getType() != null ? linkEntity.getType() : "",
linkEntity.getLink() != null ? linkEntity.getLink() : ""
);
});
List<ExposureSource> sources = new ArrayList<>();
sources.add(new ExposureSource("开屏广告", ad.getId()));
final ExposureEvent event = ExposureEvent.createEvent(null, sources, null, ExposureType.EXPOSURE);
ExposureManager.INSTANCE.log(event);
if (ad.getButton()) {
jumpDetailBtn.setOnClickListener(v -> {
DirectUtils.directToLinkPage(this, ad.getJump(), "(启动广告)", "", event);
});
jumpDetailBtn.setVisibility(View.VISIBLE);
LogUtils.logStartAd("watch_start_ads", ad);
} else {
LogUtils.logStartAd("start_ads", ad);
}
mBaseHandler.sendEmptyMessageDelayed(COUNTDOWN_AD, 1000);
} else {
LogUtils.logStartAd("start_ads", ad);
TextView adContentTv = findViewById(R.id.adContentTv);
View containerView = findViewById(R.id.maskContainer);
containerView.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
containerView.setElevation(500F);
}
containerView.setOnClickListener(v -> {
// do nothing 只是为了点击拦截事件,避免传递到下面的页面
});
adContentTv.setText(ad.getDesc());
adContentTv.setVisibility(View.VISIBLE);
if (ad.getButton()) {
View adBtn = findViewById(R.id.adBtn);
adBtn.setOnClickListener(v -> {
DirectUtils.directToLinkPage(this, ad.getJump(), "(启动广告)", "");
LogUtils.logStartAd("click_watch_start_ads", ad);
});
adBtn.setVisibility(View.VISIBLE);
LogUtils.logStartAd("watch_start_ads", ad);
} else {
LogUtils.logStartAd("start_ads", ad);
}
}
}