Skip to content
This repository has been archived by the owner on Oct 5, 2019. It is now read-only.

Commit

Permalink
Switch to banner ads
Browse files Browse the repository at this point in the history
  • Loading branch information
Grarak committed Mar 17, 2018
1 parent a343760 commit 606fc1d
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 133 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.google.firebase:firebase-ads:11.8.0'
implementation 'com.google.android.gms:play-services-ads:11.8.0'
implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.android.gms:play-services-auth:11.8.0'
implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
import com.grarak.kerneladiutor.utils.root.RootUtils;
import com.grarak.kerneladiutor.utils.tools.Backup;
import com.grarak.kerneladiutor.utils.tools.SupportedDownloads;
import com.grarak.kerneladiutor.views.AdNativeExpress;
import com.grarak.kerneladiutor.views.AdLayout;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand Down Expand Up @@ -323,7 +323,7 @@ private void init(Bundle savedInstanceState) {
mAdsFetcher = new WebpageReader(this, new WebpageReader.WebpageListener() {
@Override
public void onSuccess(String url, String raw, CharSequence html) {
AdNativeExpress.GHAds ghAds = new AdNativeExpress.GHAds(raw);
AdLayout.GHAds ghAds = new AdLayout.GHAds(raw);
if (ghAds.readable()) {
ghAds.cache(NavigationActivity.this);
Fragment fragment = getFragment(mSelection);
Expand All @@ -337,7 +337,7 @@ public void onSuccess(String url, String raw, CharSequence html) {
public void onFailure(String url) {
}
});
mAdsFetcher.get(AdNativeExpress.ADS_FETCH);
mAdsFetcher.get(AdLayout.ADS_FETCH);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ protected void addItems(List<RecyclerViewItem> items) {
protected void postInit() {
super.postInit();

showProgress();
if (mWebpageReader == null) {
showProgress();
mWebpageReader = new WebpageReader(getActivity(), new WebpageReader.WebpageListener() {
@Override
public void onSuccess(String url, String raw, CharSequence html) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/grarak/kerneladiutor/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public static String upperCaseEachWord(String text) {
for (int i = 0; i < chars.length; i++) {
if (i == 0) {
chars[i] = Character.toUpperCase(chars[0]);
} else if (Character.isWhitespace(chars[i])) {
chars[i] = Character.toUpperCase(chars[i]);
} else if (Character.isWhitespace(chars[i]) && i != chars.length - 1) {
chars[i + 1] = Character.toUpperCase(chars[i + 1]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public String getTitle(Context context) {
return context.getString(mTitle);
}

String[] paths = mPath.split("/");
return Utils.upperCaseEachWord(paths[paths.length - 1].replace("enable_", "")
.replace("_ws", "").replace("_", " "));
return Utils.upperCaseEachWord(mPath.substring(mPath.lastIndexOf('/') + 1)
.replace("enable_", "")
.replace("_ws", "")
.replace("_", " "));
}

public void enable(boolean enable, Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

Expand All @@ -40,7 +39,7 @@
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.NativeExpressAdView;
import com.google.android.gms.ads.AdView;
import com.grarak.kerneladiutor.R;
import com.grarak.kerneladiutor.utils.AppSettings;
import com.grarak.kerneladiutor.utils.Utils;
Expand All @@ -57,91 +56,58 @@
/**
* Created by willi on 08.08.16.
*/
public class AdNativeExpress extends LinearLayout {
public class AdLayout extends LinearLayout {

public static final String ADS_FETCH = "https://raw.githubusercontent.com/Grarak/KernelAdiutor/master/ads/ads.json";
private static final int MAX_WIDTH = 1200;
private static final int MIN_HEIGHT = 132;

private boolean mNativeLoaded;
private boolean mNativeLoading;
private boolean mNativeFailedLoading;
private boolean mAdFailedLoading;
private boolean mGHLoading;
private boolean mGHLoaded;
private View mProgress;
private View mAdText;
private FrameLayout mNativeAdLayout;
private NativeExpressAdView mNativeExpressAdView;
private AppCompatImageView mGHImage;
private AdView mAdView;

public AdNativeExpress(Context context) {
public AdLayout(Context context) {
this(context, null);
}

public AdNativeExpress(Context context, AttributeSet attrs) {
public AdLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public AdNativeExpress(Context context, AttributeSet attrs, int defStyleAttr) {
public AdLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

LayoutInflater.from(context).inflate(R.layout.ad_native_express_view, this);
mNativeAdLayout = findViewById(R.id.ad_layout);
FrameLayout mAdLayout = findViewById(R.id.ad_layout);
mProgress = findViewById(R.id.progress);
mAdText = findViewById(R.id.ad_text);
mGHImage = findViewById(R.id.gh_image);

findViewById(R.id.remove_ad).setOnClickListener(v
-> ViewUtils.dialogDonate(v.getContext()).show());

mNativeExpressAdView = new NativeExpressAdView(context);
mNativeExpressAdView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mNativeExpressAdView.setAdUnitId(getContext().getString(Utils.DARK_THEME ?
R.string.native_express_id_dark : R.string.native_express_id_light));
mNativeExpressAdView.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
mNativeLoading = false;
mNativeLoaded = false;
mNativeFailedLoading = true;
loadGHAd();
}

mAdView = new AdView(context);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("ca-app-pub-1851546461606210/7537613480");
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mNativeLoaded = true;
mNativeLoading = false;
mNativeFailedLoading = false;
mAdFailedLoading = false;
mProgress.setVisibility(GONE);
mNativeAdLayout.addView(mNativeExpressAdView);
mAdLayout.addView(mAdView);
}
});
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int width;
if (mNativeLoading || (mNativeLoaded && !mNativeFailedLoading)
|| (!mNativeLoaded && mNativeFailedLoading) ||
(width = mNativeAdLayout.getWidth()) == 0) {
return;
}
float deviceDensity = getResources().getDisplayMetrics().density;
if (deviceDensity > 0) {
loadNativeAd(width, deviceDensity);
}
}

private void loadNativeAd(int width, float deviceDensity) {
float adWidth = width / deviceDensity;
if (adWidth > MAX_WIDTH) adWidth = MAX_WIDTH;
mNativeExpressAdView.setAdSize(new AdSize((int) adWidth, MIN_HEIGHT));
mNativeLoading = true;
mNativeExpressAdView.loadAd(new AdRequest.Builder().build());
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
mAdFailedLoading = true;
loadGHAd();
}
});
mAdView.loadAd(new AdRequest.Builder().build());
}

private boolean isActivityDestroyed(Context context) {
Expand All @@ -158,7 +124,7 @@ private boolean isActivityDestroyed(Context context) {
}

public void loadGHAd() {
if (!mNativeFailedLoading || mGHLoading || mGHLoaded) {
if (!mAdFailedLoading || mGHLoading || mGHLoaded) {
return;
}
mGHLoading = true;
Expand Down Expand Up @@ -222,15 +188,15 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? s
}

public void resume() {
mNativeExpressAdView.resume();
mAdView.resume();
}

public void pause() {
mNativeExpressAdView.pause();
mAdView.pause();
}

public void destroy() {
mNativeExpressAdView.destroy();
mAdView.destroy();
}

public static class GHAds {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import android.view.View;

import com.grarak.kerneladiutor.R;
import com.grarak.kerneladiutor.views.AdNativeExpress;
import com.grarak.kerneladiutor.views.AdLayout;

/**
* Created by willi on 06.08.16.
*/
public class AdView extends RecyclerViewItem {

private AdNativeExpress mAd;
private AdLayout mAd;

@Override
public int getLayoutRes() {
Expand All @@ -38,7 +38,7 @@ public int getLayoutRes() {

@Override
public void onCreateView(View view) {
mAd = (AdNativeExpress) view;
mAd = (AdLayout) view;
setFullSpan(true);

super.onCreateView(view);
Expand Down
86 changes: 37 additions & 49 deletions app/src/main/res/layout/ad_native_express_view.xml
Original file line number Diff line number Diff line change
@@ -1,61 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardCornerRadius="@dimen/cardview_radius"
app:cardElevation="@dimen/cardview_elevation"
app:cardUseCompatPadding="true">
android:orientation="vertical">

<LinearLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="50dp">

<FrameLayout
android:id="@+id/ad_layout"
android:layout_width="match_parent"
android:layout_height="132dp">
android:layout_height="match_parent" />

<FrameLayout
android:id="@+id/ad_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.v7.widget.AppCompatImageView
android:id="@+id/gh_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:visibility="gone" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/gh_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:visibility="gone" />

<TextView
android:id="@+id/ad_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-thin"
android:gravity="center"
android:padding="10dp"
android:text="@string/ad_here"
android:textSize="22sp"
android:visibility="gone" />
<TextView
android:id="@+id/ad_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
android:text="@string/ad_here"
android:textSize="18sp"
android:visibility="gone" />

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>

<android.support.v7.widget.AppCompatButton
android:id="@+id/remove_ad"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="?attr/selectableItemBackground"
android:text="@string/remove_ad"
android:textColor="?attr/colorAccent" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.AppCompatButton
android:id="@+id/remove_ad"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="?attr/selectableItemBackground"
android:text="@string/remove_ad"
android:textColor="?attr/colorAccent" />
</LinearLayout>
3 changes: 0 additions & 3 deletions app/src/main/res/layout/fragment_usage_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:textColor="@color/textcolor_dark"
android:textSize="12sp" />

<TextView
Expand All @@ -40,7 +39,6 @@
android:layout_gravity="center_horizontal"
android:focusable="false"
android:padding="3dp"
android:textColor="@color/textcolor_dark"
android:textSize="12sp" />

<TextView
Expand All @@ -50,6 +48,5 @@
android:layout_gravity="end"
android:focusable="false"
android:padding="3dp"
android:textColor="@color/textcolor_dark"
android:textSize="12sp" />
</FrameLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/rv_ad_view.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<com.grarak.kerneladiutor.views.AdNativeExpress xmlns:android="http://schemas.android.com/apk/res/android"
<com.grarak.kerneladiutor.views.AdLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
5 changes: 0 additions & 5 deletions app/src/main/res/values/ids.xml

This file was deleted.

Loading

0 comments on commit 606fc1d

Please sign in to comment.