-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from clockbyte/first_adv_index
First adv index
- Loading branch information
Showing
18 changed files
with
506 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,15 @@ | ||
Admob Adapter | ||
====================== | ||
|
||
The reference to the [DOCUMENTATION](https://github.com/clockbyte/admobadapter/wiki/Cookbook) for people who are in hurry! | ||
|
||
Admob Adapter is an Android library that makes it easy to integrate [Admob native ads](https://firebase.google.com/docs/admob/android/native) (both Express and Advanced) into ```ListView/RecyclerView``` in the way that is shown in the following image/animation. | ||
|
||
![](https://raw.githubusercontent.com/clockbyte/admobadapter/master/screenshots/device-2015-08-28-012121.png) | ||
|
||
![](https://raw.githubusercontent.com/clockbyte/admobadapter/master/screenshots/ezgif.com-gif-maker.gif "Demo gif") | ||
|
||
#Main features | ||
|
||
* Publish of the native ads without changing the logic of your ```Adapter``` | ||
* Customization: you can easily set the maximum count of ads per list, the count of items shown between ad blocks and a custom layouts for ads blocks. | ||
* Easy to update ad blocks periodically | ||
|
||
The admobadapter-sampleapp shows the available features and customizations. | ||
|
||
# Installation | ||
|
||
##Cloning | ||
First of all you will have to clone the library. | ||
```shell | ||
git clone https://github.com/clockbyte/admobadapter.git | ||
``` | ||
|
||
Now that you have the library you will have to import it into Android Studio. | ||
In Android Studio navigate the menus like this. | ||
``` | ||
File -> Import Project ... | ||
``` | ||
In the following dialog navigate to ```admobadapter-master``` which you cloned to your computer in the previous steps and select the `build.gradle`. | ||
|
||
##Or | ||
you also can simply copy all the *.java from | ||
``` | ||
admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/ | ||
``` | ||
to your ```java``` sources folder (feel free to edit the package names in all files but please leave the License header as is). | ||
|
||
and all the *.xml from | ||
``` | ||
admobadapter/admobadapter/src/main/res/layout/ | ||
``` | ||
to your ```res/layout``` folder. | ||
Also please don't forget to copy the string resource ```test_admob_unit_id``` from ```admobadapter/admobadapter/src/main/res/values/strings.xml``` to your ```strings.xml``` file. Then kindly use it as demonstrated in the sampleapp demo. | ||
When you'll be ready to deploy your app to Release you'll have to register in the Admob and create Ad unit ID there. Then you'd kindly replace the ```test_admob_unit_id``` with your real Ad unit ID. And please don't forget to use the test ID instead of real one when you're debugging/testing your app otherwise Admob can ban your account (artificial inflating of impressions and so on). | ||
|
||
#Base usage | ||
The quick and dirty start is shown in the sampleapp of the project (to switch the sample between the RecyclerView and ListView examples kindly edit the ```AndroidManifest.xml```) | ||
|
||
The Developer's guide on native ads could be found [here](https://developers.google.com/admob/android/native). | ||
|
||
The cook recipes could be found in the [Wiki](https://github.com/clockbyte/admobadapter/wiki/Cookbook) | ||
|
||
Also feel free to ask me. | ||
You can read the rest info (main features, installation, base usage and so on) at the [project's home page](https://github.com/clockbyte/admobadapter/wiki/Home) if you wish. | ||
|
||
#Contributions | ||
Contributions are very welcome. If you find a bug in the library or want an improvement and feel you can work on it yourself, fork + pull request and i'll appreciate it much! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright 2015 Yahoo Inc. All rights reserved. | ||
* Copyright 2016 Clockbyte LLC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.clockbyte.admobadapter; | ||
|
||
import android.util.Log; | ||
|
||
public class AdmobAdapterCalculator { | ||
|
||
protected AdmobAdapterWrapperInterface mAdmobAdapter; | ||
|
||
public AdmobAdapterCalculator(AdmobAdapterWrapperInterface admobAdapter){ | ||
mAdmobAdapter = admobAdapter; | ||
} | ||
|
||
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 | ||
* display more than one ad block at the visible part of the screen | ||
* so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices | ||
*/ | ||
public int getNoOfDataBetweenAds() { | ||
return mNoOfDataBetweenAds; | ||
} | ||
|
||
/* | ||
* Sets 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 | ||
* display more than one ad block at the visible part of the screen | ||
* so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices | ||
*/ | ||
public void setNoOfDataBetweenAds(int mNoOfDataBetweenAds) { | ||
this.mNoOfDataBetweenAds = mNoOfDataBetweenAds; | ||
} | ||
|
||
protected int firstAdIndex = 0; | ||
|
||
public int getFirstAdIndex() { | ||
return firstAdIndex; | ||
} | ||
|
||
/* | ||
* Sets the first ad block index (zero-based) in the adapter, by default it equals to 0 | ||
*/ | ||
public void setFirstAdIndex(int firstAdIndex) { | ||
this.firstAdIndex = firstAdIndex; | ||
} | ||
|
||
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) | ||
*/ | ||
public int getLimitOfAds() { | ||
return mLimitOfAds; | ||
} | ||
|
||
/* | ||
* Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules) | ||
*/ | ||
public void setLimitOfAds(int mLimitOfAds) { | ||
this.mLimitOfAds = mLimitOfAds; | ||
} | ||
|
||
|
||
public int getAdsCountToPublish(){ | ||
//int cntFetched = adFetcher.getFetchedAdsCount(); | ||
//if(cntFetched == 0) return 0; | ||
int expected = 0; | ||
if(mAdmobAdapter.getAdapterCount() > 0 && mAdmobAdapter.getAdapterCount()>= getOffsetValue()+1) | ||
expected = (mAdmobAdapter.getAdapterCount() - getOffsetValue()) / getNoOfDataBetweenAds() + 1; | ||
expected = Math.max(0, expected); | ||
//int noOfAds = Math.min(cntFetched, expected); | ||
return Math.min(expected, getLimitOfAds()); | ||
} | ||
|
||
/** | ||
* Translates an adapter position to an actual position within the underlying dataset. | ||
* | ||
* @param position the adapter position | ||
* @return the original position that the adapter position would have been without ads | ||
*/ | ||
public int getOriginalContentPosition(int position) { | ||
int noOfAds = getAdsCountToPublish(); | ||
// No of spaces for ads in the dataset, according to ad placement rules | ||
int adSpacesCount = (getAdIndex(position) + 1); | ||
int originalPosition = position - Math.min(adSpacesCount, noOfAds); | ||
Log.d("POSITION", position + " is originally " + originalPosition); | ||
|
||
return originalPosition; | ||
} | ||
|
||
/** | ||
* Determines if an ad can be shown at the given position. Checks if the position is for | ||
* an ad, using the preconfigured ad positioning rules; and if a native ad object is | ||
* available to place in that position. | ||
* | ||
* @param position the adapter position | ||
* @return <code>true</code> if ads can | ||
*/ | ||
public boolean canShowAdAtPosition(int position) { | ||
|
||
// Is this a valid position for an ad? | ||
// Is an ad for this position available? | ||
return isAdPosition(position) && isAdAvailable(position); | ||
} | ||
|
||
/** | ||
* Gets the ad index for this adapter position within the list of currently fetched ads. | ||
* | ||
* @param position the adapter position | ||
* @return the index of the ad within the list of fetched ads | ||
*/ | ||
public int getAdIndex(int position) { | ||
int index = -1; | ||
if(position >= getOffsetValue()) | ||
index = (position - getOffsetValue()) / (getNoOfDataBetweenAds()+1); | ||
Log.d("POSITION", "index " + index + " for position " + position); | ||
return index; | ||
} | ||
|
||
/** | ||
* Checks if adapter position is an ad position. | ||
* | ||
* @param position the adapter position | ||
* @return {@code true} if an ad position, {@code false} otherwise | ||
*/ | ||
public boolean isAdPosition(int position) { | ||
int result = (position - getOffsetValue()) % (getNoOfDataBetweenAds() + 1); | ||
return result == 0; | ||
} | ||
|
||
public int getOffsetValue() { | ||
return getFirstAdIndex() > 0 ? getFirstAdIndex() : 0; | ||
} | ||
|
||
/** | ||
* Checks if an ad is available for this position. | ||
* | ||
* @param position the adapter position | ||
* @return {@code true} if an ad is available, {@code false} otherwise | ||
*/ | ||
public boolean isAdAvailable(int position) { | ||
int adIndex = getAdIndex(position); | ||
int firstAdPos = getOffsetValue(); | ||
|
||
return position >= firstAdPos && adIndex >= 0 && adIndex < getLimitOfAds(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.