Commit c0cee586 authored by mengcuiguang's avatar mengcuiguang

珊瑚接入

parent 4b78a30a
......@@ -290,10 +290,10 @@ dependencies {
exclude group: 'com.google.code.gson'
}
// 珊瑚广告
// implementation(name:'adapt-1.3.3', ext:'aar')
// implementation(name:'commonbase-1.0.2', ext:'aar')
// implementation(name:'discovery-2.0.12', ext:'aar')
// implementation(name:'shanhuAD-1.0.14', ext:'aar')
implementation(name:'adapt-1.3.3', ext:'aar')
implementation(name:'commonbase-1.0.2', ext:'aar')
implementation(name:'discovery-2.0.12', ext:'aar')
implementation(name:'shanhuAD-1.0.14', ext:'aar')
......
......@@ -10,6 +10,7 @@ import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;
import com.bun.miitmdid.core.JLibrary;
......@@ -31,6 +32,7 @@ import com.mints.highgold.utils.ForegroundOrBackground;
import com.mints.highgold.utils.RudenessScreenHelper;
import com.mints.highgold.utils.rxutil.CommonRxTask;
import com.mints.highgold.utils.rxutil.RxjavaUtil;
import com.mints.highgold.utils.shanhu.H5Browser;
import com.mints.library.utils.CommonUtils;
import com.mints.library.utils.SmartBarUtils;
import com.mob.MobSDK;
......@@ -38,7 +40,10 @@ import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.tencent.ep.shanhuad.adpublic.ShanHuAD;
import com.tendcloud.tenddata.TCAgent;
import com.tmsdk.module.coin.AbsTMSConfig;
import com.tmsdk.module.coin.TMSDKContext;
import com.umeng.commonsdk.UMConfigure;
import com.yilan.sdk.ui.YLUIInit;
import com.yilan.sdk.ui.configs.CommentConfig;
......@@ -70,6 +75,8 @@ public class MintsApplication extends MultiDexApplication {
private static boolean isSupportOaid = true;
private static int errorCode;
public volatile static boolean shanhuFlag = false;
public static String getOaid() {
return oaid;
}
......@@ -208,8 +215,12 @@ public class MintsApplication extends MultiDexApplication {
// Mintegral广告
initMintegral();
// 珊瑚广告
initShanhuAd();
}
/**
* 移动安装联盟 获取OAID等设备标识符
*/
......@@ -389,6 +400,35 @@ public class MintsApplication extends MultiDexApplication {
sdk.init(map, this);
}
/**
* 珊瑚广告
*/
public void initShanhuAd() {
/**
* setTMSDKLogEnable(true)会把tmsdk的log打开,release时可以去掉这个接口调用。
*/
if (BuildConfig.DEBUG) {
TMSDKContext.setTMSDKLogEnable(true);
}
// TMSDK初始化
shanhuFlag = TMSDKContext.init(this, new AbsTMSConfig() {
@Override
public String getServerAddress() {
if (BuildConfig.DEBUG) {
return "mazutest.3g.qq.com"; // 测试环境
} else {
return "mazu.3g.qq.com"; // 正式环境
}
}
});
/**
* 初始化后才能调用!
* setAutoConnectionSwitch()影响渠道号上报这个自动联网项是否运行。请不要一直设置为false,影响激活量和活跃量统计,后台会关注并停止相关服务,请在过工信部测试允许联网后,设置回true
*/
ShanHuAD.init(TMSDKContext.getApplicationContext(), new H5Browser(), TMSDKContext.getCoinProductId());
System.out.println("mcg __ shanhuFlag:" + shanhuFlag);
}
/**
* app退出
*/
......
package com.mints.highgold.ad;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import com.mintegral.msdk.out.MTGRewardVideoHandler;
import com.mintegral.msdk.out.RewardVideoListener;
import com.mintegral.msdk.videocommon.download.NetStateOnReceive;
import com.mints.highgold.MintsApplication;
import com.mints.highgold.common.Constant;
import com.mints.highgold.manager.MintegralManager;
import com.mints.highgold.manager.TrackManager;
import com.mints.highgold.mvp.presenters.MintegralVedioAdPresenter;
import com.mints.highgold.mvp.views.MintegralVedioAdManagerView;
import com.mints.highgold.ui.widgets.LoadingDialog;
import com.mints.highgold.utils.ToastUtil;
import com.tencent.ep.shanhuad.adpublic.ADError;
import com.tencent.ep.shanhuad.adpublic.adbuilder.RewardVideo;
import com.tencent.ep.shanhuad.adpublic.models.AdID;
import com.tmsdk.module.coin.AdConfig;
import com.tmsdk.module.coin.AdConfigManager;
import com.tmsdk.module.coin.AdRequestData;
import com.tmsdk.module.coin.CmpAdConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 珊瑚视频
*/
public class ShanhuVedioAdManager {
private static ShanhuVedioAdManager _inst;
private ShanhuVedioAdListener shanhuVedioAdListener;
Activity activity;
private RewardVideo mRewardVedio;
private int curCoin;
private String extraId;
private boolean isClickScreen = true; // 是否点击屏幕跳转广告
public static ShanhuVedioAdManager getInstance(Activity activity) {
if (_inst != null) {
return _inst;
} else {
_inst = new ShanhuVedioAdManager(activity);
return _inst;
}
}
private ShanhuVedioAdManager(Activity activity) {
init(activity);
return;
}
private void init(Activity activity) {
this.activity = activity;
}
public void destory() {
}
public void loadShanhuAd(int downloadCoin, int curCoin, String carrierType, String extraId) {
if (activity == null) {
return;
}
isClickScreen = true;
this.curCoin = curCoin;
this.extraId = extraId;
if (!MintsApplication.shanhuFlag) {
((MintsApplication) activity.getApplication()).initShanhuAd();
if (shanhuVedioAdListener != null) {
shanhuVedioAdListener.shanhuVedioAdFail();
}
} else {
new Thread() {
public void run() {
Bundle inBundle1 = new Bundle();
AdConfig aAdConfig1 = new AdConfig(104, inBundle1);
CmpAdConfig cmpAdConfig = AdConfigManager.checkParam(aAdConfig1, 5 * 1000L);
//获取请求具体广告类的参数,封装在AdRequestData
AdRequestData adRequestData = AdConfigManager.getSimplePositionAdConfig(cmpAdConfig);
//AdRequestData的position_id参数对应AdID.posID,
getAdFromBusiness(adRequestData.business + "", adRequestData);
}
}.start();
}
}
/**
* 通过business来定位使用哪个广告类进行请求
*
* @param Business1 business字符串
* @param adRequestData 请求体,AdRequestData的position_id参数对应AdID.posID,
* positionFormatTypes参数对应Adid的styleArray参数
*/
private void getAdFromBusiness(String Business1, AdRequestData adRequestData) {
if (Business1.equals("COIN_VIDEO_EXIT")) {
gotRewardVedio(adRequestData.positionId, adRequestData.positionFormatTypes);
}
}
private void gotRewardVedio(int adId, List<Integer> styleId) {
mRewardVedio = new RewardVideo();
List<AdID> list = new ArrayList<>();
list.add(new AdID(adId, 968, 300));
if (activity != null) {
mRewardVedio.load(new RewardVideo.RVListener() {
@Override
public void loaded() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mRewardVedio != null) {
mRewardVedio.showAD();
}
// TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_SHANHU, Constant.EVENT_TYPE_ZERO, "", "");
}
});
System.out.println("mcg __ gotRewardVedio loaded");
}
@Override
public void onAdError(ADError adError) {
if (adError != null) {
// TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_SHANHU, Constant.EVENT_TYPE_ONE, adError.code + "", adError.msg);
}
if (shanhuVedioAdListener != null) {
shanhuVedioAdListener.shanhuVedioAdFail();
}
System.out.println("mcg __ gotRewardVedio_onAdError");
}
@Override
public void onVideoComplete() {
System.out.println("mcg __ gotRewardVedio onVideoComplete");
Log.e("onVideoComplete", "onVideoComplete");
}
@Override
public void onVideoPlay() {
System.out.println("mcg __ gotRewardVedio onVideoPlay");
}
@Override
public void onClick() {
// TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_SHANHU, Constant.EVENT_TYPE_TWO, "", "");
if (isClickScreen) {
// 防止重复
// TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_SHANHU, Constant.EVENT_TYPE_FOUR, "", "");
isClickScreen = false;
}
if (shanhuVedioAdListener != null) {
shanhuVedioAdListener.shanhuVedioAdDownload();
}
System.out.println("mcg __ gotRewardVedio onClick");
}
@Override
public void onClose() {
// TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_SHANHU, Constant.EVENT_TYPE_THREE, "", "");
System.out.println("mcg __ gotRewardVedio onClose");
if (shanhuVedioAdListener != null) {
shanhuVedioAdListener.shanhuVedioAdAdSuccess();
}
}
}, activity, new AdID(adId, 968, 300));
}
}
public void setShanhuVedioAdListener(ShanhuVedioAdListener shanhuVedioAdListener) {
this.shanhuVedioAdListener = shanhuVedioAdListener;
}
public interface ShanhuVedioAdListener {
void shanhuVedioAdFail();
void shanhuVedioAdAdSuccess();
void shanhuVedioAdDownload();
}
}
......@@ -31,12 +31,14 @@ public class VedioAdingManager {
private CsjFullVedioAdManager csjFullVedioAdManager;
private MintegralVedioAdManager mintegralVedioAdManager;
private HytechVedioAdManager hytechVedioAdManager;
private ShanhuVedioAdManager shanhuVedioAdManager;
private int ylhWeight;
private int csjFullWeight;
private int csjVedioWeight;
private int hytechVedioWeight;
private int mintegralWeight;
private int shanhuWeight;
private ArrayList<WeightBean> weightList;
private Activity activity;
......@@ -68,6 +70,7 @@ public class VedioAdingManager {
csjFullVedioAdManager = CsjFullVedioAdManager.getInstance(activity);
hytechVedioAdManager = HytechVedioAdManager.getInstance(activity);
mintegralVedioAdManager = MintegralVedioAdManager.getInstance(activity);
shanhuVedioAdManager = ShanhuVedioAdManager.getInstance(activity);
weightList = new ArrayList<>();
}
......@@ -80,6 +83,7 @@ public class VedioAdingManager {
this.csjFullWeight = csjFullWeight;
this.hytechVedioWeight = hytechVedioWeight;
this.mintegralWeight = mintegralWeight;
this.shanhuWeight = shanhuWeight;
}
/**
......@@ -153,6 +157,11 @@ public class VedioAdingManager {
weight = weight + csjFullWeight;
System.out.print(" mcg __ 权重值:csjFullWeight:" + csjFullWeight);
}
if (AppConfig.shanhuAdCount > 0) {
weightList.add(new WeightBean(shanhuWeight, Constant.SHANHU_VEDIO_AD));
weight = weight + shanhuWeight;
System.out.print(" mcg __ 权重值:shanhuWeight:" + shanhuWeight);
}
if (AppConfig.hytechVedioAdCount > 0) {
weightList.add(new WeightBean(hytechVedioWeight, Constant.HYTECH_VEDIO_AD));
weight = weight + hytechVedioWeight;
......@@ -245,6 +254,11 @@ public class VedioAdingManager {
mintegralVedioAdManager.destory();
mintegralVedioAdManager = null;
}
if (shanhuVedioAdManager != null) {
shanhuVedioAdManager.destory();
shanhuVedioAdManager = null;
}
}
public void setVedioAdingListener(VedioAdingListener vedioAdingListener) {
......@@ -465,6 +479,45 @@ public class VedioAdingManager {
isVedioAdDownload = true;
}
});
}else if (TextUtils.equals(vedioAdType, Constant.SHANHU_VEDIO_AD)) {
// 珊瑚激励视频
if (shanhuVedioAdManager == null) {
if (activity == null) {
if (vedioAdingListener != null) {
vedioAdingListener.vedioAdingListenerError(Constant.SHANHU_VEDIO_AD);
}
return;
}
shanhuVedioAdManager = ShanhuVedioAdManager.getInstance(activity);
}
// 重置监听
shanhuVedioAdManager.setShanhuVedioAdListener(null);
shanhuVedioAdManager.loadShanhuAd(bean.getDownloadCoin(), bean.getCurCoin(), bean.getCarrierType(), bean.getExtraId());
shanhuVedioAdManager.setShanhuVedioAdListener(new ShanhuVedioAdManager.ShanhuVedioAdListener() {
@Override
public void shanhuVedioAdFail() {
if (vedioAdingListener != null) {
vedioAdingListener.vedioAdingListenerFail(Constant.SHANHU_VEDIO_AD);
}
}
@Override
public void shanhuVedioAdAdSuccess() {
AppConfig.shanhuAdCount--;
AppConfig.preAdType = Constant.SHANHU_VEDIO_AD;
AppConfig.preAdTime = Calendar.getInstance().getTimeInMillis();
if (vedioAdingListener != null) {
vedioAdingListener.vedioAdingListenerSuccess(Constant.SHANHU_VEDIO_AD);
}
}
@Override
public void shanhuVedioAdDownload() {
isVedioAdDownload = true;
}
});
}
} catch (Exception e) {
e.printStackTrace();
......
......@@ -42,7 +42,10 @@ public class InsertScreenAdManager {
public void showInsertScreenAd(String carrierType) {
if (UserManager.getInstance().getAdShowFlag()) {
initInsertScreenAd(carrierType);
if (!AppConfig.interactionAdFlag) {
AppConfig.interactionAdFlag = true;
initInsertScreenAd(carrierType);
}
}
}
......@@ -119,6 +122,7 @@ public class InsertScreenAdManager {
public void onError(int code, String message) {
//当前播放完成换下一个类型 1-优量汇
AppConfig.interactionAdType = 1;
setNextShowInteractionAd();
}
@Override
......@@ -138,6 +142,7 @@ public class InsertScreenAdManager {
ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
@Override
public void onAdDismiss() {
setNextShowInteractionAd();
}
@Override
......@@ -148,17 +153,19 @@ public class InsertScreenAdManager {
public void onAdShow(View view, int type) {
//当前播放完成换下一个类型 1-优量汇
AppConfig.interactionAdType = 1;
}
@Override
public void onRenderFail(View view, String msg, int code) {
//当前播放完成换下一个类型 1-优量汇
AppConfig.interactionAdType = 1;
setNextShowInteractionAd();
}
@Override
public void onRenderSuccess(View view, float width, float height) {
if (mTTAd != null && activity != null && !activity.isFinishing()) {
//返回view的宽高 单位 dp
mTTAd.showInteractionExpressAd(activity);
......@@ -218,6 +225,7 @@ public class InsertScreenAdManager {
@Override
public void onNoAD(AdError adError) {
AppConfig.interactionAdType = 2;
setNextShowInteractionAd();
}
@Override
......@@ -241,7 +249,7 @@ public class InsertScreenAdManager {
@Override
public void onADClosed() {
setNextShowInteractionAd();
}
});
}
......@@ -293,16 +301,19 @@ public class InsertScreenAdManager {
@Override
public void onFailedToReceiveAd() {
setNextShowInteractionAd();
AppConfig.interactionAdType = 0;
}
@Override
public void onLoadFailed() {
setNextShowInteractionAd();
AppConfig.interactionAdType = 0;
}
@Override
public void onCloseClick() {
setNextShowInteractionAd();
}
@Override
......@@ -315,9 +326,19 @@ public class InsertScreenAdManager {
@Override
public void onAdActivityClose(String s) {
setNextShowInteractionAd();
}
});
mTMItAd.loadAd(codeId, UserManager.getInstance().getUserID());
}
}
/**
* 设置插屏下次可以展示
* <p>
* false-下次显示
*/
public void setNextShowInteractionAd() {
AppConfig.interactionAdFlag = false;
}
}
......@@ -59,6 +59,10 @@ public class AppConfig {
* mintegralVedio 剩余广告播放数
*/
public static int mintegralAdCount = 0;
/**
* shanhuVedio 剩余广告播放数
*/
public static int shanhuAdCount = 0;
/**
* hytechVedio 剩余广告播放数
*/
......@@ -72,7 +76,11 @@ public class AppConfig {
/**
* 插屏广告显示类型 0-穿山甲 1-优量汇 2-推啊
*/
public static int interactionAdType = 0;
public static int interactionAdType = 2;
/**
* 插屏广告显示中 true-正在显示
*/
public static boolean interactionAdFlag = false;
/**
* banner显示类型 0-推啊 1-nice推
......
......@@ -124,6 +124,7 @@ public interface Constant {
String YILAN_VEDIO_AD = "YILAN_VEDIO_AD";
String HYTECH_VEDIO_AD = "HYTECH_VEDIO_AD";
String MINTEGRAL_VEDIO_AD = "MINTEGRAL_VEDIO_AD";
String SHANHU_VEDIO_AD = "SHANHU_VEDIO_AD";
// 载体
String CARRIER_DRINK = "DRINK";//喝水
......@@ -188,6 +189,7 @@ public interface Constant {
String AD_SOURCE_CSJFULL = "CSJFULL";
String AD_SOURCE_MINTEGRAL = "MINTEGRAL";
String AD_SOURCE_HYTECH = "HYTECH";//东方网
String AD_SOURCE_SHANHU = "SHANHU";//珊瑚
// 页面
int PAGE_ONE = 1;
......
......@@ -212,6 +212,7 @@ public class DrawcashActivity extends BaseActivity implements DrawcashView, OnLo
}
if (insertScreenAd != null) {
insertScreenAd.setNextShowInteractionAd();
insertScreenAd.ondestory();
}
......
......@@ -179,6 +179,7 @@ public class RedboxActivity extends BaseActivity implements RedboxView {
super.onDestroy();
if (insertScreenAd != null) {
insertScreenAd.setNextShowInteractionAd();
insertScreenAd.ondestory();
}
......
......@@ -27,6 +27,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mints.highgold.BuildConfig;
import com.mints.highgold.R;
import com.mints.highgold.ad.ShanhuVedioAdManager;
import com.mints.highgold.ad.VedioAdingManager;
import com.mints.highgold.ad.inscreenvedio.MintegralInscreenVedioAdManager;
import com.mints.highgold.common.AppConfig;
......@@ -357,6 +358,7 @@ public class MainFragment extends BaseFragment
switch (view.getId()) {
case R.id.tv_fragment_main_whatgold:// 如何赚金币
readyGo(WhatGoldActivity.class);
// ShanhuVedioAdManager.getInstance(getActivity()).loadShanhuAd(0,0,"","");
break;
case R.id.rl_fragment_main_redbox:// 红包
readyGo(RedboxActivity.class);
......
package com.mints.highgold.utils.shanhu;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.util.Log;
import java.io.File;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CommonUtil {
/**
* 日志标签
*/
private static final String TAG = "CommonUtil";
/**
* 获取字符串的MD5编码
*
* @param source
* @return
*/
public static String getMD5(String source) {
//定义一个字节数组
byte[] secretBytes = null;
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
//对字符串进行加密
md.update(source.getBytes());
//获得加密后的数据
secretBytes = md.digest();
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "getMD5 (NoSuchAlgorithmException)", e);
}
//将加密后的数据转换为16进制数字
StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));// 16进制数字
// 如果生成数字未满32位,需要前面补0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code.insert(0, "0");
}
return md5code.toString();
}
/**
* 应用是否已安装
*
* @param pkg 包名
* @return
*/
public static boolean isPkgInstalled(Context mContext, String pkg) {
PackageInfo info = null;
try {
info = mContext.getPackageManager().getPackageInfo(pkg, 0);//flag 0 不会返回多余的数据
} catch (Throwable e) {
Log.e(TAG, "IsPkgInstalled (Throwable)", e);
}
return (info != null);
}
/**
* 外部应用安装器安装apk(原生接口)
*
* @param path apk的路径
* @return
*/
public static boolean installApkByPath(Context mContext, String path) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
return true;
} catch (Throwable e) {
Log.e(TAG, "installApkByPath (Throwable)", e);
}
return false;
}
}
package com.mints.highgold.utils.shanhu;
import com.google.gson.Gson;
import com.tencent.ep.shanhuad.adpublic.ShanHuAD;
import com.tencent.ep.shanhuad.adpublic.models.AdMetaInfo;
import com.tmsdk.module.coin.AppRetainModel;
import com.tmsdk.module.coin.AppRetainSpUtil;
import com.tmsdk.module.coin.AppRetainUtil;
public class DownloadReportProxy {
public static Gson gson = new Gson();
public static void reportStartDownload(AdMetaInfo model){
ShanHuAD.reportStartDownload(model.getAdDisplayModel());
}
public static void reportDownloadFinish(AdMetaInfo model){
ShanHuAD.reportDownloadFinish(model.getAdDisplayModel());
}
public static void reporttinstalled(AdMetaInfo model){
ShanHuAD.reportInstalled(model.getAdDisplayModel());
AppRetainUtil.putInstalledAppToSp(model.getAdDisplayModel().packageName,new AppRetainModel(System.currentTimeMillis(),model.getAdDisplayModel().packageName,
-1,model.getAdDisplayModel().uniqueKey,"", AppRetainModel.installed,model.title,model.cta,model.icon));
}
public static void reportrtActive(AdMetaInfo model){
ShanHuAD.reportActive(model.getAdDisplayModel());
AppRetainModel appRetainModel = gson.fromJson((String) AppRetainSpUtil.getISharePreferenceImp().getString(model.getAdDisplayModel().packageName,""), AppRetainModel.class);
if(model != null){
appRetainModel.clickNum=+1;
}
AppRetainUtil.putInstalledAppToSp(model.getAdDisplayModel().packageName,appRetainModel);
}
public static void reportDownloadFinish(AdMetaInfo mStyleAdEntity, String apkFilePath) {
ShanHuAD.reportDownloadFinish(mStyleAdEntity.getAdDisplayModel());
AppRetainUtil.putDownloadAppToSp(mStyleAdEntity.getAdDisplayModel().packageName,new AppRetainModel(System.currentTimeMillis(),mStyleAdEntity.getAdDisplayModel().packageName,
-1,mStyleAdEntity.getAdDisplayModel().uniqueKey,apkFilePath, AppRetainModel.downloadComplete,mStyleAdEntity.title,mStyleAdEntity.cta,mStyleAdEntity.icon));
}
}
package com.mints.highgold.utils.shanhu;
import android.Manifest;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.tencent.ep.shanhuad.adpublic.H5BrowserListener;
import com.tencent.ep.shanhuad.adpublic.models.AdMetaInfo;
import com.tmsdk.module.coin.TMSDKContext;
import java.io.File;
/**
* sdk 广告点击回调实现
*/
public class H5Browser implements H5BrowserListener {
private BroadcastReceiver downloadBroadcastReceiver, installBroadcastReceiver;
public static final String TAG = "TMSDK_H5Browser";
@Override
public void openH5(String url) {
android.util.Log.d("H5Impl","openH5");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
TMSDKContext.getApplicationContext().startActivity(intent);
}
/**
* app安装回到,因为广告源返回情况比较复杂,
* 1 先判断 model.appDownloadUrl 尝试下载
* 2 如果没有appDownloadUrl:则用浏览器开启jumpUrl地址通过浏览器下载,
* 下载和安装后要调用对应接口上报,便于结算和对账
* @param model
*/
@Override
public void openAppDetailPage(AdMetaInfo model) {
android.util.Log.d("H5Impl","openAppDetailPage");
String url = model.getAdDisplayModel().jumpUrl;
if(TextUtils.isEmpty(url) && model.getAdDisplayModel().appDownloadUrl!=null){
url = model.getAdDisplayModel().appDownloadUrl;
downloadApk(model,"下载应用", "广告应用");
}
if(TextUtils.isEmpty(url)){
return;
}
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setData(Uri.parse(url));
// TMSDKContext.getApplicationContext().startActivity(intent);
}
/**
* 下载Apk, 并设置Apk地址,
* 默认位置: /storage/sdcard0/Download
*
* @param infoName 通知名称
* @param description 通知描述
*/
public void downloadApk(final AdMetaInfo mStyleAdEntity, String description, String infoName) {
int permission = ContextCompat.checkSelfPermission(TMSDKContext.getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
// requestPermissions((Activity) activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
Toast.makeText(TMSDKContext.getApplicationContext(), "下载需要先申请存储权限!", Toast.LENGTH_SHORT).show();
return;
}
// showToast("应用广告开始下载上报");
DownloadReportProxy.reportStartDownload(mStyleAdEntity);//开始下载
DownloadManager.Request request;
try {
request = new DownloadManager.Request(Uri.parse(mStyleAdEntity.getAdDisplayModel().appDownloadUrl));
} catch (Throwable e) {
Log.e(TAG, "DownloadManager.Request (Throwable)", e);
return;
}
request.setTitle(infoName);
request.setDescription(description);
//在通知栏显示下载进度
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
//设置保存下载apk保存路径
String apkName =
System.currentTimeMillis() + "_" + CommonUtil.getMD5(mStyleAdEntity.getAdDisplayModel().appDownloadUrl) + ".apk";
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkName);
Context appContext = TMSDKContext.getApplicationContext();
DownloadManager manager = (DownloadManager) appContext.getSystemService(Context.DOWNLOAD_SERVICE);
//进入下载队列
long id = manager.enqueue(request);
listenerDownLoad(id, mStyleAdEntity, apkName);
}
private void showToast(String str) {
Toast.makeText(TMSDKContext.getApplicationContext(),str, Toast.LENGTH_SHORT).show();
}
private void listenerDownLoad(final long Id, final AdMetaInfo mStyleAdEntity, final String apkName) {
// 注册广播监听系统的下载完成事件。
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
downloadBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TMSDKContext.getApplicationContext().unregisterReceiver(downloadBroadcastReceiver);
Bundle mBundle = intent.getExtras();
long ID = mBundle.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
if (ID == Id) {
final String apkFilePath = new StringBuilder(Environment.getExternalStorageDirectory().getAbsolutePath())
.append(File.separator).append(Environment.DIRECTORY_DOWNLOADS).append(File.separator)
.append(apkName).toString();
// showToast("应用广告下载成功上报");
DownloadReportProxy.reportDownloadFinish(mStyleAdEntity, apkFilePath);
listenerInstall(mStyleAdEntity);
CommonUtil.installApkByPath(TMSDKContext.getApplicationContext(), apkFilePath);
}
}
};
TMSDKContext.getApplicationContext().registerReceiver(downloadBroadcastReceiver, intentFilter);
}
private void listenerInstall(final AdMetaInfo mStyleAdEntity) {
// 注册广播监听系统的下载完成事件。
IntentFilter installFilter = new IntentFilter();
installFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
installFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
installFilter.addDataScheme("package");
installBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TMSDKContext.getApplicationContext().unregisterReceiver(installBroadcastReceiver);
// showToast("应用广告安装成功上报");
DownloadReportProxy.reporttinstalled(mStyleAdEntity);
startAdApp(mStyleAdEntity);
}
};
TMSDKContext.getApplicationContext().registerReceiver(installBroadcastReceiver, installFilter);
}
private void startAdApp(final AdMetaInfo mStyleAdEntity) {
try {
// showToast("应用广告打开成功上报");
DownloadReportProxy.reportrtActive(mStyleAdEntity);
PackageManager packageManager = TMSDKContext.getApplicationContext().getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(mStyleAdEntity.getAdDisplayModel().packageName);
TMSDKContext.getApplicationContext().startActivity(intent);
} catch (Throwable t) {
//IGNORE
}
}
/**检测存储权限*/
private final int REQUEST_EXTERNAL_STORAGE = 1;
private String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment