From bdd6eff8c1f887b51f3473f779e603235a9dc410 Mon Sep 17 00:00:00 2001 From: mwang <8205347@qq.com> Date: Mon, 10 Jun 2019 00:05:51 +0800 Subject: [PATCH] adjust ad --- .../com/novelbook/android/Activity_base.java | 8 +- .../com/novelbook/android/ReadActivity.java | 468 ++++-------------- .../novelbook/android/netutils/NetUtil.java | 5 +- .../novelbook/android/utils/AdInterface.java | 4 +- .../novelbook/android/utils/PageFactory.java | 119 +++-- zhuike/src/main/res/layout/zactivity_read.xml | 25 +- zhuike/zhuike.iml | 5 - 7 files changed, 197 insertions(+), 437 deletions(-) diff --git a/zhuike/src/main/java/com/novelbook/android/Activity_base.java b/zhuike/src/main/java/com/novelbook/android/Activity_base.java index 7625769..f73c4e9 100644 --- a/zhuike/src/main/java/com/novelbook/android/Activity_base.java +++ b/zhuike/src/main/java/com/novelbook/android/Activity_base.java @@ -406,7 +406,7 @@ public abstract class Activity_base extends AppCompatActivity { bannerContainer.removeAllViews(); bannerContainer.addView(bannerView); // bannerContainer.setVisibility(View.VISIBLE); - Log.d(TAG, "loadBannerAd: set banner VISIBLE"); + // Log.d(TAG, "loadBannerAd: set banner VISIBLE"); // mTTAdNative.loadBannerAd(adSlot,null); //设置广告互动监听回调 ad.setBannerInteractionListener(new TTBannerAd.AdInteractionListener() { @@ -550,12 +550,12 @@ public abstract class Activity_base extends AppCompatActivity { public void onError(int code, String message) { // TToast.show(oContext, "load error : " + code + ", " + message); Log.e(TAG, "loadBannerAd: " + code + ", " + message); - bannerContainer.setBackgroundResource(R.color.transparent); + // bannerContainer.setBackgroundResource(R.color.transparent); } @Override public void onNativeAdLoad(List ads) { - bannerContainer.setBackgroundResource(R.color.transparent); + // bannerContainer.setBackgroundResource(R.color.transparent); List tmp = new ArrayList(); for (TTNativeAd ad : ads) { if (ad != null) { @@ -648,7 +648,7 @@ public abstract class Activity_base extends AppCompatActivity { //bannerContainer.setVisibility(View.VISIBLE); if (!Constants.AD_BACKGROUND_TRANSPARENT) bannerContainer.setBackgroundResource(R.color.mintcream); // @color/mintcream - Log.d(TAG, "loadBannerAd: set banner VISIBLE,mShowAd " + mShowAd); + // Log.d(TAG, "loadBannerAd: set banner VISIBLE,mShowAd " + mShowAd); //绑定原生广告的数据 setAdData(bannerContainer, bannerView, nativeAd); // mTTAdNative.loadNativeAd(adSlot,null); diff --git a/zhuike/src/main/java/com/novelbook/android/ReadActivity.java b/zhuike/src/main/java/com/novelbook/android/ReadActivity.java index 1ea7a2b..b5f29a6 100644 --- a/zhuike/src/main/java/com/novelbook/android/ReadActivity.java +++ b/zhuike/src/main/java/com/novelbook/android/ReadActivity.java @@ -129,8 +129,8 @@ public class ReadActivity extends Activity_base implements AdInterface { @BindView(R.id.tvUrl) TextView tvUrl; - @BindView(R.id.top_banner_container) - FrameLayout mTopBannerContainer; +// @BindView(R.id.top_banner_container) +// FrameLayout mTopBannerContainer; @BindView(R.id.imgLoading) pl.droidsonroids.gif.GifImageView imgLoading; @@ -138,7 +138,7 @@ public class ReadActivity extends Activity_base implements AdInterface { FrameLayout frmAD; @BindView(R.id.native_banner_container ) - FrameLayout mNative_banner_container; + FrameLayout mNative_status_banner_container; @BindView(R.id.native_banner_container_in_lines ) FrameLayout mNative_banner_container_in_lines; @@ -212,7 +212,7 @@ public class ReadActivity extends Activity_base implements AdInterface { pageFactory = PageFactory.getInstance(getApplicationContext()); pageFactory.clear(); pageFactory.setAd(this); - mBannerContainer.setVisibility(View.GONE); + // mBannerContainer.setVisibility(View.GONE); // showProgress (false,"读取目录信息"); /* int slept = 0; @@ -480,16 +480,24 @@ public class ReadActivity extends Activity_base implements AdInterface { @Override public void showAdAfterIni() { - if(frmAD.getVisibility()== View.INVISIBLE) - // frmAD.setVisibility(View.VISIBLE); - Log.d(TAG, "pageAnimation showAdAfterIni: "); + // if(frmAD.getVisibility()== View.INVISIBLE) + // frmAD.setVisibility(View.VISIBLE); + /* if(mNative_banner_container_in_lines.getVisibility()== View.GONE) + mNative_banner_container_in_lines.setVisibility(View.VISIBLE);*/ + // Log.d(TAG, "pageAnimation showAdAfterIni: "); + pageFactory.showAd(); } @Override public void hideAdBeforeIni() { - if(frmAD.getVisibility()== View.VISIBLE) - // frmAD.setVisibility(View.INVISIBLE); - Log.d(TAG, "pageAnimation hideAdBeforeIni: "); + // if(frmAD.getVisibility()== View.VISIBLE) + // frmAD.setVisibility(View.INVISIBLE); + /* if(mNative_banner_container_in_lines.getVisibility()== View.VISIBLE) + mNative_banner_container_in_lines.setVisibility(View.GONE);*/ + // mNative_banner_container_in_lines.removeAllViews(); + // Log.d(TAG, "pageAnimation hideAdBeforeIni: "); + + } }); @@ -1071,67 +1079,104 @@ public class ReadActivity extends Activity_base implements AdInterface { } */ - public void hideAds(){ - /* if(mNative_banner_container.getVisibility() ==View.VISIBLE) { - mNative_banner_container.setVisibility(View.GONE); + public void hideStatusAds(){ + Log.d(TAG, "loadBannerAd: hideAds()"); + /* try{ + throw new Exception(""); + }catch (Exception e){ + Log.e(TAG, "loadBannerAd: ", e); }*/ + if(mNative_status_banner_container.getVisibility() ==View.VISIBLE) { + mNative_status_banner_container.setVisibility(View.GONE); + } +// mNative_banner_container_in_lines.removeAllViews(); +// mNative_banner_container_in_lines.setBackgroundResource(R.color.transparent); + } + private void hideAdSlot(ViewGroup view){ + mNative_banner_container_in_lines.removeAllViews(); + if(view.getVisibility() ==View.VISIBLE) { + view.setVisibility(View.GONE); + } +/* FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams(); + params.width =0; + view.setLayoutParams(params);*/ + // view.setBackgroundResource(R.color.transparent); + + //mNative_banner_container_in_lines.removeAllViews(); + // mNative_banner_container_in_lines.setBackgroundResource(R.color.transparent); + + } + private void showAdSlot(ViewGroup view){ + Log.d(TAG, "loadBannerAd: showAdSlot()"); + if(view.getVisibility() ==View.GONE) { + view.setVisibility(View.VISIBLE); + } + /* FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams(); + + params.width =-1; + view.setLayoutParams(params);*/ + } public void showNativeBannerInLines(int height,int adY){ - hideSystemUI(); - if(mNative_banner_container.getVisibility() ==View.VISIBLE) { - mNative_banner_container.setVisibility(View.INVISIBLE); - } - mNative_banner_container_in_lines .setVisibility(View.INVISIBLE); + // hideSystemUI(); + // hideAdSlot(mNative_banner_container); + // hideAdSlot(mNative_banner_container_in_lines); // mNative_banner_container_in_lines.removeAllViews(); // mNative_banner_container_in_lines.removeAllViews(); - mNative_banner_container_in_lines.setBackgroundResource(R.color.transparent); + // mNative_banner_container_in_lines.setBackgroundResource(R.color.transparent); + hideAdSlot(mNative_banner_container_in_lines); if(height<=0){ return; } + Log.d(TAG, String.format("loadBannerAd:showNativeBannerInLines width %s, adY %s ",Constants.SCREEN_WIDTH_PIX-50, adY/Constants.ONE_DP_SIZE )); + if( adY >400*Constants.ONE_DP_SIZE){ + return; + } Log.d(TAG, String.format("loadBannerAd:showNativeBannerInLines width %s, height %s ,adY %s ",Constants.SCREEN_WIDTH_PIX-50,height,adY)); loadNativeBannerAd(mNative_banner_container_in_lines,BuildConfig.AD_SLOT_TOUTIAO_BANNER_NATIVE_ID); FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mNative_banner_container_in_lines.getLayoutParams(); params.setMargins(10*Constants.ONE_DP_SIZE, adY+5*Constants.ONE_DP_SIZE, 10*Constants.ONE_DP_SIZE, 0*Constants.ONE_DP_SIZE); params.width =-1; + params.height = (int) getResources().getDimension(R.dimen.nativeBannerHeight); mNative_banner_container_in_lines.setLayoutParams(params); - mNative_banner_container_in_lines.setVisibility(View.VISIBLE); + showAdSlot(mNative_banner_container_in_lines); } @Override public void showTopBanner( int adHeight,int adY) { - hideSystemUI(); - mTopBannerContainer.setVisibility(View.INVISIBLE);//翻页引起抖动 - // mTopBannerContainer.removeAllViews(); + // hideSystemUI(); + // mTopBannerContainer.setVisibility(View.INVISIBLE);//翻页引起抖动 + //mTopBannerContainer.removeAllViews(); + Log.d(TAG, String.format("loadBannerAd:showTopBanner width %s, height %s ,adY %s ",Constants.SCREEN_WIDTH_PIX-50,adHeight,adY)); + hideAdSlot(mNative_banner_container_in_lines); if(adHeight<=0){ return; } Log.d(TAG, String.format("loadBannerAd:showTopBanner width %s, height %s ,adY %s ",Constants.SCREEN_WIDTH_PIX-50,adHeight,adY)); - FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mTopBannerContainer.getLayoutParams(); - loadBannerAd(mTopBannerContainer,BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID, Constants.SCREEN_WIDTH_PIX-50,adHeight); + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mNative_banner_container_in_lines.getLayoutParams(); + loadBannerAd(mNative_banner_container_in_lines,BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID, Constants.SCREEN_WIDTH_PIX-50,adHeight); // loadNativeBannerAd(mTopBannerContainer,BuildConfig.AD_SLOT_TOUTIAO_BANNER_NATIVE_ID, 304,200); params.setMargins(10*Constants.ONE_DP_SIZE, adY -5*Constants.ONE_DP_SIZE, 10*Constants.ONE_DP_SIZE, 0); params.width =-1; - mTopBannerContainer.setLayoutParams(params); - mTopBannerContainer.setVisibility(View.VISIBLE); + params.height = (int) getResources().getDimension(R.dimen.topBannerHeight); + mNative_banner_container_in_lines.setLayoutParams(params); + showAdSlot(mNative_banner_container_in_lines); } final int contentAdHight=80; @Override - public void showAd(boolean showAd ,int adHeight,int adY) { + public void showStatusAd(boolean showAd ,int adHeight,int adY) { - hideSystemUI(); mShowAd =showAd; + // hideSystemUI(); + mShowAd =showAd; // mBannerContainer.setVisibility(View.GONE); - mNative_banner_container.setVisibility(View.INVISIBLE); - if(mTopBannerContainer.getVisibility() ==View.VISIBLE) { - mTopBannerContainer.setVisibility(View.INVISIBLE); - } - if(mNative_banner_container_in_lines.getVisibility() ==View.VISIBLE) { - mNative_banner_container_in_lines.setVisibility(View.INVISIBLE); - } + + + if(!Constants.SHOWAD){ return; } @@ -1144,25 +1189,30 @@ public class ReadActivity extends Activity_base implements AdInterface { - if(adHeight >contentAdHight){ - FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mNative_banner_container.getLayoutParams(); + // Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s",Constants.SCREEN_WIDTH_PIX-50,adHeight,adY)); if(adHeight>150*Constants.ONE_DP_SIZE && adY <400*Constants.ONE_DP_SIZE){ Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s,showAd %s",Constants.SCREEN_WIDTH_PIX-50,adHeight/Constants.ONE_DP_SIZE,adY/Constants.ONE_DP_SIZE, showAd)); +// hideAdSlot(mNative_banner_container); +// hideAdSlot(mTopBannerContainer); - mNative_banner_container.removeAllViews(); - mNative_banner_container.setBackgroundResource(R.color.transparent); + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mNative_status_banner_container.getLayoutParams(); + hideAdSlot(mNative_status_banner_container); - loadNativeBannerAd(mNative_banner_container,BuildConfig.AD_SLOT_TOUTIAO_BANNER_NATIVE_ID); + // mNative_banner_container_in_lines.removeAllViews(); + // mNative_banner_container_in_lines.setBackgroundResource(R.color.transparent); + + loadNativeBannerAd(mNative_status_banner_container,BuildConfig.AD_SLOT_TOUTIAO_BANNER_NATIVE_ID); params.setMargins(10*Constants.ONE_DP_SIZE, adY-20*Constants.ONE_DP_SIZE, 10*Constants.ONE_DP_SIZE, 50*Constants.ONE_DP_SIZE); - - mNative_banner_container.setLayoutParams(params); - mNative_banner_container.setVisibility(View.VISIBLE); - showTopBanner(0,0); + params.height=-1; + mNative_status_banner_container.setLayoutParams(params); + showAdSlot(mNative_status_banner_container); + // mNative_banner_container.setVisibility(View.VISIBLE); + // showTopBanner(0,0); }else { /* loadBannerAd(mBannerContainer,BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID, Constants.SCREEN_WIDTH_PIX-50,adHeight); @@ -1177,332 +1227,4 @@ public class ReadActivity extends Activity_base implements AdInterface { } - - /*private void loadBannerAd(String codeId,int width,int height) { - //step4:创建广告请求参数AdSlot,具体参数含义参考文档 - - - *//* if(height >390){ - width =(int)(height*1.78); - if(width > Constants.SCREEN_WIDTH_PIX-50 ){ - width= Constants.SCREEN_WIDTH_PIX-50; - height =(int)(width/1.78); - } - }else if(height >150){ - width =(int)(height*1.78); - }*//* - width =990; - //height=150; - height=height > 500 ? 500: height; - Log.d(TAG, String.format("loadBannerAd:finial width %s, height %s ",width,height)); - AdSlot adSlot = new AdSlot.Builder() - .setCodeId(codeId) //广告位id - .setSupportDeepLink(true) - .setImageAcceptedSize(width, height) - .build(); - //step5:请求广告,对请求回调的广告作渲染处理 - mTTAdNative.loadBannerAd(adSlot, new TTAdNative.BannerAdListener() { - - @Override - public void onError(int code, String message) { - TToast.show(ReadActivity.this, "load error : " + code + ", " + message); - mBannerContainer.removeAllViews(); - } - - @Override - public void onBannerAdLoad(final TTBannerAd ad) { - if (ad == null) { - return; - } - View bannerView = ad.getBannerView(); - if (bannerView == null) { - return; - } - //设置轮播的时间间隔 间隔在30s到120秒之间的值,不设置默认不轮播 - ad.setSlideIntervalTime(30 * 1000); - mBannerContainer.removeAllViews(); - mBannerContainer.addView(bannerView); - //设置广告互动监听回调 - ad.setBannerInteractionListener(new TTBannerAd.AdInteractionListener() { - @Override - public void onAdClicked(View view, int type) { - TToast.show(ReadActivity.this, "广告被点击"); - } - - @Override - public void onAdShow(View view, int type) { - TToast.show(ReadActivity.this, "广告展示"); - } - }); - //(可选)设置下载类广告的下载监听 - bindDownloadListener(ad); - //在banner中显示网盟提供的dislike icon,有助于广告投放精准度提升 - ad.setShowDislikeIcon(new TTAdDislike.DislikeInteractionCallback() { - @Override - public void onSelected(int position, String value) { - TToast.show(ReadActivity.this, "点击 " + value); - //用户选择不喜欢原因后,移除广告展示 - mBannerContainer.removeAllViews(); - } - - @Override - public void onCancel() { - TToast.show(ReadActivity.this, "点击取消 "); - } - }); - - //获取网盟dislike dialog,您可以在您应用中本身自定义的dislike icon 按钮中设置 mTTAdDislike.showDislikeDialog(); - *//*mTTAdDislike = ad.getDislikeDialog(new TTAdDislike.DislikeInteractionCallback() { - @Override - public void onSelected(int position, String value) { - TToast.show(mContext, "点击 " + value); - } - - @Override - public void onCancel() { - TToast.show(mContext, "点击取消 "); - } - }); - if (mTTAdDislike != null) { - XXX.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - mTTAdDislike.showDislikeDialog(); - } - }); - } *//* - - } - }); - } - private boolean mHasShowDownloadActive = false; - private void bindDownloadListener(TTBannerAd ad) { - ad.setDownloadListener(new TTAppDownloadListener() { - @Override - public void onIdle() { - TToast.show(ReadActivity.this, "点击图片开始下载", Toast.LENGTH_LONG); - } - - @Override - public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { - if (!mHasShowDownloadActive) { - mHasShowDownloadActive = true; - TToast.show(ReadActivity.this, "下载中,点击图片暂停", Toast.LENGTH_LONG); - } - } - - @Override - public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { - TToast.show(ReadActivity.this, "下载暂停,点击图片继续", Toast.LENGTH_LONG); - } - - @Override - public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { - TToast.show(ReadActivity.this, "下载失败,点击图片重新下载", Toast.LENGTH_LONG); - } - - @Override - public void onInstalled(String fileName, String appName) { - TToast.show(ReadActivity.this, "安装完成,点击图片打开", Toast.LENGTH_LONG); - } - - @Override - public void onDownloadFinished(long totalBytes, String fileName, String appName) { - TToast.show(ReadActivity.this, "点击图片安装", Toast.LENGTH_LONG); - } - }); - } - -*/ - - /* //-------------native toutiao ad - private void loadNativeBannerAd(String codeId) { - //step4:创建广告请求参数AdSlot,注意其中的setNativeAdtype方法,具体参数含义参考文档 - final AdSlot adSlot = new AdSlot.Builder() - .setCodeId(codeId) - .setSupportDeepLink(true) - .setImageAcceptedSize(690, 388) - .setNativeAdType(AdSlot.TYPE_BANNER) //请求原生广告时候,请务必调用该方法,设置参数为TYPE_BANNER或TYPE_INTERACTION_AD - .setAdCount(1) - .build(); - - //step5:请求广告,对请求回调的广告作渲染处理 - mTTAdNative.loadNativeAd(adSlot, new TTAdNative.NativeAdListener() { - @Override - public void onError(int code, String message) { - TToast.show(ReadActivity.this, "load error : " + code + ", " + message); - } - - @Override - public void onNativeAdLoad(List ads) { - if (ads.get(0) == null) { - return; - } - View bannerView = LayoutInflater.from(ReadActivity.this).inflate(R.layout.ad_toutiao_native_ad , mBannerContainer, false); - if (bannerView == null) { - return; - } - if (mCreativeButton != null) { - //防止内存泄漏 - mCreativeButton = null; - } - mBannerContainer.removeAllViews(); - mBannerContainer.addView(bannerView); - //绑定原生广告的数据 - setAdData(bannerView, ads.get(0)); - } - }); - } - - @SuppressWarnings("RedundantCast") - private void setAdData(View nativeView, TTNativeAd nativeAd) { - ((TextView) nativeView.findViewById(R.id.tv_native_ad_title)).setText(nativeAd.getTitle()); - ((TextView) nativeView.findViewById(R.id.tv_native_ad_desc)).setText(nativeAd.getDescription()); - ImageView imgDislike = nativeView.findViewById(R.id.img_native_dislike); - bindDislikeAction(nativeAd, imgDislike); - if (nativeAd.getImageList() != null && !nativeAd.getImageList().isEmpty()) { - TTImage image = nativeAd.getImageList().get(0); - if (image != null && image.isValid()) { - mAQuery.id(nativeView.findViewById(R.id.iv_native_image)).image(image.getImageUrl()); - } - } - TTImage icon = nativeAd.getIcon(); - if (icon != null && icon.isValid()) { - ImageOptions options = new ImageOptions(); - mAQuery.id((nativeView.findViewById(R.id.iv_native_icon))).image(icon.getImageUrl(), options); - } - mCreativeButton = (Button) nativeView.findViewById(R.id.btn_native_creative); - //可根据广告类型,为交互区域设置不同提示信息 - switch (nativeAd.getInteractionType()) { - case TTAdConstant.INTERACTION_TYPE_DOWNLOAD: - //如果初始化ttAdManager.createAdNative(getApplicationContext())没有传入activity 则需要在此传activity,否则影响使用Dislike逻辑 - nativeAd.setActivityForDownloadApp(this); - mCreativeButton.setVisibility(View.VISIBLE); - nativeAd.setDownloadListener(mDownloadListener); // 注册下载监听器 - break; - case TTAdConstant.INTERACTION_TYPE_DIAL: - mCreativeButton.setVisibility(View.VISIBLE); - mCreativeButton.setText("立即拨打"); - break; - case TTAdConstant.INTERACTION_TYPE_LANDING_PAGE: - case TTAdConstant.INTERACTION_TYPE_BROWSER: - mCreativeButton.setVisibility(View.VISIBLE); - mCreativeButton.setText("查看详情"); - break; - default: - mCreativeButton.setVisibility(View.GONE); - TToast.show(ReadActivity.this, "交互类型异常"); - } - - //可以被点击的view, 也可以把nativeView放进来意味整个广告区域可被点击 - List clickViewList = new ArrayList<>(); - clickViewList.add(nativeView); - - //触发创意广告的view(点击下载或拨打电话) - List creativeViewList = new ArrayList<>(); - //如果需要点击图文区域也能进行下载或者拨打电话动作,请将图文区域的view传入 - //creativeViewList.add(nativeView); - creativeViewList.add(mCreativeButton); - - //重要! 这个涉及到广告计费,必须正确调用。convertView必须使用ViewGroup。 - nativeAd.registerViewForInteraction((ViewGroup) nativeView, clickViewList, creativeViewList, imgDislike, new TTNativeAd.AdInteractionListener() { - @Override - public void onAdClicked(View view, TTNativeAd ad) { - if (ad != null) { - TToast.show(ReadActivity.this, "广告" + ad.getTitle() + "被点击"); - } - } - - @Override - public void onAdCreativeClick(View view, TTNativeAd ad) { - if (ad != null) { - TToast.show(ReadActivity.this, "广告" + ad.getTitle() + "被创意按钮被点击"); - } - } - - @Override - public void onAdShow(TTNativeAd ad) { - if (ad != null) { - TToast.show(ReadActivity.this, "广告" + ad.getTitle() + "展示"); - } - } - }); - - } - - //接入网盟的dislike 逻辑,有助于提示广告精准投放度 - private void bindDislikeAction(TTNativeAd ad, View dislikeView) { - final TTAdDislike ttAdDislike = ad.getDislikeDialog(this); - if (ttAdDislike != null) { - ttAdDislike.setDislikeInteractionCallback(new TTAdDislike.DislikeInteractionCallback() { - @Override - public void onSelected(int position, String value) { - mBannerContainer.removeAllViews(); - } - - @Override - public void onCancel() { - - } - }); - } - dislikeView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (ttAdDislike != null) - ttAdDislike.showDislikeDialog(); - } - }); - } - - private final TTAppDownloadListener mDownloadListener = new TTAppDownloadListener() { - @Override - public void onIdle() { - if (mCreativeButton != null) { - mCreativeButton.setText("开始下载"); - } - } - - - @Override - public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { - if (mCreativeButton != null) { - if (totalBytes <= 0L) { - mCreativeButton.setText("下载中 percent: 0"); - } else { - mCreativeButton.setText("下载中 percent: " + (currBytes * 100 / totalBytes)); - } - } - } - - - @Override - public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { - if (mCreativeButton != null) { - mCreativeButton.setText("下载暂停 percent: " + (currBytes * 100 / totalBytes)); - } - } - - @Override - public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { - if (mCreativeButton != null) { - mCreativeButton.setText("重新下载"); - } - } - - @Override - public void onInstalled(String fileName, String appName) { - if (mCreativeButton != null) { - mCreativeButton.setText("点击打开"); - } - } - - @Override - public void onDownloadFinished(long totalBytes, String fileName, String appName) { - if (mCreativeButton != null) { - mCreativeButton.setText("点击安装"); - } - } - };*/ - } diff --git a/zhuike/src/main/java/com/novelbook/android/netutils/NetUtil.java b/zhuike/src/main/java/com/novelbook/android/netutils/NetUtil.java index a659221..5a9072c 100644 --- a/zhuike/src/main/java/com/novelbook/android/netutils/NetUtil.java +++ b/zhuike/src/main/java/com/novelbook/android/netutils/NetUtil.java @@ -373,8 +373,11 @@ public class NetUtil { Constants.AD_SPLASH_INVTERVAL =(long) 1000 * ads.getInt("splashInterval"); Constants.AD_SPLASH_PAGES = ads.getInt("splashPageCount"); Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT = ads.getInt("times4ChapterTopBanner"); - Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT = ads.getInt("times4ChapterContentBanner"); //TODO: put it in server + Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT = ads.getInt("times4ChapterContentBanner"); + if(Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT >1 && Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT == Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT ){ + Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT--; //顶部广告和页内广告要错开 + } Log.d(TAG, String.format("initHostConstants: Constants.AD_SPLASH_INVTERVAL %s,AD_SPLASH_PAGES %s",Constants.AD_SPLASH_INVTERVAL,Constants.AD_SPLASH_PAGES )); // try { diff --git a/zhuike/src/main/java/com/novelbook/android/utils/AdInterface.java b/zhuike/src/main/java/com/novelbook/android/utils/AdInterface.java index cab6831..431dfe1 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/AdInterface.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/AdInterface.java @@ -6,7 +6,7 @@ public interface AdInterface { * @param height * @param adY */ - public void showAd(boolean showAd,int height,int adY); + public void showStatusAd(boolean showAd,int height,int adY); public void showTopBanner(int height,int adY); public void showNativeBannerInLines(int height,int adY); public void hideSystemUI(); @@ -16,5 +16,5 @@ public interface AdInterface { public void showLoading(boolean isShow); public void hideReadSetting(); public void showReadSetting(); - public void hideAds(); + public void hideStatusAds(); } diff --git a/zhuike/src/main/java/com/novelbook/android/utils/PageFactory.java b/zhuike/src/main/java/com/novelbook/android/utils/PageFactory.java index bc214f1..d03ee7d 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/PageFactory.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/PageFactory.java @@ -5,6 +5,7 @@ import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SyncAdapterType; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; @@ -194,6 +195,12 @@ public class PageFactory implements ChangeSource{ return mAd; } + //页面宽 + private int mAdHeight; + //页面高 + private int mAdY; + private enum AdTpye {TOPBANNER,NATIVEINLINES,CHAPTEREND;}; + private AdTpye mAdType = AdTpye.TOPBANNER; public void setAd(AdInterface ad){ mAd =ad; } @@ -246,7 +253,7 @@ public class PageFactory implements ChangeSource{ // Log.d(TAG, "loadBannerAd: set banner gone 0, showingStatusAd " +showingStatusAd); if(showingStatusAd) { if (mAd != null) { - mAd.hideAds(); + mAd.hideStatusAds(); // Log.d(TAG, "loadBannerAd: set banner gone 1"); } } @@ -481,6 +488,10 @@ public class PageFactory implements ChangeSource{ page.setPageNo(pageNo); chaptPages.add(page); length= page.getEnd(); + if(length >=chars.length){ + if(page.getLines().size()< mLineCount/2) + page.setTopBannerHeight(0); + } // Log.d(TAG, String.format("prepare book build page %s ready for chapter %s,cost %s",pageNo, chaptId,new Date().getTime()-starttime1)); } // Log.d(TAG, String.format(" prepare book build pages ready for chapter %s,cost %s", chaptId,new Date().getTime()-starttime)); @@ -490,43 +501,44 @@ public class PageFactory implements ChangeSource{ - public TRPage getNextChapterPage(int chaptId,long position,int pageNo){ - mBookUtil.setPostition(chaptId,position); + public TRPage getNextChapterPage(int chaptId,long position,int pageNo) { + mBookUtil.setPostition(chaptId, position); TRPage trPage = new TRPage(); totalPageCnt++; - float bannerH =0; - if(Constants.SHOWAD && pageNo >1) { - if (Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT >0 && totalPageCnt % Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT == 0) { - bannerH = mContext.getResources().getDimension(R.dimen.topBannerHeight) +0*Constants.ONE_DP_SIZE; - trPage.setTopBannerHeight((int)bannerH); - } - Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT=4; - if (Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT >0 && totalPageCnt % Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT == 0) { - bannerH = mContext.getResources().getDimension(R.dimen.nativeBannerHeight) +4*Constants.ONE_DP_SIZE; - calculateLineCount(); - int adLines = (int) ( bannerH / ((m_fontSize + lineSpace))); + float bannerH = 0; + // Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT = 4; + if (Constants.SHOWAD && pageNo > 1) { + // Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT = 1; - int adLine = new Random().nextInt( mLineCount -adLines -1); - adLine =adLine 0) { - trPage.setNativeBannerStartLine(adLine); - //trPage.setNativeBannerHeight(Constants.AD_CHAPT_NATIVE_BANNER_HEIGHT); - trPage.setNativeBannerHeight((int)bannerH); - } + if (Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT > 0 && totalPageCnt % Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT == 0) { + bannerH = mContext.getResources().getDimension(R.dimen.nativeBannerHeight) + 4 * Constants.ONE_DP_SIZE; + calculateLineCount(); + int adLines = (int) (bannerH / ((m_fontSize + lineSpace))); + + int adLine = new Random().nextInt(mLineCount - adLines - 1); + adLine = adLine < mLineCount ? adLine : mLineCount - 2; + if (adLine > 0) { + trPage.setNativeBannerStartLine(adLine); + //trPage.setNativeBannerHeight(Constants.AD_CHAPT_NATIVE_BANNER_HEIGHT); + trPage.setNativeBannerHeight((int) bannerH); + } Log.d(TAG, String.format("getNextChapterPage: pageNo %s, topBannerHeight %s, adLines %s,adLine %s, nativeBannerHeight %s", - pageNo,trPage.getTopBannerHeight(),adLines,adLine,trPage.getNativeBannerHeight())); + pageNo, trPage.getTopBannerHeight(), adLines, adLine, trPage.getNativeBannerHeight())); + } else if (Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT > 0 && totalPageCnt % Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT == 0) { + bannerH = mContext.getResources().getDimension(R.dimen.topBannerHeight) + 0 * Constants.ONE_DP_SIZE; + trPage.setTopBannerHeight((int) bannerH); } } trPage.setPageNo(pageNo); - trPage.setBegin(position +1); + trPage.setBegin(position + 1); - // Log.d(TAG,"page postion next begin:" + (position + 1) + ""); - trPage.setLines(getNextLines(chaptId,trPage)); - if(trPage.getNativeBannerStartLine() >0 && trPage.getNativeBannerStartLine() >= trPage.getLines().size()){ - trPage.setNativeBannerStartLine(trPage.getLines().size()-1); + // Log.d(TAG,"page postion next begin:" + (position + 1) + ""); + trPage.setLines(getNextLines(chaptId, trPage)); + if (trPage.getNativeBannerStartLine() > 0 && trPage.getNativeBannerStartLine() >= trPage.getLines().size()) { + trPage.setNativeBannerStartLine(trPage.getLines().size() - 1); } - // Log.d(TAG,"page postion next end:" +mBookUtil.getPosition() + ""); + // Log.d(TAG,"page postion next end:" +mBookUtil.getPosition() + ""); trPage.setEnd(mBookUtil.getPosition(chaptId)); return trPage; } @@ -877,7 +889,7 @@ private void hideSysUI(){ while(!showingStatusAd || new Date().getTime() - showStatusAdTime > 2000) { showingStatusAd =true; showStatusAdTime = new Date().getTime(); - showAd((int) adHeight, (int) adY); + showStatusAd((int) adHeight, (int) adY); Log.d(TAG, "loadBannerAd: AD is requested, mStatus:" +mStatus); } @@ -923,6 +935,9 @@ private void hideSysUI(){ } + + + //上次翻书时间 private long lastPageTime; @@ -950,13 +965,12 @@ private void hideSysUI(){ }*/ - - handler.sendEmptyMessage(MSG_HID_AD); - - if(showAd && mAd!=null){ - // handler.sendEmptyMessage(MSG_HID_AD); + handler.sendEmptyMessage(MSG_HID_AD); + //handler.sendEmptyMessage(MSG_HID_AD); handler.sendEmptyMessage(MSG_HIDEPROGRESS); + mAdHeight=0; + mAdY=0; } mStatus =Status.FINISH; // Log.d(TAG, String.format(" prepare book onDraw chapter %s, getChapters().size() %s ",currentChapter ,getChapters().size() ) ); @@ -1034,8 +1048,12 @@ private void hideSysUI(){ if(showAd && mAd!=null){ - mAd.showTopBanner(trPage.getTopBannerHeight(),(int)(marginHeight)); - mAd.showNativeBannerInLines(0,0); + mAdHeight=trPage.getTopBannerHeight(); + mAdY=(int)marginHeight; + mAdType =AdTpye.TOPBANNER; + + // mAd.showTopBanner(trPage.getTopBannerHeight(),(int)(marginHeight)); + // mAd.showNativeBannerInLines(0,0); } if (m_lines.size() > 0) { @@ -1053,9 +1071,12 @@ private void hideSysUI(){ if(lineNo==trPage.getNativeBannerStartLine()){ if(showAd && mAd!=null){ - mAd.showNativeBannerInLines(trPage.getNativeBannerHeight(),(int)y); + mAdHeight=trPage.getNativeBannerHeight(); + mAdY=(int)y; + mAdType =AdTpye.NATIVEINLINES; + // mAd.showNativeBannerInLines(trPage.getNativeBannerHeight(),(int)y); } - y+=trPage.getNativeBannerHeight() +m_fontSize; + y+=trPage.getNativeBannerHeight() +m_fontSize*1.1; Log.d(TAG, String.format("onDraw showNativeBannerInLines : lineno %s,strline %s ",lineNo,strLine)); }else { @@ -1085,8 +1106,11 @@ private void hideSysUI(){ float adHeight = mHeight -y - space -marginHeight-statusMarginBottom; float adY =y +space; - if(showAd && trPage.getNativeBannerHeight()==0) { - showAd((int) adHeight, (int) adY); + if(mAdHeight ==0 && showAd ) { + mAdY=(int)adY; + mAdHeight =(int) adHeight; + mAdType =AdTpye.CHAPTEREND; + // showAd((int) adHeight, (int) adY); Log.d(TAG, "loadBannerAd: AD is requested, adHeight "+adHeight); } // Log.d(TAG,String.format("ad + statusMarginBottom %s ",200+ statusMarginBottom)); @@ -1176,13 +1200,24 @@ private void hideSysUI(){ } } - private void showAd(int adHeight,int adY) { + public void showAd(){ + Log.d(TAG, String.format("loadBannerAd showAd: mAdType %s",mAdType)); + if(mAdType ==AdTpye.TOPBANNER){ + mAd.showTopBanner(mAdHeight,mAdY); + }else if(mAdType == AdTpye.NATIVEINLINES){ + mAd.showNativeBannerInLines(mAdHeight,mAdY); + }else if(mAdType == AdTpye.CHAPTEREND){ + mAd.showNativeBannerInLines(mAdHeight,mAdY); + } + + } + private void showStatusAd(int adHeight,int adY) { boolean showAd = showingStatusAd || getCurrentPage()==null || getCurrentPage().getPageNo() > 1; // boolean showAd =true; if (mAd != null) { - mAd.showAd(showAd,adHeight, adY); + mAd.showStatusAd(showAd,adHeight, adY); } diff --git a/zhuike/src/main/res/layout/zactivity_read.xml b/zhuike/src/main/res/layout/zactivity_read.xml index 5c1ac6f..4185d15 100644 --- a/zhuike/src/main/res/layout/zactivity_read.xml +++ b/zhuike/src/main/res/layout/zactivity_read.xml @@ -91,31 +91,36 @@ - + diff --git a/zhuike/zhuike.iml b/zhuike/zhuike.iml index bbaaaa7..dc9cd64 100644 --- a/zhuike/zhuike.iml +++ b/zhuike/zhuike.iml @@ -106,17 +106,13 @@ - - - - @@ -126,7 +122,6 @@ -