3030import android .util .AttributeSet ;
3131import android .view .LayoutInflater ;
3232import android .view .View ;
33- import android .view .ViewGroup ;
3433import android .widget .FrameLayout ;
3534import android .widget .LinearLayout ;
3635
4039import com .google .android .gms .ads .AdListener ;
4140import com .google .android .gms .ads .AdRequest ;
4241import com .google .android .gms .ads .AdSize ;
43- import com .google .android .gms .ads .NativeExpressAdView ;
42+ import com .google .android .gms .ads .AdView ;
4443import com .grarak .kerneladiutor .R ;
4544import com .grarak .kerneladiutor .utils .AppSettings ;
4645import com .grarak .kerneladiutor .utils .Utils ;
5756/**
5857 * Created by willi on 08.08.16.
5958 */
60- public class AdNativeExpress extends LinearLayout {
59+ public class AdLayout extends LinearLayout {
6160
6261 public static final String ADS_FETCH = "https://raw.githubusercontent.com/Grarak/KernelAdiutor/master/ads/ads.json" ;
63- private static final int MAX_WIDTH = 1200 ;
64- private static final int MIN_HEIGHT = 132 ;
6562
66- private boolean mNativeLoaded ;
67- private boolean mNativeLoading ;
68- private boolean mNativeFailedLoading ;
63+ private boolean mAdFailedLoading ;
6964 private boolean mGHLoading ;
7065 private boolean mGHLoaded ;
7166 private View mProgress ;
7267 private View mAdText ;
73- private FrameLayout mNativeAdLayout ;
74- private NativeExpressAdView mNativeExpressAdView ;
7568 private AppCompatImageView mGHImage ;
69+ private AdView mAdView ;
7670
77- public AdNativeExpress (Context context ) {
71+ public AdLayout (Context context ) {
7872 this (context , null );
7973 }
8074
81- public AdNativeExpress (Context context , AttributeSet attrs ) {
75+ public AdLayout (Context context , AttributeSet attrs ) {
8276 this (context , attrs , 0 );
8377 }
8478
85- public AdNativeExpress (Context context , AttributeSet attrs , int defStyleAttr ) {
79+ public AdLayout (Context context , AttributeSet attrs , int defStyleAttr ) {
8680 super (context , attrs , defStyleAttr );
8781
8882 LayoutInflater .from (context ).inflate (R .layout .ad_native_express_view , this );
89- mNativeAdLayout = findViewById (R .id .ad_layout );
83+ FrameLayout mAdLayout = findViewById (R .id .ad_layout );
9084 mProgress = findViewById (R .id .progress );
9185 mAdText = findViewById (R .id .ad_text );
9286 mGHImage = findViewById (R .id .gh_image );
9387
9488 findViewById (R .id .remove_ad ).setOnClickListener (v
9589 -> ViewUtils .dialogDonate (v .getContext ()).show ());
9690
97- mNativeExpressAdView = new NativeExpressAdView (context );
98- mNativeExpressAdView .setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT ,
99- ViewGroup .LayoutParams .WRAP_CONTENT ));
100- mNativeExpressAdView .setAdUnitId (getContext ().getString (Utils .DARK_THEME ?
101- R .string .native_express_id_dark : R .string .native_express_id_light ));
102- mNativeExpressAdView .setAdListener (new AdListener () {
103- @ Override
104- public void onAdFailedToLoad (int i ) {
105- super .onAdFailedToLoad (i );
106- mNativeLoading = false ;
107- mNativeLoaded = false ;
108- mNativeFailedLoading = true ;
109- loadGHAd ();
110- }
111-
91+ mAdView = new AdView (context );
92+ mAdView .setAdSize (AdSize .SMART_BANNER );
93+ mAdView .setAdUnitId ("ca-app-pub-1851546461606210/7537613480" );
94+ mAdView .setAdListener (new AdListener () {
11295 @ Override
11396 public void onAdLoaded () {
11497 super .onAdLoaded ();
115- mNativeLoaded = true ;
116- mNativeLoading = false ;
117- mNativeFailedLoading = false ;
98+ mAdFailedLoading = false ;
11899 mProgress .setVisibility (GONE );
119- mNativeAdLayout .addView (mNativeExpressAdView );
100+ mAdLayout .addView (mAdView );
120101 }
121- });
122- }
123102
124- @ Override
125- protected void onLayout (boolean changed , int l , int t , int r , int b ) {
126- super .onLayout (changed , l , t , r , b );
127- int width ;
128- if (mNativeLoading || (mNativeLoaded && !mNativeFailedLoading )
129- || (!mNativeLoaded && mNativeFailedLoading ) ||
130- (width = mNativeAdLayout .getWidth ()) == 0 ) {
131- return ;
132- }
133- float deviceDensity = getResources ().getDisplayMetrics ().density ;
134- if (deviceDensity > 0 ) {
135- loadNativeAd (width , deviceDensity );
136- }
137- }
138-
139- private void loadNativeAd (int width , float deviceDensity ) {
140- float adWidth = width / deviceDensity ;
141- if (adWidth > MAX_WIDTH ) adWidth = MAX_WIDTH ;
142- mNativeExpressAdView .setAdSize (new AdSize ((int ) adWidth , MIN_HEIGHT ));
143- mNativeLoading = true ;
144- mNativeExpressAdView .loadAd (new AdRequest .Builder ().build ());
103+ @ Override
104+ public void onAdFailedToLoad (int i ) {
105+ super .onAdFailedToLoad (i );
106+ mAdFailedLoading = true ;
107+ loadGHAd ();
108+ }
109+ });
110+ mAdView .loadAd (new AdRequest .Builder ().build ());
145111 }
146112
147113 private boolean isActivityDestroyed (Context context ) {
@@ -158,7 +124,7 @@ private boolean isActivityDestroyed(Context context) {
158124 }
159125
160126 public void loadGHAd () {
161- if (!mNativeFailedLoading || mGHLoading || mGHLoaded ) {
127+ if (!mAdFailedLoading || mGHLoading || mGHLoaded ) {
162128 return ;
163129 }
164130 mGHLoading = true ;
@@ -222,15 +188,15 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? s
222188 }
223189
224190 public void resume () {
225- mNativeExpressAdView .resume ();
191+ mAdView .resume ();
226192 }
227193
228194 public void pause () {
229- mNativeExpressAdView .pause ();
195+ mAdView .pause ();
230196 }
231197
232198 public void destroy () {
233- mNativeExpressAdView .destroy ();
199+ mAdView .destroy ();
234200 }
235201
236202 public static class GHAds {
0 commit comments