├── .gitignore
├── .travis.yml
├── CONTRIBUTING.md
├── ISSUE_TEMPLATE.md
├── LICENSE
├── README.md
├── admob-adapter-master.iml
├── admobadapter
├── .gitignore
├── admobadapter.iml
├── build.gradle
├── gradle.properties
├── maven-push.gradle
├── proguard-rules.pro
└── src
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── clockbyte
│ │ │ └── admobadapter
│ │ │ ├── AdLayoutContext.java
│ │ │ ├── AdPreset.java
│ │ │ ├── AdPresetCyclingList.java
│ │ │ ├── AdViewHelper.java
│ │ │ ├── AdapterWrapperObserver.java
│ │ │ ├── AdmobAdapterCalculator.java
│ │ │ ├── AdmobAdapterWrapper.java
│ │ │ ├── AdmobFetcher.java
│ │ │ ├── AdmobFetcherBase.java
│ │ │ ├── AdmobRecyclerAdapterWrapper.java
│ │ │ ├── NativeAdLayoutContext.java
│ │ │ ├── NativeHolder.java
│ │ │ └── bannerads
│ │ │ ├── AdmobBannerAdapterWrapper.java
│ │ │ ├── AdmobBannerRecyclerAdapterWrapper.java
│ │ │ ├── AdmobFetcherBanner.java
│ │ │ ├── BannerAdPreset.java
│ │ │ ├── BannerAdPresetCyclingList.java
│ │ │ ├── BannerAdViewWrappingStrategy.java
│ │ │ ├── BannerAdViewWrappingStrategyBase.java
│ │ │ └── BannerHolder.java
│ └── res
│ │ ├── layout
│ │ ├── adlistview_item.xml
│ │ └── web_ad_container.xml
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── clockbyte
│ └── admobadapter
│ └── AdmobAdapterCalculatorTest.java
├── build.gradle
├── check_indices.xlsx
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── sampleapp
├── .gitignore
├── build.gradle
├── proguard-rules.pro
├── sampleapp.iml
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── clockbyte
│ │ └── admobadapter
│ │ └── sampleapp
│ │ └── ApplicationTest.java
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── clockbyte
│ │ └── admobadapter
│ │ └── sampleapp
│ │ ├── MainActivity_ListView.java
│ │ ├── MainActivity_RecyclerView.java
│ │ ├── RecyclerExampleAdapter.java
│ │ ├── RecyclerViewExampleItem.java
│ │ ├── ViewWrapper.java
│ │ └── banner
│ │ ├── MainActivity_ListView_Banner.java
│ │ └── MainActivity_RecyclerView_Banner.java
│ └── res
│ ├── layout
│ ├── activity_main_listview.xml
│ ├── activity_main_recycleview.xml
│ ├── native_express_ad_container.xml
│ └── recyclerview_item.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ └── values
│ ├── strings.xml
│ └── styles.xml
├── screenshots
├── Screenshot_20160809-183435.png
├── device-2015-08-28-012121.png
├── device-2017-04-24-202814.png
├── device-2018-02-19-205903.png
├── device-2018-02-19-210025.png
└── ezgif.com-gif-maker.gif
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # [Android] ========================
2 | # Built application files
3 | *.apk
4 | *.ap_
5 |
6 | # Files for the Dalvik VM
7 | *.dex
8 |
9 | # Java class files
10 | *.class
11 |
12 | # Generated files
13 | bin/
14 | gen/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 |
30 | ## Directory-based project format:
31 | .idea/
32 |
33 | ## File-based project format:
34 | *.ipr
35 | *.iws
36 |
37 | ## Plugin-specific files:
38 |
39 | # IntelliJ
40 | out/
41 |
42 | # mpeltonen/sbt-idea plugin
43 | .idea_modules/
44 |
45 | # JIRA plugin
46 | atlassian-ide-plugin.xml
47 |
48 | # Crashlytics plugin (for Android Studio and IntelliJ)
49 | com_crashlytics_export_strings.xml
50 |
51 |
52 | # [Maven] ========================
53 | target/
54 | pom.xml.tag
55 | pom.xml.releaseBackup
56 | pom.xml.versionsBackup
57 | pom.xml.next
58 | release.properties
59 |
60 |
61 | # [Gradle-Android] ========================
62 |
63 | # Ignore Gradle GUI config
64 | gradle-app.setting
65 |
66 | # Gradle Signing
67 | signing.properties
68 | trestle.keystore
69 |
70 | # Mobile Tools for Java (J2ME)
71 | .mtj.tmp/
72 |
73 | # Package Files #
74 | *.jar
75 | *.war
76 | *.ear
77 |
78 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
79 | hs_err_pid*
80 |
81 | # Misc
82 | /.idea/workspace.xml
83 | .DS_Store
84 | /captures
85 | **/*.iml
86 | *.class
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | dist: trusty
3 | jdk: oraclejdk8
4 |
5 | addons:
6 | apt:
7 | packages:
8 | - oracle-java8-installer
9 |
10 | before_install:
11 | - chmod +x gradlew
12 | - yes | sdkmanager "platforms;android-30"
13 |
14 | before_cache:
15 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
16 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
17 | cache:
18 | directories:
19 | - $HOME/.gradle/caches/
20 | - $HOME/.gradle/wrapper/
21 | - $HOME/.android/build-cache
22 |
23 | android:
24 | components:
25 | - tools # to get the new `repository-11.xml`
26 | - build-tools-30.0.2
27 | - android-30
28 | - android-22
29 | - platform-tools
30 | - extra-android-support
31 | - extra-android-m2repository
32 | - extra-google-m2repository
33 | - extra-google-google_play_services
34 | # Specify at least one system image,
35 | # if you need to run emulator(s) during your tests
36 | - sys-img-armeabi-v7a-android-22
37 |
38 | env:
39 | global:
40 | # install timeout in minutes (2 minutes by default)
41 | - ADB_INSTALL_TIMEOUT=10
42 |
43 | # Emulator Management: Create, Start and Wait
44 | before_script:
45 | - android list target
46 | - echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a
47 | - emulator -avd test -no-audio -no-window &
48 | - android-wait-for-emulator
49 | - adb shell input keyevent 82 &
50 |
51 | script:
52 | - ./gradlew clean build
53 | - ./gradlew test
54 | - ./gradlew build check
55 |
56 | notifications:
57 | webhooks:
58 | urls:
59 | - https://webhooks.gitter.im/e/7346b57d8c284592f2bb
60 | on_success: change # options: [always|never|change] default: always
61 | on_failure: always # options: [always|never|change] default: always
62 | on_start: never # options: [always|never|change] default: always
63 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Contributions are very welcome.
2 | If you find a bug in the library or want an improvement and feel you can work on it yourself, fork + pull request to the MAIN branch and we'll appreciate it much!
3 |
--------------------------------------------------------------------------------
/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | **Admobadapter version or commit:**
2 |
3 | **Android compileSdkVersion:**
4 |
5 | **Issue description briefly:**
6 |
7 | **Steps to reproduce:**
8 |
9 | **All the code which you want to share**
10 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Admob Adapter
2 |
3 | [](http://www.apache.org/licenses/LICENSE-2.0) [](https://app.travis-ci.com/github/clockbyte/admobadapter) [](https://gitter.im/clockbyte/admobadapter?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [](https://maven-badges.herokuapp.com/maven-central/com.github.clockbyte/admobadapter) [](https://android-arsenal.com/details/1/5706) [](https://android-arsenal.com/api?level=14)
4 |
5 | ======================
6 |
7 | > We've released! [v1.5.0](https://github.com/clockbyte/admobadapter/releases/tag/v1.5.0)! And we moved to the later dependencies.
8 |
9 | The reference to the [COOK RECIPES](https://github.com/clockbyte/admobadapter/wiki/Cookbook) for people who are in hurry!
10 |
11 | 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) or AdMob banners into ```ListView/RecyclerView``` in the way that is shown in the following image/animation.
12 |
13 |
14 |
15 | You can read the rest info (main features, base usage and so on) at the [project's home page](https://github.com/clockbyte/admobadapter/wiki/Home) if you wish.
16 |
17 | # Installation
18 | Now you are able to link Admobadapter via Gradle like this
19 | ```shell
20 | dependencies {
21 | //link other libs
22 | compile 'com.github.clockbyte:admobadapter:1.5.0'
23 | }
24 | ```
25 | [Here](https://github.com/clockbyte/admobadapter/wiki/Installation) you could find more ways.
26 |
--------------------------------------------------------------------------------
/admob-adapter-master.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/admobadapter/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/admobadapter/admobadapter.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | generateDebugSources
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/admobadapter/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | buildscript {
16 | repositories {
17 | mavenCentral()
18 | google()
19 | }
20 | dependencies {
21 | classpath 'com.android.tools.build:gradle:4.2.2'
22 | }
23 | }
24 | apply plugin: 'com.android.library'
25 | apply from: 'maven-push.gradle'
26 |
27 | android {
28 | compileSdkVersion 31
29 | buildToolsVersion "30.0.2"
30 |
31 | defaultConfig {
32 | minSdkVersion 16
33 | versionCode 1
34 | versionName "1.0"
35 | targetSdkVersion 31
36 | }
37 |
38 | buildTypes {
39 | release {
40 | minifyEnabled false
41 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
42 | }
43 | }
44 |
45 | lintOptions {
46 | abortOnError false
47 | }
48 | compileOptions {
49 | sourceCompatibility JavaVersion.VERSION_1_7
50 | targetCompatibility JavaVersion.VERSION_1_7
51 | }
52 | }
53 |
54 | dependencies {
55 | implementation fileTree(dir: 'libs', include: ['*.jar'])
56 |
57 | implementation "androidx.cardview:cardview:1.0.0"
58 | implementation 'androidx.recyclerview:recyclerview:1.2.1'
59 |
60 | implementation 'com.google.android.gms:play-services-ads:20.4.0'
61 |
62 | testImplementation 'junit:junit:4.13.2'
63 | testImplementation 'org.mockito:mockito-core:3.12.4'
64 | testImplementation "org.powermock:powermock-module-junit4:2.0.9"
65 | testImplementation "org.powermock:powermock-module-junit4-rule:2.0.9"
66 | testImplementation "org.powermock:powermock-api-mockito2:2.0.9"
67 | testImplementation "org.powermock:powermock-classloading-xstream:2.0.9"
68 | }
69 |
--------------------------------------------------------------------------------
/admobadapter/gradle.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | # Copyright (c) 2017 Clockbyte LLC. All rights reserved.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | #
15 |
16 | # Project-wide Gradle settings.
17 |
18 | # IDE (e.g. Android Studio) users:
19 | # Gradle settings configured through the IDE *will override*
20 | # any settings specified in this file.
21 |
22 | # For more details on how to configure your build environment visit
23 | # http://www.gradle.org/docs/current/userguide/build_environment.html
24 |
25 | # Specifies the JVM arguments used for the daemon process.
26 | # The setting is particularly useful for tweaking memory settings.
27 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
28 | #org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
29 |
30 | # When configured, Gradle will run in incubating parallel mode.
31 | # This option should only be used with decoupled projects. More details, visit
32 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
33 | # org.gradle.parallel=true
34 |
35 | POM_NAME=Admobadapter
36 | POM_ARTIFACT_ID=admobadapter
37 | POM_PACKAGING=aar
--------------------------------------------------------------------------------
/admobadapter/maven-push.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | apply plugin: 'maven'
16 | apply plugin: 'signing'
17 |
18 | def isReleaseBuild = !VERSION_NAME.contains("SNAPSHOT")
19 | def releaseRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
20 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
21 | def snapshotRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
22 | : "https://oss.sonatype.org/content/repositories/snapshots/"
23 | def repositoryUsername = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
24 | def repositoryPassword = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
25 |
26 | afterEvaluate { project ->
27 | uploadArchives {
28 | repositories {
29 | mavenDeployer {
30 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
31 |
32 | pom.groupId = GROUP
33 | pom.artifactId = POM_ARTIFACT_ID
34 | pom.version = VERSION_NAME
35 |
36 | repository(url: releaseRepositoryUrl) {
37 | authentication(userName: repositoryUsername, password: repositoryPassword)
38 | }
39 | snapshotRepository(url: snapshotRepositoryUrl) {
40 | authentication(userName: repositoryUsername, password: repositoryPassword)
41 | }
42 |
43 | pom.project {
44 | name POM_NAME
45 | packaging POM_PACKAGING
46 | description POM_DESCRIPTION
47 | url POM_URL
48 |
49 | scm {
50 | url POM_SCM_URL
51 | connection POM_SCM_CONNECTION
52 | developerConnection POM_SCM_DEV_CONNECTION
53 | }
54 |
55 | licenses {
56 | license {
57 | name POM_LICENCE_NAME
58 | url POM_LICENCE_URL
59 | distribution POM_LICENCE_DIST
60 | }
61 | }
62 |
63 | developers {
64 | developer {
65 | id POM_DEVELOPER_ID
66 | name POM_DEVELOPER_NAME
67 | }
68 | }
69 | }
70 | }
71 | }
72 | }
73 |
74 | signing {
75 | required { isReleaseBuild && gradle.taskGraph.hasTask("uploadArchives") }
76 | sign configurations.archives
77 | }
78 |
79 | task androidSourcesJar(type: Jar) {
80 | classifier = 'sources'
81 | from android.sourceSets.main.java.sourceFiles
82 | }
83 |
84 | artifacts {
85 | archives androidSourcesJar
86 | }
87 | }
--------------------------------------------------------------------------------
/admobadapter/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:/Program Files (x86)/Android/android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/admobadapter/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
14 |
15 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdLayoutContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 | import android.view.View;
18 | import android.widget.Button;
19 | import android.widget.ImageView;
20 | import android.widget.TextView;
21 |
22 | import com.google.android.gms.ads.nativead.NativeAd;
23 | import com.google.android.gms.ads.nativead.NativeAdView;
24 |
25 | public class AdLayoutContext extends NativeAdLayoutContext{
26 |
27 | public static AdLayoutContext getDefault() {
28 | return new AdLayoutContext(R.layout.adlistview_item);
29 | }
30 |
31 | public AdLayoutContext(int mAdLayoutId){
32 | setAdLayoutId(mAdLayoutId);
33 | }
34 |
35 | @Override
36 | public void bind(NativeAdView nativeAdView, NativeAd nativeAd) throws ClassCastException{
37 | if (nativeAdView == null || nativeAd == null) return;
38 |
39 | // Locate the view that will hold the headline, set its text, and call the
40 | // NativeAppInstallAdView's setHeadlineView method to register it.
41 | TextView tvHeader = nativeAdView.findViewById(R.id.tvHeader);
42 | tvHeader.setText(nativeAd.getHeadline());
43 | nativeAdView.setHeadlineView(tvHeader);
44 |
45 | TextView tvDescription = nativeAdView.findViewById(R.id.tvDescription);
46 | tvDescription.setText(nativeAd.getBody());
47 | nativeAdView.setBodyView(tvDescription);
48 |
49 | ImageView ivLogo = nativeAdView.findViewById(R.id.ivLogo);
50 | if(nativeAd.getIcon()!=null)
51 | ivLogo.setImageDrawable(nativeAd.getIcon().getDrawable());
52 | nativeAdView.setIconView(ivLogo);
53 |
54 | Button btnAction = nativeAdView.findViewById(R.id.btnAction);
55 | btnAction.setText(nativeAd.getCallToAction());
56 | nativeAdView.setCallToActionView(btnAction);
57 |
58 | TextView tvStore = nativeAdView.findViewById(R.id.tvStore);
59 | tvStore.setText(nativeAd.getStore());
60 | nativeAdView.setStoreView(tvStore);
61 |
62 | TextView tvPrice = nativeAdView.findViewById(R.id.tvPrice);
63 | tvPrice.setText(nativeAd.getPrice());
64 | nativeAdView.setPriceView(tvPrice);
65 |
66 | ImageView ivImage = nativeAdView.findViewById(R.id.ivImage);
67 | nativeAd.getImages();
68 | if (nativeAd.getImages().size() > 0) {
69 | ivImage.setImageDrawable(nativeAd.getImages().get(0).getDrawable());
70 | ivImage.setVisibility(View.VISIBLE);
71 | } else ivImage.setVisibility(View.GONE);
72 | nativeAdView.setImageView(ivImage);
73 |
74 | // Call the NativeAppInstallAdView's setNativeAd method to register the NativeAd.
75 | nativeAdView.setNativeAd(nativeAd);
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdPreset.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 |
18 | import android.text.TextUtils;
19 |
20 | import com.google.android.gms.ads.AdSize;
21 | import com.google.android.gms.ads.VideoOptions;
22 |
23 | import java.util.Locale;
24 |
25 | public class AdPreset {
26 | private static final String UNIT_ID_DEFAULT_EXPRESS = "ca-app-pub-3940256099942544/1072772517";
27 | private static final AdSize SIZE_DEFAULT_EXPRESS = new AdSize(AdSize.FULL_WIDTH, 150);
28 | public static final AdPreset DEFAULT = new AdPreset(UNIT_ID_DEFAULT_EXPRESS, SIZE_DEFAULT_EXPRESS);
29 |
30 | private String adUnitId;
31 | private AdSize adSize;
32 | private VideoOptions videoOptions;
33 |
34 | public AdPreset(){
35 | this.adUnitId = UNIT_ID_DEFAULT_EXPRESS;
36 | this.adSize = SIZE_DEFAULT_EXPRESS;
37 | }
38 |
39 | public AdPreset(String adUnitId){
40 | this();
41 | if(!TextUtils.isEmpty(adUnitId))
42 | this.adUnitId = adUnitId;
43 | }
44 |
45 | public AdPreset(String adUnitId, AdSize adSize){
46 | this(adUnitId);
47 | if(adSize != null)
48 | this.adSize = adSize;
49 | }
50 |
51 | public String getAdUnitId(){
52 | return this.adUnitId;
53 | }
54 | public void setAdUnitId(String adUnitId){
55 | this.adUnitId = adUnitId;
56 | }
57 |
58 | public AdSize getAdSize(){
59 | return this.adSize;
60 | }
61 | public void setAdSize(AdSize adSize){
62 | this.adSize = adSize;
63 | }
64 |
65 |
66 | public VideoOptions getVideoOptions() {
67 | return videoOptions;
68 | }
69 | public void setVideoOptions(VideoOptions videoOptions) {
70 | this.videoOptions = videoOptions;
71 | }
72 |
73 | public boolean isValid(){
74 | return !TextUtils.isEmpty(this.adUnitId);
75 | }
76 |
77 | @Override
78 | public boolean equals(Object o) {
79 | if (o instanceof AdPreset) {
80 | AdPreset other = (AdPreset) o;
81 | return (this.adUnitId.equals(other.adUnitId)
82 | && this.adSize.getHeight() == other.adSize.getHeight()
83 | && this.adSize.getWidth() == other.adSize.getWidth());
84 | }
85 | return false;
86 | }
87 |
88 | @Override
89 | public int hashCode() {
90 | int hash = 17;
91 | hash = hash * 31 + adUnitId.hashCode();
92 | hash = hash * 31 + adSize.hashCode();
93 | return hash;
94 | }
95 |
96 | @Override
97 | public String toString() {
98 | return String.format(Locale.getDefault(), "%s | %s", adUnitId, adSize);
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdPresetCyclingList.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 | import java.util.ArrayList;
18 | import java.util.Collection;
19 |
20 | /**
21 | * Created by FILM on 02.10.2016.
22 | */
23 |
24 | public class AdPresetCyclingList extends ArrayList {
25 |
26 | private int currentIdx = -1;
27 |
28 | public AdPresetCyclingList(){
29 | super();
30 | }
31 |
32 | public int getCurrentIdx() {
33 | return currentIdx;
34 | }
35 |
36 | /**
37 | * Gets next ad preset for Admob banners from FIFO.
38 | * It works like cycling FIFO (first in = first out, cycling from end to start).
39 | * Each ad block will get one from the queue.
40 | * If the desired count of ad blocks is greater than this collection size
41 | * then it will go again to the first item and iterate as much as it required.
42 | * ID should be active, please check it in your Admob's account.
43 | */
44 | public AdPreset get() {
45 | if(size() == 0) return null;
46 | if (size() == 1) return get(0);
47 | currentIdx = ++currentIdx % size();
48 | return get(currentIdx);
49 | }
50 |
51 | /**
52 | * Tries to add an item to collection if it is valid {@link AdPreset#isValid()}
53 | * @return true if item was added, false - otherwise
54 | */
55 | @Override
56 | public boolean add(AdPreset adPreset) {
57 | return !(adPreset == null || !adPreset.isValid())
58 | && super.add(adPreset);
59 | }
60 |
61 | /**
62 | * Tries to add items to collection if valid {@link AdPreset#isValid()}
63 | * @return true if items were added, false - otherwise
64 | */
65 | @Override
66 | public boolean addAll(Collection extends AdPreset> c) {
67 | ArrayList lst = new ArrayList();
68 | for (AdPreset eap : c) {
69 | if(eap!=null && eap.isValid())
70 | lst.add(eap);
71 | }
72 | return super.addAll(lst);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdViewHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 | import android.content.Context;
18 | import android.widget.AbsListView;
19 |
20 | import com.clockbyte.admobadapter.bannerads.BannerAdPreset;
21 | import com.google.android.gms.ads.AdSize;
22 | import com.google.android.gms.ads.AdView;
23 |
24 | public class AdViewHelper {
25 |
26 | public static AdView getBannerAdView(Context context, BannerAdPreset bannerAdPreset) {
27 | AdView adView = new AdView(context);
28 | AdSize adSize = bannerAdPreset.getAdSize();
29 | adView.setAdSize(adSize);
30 | adView.setAdUnitId(bannerAdPreset.getAdUnitId());
31 | adView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
32 | adSize.getHeightInPixels(context)));
33 |
34 | return adView;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdapterWrapperObserver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2021 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 | import android.annotation.SuppressLint;
18 |
19 | import androidx.annotation.NonNull;
20 | import androidx.recyclerview.widget.RecyclerView;
21 |
22 | /**
23 | * Created by FILM on 30.08.2017.
24 | */
25 |
26 | public class AdapterWrapperObserver extends RecyclerView.AdapterDataObserver {
27 |
28 | private final RecyclerView.Adapter adapterWrapper;
29 | private final AdmobAdapterCalculator adapterCalculator;
30 | private final AdmobFetcherBase fetcher;
31 |
32 | public AdapterWrapperObserver(@NonNull RecyclerView.Adapter adapterWrapper,
33 | @NonNull AdmobAdapterCalculator admobAdapterCalculator,
34 | @NonNull AdmobFetcherBase admobFetcher) {
35 | this.adapterWrapper = adapterWrapper;
36 | this.adapterCalculator = admobAdapterCalculator;
37 | this.fetcher = admobFetcher;
38 | }
39 |
40 | @SuppressLint("NotifyDataSetChanged")
41 | @Override
42 | public void onChanged() {
43 | adapterWrapper.notifyDataSetChanged();
44 | }
45 |
46 | @Override
47 | public void onItemRangeChanged(int positionStart, int itemCount) {
48 | this.onItemRangeChanged(positionStart, itemCount, null);
49 | }
50 |
51 | @Override
52 | public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
53 | int fetchedAdsCount = fetcher.getFetchedAdsCount();
54 | //getting the position in a final presentation
55 | int wrapperIndexFirst = adapterCalculator.translateSourceIndexToWrapperPosition(
56 | positionStart, fetchedAdsCount);
57 | int wrapperIndexLast = adapterCalculator.translateSourceIndexToWrapperPosition(
58 | positionStart + itemCount - 1, fetchedAdsCount);
59 | if (itemCount == 1)
60 | adapterWrapper.notifyItemRangeChanged(wrapperIndexFirst, 1, payload);
61 | else
62 | adapterWrapper.notifyItemRangeChanged(wrapperIndexFirst,
63 | wrapperIndexLast - wrapperIndexFirst + 1, payload);
64 |
65 | }
66 |
67 | @Override
68 | public void onItemRangeInserted(int positionStart, int itemCount) {
69 | int fetchedAdsCount = fetcher.getFetchedAdsCount();
70 | //getting the position in a final presentation
71 | int wrapperIndexFirst = adapterCalculator.translateSourceIndexToWrapperPosition(
72 | positionStart, fetchedAdsCount);
73 | int wrapperIndexLast = adapterCalculator.translateSourceIndexToWrapperPosition(
74 | positionStart + itemCount - 1, fetchedAdsCount);
75 | if (itemCount == 1)
76 | adapterWrapper.notifyItemRangeInserted(wrapperIndexFirst, 1);
77 | else
78 | adapterWrapper.notifyItemRangeInserted(wrapperIndexFirst,
79 | wrapperIndexLast - wrapperIndexFirst + 1);
80 | }
81 |
82 | @Override
83 | public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
84 | int fetchedAdsCount = fetcher.getFetchedAdsCount();
85 | //getting the position in a final presentation
86 | int fromWrapperIndexFirst = adapterCalculator.translateSourceIndexToWrapperPosition(
87 | fromPosition, fetchedAdsCount);
88 | int fromWrapperIndexLast = adapterCalculator.translateSourceIndexToWrapperPosition(
89 | fromPosition + itemCount - 1, fetchedAdsCount);
90 | int toWrapperIndexFirst = adapterCalculator.translateSourceIndexToWrapperPosition(
91 | toPosition, fetchedAdsCount);
92 | int toWrapperIndexLast = adapterCalculator.translateSourceIndexToWrapperPosition(
93 | toPosition + itemCount - 1, fetchedAdsCount);
94 | int wrapperItemCount = fromWrapperIndexLast - fromWrapperIndexFirst + 1;
95 | if (itemCount == 1)
96 | adapterWrapper.notifyItemMoved(fromWrapperIndexFirst, 1);
97 | else for (int i = 0; i < wrapperItemCount; itemCount++)
98 | adapterWrapper.notifyItemMoved(fromWrapperIndexFirst + i, toWrapperIndexFirst + i);
99 | }
100 |
101 | @Override
102 | public void onItemRangeRemoved(int positionStart, int itemCount) {
103 | int fetchedAdsCount = fetcher.getFetchedAdsCount();
104 | //getting the position in a final presentation
105 | int wrapperIndexFirst = adapterCalculator.translateSourceIndexToWrapperPosition(
106 | positionStart, fetchedAdsCount);
107 | int wrapperIndexLast = adapterCalculator.translateSourceIndexToWrapperPosition(
108 | positionStart + itemCount - 1, fetchedAdsCount);
109 | if (itemCount == 1)
110 | adapterWrapper.notifyItemRangeRemoved(wrapperIndexFirst, 1);
111 | else
112 | adapterWrapper.notifyItemRangeRemoved(wrapperIndexFirst,
113 | wrapperIndexLast - wrapperIndexFirst + 1);
114 | }
115 | }
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterCalculator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | package com.clockbyte.admobadapter;
17 |
18 | public class AdmobAdapterCalculator {
19 |
20 | protected int mNoOfDataBetweenAds;
21 | /*
22 | * Gets the number of your data items between ad blocks, by default it equals to 10.
23 | * You should set it according to the Admob's policies and rules which says not to
24 | * display more than one ad block at the visible part of the screen
25 | * so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
26 | */
27 | public int getNoOfDataBetweenAds() {
28 | return mNoOfDataBetweenAds;
29 | }
30 |
31 | /*
32 | * Sets the number of your data items between ad blocks, by default it equals to 10.
33 | * You should set it according to the Admob's policies and rules which says not to
34 | * display more than one ad block at the visible part of the screen
35 | * so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
36 | */
37 | public void setNoOfDataBetweenAds(int mNoOfDataBetweenAds) {
38 | this.mNoOfDataBetweenAds = mNoOfDataBetweenAds;
39 | }
40 |
41 | protected int firstAdIndex = 0;
42 |
43 | public int getFirstAdIndex() {
44 | return firstAdIndex;
45 | }
46 |
47 | /*
48 | * Sets the first ad block index (zero-based) in the adapter, by default it equals to 0
49 | */
50 | public void setFirstAdIndex(int firstAdIndex) {
51 | this.firstAdIndex = firstAdIndex;
52 | }
53 |
54 | protected int mLimitOfAds;
55 |
56 | /*
57 | * Gets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
58 | */
59 | public int getLimitOfAds() {
60 | return mLimitOfAds;
61 | }
62 |
63 | /*
64 | * Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
65 | */
66 | public void setLimitOfAds(int mLimitOfAds) {
67 | this.mLimitOfAds = mLimitOfAds;
68 | }
69 |
70 | /**
71 | * Gets the count of ads that could be published
72 | *
73 | * @param fetchedAdsCount the count of completely fetched ads that are ready to be published
74 | * @param sourceItemsCount the count of items in the source collection
75 | * @return the original position that the adapter position would have been without ads
76 | */
77 | public int getAdsCountToPublish(int fetchedAdsCount, int sourceItemsCount){
78 | if(fetchedAdsCount <= 0 || getNoOfDataBetweenAds() <= 0) return 0;
79 | int expected = 0;
80 | if(sourceItemsCount > 0 && sourceItemsCount >= getOffsetValue()+1)
81 | expected = (sourceItemsCount - getOffsetValue()) / getNoOfDataBetweenAds() + 1;
82 | expected = Math.max(0, expected);
83 | expected = Math.min(fetchedAdsCount, expected);
84 | return Math.min(expected, getLimitOfAds());
85 | }
86 |
87 | /**
88 | * Translates an adapter position to an actual position within the underlying dataset.
89 | *
90 | * @param position the adapter position
91 | * @param fetchedAdsCount the count of completely fetched ads that are ready to be published
92 | * @param sourceItemsCount the count of items in the source collection
93 | * @return the original position that the adapter position would have been without ads
94 | */
95 | public int getOriginalContentPosition(int position, int fetchedAdsCount, int sourceItemsCount) {
96 | int noOfAds = getAdsCountToPublish(fetchedAdsCount, sourceItemsCount);
97 | // No of spaces for ads in the dataset, according to ad placement rules
98 | int adSpacesCount = (getAdIndex(position) + 1);
99 | int originalPosition = position - Math.min(adSpacesCount, noOfAds);
100 | //Log.d("POSITION", position + " is originally " + originalPosition);
101 | return originalPosition;
102 | }
103 |
104 | /**
105 | * Translates an ad position to an actual position within the adapter wrapper.
106 | *
107 | * @param adPos the ad's position in the fetched list
108 | * @return the position of the adapter wrapper item
109 | */
110 | public int translateAdToWrapperPosition(int adPos) {
111 | int wrappedPosition = adPos*(getNoOfDataBetweenAds() + 1) + getOffsetValue();
112 | return wrappedPosition;
113 | }
114 |
115 | /**
116 | * Translates the source position to an actual position within the adapter wrapper.
117 | * @param fetchedAdsCount the count of completely fetched ads that are ready to be published
118 | * @param sourcePos the source index
119 | *
120 | * @return the position of the adapter wrapper item
121 | */
122 | public int translateSourceIndexToWrapperPosition(int sourcePos, int fetchedAdsCount) {
123 | int adSpacesCount = 0;
124 | if(sourcePos >= getOffsetValue() && getNoOfDataBetweenAds() > 0)
125 | adSpacesCount = (sourcePos - getOffsetValue())/getNoOfDataBetweenAds() + 1;
126 | adSpacesCount = Math.min(fetchedAdsCount, adSpacesCount);
127 | adSpacesCount = Math.max(0, adSpacesCount);
128 | adSpacesCount = Math.min(adSpacesCount, getLimitOfAds());
129 | int wrappedPosition = sourcePos + adSpacesCount;
130 | return wrappedPosition;
131 | }
132 |
133 | /**
134 | * Determines if an ad can be shown at the given position. Checks if the position is for
135 | * an ad, using the preconfigured ad positioning rules; and if a native ad object is
136 | * available to place in that position.
137 | *
138 | * @param position the adapter position
139 | * @param fetchedAdsCount the count of completely fetched ads that are ready to be published
140 | * @return true if ads can
141 | */
142 | public boolean canShowAdAtPosition(int position, int fetchedAdsCount) {
143 |
144 | // Is this a valid position for an ad?
145 | // Is an ad for this position available?
146 | return isAdPosition(position) && isAdAvailable(position, fetchedAdsCount);
147 | }
148 |
149 | /**
150 | * Gets the ad index for this adapter position within the list of currently fetched ads.
151 | *
152 | * @param position the adapter position
153 | * @return the index of the ad within the list of fetched ads
154 | */
155 | public int getAdIndex(int position) {
156 | int index = -1;
157 | if(position >= getOffsetValue())
158 | index = (position - getOffsetValue()) / (getNoOfDataBetweenAds()+1);
159 | //Log.d("POSITION", "index " + index + " for position " + position);
160 | return index;
161 | }
162 |
163 | /**
164 | * Checks if adapter position is an ad position.
165 | *
166 | * @param position the adapter position
167 | * @return {@code true} if an ad position, {@code false} otherwise
168 | */
169 | public boolean isAdPosition(int position) {
170 | int result = (position - getOffsetValue()) % (getNoOfDataBetweenAds() + 1);
171 | return result == 0;
172 | }
173 |
174 | public int getOffsetValue() {
175 | return getFirstAdIndex() > 0 ? getFirstAdIndex() : 0;
176 | }
177 |
178 | /**
179 | * Checks if an ad is available for this position.
180 | *
181 | * @param position the adapter position
182 | * @param fetchedAdsCount the count of completely fetched ads that are ready to be published
183 | * @return {@code true} if an ad is available, {@code false} otherwise
184 | */
185 | public boolean isAdAvailable(int position, int fetchedAdsCount) {
186 | if(fetchedAdsCount == 0) return false;
187 | int adIndex = getAdIndex(position);
188 | int firstAdPos = getOffsetValue();
189 |
190 | return position >= firstAdPos && adIndex >= 0 && adIndex < getLimitOfAds() && adIndex < fetchedAdsCount;
191 | }
192 |
193 | /**
194 | * Checks if we have to request the next ad block for this position.
195 | *
196 | * @param position the adapter position
197 | * @param fetchingAdsCount the count of fetched and currently fetching ads
198 | * @return {@code true} if an ad is not available to publish and we should fetch one, {@code false} otherwise
199 | */
200 | public boolean hasToFetchAd(int position, int fetchingAdsCount){
201 | int adIndex = getAdIndex(position);
202 | int firstAdPos = getOffsetValue();
203 | return position >= firstAdPos && adIndex >= 0 && adIndex < getLimitOfAds() && adIndex >= fetchingAdsCount;
204 | }
205 | }
206 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | * Copyright (c) 2021 Clockbyte LLC. All rights reserved.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | package com.clockbyte.admobadapter;
17 |
18 | import android.content.Context;
19 | import android.database.DataSetObserver;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 | import android.widget.BaseAdapter;
23 |
24 | import com.google.android.gms.ads.nativead.NativeAd;
25 | import com.google.android.gms.ads.nativead.NativeAdView;
26 |
27 | import java.util.Collection;
28 | import java.util.Collections;
29 |
30 | /**
31 | * Adapter that has common functionality for any adapters that need to show ads in-between
32 | * other data.
33 | */
34 | public class AdmobAdapterWrapper extends BaseAdapter implements AdmobFetcherBase.AdmobListener {
35 |
36 | private final String TAG = AdmobAdapterWrapper.class.getCanonicalName();
37 |
38 | private BaseAdapter mAdapter;
39 |
40 | public BaseAdapter getAdapter() {
41 | return mAdapter;
42 | }
43 |
44 | public void setAdapter(BaseAdapter adapter) {
45 | mAdapter = adapter;
46 | mAdapter.registerDataSetObserver(new DataSetObserver() {
47 | @Override
48 | public void onChanged() {
49 | notifyDataSetChanged();
50 | }
51 |
52 | @Override
53 | public void onInvalidated() {
54 | notifyDataSetInvalidated();
55 | }
56 | });
57 | }
58 |
59 | private AdmobFetcher adFetcher;
60 | private AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator();
61 | /*
62 | * Gets an object which incapsulates transformation of the source and ad blocks indices
63 | */
64 | public AdmobAdapterCalculator getAdapterCalculator(){return AdapterCalculator;}
65 | /*
66 | * Injects an object which incapsulates transformation of the source and ad blocks indices. You could override calculations
67 | * by inheritance of AdmobAdapterCalculator class
68 | */
69 | public void setAdapterCalculator(AdmobAdapterCalculator adapterCalculatordmob){AdapterCalculator = adapterCalculatordmob;}
70 |
71 | private static final int VIEW_TYPE_COUNT = 1;
72 | private static final int VIEW_TYPE_AD = 0;
73 | private final static int DEFAULT_NO_OF_DATA_BETWEEN_ADS = 10;
74 | private final static int DEFAULT_LIMIT_OF_ADS = 3;
75 |
76 | /**
77 | * Gets the number of ads that have been fetched so far.
78 | *
79 | * @return the number of ads that have been fetched
80 | */
81 | public int getFetchedAdsCount() {
82 | return adFetcher.getFetchedAdsCount();
83 | }
84 |
85 | /**
86 | * Gets the number of ads have been fetched so far + currently fetching ads
87 | *
88 | * @return the number of already fetched ads + currently fetching ads
89 | */
90 | public int getFetchingAdsCount(){
91 | return adFetcher.getFetchingAdsCount();
92 | }
93 |
94 | public int getViewTypeAd(){
95 | return mAdapter.getViewTypeCount() + VIEW_TYPE_AD;
96 | }
97 |
98 | /*
99 | * Gets the number of your data items between ad blocks, by default it equals to 10.
100 | * You should set it according to the Admob's policies and rules which says not to
101 | * display more than one ad block at the visible part of the screen
102 | * so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
103 | */
104 | public int getNoOfDataBetweenAds() {
105 | return AdapterCalculator.getNoOfDataBetweenAds();
106 | }
107 | /*
108 | * Sets the number of your data items between ad blocks, by default it equals to 10.
109 | * You should set it according to the Admob's policies and rules which says not to
110 | * display more than one ad block at the visible part of the screen
111 | * so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
112 | */
113 | public void setNoOfDataBetweenAds(int mNoOfDataBetweenAds) {
114 | AdapterCalculator.setNoOfDataBetweenAds(mNoOfDataBetweenAds);
115 | }
116 |
117 | public int getFirstAdIndex() {
118 | return AdapterCalculator.getFirstAdIndex();
119 | }
120 | /*
121 | * Sets the first ad block index (zero-based) in the adapter, by default it equals to 0
122 | */
123 | public void setFirstAdIndex(int firstAdIndex) {
124 | AdapterCalculator.setFirstAdIndex(firstAdIndex);
125 | }
126 |
127 | /*
128 | * Gets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
129 | */
130 | public int getLimitOfAds() {
131 | return AdapterCalculator.getLimitOfAds();
132 | }
133 |
134 | /*
135 | * Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
136 | */
137 | public void setLimitOfAds(int mLimitOfAds) {
138 | AdapterCalculator.setLimitOfAds(mLimitOfAds);
139 | }
140 |
141 | private NativeAdLayoutContext mAdsLayoutContext;
142 |
143 | /*
144 | * Gets the context (the res layout id and a strategy of inflating and binding) for published install app ads {@link https://support.google.com/admob/answer/6240809}
145 | */
146 | public NativeAdLayoutContext getAdsLayoutContext() {
147 | return mAdsLayoutContext;
148 | }
149 |
150 | /**
151 | * Sets the context (the res layout id and a strategy of inflating and binding) for published app ads
152 | */
153 | public void setAdsLayoutContext(NativeAdLayoutContext mAdsLayoutContext) {
154 | this.mAdsLayoutContext = mAdsLayoutContext;
155 | }
156 |
157 | /**
158 | * Use this constructor for test purposes. if you are going to release the live version
159 | * please use the appropriate constructor
160 | * @see #AdmobAdapterWrapper(Context, String)
161 | * @param testDevicesId sets a devices ID to test ads interaction.
162 | * You could pass null but it's better to set ids for all your test devices
163 | * including emulators. for emulator just use the
164 | * @see {AdRequest.DEVICE_ID_EMULATOR}
165 | */
166 | public AdmobAdapterWrapper(Context context, String[] testDevicesId) {
167 | init(context, null, testDevicesId);
168 | }
169 | /**
170 | * @param admobReleaseUnitId sets a release unit ID for admob banners.
171 | * If you are testing the ads please use constructor for tests
172 | * @see #AdmobAdapterWrapper(Context, String[])
173 | * ID should be active, please check it in your Admob's account.
174 | * Be careful: don't set it or set to null if you still haven't deployed a Release.
175 | * Otherwise your Admob account could be banned
176 | */
177 | public AdmobAdapterWrapper(Context context, String admobReleaseUnitId) {
178 | Collection releaseUnitIds = admobReleaseUnitId==null
179 | ? null
180 | : Collections.singletonList(admobReleaseUnitId);
181 | init(context, releaseUnitIds, null);
182 | }
183 |
184 | /**
185 | * @param admobReleaseUnitId sets a release unit ID for admob banners.
186 | * If you are testing the ads please use constructor for tests
187 | * @see #AdmobAdapterWrapper(Context, String[]) or supply a
188 | * test ID here.
189 | * ID should be active, please check it in your Admob's account.
190 | * Be careful: don't set it or set to null if you still haven't deployed a Release.
191 | * Otherwise your Admob account could be banned
192 | */
193 | public AdmobAdapterWrapper(Context context, String admobReleaseUnitId, String[] testDevicesId) {
194 | init(context, Collections.singletonList(admobReleaseUnitId), testDevicesId);
195 | }
196 |
197 | private void init(Context context, Collection admobReleaseUnitIds, String[] testDevicesId){
198 | setNoOfDataBetweenAds(DEFAULT_NO_OF_DATA_BETWEEN_ADS);
199 | setLimitOfAds(DEFAULT_LIMIT_OF_ADS);
200 | setAdsLayoutContext(AdLayoutContext.getDefault());
201 |
202 | adFetcher = new AdmobFetcher();
203 | if(testDevicesId!=null)
204 | for (String testId: testDevicesId)
205 | adFetcher.addTestDeviceId(testId);
206 | if(admobReleaseUnitIds!=null)
207 | adFetcher.setReleaseUnitIds(admobReleaseUnitIds);
208 | adFetcher.addListener(this);
209 | // Start prefetching ads
210 | adFetcher.prefetchAds(context.getApplicationContext());
211 | }
212 |
213 | @Override
214 | public View getView(int position, View convertView, ViewGroup parent) {
215 | int itemViewType = getItemViewType(position);
216 | if(itemViewType == getViewTypeAd()) {
217 | NativeAd ad1 = (NativeAd) getItem(position);
218 | NativeAdView lvi1 = (convertView == null)
219 | ? getAdsLayoutContext().inflateView(parent)
220 | : (NativeAdView) convertView;
221 | getAdsLayoutContext().bind(lvi1, ad1);
222 | return lvi1;
223 | } else {
224 | int origPos = AdapterCalculator.getOriginalContentPosition(position,
225 | adFetcher.getFetchedAdsCount(), mAdapter.getCount());
226 | return mAdapter.getView(origPos, convertView, parent);
227 | }
228 | }
229 |
230 | /**
231 | *
Gets the count of all data, including interspersed ads.
232 | *
233 | *
If data size is 10 and an ad is to be showed after every 5 items starting at the index 0, this method
234 | * will return 12.
235 | *
236 | * @return the total number of items this adapter can show, including ads.
237 | * @see AdmobAdapterWrapper#setNoOfDataBetweenAds(int)
238 | * @see AdmobAdapterWrapper#getNoOfDataBetweenAds()
239 | */
240 | @Override
241 | public int getCount() {
242 |
243 | if (mAdapter != null) {
244 | /*
245 | No of currently fetched ads, as long as it isn't more than no of max ads that can
246 | fit dataset.
247 | */
248 | int noOfAds = AdapterCalculator.getAdsCountToPublish(adFetcher.getFetchedAdsCount(), mAdapter.getCount());
249 | return mAdapter.getCount() > 0 ? mAdapter.getCount() + noOfAds : 0;
250 | } else {
251 | return 0;
252 | }
253 | }
254 |
255 | /**
256 | * Gets the item in a given position in the dataset. If an ad is to be returned,
257 | * a {@link NativeAd} object is returned.
258 | *
259 | * @param position the adapter position
260 | * @return the object or ad contained in this adapter position
261 | */
262 | @Override
263 | public Object getItem(int position) {
264 |
265 | if (AdapterCalculator.canShowAdAtPosition(position, adFetcher.getFetchedAdsCount())) {
266 | int adPos = AdapterCalculator.getAdIndex(position);
267 | return adFetcher.getAdForIndex(adPos);
268 | } else {
269 | int origPos = AdapterCalculator.getOriginalContentPosition(position,
270 | adFetcher.getFetchedAdsCount(), mAdapter.getCount());
271 | return mAdapter.getItem(origPos);
272 | }
273 | }
274 |
275 | @Override
276 | public long getItemId(int position) {
277 | return position;
278 | }
279 |
280 | @Override
281 | public int getViewTypeCount() {
282 | return VIEW_TYPE_COUNT + getAdapter().getViewTypeCount();
283 | }
284 |
285 | @Override
286 | public int getItemViewType(int position) {
287 | if (AdapterCalculator.canShowAdAtPosition(position, adFetcher.getFetchedAdsCount())) {
288 | return getViewTypeAd();
289 | } else {
290 | int origPos = AdapterCalculator.getOriginalContentPosition(position,
291 | adFetcher.getFetchedAdsCount(), mAdapter.getCount());
292 | return mAdapter.getItemViewType(origPos);
293 | }
294 | }
295 |
296 | /**
297 | * Destroys all currently fetched ads
298 | */
299 | public void destroyAds() {
300 | adFetcher.destroyAllAds();
301 | }
302 |
303 | /**
304 | * Clears all currently displaying ads to update them
305 | */
306 | public void requestUpdateAd() {
307 | adFetcher.clearMapAds();
308 | }
309 |
310 | @Override
311 | public void onAdLoaded(int adIdx) {
312 | notifyDataSetChanged();
313 | }
314 |
315 | @Override
316 | public void onAdsCountChanged() {
317 | notifyDataSetChanged();
318 | }
319 |
320 | @Override
321 | public void onAdFailed(int adIdx, int errorCode, Object adPayload) {
322 | notifyDataSetChanged();
323 | }
324 |
325 | }
326 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobFetcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | * Copyright (c) 2021 Clockbyte LLC. All rights reserved.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | package com.clockbyte.admobadapter;
17 |
18 | import android.content.Context;
19 | import android.text.TextUtils;
20 | import android.util.Log;
21 | import android.util.SparseArray;
22 |
23 | import androidx.annotation.NonNull;
24 |
25 | import com.google.android.gms.ads.AdListener;
26 | import com.google.android.gms.ads.AdLoader;
27 | import com.google.android.gms.ads.LoadAdError;
28 | import com.google.android.gms.ads.nativead.NativeAd;
29 | import com.google.android.gms.ads.nativead.NativeAdOptions;
30 |
31 | import java.util.ArrayList;
32 | import java.util.Collection;
33 | import java.util.List;
34 |
35 | public class AdmobFetcher extends AdmobFetcherBase{
36 |
37 | private final String TAG = AdmobFetcher.class.getCanonicalName();
38 |
39 | /**
40 | * Maximum number of ads to prefetch.
41 | */
42 | public static final int PREFETCHED_ADS_SIZE = 2;
43 | /**
44 | * Maximum number of times to try fetch an ad after failed attempts.
45 | */
46 | private static final int MAX_FETCH_ATTEMPT = 4;
47 |
48 | private int mFetchingAdsCnt = 0;
49 | private AdLoader adLoader;
50 | private final List mPrefetchedAdList = new ArrayList<>();
51 | private final SparseArray adMapAtIndex = new SparseArray<>();
52 |
53 | private final List mAdmobReleaseUnitIds = new ArrayList<>();
54 |
55 | /**
56 | * Gets native ad at a particular index in the fetched ads list.
57 | *
58 | * @param index the index of ad in the fetched ads list
59 | * @return the native ad in the list
60 | * @see #getFetchedAdsCount()
61 | */
62 | public synchronized NativeAd getAdForIndex(final int index) {
63 | NativeAd adNative = null;
64 | if(index >= 0)
65 | adNative = adMapAtIndex.get(index);
66 |
67 | if (adNative == null && mPrefetchedAdList.size() > 0) {
68 | adNative = mPrefetchedAdList.remove(0);
69 |
70 | if (adNative != null) {
71 | adMapAtIndex.put(index, adNative);
72 | }
73 | }
74 |
75 | ensurePrefetchAmount(); // Make sure we have enough pre-fetched ads
76 | return adNative;
77 | }
78 |
79 | @Override
80 | public synchronized int getFetchingAdsCount() {
81 | return mFetchingAdsCnt;
82 | }
83 |
84 | /**
85 | * Fetches a new native ad.
86 | *
87 | * @param context the current context.
88 | * @see #destroyAllAds()
89 | */
90 | public synchronized void prefetchAds(Context context) {
91 | super.prefetchAds(context);
92 | setupAds();
93 | fetchAd();
94 | }
95 |
96 | /**
97 | * Destroys ads that have been fetched, that are still being fetched and removes all resource
98 | * references that this instance still has. This should only be called when the Activity that
99 | * is showing ads is closing.
100 | *
101 | * The converse of this call is {@link #prefetchAds(Context)}.
102 | */
103 | public synchronized void destroyAllAds() {
104 | mFetchingAdsCnt = 0;
105 | adMapAtIndex.clear();
106 | mPrefetchedAdList.clear();
107 |
108 | Log.i(TAG, "destroyAllAds adList " + adMapAtIndex.size() + " prefetched " +
109 | mPrefetchedAdList.size());
110 |
111 | super.destroyAllAds();
112 | }
113 |
114 | /**
115 | * Destroys all the ads in Map to refresh it with new one
116 | * */
117 | public synchronized void clearMapAds() {
118 | adMapAtIndex.clear();
119 | mFetchingAdsCnt = mPrefetchedAdList.size();
120 | }
121 |
122 | /**
123 | * Fetches a new native ad.
124 | */
125 | protected synchronized void fetchAd() {
126 | Context context = mContext.get();
127 |
128 | if (context != null) {
129 | Log.i(TAG, "Fetching Ad now");
130 | if(lockFetch.getAndSet(true))
131 | return;
132 | mFetchingAdsCnt++;
133 | adLoader.loadAd(getAdRequest()); //Fetching the ads item
134 | } else {
135 | mFetchFailCount++;
136 | Log.i(TAG, "Context is null, not fetching Ad");
137 | }
138 | }
139 |
140 | /**
141 | * Ensures that the necessary amount of prefetched native ads are available.
142 | */
143 | protected synchronized void ensurePrefetchAmount() {
144 | if (mPrefetchedAdList.size() < PREFETCHED_ADS_SIZE &&
145 | (mFetchFailCount < MAX_FETCH_ATTEMPT)) {
146 | fetchAd();
147 | }
148 | }
149 |
150 | /**
151 | * Determines if the native ad can be used.
152 | *
153 | * @param adNative the native ad object
154 | * @return true if the ad object can be used, false otherwise
155 | */
156 | private boolean canUseThisAd(NativeAd adNative) {
157 | if (adNative == null)
158 | return false;
159 | NativeAd.Image logoImage = adNative.getIcon();
160 | CharSequence header = adNative.getHeadline();
161 | CharSequence body = adNative.getBody();
162 |
163 | return !TextUtils.isEmpty(header)
164 | && !TextUtils.isEmpty(body)
165 | && logoImage != null;
166 | }
167 |
168 | public String getDefaultUnitId() {
169 | return mContext.get().getResources().getString(R.string.test_admob_unit_id);
170 | }
171 |
172 | private String getReleaseUnitId() {
173 | return mAdmobReleaseUnitIds.size() > 0 ? mAdmobReleaseUnitIds.get(0) : null;
174 | }
175 |
176 | /**
177 | * Subscribing to the native ads events
178 | */
179 | protected synchronized void setupAds() {
180 | String unitId = getReleaseUnitId() != null ? getReleaseUnitId() : getDefaultUnitId();
181 | AdLoader.Builder adloaderBuilder = new AdLoader.Builder(mContext.get(), unitId)
182 | .withAdListener(new AdListener() {
183 | @Override
184 | public void onAdFailedToLoad(@NonNull LoadAdError error) {
185 | // Handle the failure by logging, altering the UI, etc.
186 | Log.i(TAG, "onAdFailedToLoad " + error.getCode());
187 | lockFetch.set(false);
188 | mFetchFailCount++;
189 | mFetchingAdsCnt--;
190 | ensurePrefetchAmount();
191 | onAdFailed(mPrefetchedAdList.size(), error.getCode(), null);
192 | }
193 | })
194 | .withNativeAdOptions(new NativeAdOptions.Builder()
195 | // Methods in the NativeAdOptions.Builder class can be
196 | // used here to specify individual options settings.
197 | .build());
198 |
199 | adloaderBuilder.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
200 | @Override
201 | public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
202 | onAdFetched(nativeAd);
203 | }
204 | });
205 | adLoader = adloaderBuilder.build();
206 | }
207 |
208 | /**
209 | * A handler for received native ads
210 | */
211 | private synchronized void onAdFetched(NativeAd adNative) {
212 | Log.i(TAG, "onAdFetched");
213 | int index = -1;
214 | if (canUseThisAd(adNative)) {
215 | mPrefetchedAdList.add(adNative);
216 | index = mPrefetchedAdList.size()-1;
217 | mNoOfFetchedAds++;
218 | }
219 | lockFetch.set(false);
220 | mFetchFailCount = 0;
221 | ensurePrefetchAmount();
222 | onAdLoaded(index);
223 | }
224 |
225 | public void setReleaseUnitIds(Collection admobReleaseUnitIds) {
226 | if(admobReleaseUnitIds.size() > 1)
227 | throw new RuntimeException("Currently only supports one unit id.");
228 |
229 | mAdmobReleaseUnitIds.addAll(admobReleaseUnitIds);
230 | }
231 | }
232 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobFetcherBase.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | package com.clockbyte.admobadapter;
17 |
18 | import android.content.Context;
19 | import android.os.Handler;
20 |
21 | import com.google.android.gms.ads.AdRequest;
22 |
23 | import java.lang.ref.WeakReference;
24 | import java.util.ArrayList;
25 | import java.util.List;
26 | import java.util.concurrent.atomic.AtomicBoolean;
27 |
28 | public abstract class AdmobFetcherBase {
29 |
30 | protected List mAdNativeListeners = new ArrayList<>();
31 | protected int mNoOfFetchedAds;
32 | protected int mFetchFailCount;
33 | protected WeakReference mContext = new WeakReference<>(null);
34 | protected AtomicBoolean lockFetch = new AtomicBoolean();
35 |
36 | protected ArrayList testDeviceId = new ArrayList<>();
37 | /*
38 | *Gets a test device ID. Normally you don't have to set it
39 | */
40 | public ArrayList getTestDeviceIds() {
41 | return testDeviceId;
42 | }
43 | /*
44 | *Sets a test device ID. Normally you don't have to set it
45 | */
46 | public void addTestDeviceId(String testDeviceId) {
47 | this.testDeviceId.add(testDeviceId);
48 | }
49 |
50 | /**
51 | * Adds an {@link AdmobListener} that would be notified for any changes to the native ads
52 | * list.
53 | *
54 | * @param listener the listener to be notified
55 | */
56 | public synchronized void addListener(AdmobListener listener) {
57 | mAdNativeListeners.add(listener);
58 | }
59 |
60 | /**
61 | * Gets the number of ads that have been fetched so far.
62 | *
63 | * @return the number of ads that have been fetched
64 | */
65 | public synchronized int getFetchedAdsCount() {
66 | return mNoOfFetchedAds;
67 | }
68 | /**
69 | * Gets the number of ads that have been fetched and are currently being fetched
70 | *
71 | * @return the number of ads that have been fetched and are currently being fetched
72 | */
73 | public abstract int getFetchingAdsCount();
74 |
75 | public int getFetchFailCount() {
76 | return mFetchFailCount;
77 | }
78 |
79 | /**
80 | * Fetches a new native ad.
81 | *
82 | * @param context the current context.
83 | * @see #destroyAllAds()
84 | */
85 | public synchronized void prefetchAds(Context context) {
86 | mContext = new WeakReference<>(context);
87 | }
88 |
89 | /**
90 | * Destroys ads that have been fetched, that are still being fetched and removes all resource
91 | * references that this instance still has. This should only be called when the Activity that
92 | * is showing ads is closing.
93 | *
94 | * The converse of this call is {@link #prefetchAds(Context)}.
95 | */
96 | public synchronized void destroyAllAds() {
97 | mFetchFailCount = 0;
98 | mNoOfFetchedAds = 0;
99 | onAdsCountChanged();
100 | }
101 |
102 | /**
103 | * Frees all weak refs and collections
104 | */
105 | public void release(){
106 | destroyAllAds();
107 | mContext.clear();
108 | }
109 |
110 | /**
111 | * Notifies all registered {@link AdmobListener} on a change of ad count.
112 | */
113 | protected void onAdsCountChanged() {
114 | final Context context = mContext.get();
115 | //context may be null if activity is destroyed
116 | if(context != null) {
117 | new Handler(context.getMainLooper()).post(new Runnable() {
118 | @Override
119 | public void run() {
120 | for (AdmobListener listener : mAdNativeListeners)
121 | listener.onAdsCountChanged();
122 | }
123 | });
124 | }
125 | }
126 |
127 | /**
128 | * Notifies all registered {@link AdmobListener} on a loaded ad.
129 | */
130 | protected void onAdLoaded(final int adIdx) {
131 | final Context context = mContext.get();
132 | //context may be null if activity is destroyed
133 | if(context != null) {
134 | new Handler(context.getMainLooper()).post(new Runnable() {
135 | @Override
136 | public void run() {
137 | for (AdmobListener listener : mAdNativeListeners)
138 | listener.onAdLoaded(adIdx);
139 | }
140 | });
141 | }
142 | }
143 |
144 | /**
145 | * Notifies all registered {@link AdmobListener} on a failed ad.
146 | */
147 | protected void onAdFailed(final int adIdx, final int errorCode, final Object adPayload) {
148 | final Context context = mContext.get();
149 | //context may be null if activity is destroyed
150 | if(context != null) {
151 | for (AdmobListener listener : mAdNativeListeners)
152 | listener.onAdFailed(adIdx, errorCode, adPayload);
153 | }
154 | }
155 |
156 | /**
157 | * Setup and get an ads request
158 | */
159 | protected synchronized AdRequest getAdRequest() {
160 | return new AdRequest.Builder().build();
161 | }
162 |
163 | /**
164 | * Listener that is notified when changes to the list of fetched native ads are made.
165 | */
166 | public interface AdmobListener {
167 | /**
168 | * Raised when the ad has loaded. Adapters that implement this class
169 | * should notify their data views that the dataset has changed.
170 | * @param adIdx the index of ad block which state was changed.
171 | * See {@link AdmobAdapterCalculator} for methods to transform {@param adIdx} to adapter wrapper's indices
172 | */
173 | void onAdLoaded(int adIdx);
174 | /**
175 | * Raised when the number of ads have changed. Adapters that implement this class
176 | * should notify their data views that the dataset has changed.
177 | */
178 | void onAdsCountChanged();
179 |
180 | /**
181 | * Raised when the ad has failed to load.
182 | * @param adIdx the index of ad block which state was changed.
183 | * @param adPayload filled with some specific for current platform payload
184 | * See {@link AdmobAdapterCalculator} for methods to transform {@param adIdx} to adapter wrapper's indices
185 | */
186 | void onAdFailed(int adIdx, int errorCode, Object adPayload);
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobRecyclerAdapterWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | package com.clockbyte.admobadapter;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.content.Context;
20 | import android.view.ViewGroup;
21 |
22 | import androidx.annotation.NonNull;
23 | import androidx.recyclerview.widget.RecyclerView;
24 |
25 | import com.google.android.gms.ads.nativead.NativeAd;
26 | import com.google.android.gms.ads.nativead.NativeAdView;
27 |
28 | import java.util.Collection;
29 | import java.util.Collections;
30 | import java.util.EnumSet;
31 |
32 | /**
33 | * Adapter that has common functionality for any adapters that need to show ads in-between
34 | * other data.
35 | */
36 | public class AdmobRecyclerAdapterWrapper
37 | extends RecyclerView.Adapter
38 | implements AdmobFetcherBase.AdmobListener {
39 |
40 | private final String TAG = AdmobRecyclerAdapterWrapper.class.getCanonicalName();
41 |
42 | private RecyclerView.Adapter mAdapter;
43 |
44 | public RecyclerView.Adapter getAdapter() {
45 | return mAdapter;
46 | }
47 |
48 | @SuppressLint("NotifyDataSetChanged")
49 | public void setAdapter(RecyclerView.Adapter adapter) {
50 | mAdapter = adapter;
51 | mAdapter.registerAdapterDataObserver(new AdapterWrapperObserver(this, getAdapterCalculator(), adFetcher));
52 | notifyDataSetChanged();
53 | }
54 |
55 | private AdmobFetcher adFetcher;
56 | private AdmobAdapterCalculator AdapterCalculator = new AdmobAdapterCalculator();
57 | /**
58 | * Gets an object which incapsulates transformation of the source and ad blocks indices
59 | */
60 | public AdmobAdapterCalculator getAdapterCalculator(){return AdapterCalculator;}
61 | /**
62 | * Injects an object which incapsulates transformation of the source and ad blocks indices. You could override calculations
63 | * by inheritance of AdmobAdapterCalculator class
64 | */
65 | public void setAdapterCalculator(AdmobAdapterCalculator adapterCalculatordmob){AdapterCalculator = adapterCalculatordmob;}
66 |
67 | private static final int VIEW_TYPE_AD = 0;
68 |
69 | private final static int DEFAULT_NO_OF_DATA_BETWEEN_ADS = 10;
70 | private final static int DEFAULT_LIMIT_OF_ADS = 3;
71 | private final static int DEFAULT_VIEWTYPE_SOURCE_MAX = 0;
72 |
73 | /**
74 | * Gets the number of ads that have been fetched so far.
75 | *
76 | * @return the number of ads that have been fetched
77 | */
78 | public int getFetchedAdsCount() {
79 | return adFetcher.getFetchedAdsCount();
80 | }
81 |
82 | /**
83 | * Gets the number of ads have been fetched so far + currently fetching ads
84 | *
85 | * @return the number of already fetched ads + currently fetching ads
86 | */
87 | public int getFetchingAdsCount(){
88 | return adFetcher.getFetchingAdsCount();
89 | }
90 |
91 | private int getViewTypeAd(){
92 | return getViewTypeBiggestSource() + VIEW_TYPE_AD + 1;
93 | }
94 |
95 | /*
96 | * Gets the number of your data items between ad blocks, by default it equals to 10.
97 | * You should set it according to the Admob's policies and rules which says not to
98 | * display more than one ad block at the visible part of the screen
99 | * so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
100 | */
101 | public int getNoOfDataBetweenAds() {
102 | return AdapterCalculator.getNoOfDataBetweenAds();
103 | }
104 | /*
105 | * Sets the number of your data items between ad blocks, by default it equals to 10.
106 | * You should set it according to the Admob's policies and rules which says not to
107 | * display more than one ad block at the visible part of the screen
108 | * so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
109 | */
110 | public void setNoOfDataBetweenAds(int mNoOfDataBetweenAds) {
111 | AdapterCalculator.setNoOfDataBetweenAds(mNoOfDataBetweenAds);
112 | }
113 |
114 | public int getFirstAdIndex() {
115 | return AdapterCalculator.getFirstAdIndex();
116 | }
117 | /*
118 | * Sets the first ad block index (zero-based) in the adapter, by default it equals to 0
119 | */
120 | public void setFirstAdIndex(int firstAdIndex) {
121 | AdapterCalculator.setFirstAdIndex(firstAdIndex);
122 | }
123 |
124 | /*
125 | * Gets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
126 | */
127 | public int getLimitOfAds() {
128 | return AdapterCalculator.getLimitOfAds();
129 | }
130 |
131 | /*
132 | * Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
133 | */
134 | public void setLimitOfAds(int mLimitOfAds) {
135 | AdapterCalculator.setLimitOfAds(mLimitOfAds);
136 | }
137 |
138 | private int viewTypeBiggestSource;
139 | /*
140 | * Gets the biggest value among all the view types in the underlying source adapter, by default it equals to 0.
141 | */
142 | public int getViewTypeBiggestSource() {
143 | return viewTypeBiggestSource;
144 | }
145 |
146 | /*
147 | * Sets the biggest value among all the view types in the underlying source adapter, by default it equals to 0.
148 | */
149 | public void setViewTypeBiggestSource(int viewTypeBiggestSource) {
150 | this.viewTypeBiggestSource = viewTypeBiggestSource;
151 | }
152 |
153 | private NativeAdLayoutContext mAdsLayoutContext;
154 |
155 | /**
156 | * Gets the context (the res layout id and a strategy of inflating and binding) for published
157 | * app ads
158 | */
159 | public NativeAdLayoutContext getAdsLayoutContext() {
160 | return mAdsLayoutContext;
161 | }
162 |
163 | /**
164 | * Sets the context (the res layout id and a strategy of inflating and binding) for published
165 | * app ads
166 | */
167 | public void setAdsLayoutContext(NativeAdLayoutContext mAdsLayoutContext) {
168 | this.mAdsLayoutContext = mAdsLayoutContext;
169 | }
170 |
171 | /**
172 | * Use this constructor for test purposes. if you are going to release the live version
173 | * please use the appropriate constructor
174 | * @see #AdmobRecyclerAdapterWrapper(Context, String)
175 | * @param testDevicesId sets a devices ID to test ads interaction.
176 | * You could pass null but it's better to set ids for all your test devices
177 | * including emulators. for emulator just use the
178 | * @see {AdRequest.DEVICE_ID_EMULATOR}
179 | */
180 | public AdmobRecyclerAdapterWrapper(Context context, String[] testDevicesId) {
181 | init(context, null, testDevicesId);
182 | }
183 |
184 | /**
185 | * @param admobReleaseUnitId sets a release unit ID for admob banners.
186 | * If you are testing the ads please use constructor for tests
187 | * @see #AdmobRecyclerAdapterWrapper(Context, String[])
188 | * ID should be active, please check it in your Admob's account.
189 | * Be careful: don't set it or set to null if you still haven't deployed a Release.
190 | * Otherwise your Admob account could be banned
191 | */
192 | public AdmobRecyclerAdapterWrapper(Context context, String admobReleaseUnitId) {
193 | Collection releaseUnitIds = admobReleaseUnitId==null
194 | ? null
195 | : Collections.singletonList(admobReleaseUnitId);
196 | init(context, releaseUnitIds, null);
197 | }
198 |
199 | /**
200 | * @param admobReleaseUnitIds sets a release unit ID for admob banners.
201 | * It works like FIFO (first in = first out). Each ad block will get one from the queue.
202 | * If the desired count of ad blocks is greater than this collection size
203 | * then the last entry will be duplicated to remaining ad blocks.
204 | * If you are testing the ads please use constructor for tests
205 | * @see #AdmobRecyclerAdapterWrapper(Context, String[])
206 | * ID should be active, please check it in your Admob's account.
207 | * Be careful: don't set it or set to null if you still haven't deployed a Release.
208 | * Otherwise your Admob account could be banned
209 | */
210 | public AdmobRecyclerAdapterWrapper(Context context, Collection admobReleaseUnitIds) {
211 | init(context, admobReleaseUnitIds, null);
212 | }
213 |
214 | /**
215 | * @param admobReleaseUnitId sets a release unit ID for admob banners.
216 | * If you are testing the ads please use constructor for tests
217 | * @see #AdmobRecyclerAdapterWrapper(Context, String[]) or supply a
218 | * test ID here.
219 | * ID should be active, please check it in your Admob's account.
220 | * Be careful: don't set it or set to null if you still haven't deployed a Release.
221 | * Otherwise your Admob account could be banned
222 | */
223 | public AdmobRecyclerAdapterWrapper(Context context, String admobReleaseUnitId, String[] testDevicesId) {
224 | init(context, Collections.singletonList(admobReleaseUnitId), testDevicesId);
225 | }
226 |
227 | private void init(Context context, Collection admobReleaseUnitIds, String[] testDevicesId){
228 | setViewTypeBiggestSource(DEFAULT_VIEWTYPE_SOURCE_MAX);
229 | setNoOfDataBetweenAds(DEFAULT_NO_OF_DATA_BETWEEN_ADS);
230 | setLimitOfAds(DEFAULT_LIMIT_OF_ADS);
231 | setAdsLayoutContext(AdLayoutContext.getDefault());
232 |
233 | adFetcher = new AdmobFetcher();
234 | if(testDevicesId!=null)
235 | for (String testId: testDevicesId)
236 | adFetcher.addTestDeviceId(testId);
237 | if(admobReleaseUnitIds!=null)
238 | adFetcher.setReleaseUnitIds(admobReleaseUnitIds);
239 | adFetcher.addListener(this);
240 | // Start prefetching ads
241 | adFetcher.prefetchAds(context.getApplicationContext());
242 | }
243 |
244 | @Override
245 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
246 | int itemViewType = viewHolder.getItemViewType();
247 | if (itemViewType == getViewTypeAd()) {
248 | NativeAdView lvi1 = (NativeAdView) viewHolder.itemView;
249 | NativeAd ad1 = (NativeAd) getItem(position);
250 | getAdsLayoutContext().bind(lvi1, ad1);
251 | } else {
252 | int origPos = AdapterCalculator.getOriginalContentPosition(position,
253 | adFetcher.getFetchedAdsCount(), mAdapter.getItemCount());
254 | mAdapter.onBindViewHolder(viewHolder, origPos);
255 | }
256 | }
257 |
258 | @Override
259 | public final RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
260 | if (viewType == getViewTypeAd()) {
261 | return new NativeHolder(onCreateItemView(parent, viewType));
262 | }
263 | else{
264 | return mAdapter.onCreateViewHolder(parent, viewType);
265 | }
266 | }
267 |
268 | private NativeAdView onCreateItemView(ViewGroup parent, int viewType) {
269 | if (viewType == getViewTypeAd())
270 | return getAdsLayoutContext().inflateView(parent);
271 | else return null;
272 | }
273 |
274 | /**
275 | *
Gets the count of all data, including interspersed ads.
276 | *
277 | *
If data size is 10 and an ad is to be showed after every 5 items starting at the index 0,
278 | * this method will return 12.
279 | *
280 | * @return the total number of items this adapter can show, including ads.
281 | * @see AdmobRecyclerAdapterWrapper#setNoOfDataBetweenAds(int)
282 | * @see AdmobRecyclerAdapterWrapper#getNoOfDataBetweenAds()
283 | */
284 | @Override
285 | public int getItemCount() {
286 |
287 | if (mAdapter != null) {
288 | /*
289 | No of currently fetched ads, as long as it isn't more than no of max ads that can
290 | fit dataset.
291 | */
292 | int noOfAds = AdapterCalculator.getAdsCountToPublish(adFetcher.getFetchedAdsCount(),
293 | mAdapter.getItemCount());
294 | return mAdapter.getItemCount() > 0 ? mAdapter.getItemCount() + noOfAds : 0;
295 | } else {
296 | return 0;
297 | }
298 | }
299 |
300 | /**
301 | * Gets the item in a given position in the dataset. If an ad is to be returned,
302 | * a {@link NativeAd} object is returned.
303 | *
304 | *
305 | * @return the object or ad contained in this adapter position
306 | */
307 | public Object getItem(int position) {
308 |
309 | if (AdapterCalculator.canShowAdAtPosition(position, adFetcher.getFetchedAdsCount())) {
310 | int adPos = AdapterCalculator.getAdIndex(position);
311 | return adFetcher.getAdForIndex(adPos);
312 | }
313 | else return null;
314 | }
315 |
316 | @Override
317 | public long getItemId(int position) {
318 | return position;
319 | }
320 |
321 | @Override
322 | public int getItemViewType(int position) {
323 | if (AdapterCalculator.canShowAdAtPosition(position, adFetcher.getFetchedAdsCount())) {
324 | return getViewTypeAd();
325 | } else {
326 | int origPos = AdapterCalculator.getOriginalContentPosition(position,
327 | adFetcher.getFetchedAdsCount(), mAdapter.getItemCount());
328 | return mAdapter.getItemViewType(origPos);
329 | }
330 | }
331 |
332 | /**
333 | * Destroys all currently fetched ads
334 | */
335 | public void destroyAds() {
336 | adFetcher.destroyAllAds();
337 | }
338 |
339 | /**
340 | * Clears all currently displaying ads to update them
341 | */
342 | public void requestUpdateAd() {
343 | adFetcher.clearMapAds();
344 | }
345 |
346 | @SuppressLint("NotifyDataSetChanged")
347 | @Override
348 | public void onAdLoaded(int adIdx) {
349 | notifyDataSetChanged();
350 | }
351 |
352 | @SuppressLint("NotifyDataSetChanged")
353 | @Override
354 | public void onAdsCountChanged() {
355 | notifyDataSetChanged();
356 | }
357 |
358 | @SuppressLint("NotifyDataSetChanged")
359 | @Override
360 | public void onAdFailed(int adIdx, int errorCode, Object adPayload) {
361 | notifyDataSetChanged();
362 | }
363 |
364 | }
365 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/NativeAdLayoutContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 | import android.content.Context;
18 | import android.view.LayoutInflater;
19 | import android.view.ViewGroup;
20 |
21 | import com.google.android.gms.ads.nativead.NativeAd;
22 | import com.google.android.gms.ads.nativead.NativeAdView;
23 |
24 |
25 | /**
26 | * Created by FILM on 07.08.2016.
27 | */
28 | public abstract class NativeAdLayoutContext {
29 |
30 | private int mAdLayoutId;
31 |
32 | /*
33 | * Gets the res layout id for ads
34 | */
35 | public int getAdLayoutId() {
36 | return mAdLayoutId;
37 | }
38 |
39 | /*
40 | * Sets the res layout id for ads
41 | */
42 | public void setAdLayoutId(int mAdLayoutId) {
43 | this.mAdLayoutId = mAdLayoutId;
44 | }
45 |
46 | public abstract void bind(NativeAdView nativeAdView, NativeAd nativeAd);
47 |
48 | public NativeAdView inflateView(ViewGroup root) throws IllegalArgumentException{
49 | if(root == null) throw new IllegalArgumentException("root should be not null");
50 | // Inflate a layout and add it to the parent ViewGroup.
51 | LayoutInflater inflater = (LayoutInflater) root.getContext()
52 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
53 | return (NativeAdView) inflater
54 | .inflate(getAdLayoutId(), root, false);
55 | }
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/NativeHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter;
16 |
17 | import android.view.ViewGroup;
18 |
19 | import androidx.recyclerview.widget.RecyclerView;
20 |
21 | /**
22 | * Created by FILM on 28.10.2016.
23 | */
24 | public class NativeHolder extends RecyclerView.ViewHolder {
25 |
26 | public NativeHolder(ViewGroup adViewWrapper){
27 | super(adViewWrapper);
28 | }
29 |
30 | public ViewGroup getAdViewWrapper() {
31 | return (ViewGroup)itemView;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/bannerads/AdmobFetcherBanner.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Yahoo Inc. All rights reserved.
3 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | * Unless required by applicable law or agreed to in writing, software
10 | * distributed under the License is distributed on an "AS IS" BASIS,
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | * See the License for the specific language governing permissions and
13 | * limitations under the License.
14 | */
15 |
16 | package com.clockbyte.admobadapter.bannerads;
17 |
18 | import android.content.Context;
19 | import android.os.Handler;
20 | import android.util.Log;
21 |
22 | import androidx.annotation.NonNull;
23 |
24 | import com.clockbyte.admobadapter.AdmobFetcherBase;
25 | import com.google.android.gms.ads.AdListener;
26 | import com.google.android.gms.ads.AdView;
27 | import com.google.android.gms.ads.LoadAdError;
28 |
29 | import java.lang.ref.WeakReference;
30 | import java.util.ArrayList;
31 | import java.util.Collection;
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | public class AdmobFetcherBanner extends AdmobFetcherBase {
36 |
37 | private final String TAG = AdmobFetcherBanner.class.getCanonicalName();
38 |
39 | /**
40 | * Maximum number of ads to prefetch.
41 | */
42 | public static final int PREFETCHED_ADS_SIZE = 2;
43 |
44 | /**
45 | * Maximum number of times to try fetch an ad after failed attempts.
46 | */
47 | public static final int MAX_FETCH_ATTEMPT = 4;
48 |
49 | public AdmobFetcherBanner(Context context){
50 | super();
51 | mContext = new WeakReference<>(context);
52 | }
53 |
54 | private final List mPrefetchedAds = new ArrayList<>();
55 | private final BannerAdPresetCyclingList mAdPresetCyclingList = new BannerAdPresetCyclingList();
56 |
57 | /**
58 | * Gets next ad preset for Admob banners from FIFO .
59 | * It works like cycling FIFO (first in = first out, cycling from end to start).
60 | * Each ad block will get one from the queue.
61 | * If the desired count of ad blocks is greater than this collection size
62 | * then it will go again to the first item and iterate as much as it required.
63 | * ID should be active, please check it in your Admob's account.
64 | */
65 | public BannerAdPreset takeNextAdPreset() {
66 | return this.mAdPresetCyclingList.get();
67 | }
68 |
69 | /**
70 | * Sets an unit IDs for admob banners.
71 | * It works like cycling FIFO (first in = first out, cycling from end to start).
72 | * Each ad block will get one from the queue.
73 | * If the desired count of ad blocks is greater than this collection size
74 | * then it will go again to the first item and iterate as much as it required.
75 | * ID should be active, please check it in your Admob's account.
76 | */
77 | public void setAdPresets(Collection adPresets) {
78 | Collection mAdPresets = adPresets==null||adPresets.size() == 0
79 | ? Collections.singletonList(BannerAdPreset.DEFAULT)
80 | :adPresets;
81 | mAdPresetCyclingList.clear();
82 | mAdPresetCyclingList.addAll(mAdPresets);
83 | }
84 |
85 | public int getAdPresetsCount(){
86 | return this.mAdPresetCyclingList.size();
87 | }
88 |
89 | public BannerAdPreset getAdPresetSingleOr(BannerAdPreset defaultValue){
90 | return this.mAdPresetCyclingList.size() == 1 ? this.mAdPresetCyclingList.get() : defaultValue;
91 | }
92 |
93 | /**
94 | * Gets banner ad at a particular index in the fetched ads list.
95 | *
96 | * @param adPos the index of ad in the fetched ads list
97 | * @return the banner ad in the list
98 | * @see #getFetchedAdsCount()
99 | */
100 | public synchronized AdView getAdForIndex(int adPos) {
101 | if(adPos >= 0 && mPrefetchedAds.size() > adPos)
102 | return mPrefetchedAds.get(adPos);
103 | return null;
104 | }
105 |
106 | /**
107 | * Fetches a new banner ad.
108 | */
109 | protected synchronized void fetchAd(final AdView adView) {
110 | if(mFetchFailCount > MAX_FETCH_ATTEMPT)
111 | return;
112 |
113 | Context context = mContext.get();
114 |
115 | if (context != null) {
116 | Log.i(TAG, "Fetching Ad now");
117 | new Handler(context.getMainLooper()).post(new Runnable() {
118 | @Override
119 | public void run() {
120 | adView.loadAd(getAdRequest()); //Fetching the ads item
121 | }
122 | });
123 | } else {
124 | mFetchFailCount++;
125 | Log.i(TAG, "Context is null, not fetching Ad");
126 | }
127 | }
128 |
129 | /**
130 | * Subscribing to the banner ads events
131 | * @param adView
132 | */
133 | protected synchronized void setupAd(final AdView adView) {
134 | if(mFetchFailCount > MAX_FETCH_ATTEMPT)
135 | return;
136 |
137 | if(!mPrefetchedAds.contains(adView))
138 | mPrefetchedAds.add(adView);
139 | adView.setAdListener(new AdListener() {
140 | @Override
141 | public void onAdFailedToLoad(@NonNull LoadAdError error) {
142 | super.onAdFailedToLoad(error);
143 | // Handle the failure by logging, altering the UI, etc.
144 | onFailedToLoad(adView, error.getCode());
145 | }
146 | @Override
147 | public void onAdLoaded() {
148 | super.onAdLoaded();
149 | onFetched(adView);
150 | }
151 | });
152 | }
153 |
154 | /**
155 | * A handler for received banner ads
156 | * @param adView
157 | */
158 | private synchronized void onFetched(AdView adView) {
159 | Log.i(TAG, "onAdFetched");
160 | mFetchFailCount = 0;
161 | mNoOfFetchedAds++;
162 | onAdLoaded(mNoOfFetchedAds - 1);
163 | }
164 |
165 | /**
166 | * A handler for failed banner ads
167 | * @param adView
168 | */
169 | private synchronized void onFailedToLoad(AdView adView, int errorCode) {
170 | Log.i(TAG, "onAdFailedToLoad " + errorCode);
171 | mFetchFailCount++;
172 | mNoOfFetchedAds = Math.max(mNoOfFetchedAds - 1, 0);
173 | //Since Fetch Ad is only called once without retries
174 | //hide ad row / rollback its count if still not added to list
175 | mPrefetchedAds.remove(adView);
176 | onAdFailed(mNoOfFetchedAds - 1, errorCode, adView);
177 | }
178 |
179 | @Override
180 | public synchronized int getFetchingAdsCount() {
181 | return mPrefetchedAds.size();
182 | }
183 |
184 | public synchronized void pauseAll(){
185 | for(AdView ad:mPrefetchedAds){
186 | ad.pause();
187 | }
188 | }
189 |
190 | public synchronized void resumeAll(){
191 | for(AdView ad:mPrefetchedAds){
192 | ad.resume();
193 | }
194 | }
195 |
196 | @Override
197 | public synchronized void destroyAllAds() {
198 | super.destroyAllAds();
199 | for(AdView ad:mPrefetchedAds){
200 | ad.destroy();
201 | }
202 | mPrefetchedAds.clear();
203 | }
204 |
205 | @Override
206 | public void release() {
207 | super.release();
208 | mAdPresetCyclingList.clear();
209 | }
210 | }
211 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/bannerads/BannerAdPreset.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter.bannerads;
16 |
17 | import android.text.TextUtils;
18 |
19 | import com.google.android.gms.ads.AdSize;
20 |
21 | import java.util.Locale;
22 |
23 | public class BannerAdPreset {
24 | private static final String UNIT_ID_DEFAULT_BANNER = "ca-app-pub-3940256099942544/6300978111"; // Google's Test unit ID
25 | private static final AdSize SIZE_DEFAULT_BANNER = AdSize.FLUID;
26 | public static final BannerAdPreset DEFAULT = new BannerAdPreset(UNIT_ID_DEFAULT_BANNER, SIZE_DEFAULT_BANNER);
27 |
28 | private String adUnitId;
29 | private AdSize adSize;
30 |
31 | public BannerAdPreset(){
32 | this.adUnitId = UNIT_ID_DEFAULT_BANNER;
33 | this.adSize = SIZE_DEFAULT_BANNER;
34 | }
35 |
36 | public BannerAdPreset(String adUnitId){
37 | this();
38 | if(!TextUtils.isEmpty(adUnitId))
39 | this.adUnitId = adUnitId;
40 | }
41 |
42 | public BannerAdPreset(String adUnitId, AdSize adSize){
43 | this(adUnitId);
44 | if(adSize != null)
45 | this.adSize = adSize;
46 | }
47 |
48 | public String getAdUnitId(){
49 | return this.adUnitId;
50 | }
51 | public void setAdUnitId(String adUnitId){
52 | this.adUnitId = adUnitId;
53 | }
54 |
55 | public AdSize getAdSize(){
56 | return this.adSize;
57 | }
58 | public void setAdSize(AdSize adSize){
59 | this.adSize = adSize;
60 | }
61 |
62 | public boolean isValid(){
63 | return !TextUtils.isEmpty(this.adUnitId);
64 | }
65 |
66 | @Override
67 | public boolean equals(Object o) {
68 | if (o instanceof BannerAdPreset) {
69 | BannerAdPreset other = (BannerAdPreset) o;
70 | return (this.adUnitId.equals(other.adUnitId)
71 | && this.adSize.getHeight() == other.adSize.getHeight()
72 | && this.adSize.getWidth() == other.adSize.getWidth());
73 | }
74 | return false;
75 | }
76 |
77 | @Override
78 | public int hashCode() {
79 | int hash = 17;
80 | hash = hash * 31 + adUnitId.hashCode();
81 | hash = hash * 31 + adSize.hashCode();
82 | return hash;
83 | }
84 |
85 | @Override
86 | public String toString() {
87 | return String.format(Locale.getDefault(), "%s | %s", adUnitId, adSize);
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/bannerads/BannerAdPresetCyclingList.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter.bannerads;
16 |
17 | import java.util.ArrayList;
18 | import java.util.Collection;
19 |
20 | /**
21 | * Created by FILM on 02.10.2016.
22 | */
23 |
24 | public class BannerAdPresetCyclingList extends ArrayList {
25 |
26 | private int currentIdx = -1;
27 |
28 | public BannerAdPresetCyclingList(){
29 | super();
30 | }
31 |
32 | public int getCurrentIdx() {
33 | return currentIdx;
34 | }
35 |
36 | /**
37 | * Gets next ad preset for Admob banners from FIFO.
38 | * It works like cycling FIFO (first in = first out, cycling from end to start).
39 | * Each ad block will get one from the queue.
40 | * If the desired count of ad blocks is greater than this collection size
41 | * then it will go again to the first item and iterate as much as it required.
42 | * ID should be active, please check it in your Admob's account.
43 | */
44 | public BannerAdPreset get() {
45 | if(size() == 0) return null;
46 | if (size() == 1) return get(0);
47 | currentIdx = ++currentIdx % size();
48 | return get(currentIdx);
49 | }
50 |
51 | /**
52 | * Tries to add an item to collection if it is valid {@link BannerAdPreset#isValid()}
53 | * @return true if item was added, false - otherwise
54 | */
55 | @Override
56 | public boolean add(BannerAdPreset expressAdPreset) {
57 | return !(expressAdPreset == null || !expressAdPreset.isValid())
58 | && super.add(expressAdPreset);
59 | }
60 |
61 | /**
62 | * Tries to add items to collection if valid {@link BannerAdPreset#isValid()}
63 | * @return true if items were added, false - otherwise
64 | */
65 | @Override
66 | public boolean addAll(Collection extends BannerAdPreset> c) {
67 | ArrayList lst = new ArrayList<>();
68 | for (BannerAdPreset eap : c) {
69 | if(eap!=null && eap.isValid())
70 | lst.add(eap);
71 | }
72 | return super.addAll(lst);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/bannerads/BannerAdViewWrappingStrategy.java:
--------------------------------------------------------------------------------
1 | package com.clockbyte.admobadapter.bannerads;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 |
7 | import androidx.annotation.NonNull;
8 |
9 | import com.clockbyte.admobadapter.R;
10 | import com.google.android.gms.ads.AdView;
11 |
12 | /**
13 | * Created by L4grange on 14/02/2018
14 | */
15 |
16 | public class BannerAdViewWrappingStrategy extends BannerAdViewWrappingStrategyBase {
17 | /**
18 | * Add the Banner {@param ad} to {@param wrapper}.
19 | * See the super's implementation for instance.
20 | */
21 | @Override
22 | protected void addAdViewToWrapper(@NonNull ViewGroup wrapper, @NonNull AdView ad) {
23 | wrapper.addView(ad);
24 | }
25 |
26 | /**
27 | * This method can be overridden to recycle (remove) {@param ad} from {@param wrapper} view before adding to wrap.
28 | * By default it will look for {@param ad} in the direct children of {@param wrapper} and remove the first occurrence.
29 | * See the super's implementation for instance.
30 | * The NativeExpressHolder recycled by the RecyclerView may be a different
31 | * instance than the one used previously for this position. Clear the
32 | * wrapper of any subviews in case it has a different
33 | * AdView associated with it
34 | */
35 | @Override
36 | protected void recycleAdViewWrapper(@NonNull ViewGroup wrapper, @NonNull AdView ad) {
37 | for (int i = 0; i < wrapper.getChildCount(); i++) {
38 | View v = wrapper.getChildAt(i);
39 | if (v instanceof AdView) {
40 | wrapper.removeViewAt(i);
41 | break;
42 | }
43 | }
44 | }
45 |
46 | /**
47 | * This method can be overridden to wrap the created ad view with a custom {@link ViewGroup}.
48 | * For example if you need to wrap the ad with your custom CardView
49 | * @return The wrapper {@link ViewGroup} for ad, by default {@link AdView} ad would be wrapped with a CardView which is returned by this method
50 | */
51 | @Override
52 | @NonNull
53 | protected ViewGroup getAdViewWrapper(ViewGroup parent) {
54 | return (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(R.layout.web_ad_container,
55 | parent, false);
56 | }
57 | }
58 |
59 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/bannerads/BannerAdViewWrappingStrategyBase.java:
--------------------------------------------------------------------------------
1 | package com.clockbyte.admobadapter.bannerads;
2 |
3 | import android.view.ViewGroup;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | import com.google.android.gms.ads.AdView;
8 |
9 | /**
10 | * Created by L4grange on 14/02/2018
11 | */
12 |
13 | public abstract class BannerAdViewWrappingStrategyBase {
14 | /**
15 | * Add the Banner {@param ad} to {@param wrapper}.
16 | * See the super's implementation for instance.
17 | */
18 | protected abstract void addAdViewToWrapper(@NonNull ViewGroup wrapper, @NonNull AdView ad);
19 |
20 | /**
21 | * This method can be overridden to recycle (remove) {@param ad} from {@param wrapper} view before adding to wrap.
22 | * By default it will look for {@param ad} in the direct children of {@param wrapper} and remove the first occurrence.
23 | * See the super's implementation for instance.
24 | * The BannerHolder recycled by the RecyclerView may be a different
25 | * instance than the one used previously for this position. Clear the
26 | * wrapper of any subviews in case it has a different
27 | * AdView associated with it
28 | */
29 | protected abstract void recycleAdViewWrapper(@NonNull ViewGroup wrapper, @NonNull AdView ad);
30 |
31 | /**
32 | * This method can be overridden to wrap the created ad view with a custom {@link ViewGroup}.
33 | * For example if you need to wrap the ad with your custom CardView
34 | * @return The wrapper {@link ViewGroup} for ad, by default {@link AdView} ad would be wrapped with a CardView which is returned by this method
35 | */
36 | @NonNull
37 | protected abstract ViewGroup getAdViewWrapper(ViewGroup parent);
38 | }
39 |
--------------------------------------------------------------------------------
/admobadapter/src/main/java/com/clockbyte/admobadapter/bannerads/BannerHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 Clockbyte LLC. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | * Unless required by applicable law or agreed to in writing, software
9 | * distributed under the License is distributed on an "AS IS" BASIS,
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 | * See the License for the specific language governing permissions and
12 | * limitations under the License.
13 | */
14 |
15 | package com.clockbyte.admobadapter.bannerads;
16 |
17 | import android.view.ViewGroup;
18 |
19 | import androidx.recyclerview.widget.RecyclerView;
20 |
21 | /**
22 | * Created by L4grange on 14/02/2018
23 | */
24 |
25 | public class BannerHolder extends RecyclerView.ViewHolder {
26 |
27 | public BannerHolder(ViewGroup adViewWrapper){
28 | super(adViewWrapper);
29 | }
30 |
31 | public ViewGroup getAdViewWrapper() {
32 | return (ViewGroup)itemView;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/admobadapter/src/main/res/layout/adlistview_item.xml:
--------------------------------------------------------------------------------
1 |
14 |
15 |
18 |
19 |
21 |
23 |
26 |
28 |
29 |
32 |
34 |
36 |
39 |
42 |
43 |
44 |
45 |
46 |
47 |
50 |
51 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/admobadapter/src/main/res/layout/web_ad_container.xml:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
17 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/admobadapter/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
14 |
15 |
16 | admobadapter
17 | ca-app-pub-3940256099942544/3986624511
18 |
19 |
--------------------------------------------------------------------------------
/admobadapter/src/test/java/com/clockbyte/admobadapter/AdmobAdapterCalculatorTest.java:
--------------------------------------------------------------------------------
1 | package com.clockbyte.admobadapter;
2 |
3 | import android.util.Log;
4 |
5 | import org.junit.Before;
6 | import org.junit.Test;
7 | import org.junit.runner.RunWith;
8 | import org.mockito.invocation.InvocationOnMock;
9 | import org.mockito.stubbing.Answer;
10 | import org.powermock.core.classloader.annotations.PrepareForTest;
11 | import org.powermock.modules.junit4.PowerMockRunner;
12 |
13 | import static org.hamcrest.CoreMatchers.is;
14 | import static org.hamcrest.MatcherAssert.assertThat;
15 | import static org.mockito.Matchers.any;
16 | import static org.mockito.Mockito.when;
17 | import static org.powermock.api.mockito.PowerMockito.mockStatic;
18 |
19 | /**
20 | * Created by FILM on 21.08.2016.
21 | */
22 | @RunWith(PowerMockRunner.class)
23 | @PrepareForTest(Log.class)
24 | public class AdmobAdapterCalculatorTest {
25 |
26 | AdmobAdapterCalculator admobAdapterCalculator;
27 | @Before
28 | public void setUp() {
29 | admobAdapterCalculator = new AdmobAdapterCalculator();
30 | }
31 |
32 | @Before
33 | public void prepareMocks(){
34 | mockStatic(Log.class);
35 |
36 | when(Log.d(any(String.class), any(String.class))).thenAnswer(new Answer