feat: 新增广告位管理功能 https://jira.shanqu.cc/browse/GHZS-2919
This commit is contained in:
@ -21,7 +21,6 @@ import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
@ -46,7 +45,6 @@ import androidx.lifecycle.ViewModelProviders;
|
||||
|
||||
import com.facebook.drawee.view.SimpleDraweeView;
|
||||
import com.gh.ad.AdDelegateHelper;
|
||||
import com.gh.common.DefaultUrlHandler;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.exposure.ExposureManager;
|
||||
import com.gh.common.filter.RegionSettingHelper;
|
||||
@ -92,7 +90,6 @@ 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.provider.IAdProvider;
|
||||
import com.gh.gamecenter.core.utils.ClassUtils;
|
||||
import com.gh.gamecenter.core.utils.DisplayUtils;
|
||||
import com.gh.gamecenter.core.utils.GsonUtils;
|
||||
@ -102,8 +99,6 @@ 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.InnerMetaInfoEntity;
|
||||
import com.gh.gamecenter.entity.StartupAdEntity;
|
||||
import com.gh.gamecenter.eventbus.EBSkip;
|
||||
import com.gh.gamecenter.feature.entity.GameEntity;
|
||||
@ -141,22 +136,16 @@ import org.greenrobot.eventbus.ThreadMode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
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;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import io.reactivex.SingleSource;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
@ -174,7 +163,7 @@ public class MainActivity extends BaseActivity {
|
||||
public static final String SWITCH_TO_COMMUNITY = "switch_to_community";
|
||||
public static final String SWITCH_TO_VIDEO = "switch_to_video";
|
||||
public static final String SHOW_AD = "show_ad";
|
||||
private static final int COUNTDOWN_AD = 100;
|
||||
public static final int COUNTDOWN_AD = 100;
|
||||
private static final int COUNTDOWN_MAX_COUNT = 3;
|
||||
private int countdownCount = 0;
|
||||
|
||||
@ -190,11 +179,11 @@ public class MainActivity extends BaseActivity {
|
||||
public static boolean isNewFirstLaunch;
|
||||
|
||||
private final Handler handler = new Handler();
|
||||
public boolean showAd = false; // 是否显示广告
|
||||
private boolean mShouldShowAd = false; // 是否显示广告
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
showAd = getIntent().getBooleanExtra(SHOW_AD, false) && savedInstanceState == null;
|
||||
mShouldShowAd = getIntent().getBooleanExtra(SHOW_AD, false) && savedInstanceState == null;
|
||||
|
||||
HaloApp.getInstance().isAlreadyUpAndRunning = true;
|
||||
|
||||
@ -228,7 +217,6 @@ public class MainActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
}, 2000L);
|
||||
getPluginUpdate();
|
||||
sp.edit().putBoolean("isNewFirstLaunchV" + PackageUtils.getGhVersionName(), false).apply();
|
||||
|
||||
// 记录曾安装过的版本 + 渠道
|
||||
@ -282,8 +270,6 @@ public class MainActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
|
||||
// checkTinkerPath(); // 看情况是否需要显示补丁弹窗
|
||||
|
||||
// 必须放在这里,否则会导致获取 baseActivity 不是本应用包名
|
||||
DownloadManager.getInstance().initDownloadService();
|
||||
|
||||
@ -300,17 +286,16 @@ public class MainActivity extends BaseActivity {
|
||||
doSkip();
|
||||
}
|
||||
|
||||
// debug 模式下的快速跳转页面
|
||||
if (BuildConfig.DEBUG) {
|
||||
handler.postDelayed(() -> {
|
||||
EntranceUtils.jumpShortcut(this);
|
||||
}, 500);
|
||||
handler.postDelayed(() -> EntranceUtils.jumpShortcut(this), 500);
|
||||
}
|
||||
|
||||
if (showAd) {
|
||||
observeStartUp();
|
||||
if (mShouldShowAd) {
|
||||
showAd();
|
||||
} else {
|
||||
hideStartUp();
|
||||
hideStartUpAd();
|
||||
hideTextAd();
|
||||
hideSplashAd();
|
||||
}
|
||||
|
||||
// 默认配置为空时重试
|
||||
@ -473,7 +458,7 @@ public class MainActivity extends BaseActivity {
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
AdDelegateHelper.INSTANCE.cancelStartUpAd(this);
|
||||
AdDelegateHelper.INSTANCE.cancelSplashAd(this);
|
||||
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
releaseExoSourceCache();
|
||||
@ -497,86 +482,56 @@ public class MainActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void observeStartUp() {
|
||||
if (!showAd) {
|
||||
hideStartUp();
|
||||
hideStartUpAd();
|
||||
/**
|
||||
* 显示广告 (包括文本广告和开屏广告)
|
||||
*/
|
||||
private void showAd() {
|
||||
if (!mShouldShowAd) {
|
||||
hideTextAd();
|
||||
hideSplashAd();
|
||||
return;
|
||||
}
|
||||
final StartupAdEntity startUp = AdHelper.getStartUp();
|
||||
if (startUp != null) {
|
||||
showStartUp(startUp);
|
||||
final StartupAdEntity textAd = AdHelper.getStartUp();
|
||||
if (textAd != null) {
|
||||
showTextAd(textAd);
|
||||
AppExecutor.getUiExecutor().executeWithDelay(() -> {
|
||||
hideStartUp();
|
||||
initStartUpAd();
|
||||
hideTextAd();
|
||||
showSplashAd();
|
||||
}, 2000);
|
||||
} else {
|
||||
initStartUpAd();
|
||||
showSplashAd();
|
||||
}
|
||||
}
|
||||
|
||||
private void initStartUpAd() {
|
||||
/**
|
||||
* 显示开屏广告
|
||||
*/
|
||||
private void showSplashAd() {
|
||||
if (AdDelegateHelper.INSTANCE.shouldShowStartUpAd()) {
|
||||
initSDKStartUpAd();
|
||||
} else {
|
||||
observeStartUpAd();
|
||||
}
|
||||
}
|
||||
ViewGroup startAdContainer = findViewById(R.id.startAdContainer);
|
||||
ViewGroup sdkStartAdContainer = findViewById(R.id.sdkStartAdContainer);
|
||||
FrameLayout adsFl = findViewById(R.id.adsFl);
|
||||
|
||||
private void observeStartUpAd() {
|
||||
final StartupAdEntity startUpAd = AdHelper.getStartUpAd();
|
||||
int screenWidthInPx = DisplayUtils.getScreenWidth(this);
|
||||
int screenHeightInPx = DisplayUtils.getScreenHeight(this) + DisplayUtils.getStatusBarHeight(this.getResources());
|
||||
float screenWidthInDp = DisplayUtils.px2dip(this, screenWidthInPx);
|
||||
float screenHeightInDp = DisplayUtils.px2dip(this, screenHeightInPx);
|
||||
|
||||
if (startUpAd != null && !TextUtils.isEmpty(startUpAd.getImg())) {
|
||||
// 根据接口返回的广告时间,判断该图片广告是否还有必要显示
|
||||
boolean isAdValid = false;
|
||||
if (startUpAd.getTime() != null) {
|
||||
long currentTimeInSecond = System.currentTimeMillis() / 1000;
|
||||
if (currentTimeInSecond > startUpAd.getTime().getStart()
|
||||
&& currentTimeInSecond < startUpAd.getTime().getEnd()) {
|
||||
isAdValid = true;
|
||||
}
|
||||
} else {
|
||||
// 接口没有返回开始和结束时间时,默认有效
|
||||
isAdValid = true;
|
||||
}
|
||||
|
||||
// 图片广告无效,跳过
|
||||
if (!isAdValid) {
|
||||
hideStartUpAd();
|
||||
return;
|
||||
}
|
||||
|
||||
final String showedTodayTimestamp = SPUtils.getString(Constants.SP_STARTUP_AD_TIMESTAMP, "");
|
||||
final String rule = startUpAd.getRule();
|
||||
switch (rule) {
|
||||
case "each":
|
||||
showStartUpAd(startUpAd);
|
||||
break;
|
||||
case "once":
|
||||
if (TextUtils.isEmpty(showedTodayTimestamp)
|
||||
|| !showedTodayTimestamp.contains(startUpAd.getId())) {
|
||||
showStartUpAd(startUpAd);
|
||||
} else {
|
||||
hideStartUpAd();
|
||||
AdDelegateHelper.requestSplashAd(
|
||||
this,
|
||||
screenWidthInPx,
|
||||
screenHeightInPx,
|
||||
screenWidthInDp,
|
||||
screenHeightInDp,
|
||||
startAdContainer,
|
||||
sdkStartAdContainer,
|
||||
adsFl,
|
||||
(BaseHandler) mBaseHandler,
|
||||
() -> {
|
||||
hideSplashAd();
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case "everyday":
|
||||
final String today = TimeUtils.getToday();
|
||||
if (TextUtils.isEmpty(showedTodayTimestamp)
|
||||
|| !showedTodayTimestamp.contains(today)
|
||||
|| !showedTodayTimestamp.contains(startUpAd.getId())) {
|
||||
showStartUpAd(startUpAd);
|
||||
} else {
|
||||
hideStartUpAd();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
hideStartUpAd();
|
||||
break;
|
||||
}
|
||||
SPUtils.setString(Constants.SP_STARTUP_AD_TIMESTAMP, startUpAd.getId() + TimeUtils.getToday());
|
||||
} else {
|
||||
hideStartUpAd();
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -588,14 +543,17 @@ public class MainActivity extends BaseActivity {
|
||||
TextView jumpBtn = findViewById(R.id.jumpBtn);
|
||||
jumpBtn.setText(String.format(Locale.CHINA, "跳过 %d", COUNTDOWN_MAX_COUNT - countdownCount));
|
||||
if (COUNTDOWN_MAX_COUNT < countdownCount) {
|
||||
hideStartUpAd();
|
||||
hideSplashAd();
|
||||
} else {
|
||||
mBaseHandler.sendEmptyMessageDelayed(COUNTDOWN_AD, 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void hideStartUp() {
|
||||
/**
|
||||
* 隐藏开屏文案
|
||||
*/
|
||||
private void hideTextAd() {
|
||||
View maskContainer = findViewById(R.id.maskContainer);
|
||||
if (maskContainer != null) {
|
||||
maskContainer.setVisibility(View.GONE);
|
||||
@ -603,8 +561,11 @@ public class MainActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void hideStartUpAd() {
|
||||
showAd = false;
|
||||
/**
|
||||
* 隐藏开屏广告
|
||||
*/
|
||||
private void hideSplashAd() {
|
||||
mShouldShowAd = false;
|
||||
getIntent().putExtra(SHOW_AD, false);
|
||||
View startAdContainer = findViewById(R.id.startAdContainer);
|
||||
if (startAdContainer != null) {
|
||||
@ -615,7 +576,7 @@ public class MainActivity extends BaseActivity {
|
||||
if (startSdkAdContainer != null) {
|
||||
startSdkAdContainer.setVisibility(View.GONE);
|
||||
ExtensionsKt.removeFromParent(startSdkAdContainer);
|
||||
AdDelegateHelper.INSTANCE.cancelStartUpAd(this);
|
||||
AdDelegateHelper.INSTANCE.cancelSplashAd(this);
|
||||
}
|
||||
checkDialog();
|
||||
}
|
||||
@ -631,74 +592,10 @@ public class MainActivity extends BaseActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private void showStartUpAd(StartupAdEntity ad) {
|
||||
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());
|
||||
ExtensionsKt.setDrawableEnd(jumpDetailBtn, AppCompatResources.getDrawable(this, R.drawable.ic_startup_ad_arrow), null, null);
|
||||
ImageUtils.display(adImage, ad.getImg());
|
||||
startAdContainer.setOnClickListener(v -> {
|
||||
// do nothing 只是为了点击拦截事件,避免传递到下面的页面
|
||||
});
|
||||
jumpBtn.setOnClickListener(v -> {
|
||||
mBaseHandler.removeMessages(COUNTDOWN_AD);
|
||||
hideStartUpAd();
|
||||
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);
|
||||
v.postDelayed(() -> {
|
||||
mBaseHandler.removeMessages(COUNTDOWN_AD);
|
||||
hideStartUpAd();
|
||||
}, 1000);
|
||||
});
|
||||
jumpDetailBtn.setVisibility(View.VISIBLE);
|
||||
LogUtils.logStartAd("watch_start_ads", ad);
|
||||
} else {
|
||||
LogUtils.logStartAd("start_ads", ad);
|
||||
}
|
||||
mBaseHandler.sendEmptyMessageDelayed(COUNTDOWN_AD, 1000);
|
||||
}
|
||||
|
||||
private void initSDKStartUpAd() {
|
||||
ViewGroup startAdContainer = findViewById(R.id.sdkStartAdContainer);
|
||||
startAdContainer.setVisibility(View.VISIBLE);
|
||||
FrameLayout adsFl = findViewById(R.id.adsFl);
|
||||
|
||||
float screenWidthInDp = DisplayUtils.getScreenWidth(this);
|
||||
float screenHeightInDp = DisplayUtils.getScreenHeight(this);
|
||||
int screenWidthInPx = DisplayUtils.px2dip(this, screenWidthInDp);
|
||||
int screenHeightInPx = DisplayUtils.px2dip(this, screenHeightInDp);
|
||||
|
||||
AdDelegateHelper.requestStartUpAd(
|
||||
this,
|
||||
"888381490",
|
||||
screenWidthInPx,
|
||||
screenHeightInPx,
|
||||
screenWidthInDp,
|
||||
screenHeightInDp,
|
||||
startAdContainer,
|
||||
() -> {
|
||||
hideStartUpAd();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void showStartUp(StartupAdEntity ad) {
|
||||
/**
|
||||
* 显示文本广告
|
||||
*/
|
||||
private void showTextAd(StartupAdEntity ad) {
|
||||
TextView adContentTv = findViewById(R.id.adContentTv);
|
||||
View containerView = findViewById(R.id.maskContainer);
|
||||
containerView.setVisibility(View.VISIBLE);
|
||||
@ -706,7 +603,7 @@ public class MainActivity extends BaseActivity {
|
||||
containerView.setElevation(500F);
|
||||
}
|
||||
containerView.setOnClickListener(v -> {
|
||||
// do nothing 只是为了点击拦截事件,避免传递到下面的页面
|
||||
// 拦截点击事件,避免传递到下面的页面
|
||||
});
|
||||
adContentTv.setText(ad.getDesc());
|
||||
adContentTv.setVisibility(View.VISIBLE);
|
||||
@ -756,7 +653,6 @@ public class MainActivity extends BaseActivity {
|
||||
} else {
|
||||
Intent skipIntent = new Intent(MainActivity.this, clazz);
|
||||
skipIntent.putExtras(bundle);
|
||||
// startActivity(skipIntent);
|
||||
AvoidOnResultManager.Companion.getInstance(this)
|
||||
.startForResult(skipIntent, (resultCode, data) -> {
|
||||
Bundle nextToBundle = getIntent().getBundleExtra(KEY_NEXT_TO);
|
||||
@ -878,7 +774,7 @@ public class MainActivity extends BaseActivity {
|
||||
@Override
|
||||
public Boolean invoke(Integer code) {
|
||||
if (code == 404001) {
|
||||
if (showAd) {
|
||||
if (mShouldShowAd) {
|
||||
AppExecutor.getUiExecutor().executeWithDelay(() -> {
|
||||
toast("抱歉,暂未找到相关内容");
|
||||
}, 1000);
|
||||
@ -989,7 +885,7 @@ public class MainActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
if (showAd) {
|
||||
if (mShouldShowAd) {
|
||||
return R.layout.activity_main;
|
||||
} else {
|
||||
return R.layout.layout_wrapper_activity;
|
||||
@ -1012,71 +908,6 @@ public class MainActivity extends BaseActivity {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取META-INF中的plugin_update 文件,判断是否从游戏插件中下载的app,是则获取游戏id,启动游戏更新,下载该游戏
|
||||
private void getPluginUpdate() {
|
||||
AppExecutor.getIoExecutor().execute(() -> {
|
||||
ApplicationInfo appinfo = getApplicationInfo();
|
||||
String sourceDir = appinfo.sourceDir;
|
||||
ZipFile zipfile = null;
|
||||
|
||||
try {
|
||||
zipfile = new ZipFile(sourceDir);
|
||||
Enumeration<?> entries = zipfile.entries();
|
||||
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = ((ZipEntry) entries.nextElement());
|
||||
String entryName = entry.getName();
|
||||
if (entryName.contains("gh_assist")) {
|
||||
String packageName = entryName.substring(entryName.lastIndexOf("_") + 1);
|
||||
startActivity(DownloadManagerActivity.getDownloadMangerIntent(MainActivity.this,
|
||||
packageName, DownloadFragment.INDEX_UPDATE, "(游戏插件)"));
|
||||
break;
|
||||
} else if (entryName.contains("halo_skip.json")) {
|
||||
InputStream in = zipfile.getInputStream(entry);
|
||||
if (in != null) {
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
|
||||
InnerMetaInfoEntity info = GsonUtils.getGson().fromJson(reader, InnerMetaInfoEntity.class);
|
||||
if (info != null) {
|
||||
if (EntranceConsts.HOST_COMMUNITY.equals(info.getType())) {
|
||||
runOnUiThread(() -> mMainWrapperFragment.setCurrentItem(MainWrapperFragment.INDEX_BBS));
|
||||
} else {
|
||||
DirectUtils.directToSpecificPage(this,
|
||||
info.getType(),
|
||||
info.getLink(),
|
||||
info.getText(),
|
||||
EntranceConsts.KEY_PLUGIN,
|
||||
"特定包启动跳转");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (entryName.contains("halo_skip.dat")) {
|
||||
InputStream in = zipfile.getInputStream(entry);
|
||||
if (in != null) {
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
String content = "";
|
||||
for (String line; (line = reader.readLine()) != null; content += line) ;
|
||||
|
||||
if (!TextUtils.isEmpty(content)) {
|
||||
DefaultUrlHandler.interceptUrl(this, content, "(特定包启动跳转)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (zipfile != null) {
|
||||
try {
|
||||
zipfile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 连接上网络事件
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMainThread(EBNetworkState busNetworkState) {
|
||||
|
||||
Reference in New Issue
Block a user