From caf239f98c6da9d8180da3beb1c279aedcf43592 Mon Sep 17 00:00:00 2001 From: shehabic Date: Tue, 21 Jun 2016 00:38:54 +0200 Subject: [PATCH 01/11] Experiment --- admobadapter/build.gradle | 2 +- .../AdmobExpressRecyclerAdapterWrapper.java | 49 ++++++++++++++----- .../expressads/AdmobFetcherExpress.java | 4 -- .../res/layout/adexpresslistview_item.xml | 2 +- admobadapter/src/main/res/values/strings.xml | 4 +- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/admobadapter/build.gradle b/admobadapter/build.gradle index 8979ef8..deaf7cb 100644 --- a/admobadapter/build.gradle +++ b/admobadapter/build.gradle @@ -17,7 +17,7 @@ android { buildToolsVersion "23.0.3" defaultConfig { - minSdkVersion 9 + minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java index 6d7ae71..7cf2f85 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java @@ -19,10 +19,10 @@ import android.content.Context; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.FrameLayout; import com.clockbyte.admobadapter.AdmobFetcherBase; import com.clockbyte.admobadapter.R; @@ -34,8 +34,7 @@ * Adapter that has common functionality for any adapters that need to show ads in-between * other data. */ -public class AdmobExpressRecyclerAdapterWrapper - extends RecyclerView.Adapter> +public class AdmobExpressRecyclerAdapterWrapper extends RecyclerView.Adapter> implements AdmobFetcherBase.AdmobListener { private final String TAG = AdmobExpressRecyclerAdapterWrapper.class.getCanonicalName(); @@ -66,6 +65,15 @@ public void onChanged() { private final static int DEFAULT_LIMIT_OF_ADS = 3; private int mNoOfDataBetweenAds; + private int firstAdPosition = -1; + + public int getFirstAdPosition() { + return firstAdPosition; + } + + public void setFirstAdPosition(int firstAdPosition) { + this.firstAdPosition = firstAdPosition; + } /* * Gets the number of your data items between ad blocks, by default it equals to 10. @@ -195,8 +203,9 @@ public int getItemCount() { } public int getAdsCountToPublish(){ - int noOfAds = Math.min(adFetcher.getFetchedAdsCount(), - mAdapter.getItemCount() / getNoOfDataBetweenAds()); + int expected = (mAdapter.getItemCount() + getOffsetValue()) / getNoOfDataBetweenAds(); + expected += firstAdPosition > -1 ? 1 : 0; + int noOfAds = Math.min(adFetcher.getFetchedAdsCount(), expected); return Math.min(noOfAds, getLimitOfAds()); } @@ -224,8 +233,11 @@ public int getItemViewType(int position) { protected int getOriginalContentPosition(int position) { int noOfAds = getAdsCountToPublish(); // No of spaces for ads in the dataset, according to ad placement rules - int adSpacesCount = position / (getNoOfDataBetweenAds() + 1); - return position - Math.min(adSpacesCount, noOfAds); + int adSpacesCount = (getAdIndex(position) + 1); + int originalPosition = position - Math.min(adSpacesCount, noOfAds); + Log.d("POSITION", position + " is originally " + originalPosition); + + return originalPosition; } /** @@ -250,7 +262,16 @@ protected boolean canShowAdAtPosition(int position) { * @return the index of the ad within the list of fetched ads */ private int getAdIndex(int position) { - return (position / getNoOfDataBetweenAds()) - 1; + int index = -1; + if (firstAdPosition > -1 && + (position <= firstAdPosition + getNoOfDataBetweenAds()) && + position >= firstAdPosition) { + index = 0; + } else if (firstAdPosition > -1 && (position > firstAdPosition + getNoOfDataBetweenAds())) { + index = (position + getOffsetValue()) / getNoOfDataBetweenAds(); + } + Log.d("POSITION", "index " + index + " for position " + position); + return index; } /** @@ -260,7 +281,11 @@ private int getAdIndex(int position) { * @return {@code true} if an ad position, {@code false} otherwise */ private boolean isAdPosition(int position) { - return (position + 1) % (getNoOfDataBetweenAds() + 1) == 0; + return position % (getNoOfDataBetweenAds() + 1) == getOffsetValue(); + } + + private int getOffsetValue() { + return firstAdPosition > -1 ? firstAdPosition : 0; } /** @@ -271,9 +296,9 @@ private boolean isAdPosition(int position) { */ private boolean isAdAvailable(int position) { int adIndex = getAdIndex(position); - return position >= getNoOfDataBetweenAds() - && adIndex >= 0 - && adIndex < getLimitOfAds(); + int firstAdPos = getFirstAdPosition() > -1 ? getFirstAdPosition() -1 : getNoOfDataBetweenAds(); + + return position >= firstAdPos && adIndex >= 0 && adIndex < getLimitOfAds(); } /** diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobFetcherExpress.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobFetcherExpress.java index f2c1f36..8584528 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobFetcherExpress.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobFetcherExpress.java @@ -19,14 +19,10 @@ import android.content.Context; import android.os.Handler; -import android.text.TextUtils; import android.util.Log; -import android.view.View; import com.clockbyte.admobadapter.AdmobFetcherBase; -import com.clockbyte.admobadapter.R; import com.google.android.gms.ads.AdListener; -import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.NativeExpressAdView; import java.lang.ref.WeakReference; diff --git a/admobadapter/src/main/res/layout/adexpresslistview_item.xml b/admobadapter/src/main/res/layout/adexpresslistview_item.xml index a16ffaa..99dcc39 100644 --- a/admobadapter/src/main/res/layout/adexpresslistview_item.xml +++ b/admobadapter/src/main/res/layout/adexpresslistview_item.xml @@ -3,7 +3,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" - ads:adSize="320x150" + ads:adSize="340x330" ads:adUnitId="@string/test_admob_express_unit_id"/> \ No newline at end of file diff --git a/admobadapter/src/main/res/values/strings.xml b/admobadapter/src/main/res/values/strings.xml index 4814cf1..0070d5b 100644 --- a/admobadapter/src/main/res/values/strings.xml +++ b/admobadapter/src/main/res/values/strings.xml @@ -2,5 +2,4 @@ admobadapter ca-app-pub-3940256099942544/3986624511 ca-app-pub-3940256099942544/1072772517 - ca-app-pub-3940256099942544~3347511713 diff --git a/sampleapp/sampleapp.iml b/sampleapp/sampleapp.iml index 0da6942..7b3ac4d 100644 --- a/sampleapp/sampleapp.iml +++ b/sampleapp/sampleapp.iml @@ -1,5 +1,5 @@ - + @@ -98,6 +98,7 @@ + @@ -115,7 +116,6 @@ - - + \ No newline at end of file diff --git a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_ListView.java b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_ListView.java index b8b687f..7f0c35a 100644 --- a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_ListView.java +++ b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_ListView.java @@ -5,6 +5,7 @@ import android.widget.ArrayAdapter; import android.widget.ListView; import com.clockbyte.admobadapter.AdmobAdapterWrapper; +import com.google.android.gms.ads.MobileAds; import java.util.ArrayList; import java.util.Timer; @@ -21,6 +22,10 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_listview); + //highly-recommended in Firebase docs to initialize things early as possible + //test_admob_app_id is different with unit_id! you could get it in your Admob console + MobileAds.initialize(getApplicationContext(), getString(R.string.test_admob_app_id)); + initListViewItems(); initUpdateAdsTimer(); } diff --git a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_RecyclerView.java b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_RecyclerView.java index 29f3993..ce180d1 100644 --- a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_RecyclerView.java +++ b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/MainActivity_RecyclerView.java @@ -7,6 +7,7 @@ import android.widget.ArrayAdapter; import com.clockbyte.admobadapter.AdmobRecyclerAdapterWrapper; +import com.google.android.gms.ads.MobileAds; import java.util.ArrayList; import java.util.Timer; @@ -23,6 +24,10 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_recycleview); + //highly-recommended in Firebase docs to initialize things early as possible + //test_admob_app_id is different with unit_id! you could get it in your Admob console + MobileAds.initialize(getApplicationContext(), getString(R.string.test_admob_app_id)); + initRecyclerViewItems(); initUpdateAdsTimer(); } diff --git a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_ListView_Express.java b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_ListView_Express.java index beb3562..7c113bf 100644 --- a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_ListView_Express.java +++ b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_ListView_Express.java @@ -8,6 +8,8 @@ import com.clockbyte.admobadapter.expressads.AdmobExpressAdapterWrapper; import com.clockbyte.admobadapter.sampleapp.R; import com.google.android.gms.ads.AdRequest; +import com.google.android.gms.ads.AdSize; +import com.google.android.gms.ads.MobileAds; import java.util.ArrayList; import java.util.Timer; @@ -24,6 +26,10 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_listview); + //highly-recommended in Firebase docs to initialize things early as possible + //test_admob_app_id is different with unit_id! you could get it in your Admob console + MobileAds.initialize(getApplicationContext(), getString(R.string.test_admob_app_id)); + initListViewItems(); initUpdateAdsTimer(); } @@ -60,11 +66,10 @@ private void initListViewItems() { adapterWrapper.setNoOfDataBetweenAds(10); adapterWrapper.setFirstAdIndex(2); - - //It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release! - //Otherwise your Admob account could be banned - //String admobUnitId = getResources().getString(R.string.banner_admob_unit_id); - //adapterWrapper.setAdmobReleaseUnitId(admobUnitId); + //due to the docs you should set the ad size before ads will be loaded + //AdSize.FULL_WIDTH x 150 is default size. + adapterWrapper.setAdSize(new AdSize(AdSize.FULL_WIDTH,150)); + adapterWrapper.setAdsUnitId(getString(R.string.test_admob_express_unit_id)); lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView diff --git a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_RecyclerView_Express.java b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_RecyclerView_Express.java index 01aa076..6c39e9f 100644 --- a/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_RecyclerView_Express.java +++ b/sampleapp/src/main/java/com/clockbyte/admobadapter/sampleapp/express/MainActivity_RecyclerView_Express.java @@ -9,6 +9,8 @@ import com.clockbyte.admobadapter.sampleapp.R; import com.clockbyte.admobadapter.sampleapp.RecyclerExampleAdapter; import com.google.android.gms.ads.AdRequest; +import com.google.android.gms.ads.AdSize; +import com.google.android.gms.ads.MobileAds; import java.util.ArrayList; import java.util.Timer; @@ -25,6 +27,10 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_recycleview); + //highly-recommended in Firebase docs to initialize things early as possible + //test_admob_app_id is different with unit_id! you could get it in your Admob console + MobileAds.initialize(getApplicationContext(), getString(R.string.test_admob_app_id)); + initRecyclerViewItems(); initUpdateAdsTimer(); } @@ -61,11 +67,10 @@ private void initRecyclerViewItems() { adapterWrapper.setNoOfDataBetweenAds(10); adapterWrapper.setFirstAdIndex(2); - - //It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release! - //Otherwise your Admob account could be banned - //String admobUnitId = getResources().getString(R.string.banner_admob_unit_id); - //adapterWrapper.setAdmobReleaseUnitId(admobUnitId); + //due to the docs you should set the ad size before ads will be loaded + //AdSize.FULL_WIDTH x 150 is default size. + adapterWrapper.setAdSize(new AdSize(AdSize.FULL_WIDTH,150)); + adapterWrapper.setAdsUnitId(getString(R.string.test_admob_express_unit_id)); rvMessages.setAdapter(adapterWrapper); // setting an AdmobExpressRecyclerAdapterWrapper to a RecyclerView diff --git a/sampleapp/src/main/res/values/strings.xml b/sampleapp/src/main/res/values/strings.xml index 7ed638e..9d16040 100644 --- a/sampleapp/src/main/res/values/strings.xml +++ b/sampleapp/src/main/res/values/strings.xml @@ -2,4 +2,6 @@ admobadapter-sampleapp ca-app-pub-xxx/xxx 6BA3752AF66F8D8553248A71886E648D + ca-app-pub-3940256099942544~3347511713 + ca-app-pub-3940256099942544/1072772517 From f89fb6257e594110637c47640146b53530acbf37 Mon Sep 17 00:00:00 2001 From: Clockbyte Team Date: Mon, 18 Jul 2016 15:13:39 +0300 Subject: [PATCH 11/11] add getAdapterCalculator() and setAdapterCalculator to all supported adapter wrappers. It allows you to inject your implementation of ads' indices logic etc. You could simply inherit from class AdmobAdapterCalculator.java and inject the existance of your class to the Adapter Wrapper. --- admobadapter/admobadapter.iml | 16 ++++++++-------- .../admobadapter/AdmobAdapterCalculator.java | 8 ++++---- .../admobadapter/AdmobAdapterWrapper.java | 12 +++++++++++- .../AdmobRecyclerAdapterWrapper.java | 12 +++++++++++- .../expressads/AdmobExpressAdapterWrapper.java | 11 ++++++++++- .../AdmobExpressRecyclerAdapterWrapper.java | 12 +++++++++++- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/admobadapter/admobadapter.iml b/admobadapter/admobadapter.iml index 909d4ee..171b404 100644 --- a/admobadapter/admobadapter.iml +++ b/admobadapter/admobadapter.iml @@ -65,14 +65,6 @@ - - - - - - - - @@ -81,6 +73,14 @@ + + + + + + + + diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterCalculator.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterCalculator.java index 1eb9258..b0cb4c1 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterCalculator.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterCalculator.java @@ -21,13 +21,13 @@ public class AdmobAdapterCalculator { - private AdmobAdapterWrapperInterface mAdmobAdapter; + protected AdmobAdapterWrapperInterface mAdmobAdapter; public AdmobAdapterCalculator(AdmobAdapterWrapperInterface admobAdapter){ mAdmobAdapter = admobAdapter; } - private int mNoOfDataBetweenAds; + protected int mNoOfDataBetweenAds; /* * Gets the number of your data items between ad blocks, by default it equals to 10. * You should set it according to the Admob's policies and rules which says not to @@ -48,7 +48,7 @@ public void setNoOfDataBetweenAds(int mNoOfDataBetweenAds) { this.mNoOfDataBetweenAds = mNoOfDataBetweenAds; } - private int firstAdIndex = 0; + protected int firstAdIndex = 0; public int getFirstAdIndex() { return firstAdIndex; @@ -61,7 +61,7 @@ public void setFirstAdIndex(int firstAdIndex) { this.firstAdIndex = firstAdIndex; } - private int mLimitOfAds; + protected int mLimitOfAds; /* * Gets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules) diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java index b6afb49..26b2265 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java @@ -60,7 +60,17 @@ public void onInvalidated() { AdmobFetcher adFetcher; Context mContext; - private final AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + private AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + /* + * Gets an object which incapsulates transformation of the source and ad blocks indices + */ + public AdmobAdapterCalculator getAdapterCalculator(){return AdapterCalculator;} + /* +* Injects an object which incapsulates transformation of the source and ad blocks indices. You could override calculations +* by inheritance of AdmobAdapterCalculator class +*/ + public void setAdapterCalculator(AdmobAdapterCalculator adapterCalculatordmob){AdapterCalculator = adapterCalculatordmob;} + private static final int VIEW_TYPE_COUNT = 2; private static final int VIEW_TYPE_AD_CONTENT = 1; diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobRecyclerAdapterWrapper.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobRecyclerAdapterWrapper.java index 3a44401..b82cc46 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobRecyclerAdapterWrapper.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobRecyclerAdapterWrapper.java @@ -57,7 +57,17 @@ public void onChanged() { AdmobFetcher adFetcher; Context mContext; - private final AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + private AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + /* + * Gets an object which incapsulates transformation of the source and ad blocks indices + */ + public AdmobAdapterCalculator getAdapterCalculator(){return AdapterCalculator;} + /* +* Injects an object which incapsulates transformation of the source and ad blocks indices. You could override calculations +* by inheritance of AdmobAdapterCalculator class +*/ + public void setAdapterCalculator(AdmobAdapterCalculator adapterCalculatordmob){AdapterCalculator = adapterCalculatordmob;} + private static final int VIEW_TYPE_COUNT = 2; private static final int VIEW_TYPE_AD_CONTENT = 1; diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressAdapterWrapper.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressAdapterWrapper.java index 94b5954..df3e0a2 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressAdapterWrapper.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressAdapterWrapper.java @@ -65,7 +65,16 @@ public void onInvalidated() { AdmobFetcherExpress adFetcher; Context mContext; - private final AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + private AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + /* + * Gets an object which incapsulates transformation of the source and ad blocks indices + */ + public AdmobAdapterCalculator getAdapterCalculator(){return AdapterCalculator;} + /* +* Injects an object which incapsulates transformation of the source and ad blocks indices. You could override calculations +* by inheritance of AdmobAdapterCalculator class +*/ + public void setAdapterCalculator(AdmobAdapterCalculator adapterCalculatordmob){AdapterCalculator = adapterCalculatordmob;} private static final int VIEW_TYPE_COUNT = 1; private static final int VIEW_TYPE_AD_EXPRESS = 1; diff --git a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java index b55d286..7111543 100644 --- a/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java +++ b/admobadapter/src/main/java/com/clockbyte/admobadapter/expressads/AdmobExpressRecyclerAdapterWrapper.java @@ -59,7 +59,17 @@ public void onChanged() { AdmobFetcherExpress adFetcher; Context mContext; - private final AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + private AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator(this); + /* + * Gets an object which incapsulates transformation of the source and ad blocks indices + */ + public AdmobAdapterCalculator getAdapterCalculator(){return AdapterCalculator;} + /* +* Injects an object which incapsulates transformation of the source and ad blocks indices. You could override calculations +* by inheritance of AdmobAdapterCalculator class +*/ + public void setAdapterCalculator(AdmobAdapterCalculator adapterCalculatordmob){AdapterCalculator = adapterCalculatordmob;} + private static final int VIEW_TYPE_AD_EXPRESS = 1;