오직 Reward Banner 광고 만을 위한 WebView를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
...
<WebView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/rcpc_webview"/>
...
</ScrollView>
</FrameLayout>
private WebView webView;
private boolean mIsClicked = false;
private void adSetting()
{
initializeWebViewSetting();
loadScript();
}
webView 관련 설정을 아래와 같이 진행합니다. (initializeWebViewSetting
)
private void initializeWebViewSetting()
{
webView = (WebView)findViewById(R.id.rcpc_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(mWebViewClient);
webView.setOnTouchListener(mOnTouchListener);
webView.getSettings().setDefaultTextEncodingName("UTF-8");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
}
<iframe class="adpopcorn-ads" src="https://ssp.igaw.io/sdk/html/mediation_v2.html?{파라미터}" width="{WIDTH}" height="{HEIGHT}" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"></iframe>
private void loadScript()
{
String adid = getGoogleAdvertisingId();
String script = "<iframe id="adpopcorn-ads" src="https://ssp.igaw.io/sdk/html/mediation.html_v2?{파라미터}"
width="{WIDTH}" height="{HEIGHT}" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"></iframe>";
script = script.replace("{adid}", adid);
webView.loadData(script, "text/html", "UTF-8");
}
단, script의 adid 매크로 값에는 ADID 값을 치환하여 전달해 주시면 됩니다.
ADID를 가져오는 방법은 아래의 링크를 참고해 주시기 바랍니다.
해당 webview에서 클릭 발생 시, 클릭을 캐치하여 리워드 지급 후, 랜딩 주소로 랜딩 시켜줍니다.
private WebViewClient mWebViewClient = new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(final WebView view, WebResourceRequest request) {
try {
String url;
if (Build.VERSION.SDK_INT >= 21) {
url = request.getUrl().toString();
} else {
url = view.getUrl();
}
return urlLoading(view, url);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
try {
return urlLoading(view, url);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
mIsClicked = false;
}
private boolean urlLoading(WebView view, String url) {
if (mIsClicked && url != null) {
mIsClicked = false;
// RCPC 리워드 지급 or 이미 지급 된 경우 바로 랜딩
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} else {
view.loadUrl(url);
return false;
}
}
};
private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
private float startX;
private float startY;
private float totalScrollY = 0;
private int CLICK_ACTION_THRESHOLD = 200;
private int SCROLL_ACTION_THRESHOLD = 1000;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
if (isAClick(startX, endX, startY, endY)) {
mIsClicked = true;
}
break;
}
return false;
}
private boolean isAClick(float startX, float endX, float startY, float endY) {
float differenceX = Math.abs(startX - endX);
float differenceY = Math.abs(startY - endY);
if (differenceX > CLICK_ACTION_THRESHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHOLD) {
return false;
}
return true;
}
};