diff --git a/zhuike/src/main/java/com/novelbook/android/ActivitySetup.java b/zhuike/src/main/java/com/novelbook/android/ActivitySetup.java index 08d10bb..ff8fd91 100644 --- a/zhuike/src/main/java/com/novelbook/android/ActivitySetup.java +++ b/zhuike/src/main/java/com/novelbook/android/ActivitySetup.java @@ -34,7 +34,8 @@ static final String TAG = ActivitySetup.class.getSimpleName(); TextView tvVersion; - + String mVersion =""; + String mCacheSize =""; @Override public int getLayoutRes() { @@ -43,8 +44,15 @@ static final String TAG = ActivitySetup.class.getSimpleName(); @Override protected void initViews() { - tvVersion.setText(CommonUtil.getVersion(this)); - tvCache.setText(FileUtils.getCachedSize()); + new Thread() { + @Override + public void run() { + super.run(); + mVersion =CommonUtil.getVersion(ActivitySetup.this); + mCacheSize =FileUtils.getCachedSize(); + handler.sendEmptyMessage(1); + } + }.start() ; } @@ -61,7 +69,8 @@ static final String TAG = ActivitySetup.class.getSimpleName(); @Override public void fillData() { - + tvVersion.setText(mVersion); + tvCache.setText(mCacheSize); 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 c6cb964..7625769 100644 --- a/zhuike/src/main/java/com/novelbook/android/Activity_base.java +++ b/zhuike/src/main/java/com/novelbook/android/Activity_base.java @@ -53,7 +53,10 @@ import com.novelbook.android.adapter.BookListAdapter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import butterknife.BindView; import butterknife.ButterKnife; @@ -500,52 +503,156 @@ public abstract class Activity_base extends AppCompatActivity { loadNativeBannerAd(bannerContainer,codeId,690,388); } - void loadNativeBannerAd(FrameLayout bannerContainer,String codeId,int width,int height) { - //step4:创建广告请求参数AdSlot,注意其中的setNativeAdtype方法,具体参数含义参考文档 - if(!Constants.SHOWAD || getTTAdNative()==null ){ + + // List toutiaoNati_Banner_AdCache = new ArrayList(); + Map > toutiaoNati_Banner_AdCache = new ConcurrentHashMap>(); + void loadNativeBannerAd(FrameLayout bannerContainer,String codeId,int width,int height) { + //step4:创建广告请求参数AdSlot,注意其中的setNativeAdtype方法,具体参数含义参考文档 + + if (!Constants.SHOWAD || getTTAdNative() == null) { return; } - if(bannerContainer==null) return; - final AdSlot adSlot = new AdSlot.Builder() - .setCodeId(codeId) - .setSupportDeepLink(true) - .setImageAcceptedSize(width, height) - .setNativeAdType(AdSlot.TYPE_BANNER) //请求原生广告时候,请务必调用该方法,设置参数为TYPE_BANNER或TYPE_INTERACTION_AD - .setAdCount(1) - .build(); - //step5:请求广告,对请求回调的广告作渲染处理 + + final String key =width+"_"+height; + List adCache =null; + if( toutiaoNati_Banner_AdCache.containsKey(key)){ + adCache = toutiaoNati_Banner_AdCache.get(key); + + } + final boolean isfillAd = adCache!=null &&adCache.size()>1; + + // final boolean isfillAd =!toutiaoNati_Banner_AdCache.containsKey(key); + + if(adCache!=null && isfillAd){ + Log.d(TAG, "loadNativeBannerAd: set ad from cache, cache Size " + adCache.size()); + setNativeBanner(bannerContainer, key); + if(adCache.size()>0){ + return; + } + + } + + + + final AdSlot adSlot = new AdSlot.Builder() + .setCodeId(codeId) + .setSupportDeepLink(true) + .setImageAcceptedSize(width, height) + .setNativeAdType(AdSlot.TYPE_BANNER) //请求原生广告时候,请务必调用该方法,设置参数为TYPE_BANNER或TYPE_INTERACTION_AD + .setAdCount(3) + .build(); + + //step5:请求广告,对请求回调的广告作渲染处理 getTTAdNative().loadNativeAd(adSlot, new TTAdNative.NativeAdListener() { - @Override - public void onError(int code, String message) { - // TToast.show(oContext, "load error : " + code + ", " + message); - Log.e(TAG, "loadBannerAd: " + code + ", " + message); - } + @Override + 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); + } - @Override - public void onNativeAdLoad(List ads) { - if (ads.get(0) == null||!mShowAd) { - return; - } - View bannerView = LayoutInflater.from(oContext).inflate(R.layout.ad_toutiao_native_ad , bannerContainer, false); - if (bannerView == null) { - return; - } - if (mCreativeButton != null) { - //防止内存泄漏 - mCreativeButton = null; - } - bannerContainer.removeAllViews(); - bannerContainer.addView(bannerView); - // bannerContainer.setVisibility(View.VISIBLE); - Log.d(TAG, "loadBannerAd: set banner VISIBLE,mShowAd " +mShowAd); - //绑定原生广告的数据 - setAdData(bannerContainer,bannerView, ads.get(0)); - // mTTAdNative.loadNativeAd(adSlot,null); - } - }); - } + @Override + public void onNativeAdLoad(List ads) { + bannerContainer.setBackgroundResource(R.color.transparent); + List tmp = new ArrayList(); + for (TTNativeAd ad : ads) { + if (ad != null) { + tmp.add(ad); + } + } + if (!toutiaoNati_Banner_AdCache.containsKey(key)) { + toutiaoNati_Banner_AdCache.put(key, tmp); + } else { + toutiaoNati_Banner_AdCache.get(key).addAll(tmp); + } + + boolean isfillAd1 =toutiaoNati_Banner_AdCache.get(key).size()>1; + Log.d(TAG, String.format("loadNativeBannerAd: ads count %s ,not null count %s,key %s,will fill ad %s", ads.size(), tmp.size(), key, isfillAd1)); + if (isfillAd1) { + setNativeBanner(bannerContainer, key); + }else{ + loadNativeBannerAd(bannerContainer, codeId, width, height); + Log.d(TAG, String.format("loadNativeBannerAd: load again ... containsKey(key) ? %s,ads size %s",toutiaoNati_Banner_AdCache.containsKey(key), + toutiaoNati_Banner_AdCache.containsKey(key)?toutiaoNati_Banner_AdCache.get(key).size() :0 + )); + } + /* if (!toutiaoNati_Banner_AdCache.containsKey(key)){ + loadNativeBannerAd(bannerContainer, codeId, width, height); + Log.d(TAG, String.format("loadNativeBannerAd: load again ... containsKey(key) ? %s,ads size %s",toutiaoNati_Banner_AdCache.containsKey(key), + toutiaoNati_Banner_AdCache.containsKey(key)?toutiaoNati_Banner_AdCache.get(key).size() :0 + )); + }*/ + + /* if (ads.get(0) == null || !mShowAd) { + return; + } + View bannerView = LayoutInflater.from(oContext).inflate(R.layout.ad_toutiao_native_ad, bannerContainer, false); + if (bannerView == null) { + return; + } + if (mCreativeButton != null) { + //防止内存泄漏 + mCreativeButton = null; + } + bannerContainer.removeAllViews(); + bannerContainer.addView(bannerView); + 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); + //绑定原生广告的数据 + setAdData(bannerContainer, bannerView, ads.get(0));*/ + // mTTAdNative.loadNativeAd(adSlot,null); + } + }); + } + void setNativeBanner(FrameLayout bannerContainer,String key ){ + + if(!toutiaoNati_Banner_AdCache.containsKey(key)){ + return; + } + List adCache = toutiaoNati_Banner_AdCache.get(key); + TTNativeAd nativeAd =null ; + int i=0; + for (TTNativeAd ad :adCache){ + if(ad!=null){ + nativeAd =ad; + break; + } + i++; + } + + if (nativeAd == null || !mShowAd) { + return; + } + adCache.remove(i); + if(adCache.size()>0) { + toutiaoNati_Banner_AdCache.put(key, adCache); + }else{ + toutiaoNati_Banner_AdCache.remove(key); + } + + View bannerView = LayoutInflater.from(oContext).inflate(R.layout.ad_toutiao_native_ad, bannerContainer, false); + if (bannerView == null) { + return; + } + if (mCreativeButton != null) { + //防止内存泄漏 + mCreativeButton = null; + } + + bannerContainer.removeAllViews(); + bannerContainer.addView(bannerView); + //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); + //绑定原生广告的数据 + setAdData(bannerContainer, bannerView, nativeAd); + // mTTAdNative.loadNativeAd(adSlot,null); + } @SuppressWarnings("RedundantCast") private void setAdData(FrameLayout bannerContainer,View nativeView, TTNativeAd nativeAd) { diff --git a/zhuike/src/main/java/com/novelbook/android/Fragments/BasicFragment.java b/zhuike/src/main/java/com/novelbook/android/Fragments/BasicFragment.java index 4ac9a22..17d8543 100644 --- a/zhuike/src/main/java/com/novelbook/android/Fragments/BasicFragment.java +++ b/zhuike/src/main/java/com/novelbook/android/Fragments/BasicFragment.java @@ -141,7 +141,7 @@ public abstract class BasicFragment extends Fragment { initData(); initViews(); initSwipeRefreshLayout(); - showBanner(mBannerContainer,260); + showBanner(mBannerContainer,(int) getResources().getDimension(R.dimen.topBannerHeight) ); return view; // Inflate the layout for this fragment diff --git a/zhuike/src/main/java/com/novelbook/android/MyApp.java b/zhuike/src/main/java/com/novelbook/android/MyApp.java index bba482f..8f77ab2 100644 --- a/zhuike/src/main/java/com/novelbook/android/MyApp.java +++ b/zhuike/src/main/java/com/novelbook/android/MyApp.java @@ -46,10 +46,16 @@ public class MyApp extends Application { initAD(); } private void initialConstants(){ - Config confg= Config.createConfig(this); - Constants.SEX = confg.getSexOption(); - Constants.SHOWAD =confg.isShowAd(); - Constants.PRE_LOAD_CHAPT =confg.isPreLoadChapter(); + Config config= Config.createConfig(this); + Constants.SEX = config.getSexOption(); + Constants.SHOWAD =config.isShowAd(); + Constants.PRE_LOAD_CHAPT =config.isPreLoadChapter(); + + Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT = config.getAdTopBannerRate(); + Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT= config.getAdNativeBannerInLinesRate() ; + + + Constants.ONE_DP_SIZE = (int)applicationContext.getResources().getDimension(R.dimen.one_dp); } private void initUmengApi(){ UMConfigure.init(applicationContext,UMConfigure.DEVICE_TYPE_PHONE,null); diff --git a/zhuike/src/main/java/com/novelbook/android/ReadActivity.java b/zhuike/src/main/java/com/novelbook/android/ReadActivity.java index 0f99efc..1ea7a2b 100644 --- a/zhuike/src/main/java/com/novelbook/android/ReadActivity.java +++ b/zhuike/src/main/java/com/novelbook/android/ReadActivity.java @@ -14,8 +14,10 @@ import android.os.Build; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; +import android.support.constraint.solver.GoalRow; import android.support.design.widget.AppBarLayout; import android.support.v7.widget.Toolbar; +import android.text.TextUtils; import android.util.Log; import android.view.Display; import android.view.KeyEvent; @@ -124,17 +126,24 @@ public class ReadActivity extends Activity_base implements AdInterface { @BindView(R.id.appbar) AppBarLayout appbar; + @BindView(R.id.tvUrl) + TextView tvUrl; @BindView(R.id.top_banner_container) FrameLayout mTopBannerContainer; @BindView(R.id.imgLoading) pl.droidsonroids.gif.GifImageView imgLoading; - + @BindView(R.id.frmAD) + FrameLayout frmAD; @BindView(R.id.native_banner_container ) FrameLayout mNative_banner_container; + @BindView(R.id.native_banner_container_in_lines ) + FrameLayout mNative_banner_container_in_lines; + @BindView(R.id.llShelfBottom) + LinearLayout llShelf; private Config config; private WindowManager.LayoutParams lp; private Novel book; @@ -468,6 +477,20 @@ public class ReadActivity extends Activity_base implements AdInterface { // Log.d(TAG, "canCancel: cancelPage canCancel ?" + pageFactory.canCancelPage()); return true;//pageFactory.canCancelPage(); } + + @Override + public void showAdAfterIni() { + if(frmAD.getVisibility()== View.INVISIBLE) + // frmAD.setVisibility(View.VISIBLE); + Log.d(TAG, "pageAnimation showAdAfterIni: "); + } + + @Override + public void hideAdBeforeIni() { + if(frmAD.getVisibility()== View.VISIBLE) + // frmAD.setVisibility(View.INVISIBLE); + Log.d(TAG, "pageAnimation hideAdBeforeIni: "); + } }); } @@ -567,12 +590,20 @@ public class ReadActivity extends Activity_base implements AdInterface { return true; }*/ + if(isShow){ + hideReadSetting(); + return true; + } if(!pageFactory.canExitSilent()){ if(book.isOnShelf()){ finish(); }else { - showNormalDialog(); + // showNormalDialog(); + Animation bottomAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_enter); + llShelf.startAnimation(bottomAnim); + llShelf.setVisibility(View.VISIBLE); + return true; } } @@ -822,22 +853,37 @@ public class ReadActivity extends Activity_base implements AdInterface { public void showReadSetting(){ isShow = true; // rl_progress.setVisibility(View.GONE); + Animation topAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_top_enter); + Animation botoomAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_exit); + + if (llShelf.getVisibility() == View.VISIBLE) { + llShelf.startAnimation(botoomAnim); + llShelf.setVisibility(View.GONE); + } if (isSpeaking){ - Animation topAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_top_enter); + rl_read_bottom.startAnimation(topAnim); rl_read_bottom.setVisibility(View.VISIBLE); }else { showSystemUI(); - Animation bottomAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_enter); - Animation topAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_top_enter); + // Animation bottomAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_enter); + rl_bottom.startAnimation(topAnim); appbar.startAnimation(topAnim); // ll_top.startAnimation(topAnim);// ll_top.setVisibility(View.VISIBLE); rl_bottom.setVisibility(View.VISIBLE); - appbar.setVisibility(View.VISIBLE); + + tvUrl.setVisibility(View.VISIBLE); + tvUrl.setText("abcd+" + pageFactory.getChapter().getChapterUrl()); + + if(pageFactory.getChapter()!=null && !TextUtils.isEmpty(pageFactory.getChapter().getChapterUrl())) { + tvUrl.setVisibility(View.VISIBLE); + tvUrl.setText(pageFactory.getChapter().getChapterUrl()); + } + } } /** @@ -861,6 +907,12 @@ public class ReadActivity extends Activity_base implements AdInterface { getWindow().setNavigationBarColor(getResources().getColor(R.color.read_dialog_bg)); } pageFactory.setBusy(false); + + if (llShelf.getVisibility() == View.VISIBLE) { + Animation topAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_exit); + llShelf.startAnimation(topAnim); + llShelf.setVisibility(View.GONE); + } } @Override @@ -918,13 +970,16 @@ public class ReadActivity extends Activity_base implements AdInterface { rl_read_bottom.setVisibility(View.GONE); // ll_top.setVisibility(View.GONE); appbar.setVisibility(View.GONE); + tvUrl.setVisibility(View.GONE); hideSystemUI(); + // Animation bottomAnim = AnimationUtils.loadAnimation(this, R.anim.dialog_enter); + } @OnClick({R.id.tv_progress, R.id.rl_progress, R.id.tv_pre, R.id.sb_progress, R.id.tv_next, R.id.tv_directory, R.id.tv_dayornight,R.id.tv_pagemode, R.id.tv_setting, R.id.bookpop_bottom, /*R.id.rl_bottom,*/R.id.tv_stop_read - ,R.id.btnRefresh}) + ,R.id.btnRefresh,R.id.btnCancel,R.id.btnShelf}) public void onClick(View view) { pageFactory.setPageWidget(bookpage); // if( pageFactory.isReady()) @@ -985,6 +1040,15 @@ public class ReadActivity extends Activity_base implements AdInterface { pageFactory.retryChapt(pageFactory.getCurrentChapter()); btnRefresh.setVisibility(View.GONE); break; + case R.id.btnShelf: + book.setOnShelf(true); + book.update(book.getId()); + finish(); + break; + case R.id.btnCancel: + finish(); + break; + } // hideSystemUI(); } @@ -1007,18 +1071,47 @@ 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 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); + // mNative_banner_container_in_lines.removeAllViews(); + // mNative_banner_container_in_lines.removeAllViews(); + mNative_banner_container_in_lines.setBackgroundResource(R.color.transparent); + if(height<=0){ + 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; + mNative_banner_container_in_lines.setLayoutParams(params); + mNative_banner_container_in_lines.setVisibility(View.VISIBLE); + + + + } @Override public void showTopBanner( int adHeight,int adY) { hideSystemUI(); - mTopBannerContainer.setVisibility(View.GONE); + mTopBannerContainer.setVisibility(View.INVISIBLE);//翻页引起抖动 + // mTopBannerContainer.removeAllViews(); 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, 690,adHeight); + loadBannerAd(mTopBannerContainer,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(20, adY-10, 20, 10); + 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); @@ -1031,41 +1124,52 @@ public class ReadActivity extends Activity_base implements AdInterface { public void showAd(boolean showAd ,int adHeight,int adY) { hideSystemUI(); mShowAd =showAd; - mBannerContainer.setVisibility(View.GONE); - mNative_banner_container.setVisibility(View.GONE); + // 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; } - Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s,showAd %s",Constants.SCREEN_WIDTH_PIX-50,adHeight,adY, showAd)); if(!showAd){ return; } - + // 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)); if(adHeight >contentAdHight){ - FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mBannerContainer.getLayoutParams(); + 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>500 && adY <500){ - loadNativeBannerAd(mNative_banner_container,BuildConfig.AD_SLOT_TOUTIAO_BANNER_NATIVE_ID); - params.setMargins(20, adY-30, 20, 150); - mNative_banner_container.setLayoutParams(params); - mNative_banner_container.setVisibility(View.VISIBLE); + 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)); + mNative_banner_container.removeAllViews(); + mNative_banner_container.setBackgroundResource(R.color.transparent); + + loadNativeBannerAd(mNative_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); }else { - loadBannerAd(mBannerContainer,BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID, Constants.SCREEN_WIDTH_PIX-50,adHeight); + /* loadBannerAd(mBannerContainer,BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID, Constants.SCREEN_WIDTH_PIX-50,adHeight); params.setMargins(20, adY-10, 20, 15); mBannerContainer.setLayoutParams(params); - mBannerContainer.setVisibility(View.VISIBLE); - } + mBannerContainer.setVisibility(View.VISIBLE);*/ + } 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 de97b2f..a659221 100644 --- a/zhuike/src/main/java/com/novelbook/android/netutils/NetUtil.java +++ b/zhuike/src/main/java/com/novelbook/android/netutils/NetUtil.java @@ -372,7 +372,8 @@ 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 Log.d(TAG, String.format("initHostConstants: Constants.AD_SPLASH_INVTERVAL %s,AD_SPLASH_PAGES %s",Constants.AD_SPLASH_INVTERVAL,Constants.AD_SPLASH_PAGES )); @@ -393,10 +394,13 @@ public class NetUtil { TTAdManagerHolder.init(MyApp.applicationContext); } } + Config config = Config.getInstance(); Constants.SHOWAD = ads.getBoolean("showAdsense"); - Constants.PRE_LOAD_CHAPT= false;//jsonObject.getBoolean("preLoadChapter"); - Config.getInstance().setShowAd(Constants.SHOWAD); - Config.getInstance().setPreLoadChapter(Constants.PRE_LOAD_CHAPT); + Constants.PRE_LOAD_CHAPT= jsonObject.getBoolean("preLoadChapter"); + config.setShowAd(Constants.SHOWAD); + config.setPreLoadChapter(Constants.PRE_LOAD_CHAPT); + config.setAdTopBannerRate( Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT); + config.setAdNativeBannerInLinesRate(Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT); Log.d(TAG, String.format("initHostConstants: Constants.AD_SPLASH_INVTERVAL %s,AD_SPLASH_PAGES %s, Constants.SHOWAD %s,Constants.AD_PRE_LOAD_CHAPT %s " ,Constants.AD_SPLASH_INVTERVAL,Constants.AD_SPLASH_PAGES, Constants.SHOWAD, Constants.PRE_LOAD_CHAPT )); 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 f271d32..cab6831 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/AdInterface.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/AdInterface.java @@ -8,6 +8,7 @@ public interface AdInterface { */ public void showAd(boolean showAd,int height,int adY); public void showTopBanner(int height,int adY); + public void showNativeBannerInLines(int height,int adY); public void hideSystemUI(); public void showRefresh(int visible); public void showProgressbar(boolean dismissAble,String msg); @@ -15,4 +16,5 @@ public interface AdInterface { public void showLoading(boolean isShow); public void hideReadSetting(); public void showReadSetting(); + public void hideAds(); } diff --git a/zhuike/src/main/java/com/novelbook/android/utils/Config.java b/zhuike/src/main/java/com/novelbook/android/utils/Config.java index bd9ac6a..9907b8d 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/Config.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/Config.java @@ -43,6 +43,8 @@ public class Config { public final static boolean SHOW_AD = false; public final static String SHOW_AD_KEY = "showAd"; private static final String PRE_LOAD_CHAPT_KEY ="preloadchapt" ; + private static final String AD_TOP_BANNER_SEEDS_KEY ="topbannerseeds" ; + private static final String AD_NATVIE_BANNER_IN_LINES_SEEDS_KEY ="nativebannerinlinesseeds" ; private Context mContext; private static Config config; private SharedPreferences sp; @@ -224,4 +226,19 @@ public class Config { public void setPreLoadChapter(boolean isPreloadChapt){ sp.edit().putBoolean(PRE_LOAD_CHAPT_KEY,isPreloadChapt).commit(); } + public int getAdTopBannerRate(){ + return sp.getInt(AD_TOP_BANNER_SEEDS_KEY,0); + } + + public void setAdTopBannerRate(int pageCnt){ + sp.edit().putInt(AD_TOP_BANNER_SEEDS_KEY,pageCnt).commit(); + } + + public int getAdNativeBannerInLinesRate(){ + return sp.getInt(AD_NATVIE_BANNER_IN_LINES_SEEDS_KEY,0); + } + + public void setAdNativeBannerInLinesRate(int pageCnt){ + sp.edit().putInt(AD_NATVIE_BANNER_IN_LINES_SEEDS_KEY,pageCnt).commit(); + } } diff --git a/zhuike/src/main/java/com/novelbook/android/utils/Constants.java b/zhuike/src/main/java/com/novelbook/android/utils/Constants.java index 3d694ae..29ebdb2 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/Constants.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/Constants.java @@ -1,6 +1,7 @@ package com.novelbook.android.utils; +import com.novelbook.android.R; import com.novelbook.android.bean.ProgressType; import java.util.List; @@ -13,14 +14,17 @@ public class Constants { public static final int MAXAGE_MAX =60*60*24*28; //28 天 ; public static final int MAXAGE_MULU = 60*60*24*7*2; //2周;; public static final String VERSION_ADDRESS ="version.xml"; + public static boolean AD_BACKGROUND_TRANSPARENT =true ; public static boolean SHOWAD =false ; public static boolean PRE_LOAD_CHAPT = false; public static long AD_SPLASH_INVTERVAL = 5*60*1000;//间隔5分钟 public static int AD_SPLASH_PAGES = 15; public static int SCREEN_HEIGHT_PIX =1920 ; public static int SCREEN_WIDTH_PIX = 1080; - public static int AD_CHAPT_TOP_BANNER_HEIGHT=200; - public static int AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT=1; //每几页show topbanner + public static int AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT=0; //每几页show topbanner + public static int AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT=0; //每几页show nativebanner + public static int ONE_DP_SIZE =1; + public static long LAST_G = 0;//主目录API上次访问时间 public static long MAXAGE_G = 3600;//主目录API上次访问时间 public static String[] HOT_KEYS_VALUE = {}; 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 70ddb2f..bc214f1 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/PageFactory.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/PageFactory.java @@ -48,6 +48,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.concurrent.ConcurrentHashMap; //import static com.baidu.tts.loopj.AsyncHttpClient.log; @@ -188,6 +189,7 @@ public class PageFactory implements ChangeSource{ private final int MSG_NEXTPAGE_FAIL=4; private final int MSG_HID_AD=5; private final int MSG_SHOW_LOADING=5; + private static int totalPageCnt =0; public AdInterface getmAd() { return mAd; } @@ -237,19 +239,17 @@ public class PageFactory implements ChangeSource{ // mAd.hideProgressbar(); mAd.showLoading(false); - Log.d(TAG, "showloading: case MSG_HIDEPROGRESS: false" ); + // Log.d(TAG, "showloading: case MSG_HIDEPROGRESS: false" ); } }else if (msg.what == MSG_HID_AD) { - /* Log.d(TAG, "loadBannerAd: set banner gone 0, showingStatusAd " +showingStatusAd); + // Log.d(TAG, "loadBannerAd: set banner gone 0, showingStatusAd " +showingStatusAd); if(showingStatusAd) { if (mAd != null) { - mAd.showAd(false, 0, 0); - Log.d(TAG, "loadBannerAd: set banner gone 1"); + mAd.hideAds(); + // Log.d(TAG, "loadBannerAd: set banner gone 1"); } - showingStatusAd = false; - Log.d(TAG, "loadBannerAd: set banner gone 2"); - }*/ + } }else if (msg.what == MSG_SHOW_LOADING) { @@ -492,15 +492,40 @@ public class PageFactory implements ChangeSource{ public TRPage getNextChapterPage(int chaptId,long position,int pageNo){ mBookUtil.setPostition(chaptId,position); - int topAdH =0; - if(Constants.SHOWAD && pageNo >1 && pageNo%Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT==0){ - topAdH =Constants.AD_CHAPT_TOP_BANNER_HEIGHT; - } + 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))); + + 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); + } + Log.d(TAG, String.format("getNextChapterPage: pageNo %s, topBannerHeight %s, adLines %s,adLine %s, nativeBannerHeight %s", + pageNo,trPage.getTopBannerHeight(),adLines,adLine,trPage.getNativeBannerHeight())); + } + } + trPage.setPageNo(pageNo); trPage.setBegin(position +1); - trPage.setTopBannerHeight(topAdH); + // Log.d(TAG,"page postion next begin:" + (position + 1) + ""); - trPage.setLines(getNextLines(chaptId,topAdH)); + 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() + ""); trPage.setEnd(mBookUtil.getPosition(chaptId)); return trPage; @@ -741,8 +766,8 @@ private void hideSysUI(){ } private void drawStatus(){ if(mBookPageWidget!=null) { - drawStatus(mBookPageWidget.getCurPage()); - drawStatus(mBookPageWidget.getNextPage()); + // drawStatus(mBookPageWidget.getCurPage()); + drawStatus(mBookPageWidget.getNextPage()); } } private String loadingTxt =""; @@ -872,8 +897,9 @@ private void hideSysUI(){ fontMetrics = waitPaint.getFontMetricsInt(); baseline = baseline +150; // 下面这行是实现水平居中,drawText对应改为传入targetRect.centerX() + String site = getSite().getName() ==null ? "":getSite().getName(); waitPaint.setTextAlign(Paint.Align.CENTER); - c.drawText("转向第三方网站 " + getSite().getName() , targetRect.centerX(), baseline, waitPaint); + c.drawText("正加载第三方网站 " +site, targetRect.centerX(), baseline, waitPaint); baseline+=40; c.drawText(url , targetRect.centerX(), baseline, waitPaint); @@ -904,17 +930,32 @@ private void hideSysUI(){ hideSysUI(); List m_lines = trPage.getLines(); // mAd.showRefresh(View.GONE); + + Log.d(TAG, String.format("onDraw: trPage pageno %s,showAd %s",trPage.getPageNo(),showAd)); + + if(m_lines.size()==0 ){ return; } + /* try { + throw new Exception("who's ad"); + }catch (Exception e){ + Log.e(TAG, "onDraw:showTopBanner ",e ); + }*/ /* Log.d(TAG, "showloading: ondraw showAd: " +showAd ); try{ throw new Exception("showloading: ondraw mStatus: " +mStatus ); }catch (Exception e){ Log.e(TAG, "showloading: ", e); }*/ + + + + handler.sendEmptyMessage(MSG_HID_AD); + + if(showAd && mAd!=null){ - handler.sendEmptyMessage(MSG_HID_AD); + // handler.sendEmptyMessage(MSG_HID_AD); handler.sendEmptyMessage(MSG_HIDEPROGRESS); } mStatus =Status.FINISH; @@ -992,35 +1033,51 @@ private void hideSysUI(){ paragraphSpace = prate * lineSpace; - if(mAd!=null){ - mAd.showTopBanner(trPage.getTopBannerHeight(),(int)(marginHeight + space)); + if(showAd && mAd!=null){ + mAd.showTopBanner(trPage.getTopBannerHeight(),(int)(marginHeight)); + mAd.showNativeBannerInLines(0,0); } - - - if (m_lines.size() > 0) { - float y = marginHeight +trPage.getTopBannerHeight(); + float y = marginHeight +(trPage.getTopBannerHeight() >0 ? trPage.getTopBannerHeight() :0); String lastLine =""; + int lineNo =0; for (String strLine : m_lines) { + lineNo++; // if(strLine.endsWith("\n")) { // if(strLine.charAt(strLine.length()-1) == ('\n' )) { // Log.d(TAG, strLine); // Log.d(TAG,"最后字符 。。。" +strLine.charAt(strLine.length()-1) + ""); lastLine =strLine; - if(strLine.length()>0 && ( strLine.charAt(strLine.length()-1) + "" ).equals("\n")){ - strLine =strLine.replace("\n",""); - y += space; - space =m_fontSize + paragraphSpace; - // Log.d( TAG ,String.format("prepare book 开始新段落 %s, y plus is %s" ,strLine, m_fontSize + paragraphSpace)); - }else{ - y += space; - space =m_fontSize + lineSpace; - // Log.d(TAG,String.format("prepare book %s,y plus is %s" ,strLine, m_fontSize + lineSpace)); + + if(lineNo==trPage.getNativeBannerStartLine()){ + if(showAd && mAd!=null){ + mAd.showNativeBannerInLines(trPage.getNativeBannerHeight(),(int)y); + } + y+=trPage.getNativeBannerHeight() +m_fontSize; + Log.d(TAG, String.format("onDraw showNativeBannerInLines : lineno %s,strline %s ",lineNo,strLine)); + + }else { + + if (strLine.length() > 0 && (strLine.charAt(strLine.length() - 1) + "").equals("\n")) { + strLine = strLine.replace("\n", ""); + + y += space; + space = m_fontSize + paragraphSpace; + + // Log.d( TAG ,String.format("prepare book 开始新段落 %s, y plus is %s" ,strLine, m_fontSize + paragraphSpace)); + } else { + y += space; + space = m_fontSize + lineSpace; + + // Log.d(TAG,String.format("prepare book %s,y plus is %s" ,strLine, m_fontSize + lineSpace)); + } } c.drawText(strLine, measureMarginWidth, y, mPaint); + // word.append(strLine); + } // Log.d(TAG,String.format("getNavigationBarSize mHeight is %s ,last line height %s, power %s, %s", @@ -1028,9 +1085,9 @@ private void hideSysUI(){ float adHeight = mHeight -y - space -marginHeight-statusMarginBottom; float adY =y +space; - if(showAd) { + if(showAd && trPage.getNativeBannerHeight()==0) { showAd((int) adHeight, (int) adY); - Log.d(TAG, "loadBannerAd: AD is requested"); + Log.d(TAG, "loadBannerAd: AD is requested, adHeight "+adHeight); } // Log.d(TAG,String.format("ad + statusMarginBottom %s ",200+ statusMarginBottom)); // Log.d(TAG,String.format("adHeight %s, adY %s",adHeight,adY)); @@ -1269,11 +1326,14 @@ private void hideSysUI(){ currentChaptPages = loadCurrentChapt(cancelPage.getChapterNo()); currentPage = getPageForBegin(cancelPage.getBegin()) ;// currentChaptPages.get(0); // currentPage = getPageForBegin(begin); - if (mBookPageWidget != null) { + /* if (mBookPageWidget != null) { currentPage(true); - } + }*/ } currentPage = cancelPage; + if (mBookPageWidget != null) { + currentPage(true); + } } public void prepareBook(Novel book){ @@ -1499,7 +1559,7 @@ private void hideSysUI(){ } boolean showChapTitleOnTopWhenNextPage =false; - public List getNextLines(int chaptId,int topAdHeight){ + public List getNextLines(int chaptId,TRPage trpage){ List lines = new ArrayList<>(); float width = 0; float height = 0; @@ -1509,8 +1569,9 @@ private void hideSysUI(){ if(mBookUtil.getPosition(chaptId)==0) { lines.add("\n");//lines.add("\n"); } - - calculateLineCount(topAdHeight); + int adHeight = trpage.getNativeBannerHeight()+trpage.getTopBannerHeight(); + calculateLineCount(adHeight ); + Log.d(TAG, String.format("getNextChapterPage getNextLines: pageNo %s, adHeight %s, totalLines %s",trpage.getPageNo(),adHeight,mLineCount)); while (mBookUtil.next(true,chaptId) != -1){ char word = (char) mBookUtil.next(false,chaptId); // Log.d(TAG, String.format(" loadchapt getNextLines(), chaptId %s, word '%s'", chaptId,word )); @@ -1530,7 +1591,7 @@ private void hideSysUI(){ line = ""; width = 0; height += paragraphSpace - lineSpace; - calculateLineCount(topAdHeight+height); + calculateLineCount(adHeight+height); if (lines.size()>= mLineCount +firstPageLine){ // Log.d(TAG,String.format("lines count limit a %s,lines size %s",mLineCount,lines.size())); line =""; @@ -1941,6 +2002,7 @@ private void hideSysUI(){ currentChaptPages =null; preChaptPages=null; nextChaptPages=null; + totalPageCnt=0; } public static Status getStatus(){ diff --git a/zhuike/src/main/java/com/novelbook/android/utils/TRPage.java b/zhuike/src/main/java/com/novelbook/android/utils/TRPage.java index c4a2599..976afb5 100644 --- a/zhuike/src/main/java/com/novelbook/android/utils/TRPage.java +++ b/zhuike/src/main/java/com/novelbook/android/utils/TRPage.java @@ -13,6 +13,8 @@ public class TRPage { private boolean isFirstPage; private List lines = new ArrayList<>(); private int topBannerHeight=0; + private int nativeBannerStartLine=0; + private int nativeBannerHeight=0; public TRPage( ){ } @@ -52,10 +54,21 @@ public class TRPage { isFirstPage = firstPage; } + public int getNativeBannerStartLine() { + return nativeBannerStartLine; + } + public void setNativeBannerStartLine(int nativeBannerStartLine) { + this.nativeBannerStartLine = nativeBannerStartLine; + } + public int getNativeBannerHeight() { + return nativeBannerHeight; + } - + public void setNativeBannerHeight(int nativeBannerHeight) { + this.nativeBannerHeight = nativeBannerHeight; + } public int getPageNo() { return pageNo; diff --git a/zhuike/src/main/java/com/novelbook/android/view/BookPageWidget.java b/zhuike/src/main/java/com/novelbook/android/view/BookPageWidget.java index acaf94b..4515e8b 100644 --- a/zhuike/src/main/java/com/novelbook/android/view/BookPageWidget.java +++ b/zhuike/src/main/java/com/novelbook/android/view/BookPageWidget.java @@ -415,6 +415,7 @@ public class BookPageWidget extends View { Boolean prePage(); Boolean nextPage(); void cancel(); + void showAdAfterAni(); } private void startAnimation(int delayMillis) { diff --git a/zhuike/src/main/java/com/novelbook/android/view/PageWidget.java b/zhuike/src/main/java/com/novelbook/android/view/PageWidget.java index b54681b..e89dcbf 100644 --- a/zhuike/src/main/java/com/novelbook/android/view/PageWidget.java +++ b/zhuike/src/main/java/com/novelbook/android/view/PageWidget.java @@ -102,6 +102,33 @@ public class PageWidget extends View { } } + /* public void setPageMode2(int pageMode){ + switch (pageMode){ + case PAGE_MODE_SIMULATION: + mAnimationProvider = new SimulationAnimation(mCurPageBitmap,mNextPageBitmap,mScreenWidth,mScreenHeight); + break; + case PAGE_MODE_COVER: + mAnimationProvider = new CoverAnimation(mCurPageBitmap,mNextPageBitmap,mScreenWidth,mScreenHeight); + break; + case PAGE_MODE_SLIDE: + mAnimationProvider = new SlideAnimation(mCurPageBitmap,mNextPageBitmap,mScreenWidth,mScreenHeight); + break; + case PAGE_MODE_NONE: + mAnimationProvider = new NoneAnimation(mCurPageBitmap,mNextPageBitmap,mScreenWidth,mScreenHeight); + break; + default: + mAnimationProvider = new SimulationAnimation(mCurPageBitmap,mNextPageBitmap,mScreenWidth,mScreenHeight); + } + } + + private Bitmap getCurPageBitmap(){ + return Bitmap.createBitmap(mScreenWidth, mScreenHeight, Bitmap.Config.RGB_565); + } + + private Bitmap getNextPageBitmap(){ + return mNextPageBitmap; + }*/ + public Bitmap getCurPage(){ return mCurPageBitmap; } @@ -165,6 +192,7 @@ public class PageWidget extends View { if(!PageFactory.busy()) if (isMove){ isMove = true; + mTouchListener.hideAdBeforeIni() ; if (moveX == 0 && moveY ==0) { //Log.e(TAG,"isMove"); //判断翻得是上一页还是下一页 @@ -272,7 +300,9 @@ public class PageWidget extends View { //Log.e(TAG, "isNext:" + isNext); if (!noNext) { isRuning = true; + mAnimationProvider.startAnimation(mScroller); + mTouchListener.showAdAfterIni(); this.postInvalidate(); } } @@ -318,6 +348,8 @@ public class PageWidget extends View { Boolean nextPage(); void cancel(); Boolean canCancel(); + void showAdAfterIni(); + void hideAdBeforeIni(); } } diff --git a/zhuike/src/main/java/com/novelbook/android/view/animation/CoverAnimation.java b/zhuike/src/main/java/com/novelbook/android/view/animation/CoverAnimation.java index bd7d287..ed84d20 100644 --- a/zhuike/src/main/java/com/novelbook/android/view/animation/CoverAnimation.java +++ b/zhuike/src/main/java/com/novelbook/android/view/animation/CoverAnimation.java @@ -89,6 +89,8 @@ public class CoverAnimation extends AnimationProvider { int duration = (duation * Math.abs(dx)) / mScreenWidth; Log.e("duration",duration + ""); scroller.startScroll((int) mTouch.x, 0, dx, 0, duration); + + Log.d("pageAnimation", "startAnimation: annimaiton is end"); } } diff --git a/zhuike/src/main/res/layout/ad_toutiao_native_ad.xml b/zhuike/src/main/res/layout/ad_toutiao_native_ad.xml index b54925a..5c30594 100644 --- a/zhuike/src/main/res/layout/ad_toutiao_native_ad.xml +++ b/zhuike/src/main/res/layout/ad_toutiao_native_ad.xml @@ -3,73 +3,104 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> - - + + + + - + - - - + + + +