nuclear@24: package com.mutantstargoat.stereotunnel; nuclear@24: nuclear@24: import android.os.Bundle; nuclear@24: import android.app.NativeActivity; nuclear@24: import android.view.*; nuclear@24: import android.view.WindowManager.LayoutParams; nuclear@25: import android.view.ViewGroup.MarginLayoutParams; nuclear@25: import android.widget.*; nuclear@25: import com.google.android.gms.ads.*; nuclear@25: import android.util.Log; nuclear@25: import android.content.res.Configuration; nuclear@24: nuclear@24: public class MainActivity extends NativeActivity nuclear@24: { nuclear@25: public static String tag = "stereotunnel"; nuclear@25: public static String ad_id_test = "ca-app-pub-3940256099942544/6300978111"; nuclear@25: public static String ad_id_prod = "ca-app-pub-3466324067850759/4861471428"; nuclear@25: nuclear@25: public static String dev_id_gnexus = "B6FBA93004067A8DA892B85127D9454C"; nuclear@28: public static String dev_id_nexus7 = "EC3418DBCF3628D89527BCE612BBA299"; nuclear@25: nuclear@25: MainActivity act; nuclear@25: nuclear@25: AdView ad_view; nuclear@25: PopupWindow ad_win; nuclear@25: LinearLayout ad_layout, ad_main_layout; nuclear@25: boolean ad_ready = false; nuclear@25: boolean waiting_for_ad = false; nuclear@24: nuclear@24: @Override nuclear@24: protected void onCreate(Bundle saved_inst) nuclear@24: { nuclear@24: super.onCreate(saved_inst); nuclear@24: nuclear@24: // go fullscreen nuclear@24: int winflags = LayoutParams.FLAG_FULLSCREEN | nuclear@25: LayoutParams.FLAG_LAYOUT_NO_LIMITS | nuclear@25: LayoutParams.FLAG_LAYOUT_IN_SCREEN | nuclear@25: LayoutParams.FLAG_KEEP_SCREEN_ON; nuclear@25: nuclear@24: Window win = getWindow(); nuclear@24: win.setFlags(winflags, winflags); nuclear@24: } nuclear@24: nuclear@24: @Override nuclear@25: public void onDestroy() nuclear@25: { nuclear@25: destroy_ad_popup(); nuclear@25: super.onDestroy(); nuclear@25: } nuclear@25: nuclear@25: @Override nuclear@25: public void onAttachedToWindow() nuclear@25: { nuclear@25: super.onAttachedToWindow(); nuclear@25: nuclear@25: create_ad_popup(); nuclear@25: } nuclear@25: nuclear@25: @Override nuclear@24: public void onWindowFocusChanged(boolean focus) nuclear@24: { nuclear@24: super.onWindowFocusChanged(focus); nuclear@24: if(focus) { nuclear@24: set_fullscreen(); nuclear@24: } nuclear@24: } nuclear@24: nuclear@24: protected void onResume() nuclear@24: { nuclear@24: super.onResume(); nuclear@25: if(ad_view != null) { nuclear@25: ad_view.resume(); nuclear@25: } nuclear@24: set_fullscreen(); nuclear@24: } nuclear@24: nuclear@25: protected void onPause() nuclear@25: { nuclear@25: super.onPause(); nuclear@25: if(ad_view != null) { nuclear@25: ad_view.pause(); nuclear@25: } nuclear@25: } nuclear@25: nuclear@25: public void onConfigurationChanged(Configuration config) nuclear@25: { nuclear@25: super.onConfigurationChanged(config); nuclear@25: nuclear@25: destroy_ad_popup(); nuclear@25: create_ad_popup(); nuclear@25: } nuclear@25: nuclear@24: public void set_fullscreen() nuclear@24: { nuclear@24: int uiflags = View.SYSTEM_UI_FLAG_FULLSCREEN | nuclear@24: View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | nuclear@24: View.SYSTEM_UI_FLAG_LAYOUT_STABLE | nuclear@24: View.SYSTEM_UI_FLAG_LOW_PROFILE; nuclear@24: nuclear@24: if(android.os.Build.VERSION.SDK_INT >= 19) { nuclear@24: uiflags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | nuclear@24: View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | nuclear@24: View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; nuclear@24: } nuclear@24: nuclear@24: View decor = getWindow().getDecorView(); nuclear@24: decor.setSystemUiVisibility(uiflags); nuclear@24: } nuclear@25: nuclear@25: // ads ... nuclear@25: public void create_ad_popup() nuclear@25: { nuclear@25: Log.i(tag, "create_ad_popup called"); nuclear@25: if(ad_view != null) return; nuclear@25: nuclear@25: act = this; nuclear@25: nuclear@25: this.runOnUiThread(new Runnable() { nuclear@25: @Override nuclear@25: public void run() nuclear@25: { nuclear@25: Log.i(tag, "Creating Ad popup"); nuclear@25: nuclear@25: ad_win = new PopupWindow(act); nuclear@25: // set minimum size nuclear@25: int xsz = 320;//AdSize.SMART_BANNER.getWidthInPixels(act); nuclear@25: int ysz = 50;//AdSize.SMART_BANNER.getHeightInPixels(act); nuclear@25: ad_win.setWidth(xsz); // orig:320 nuclear@25: ad_win.setHeight(ysz); // orig:50 nuclear@25: ad_win.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); nuclear@25: ad_win.setClippingEnabled(false); nuclear@25: nuclear@25: ad_main_layout = new LinearLayout(act); nuclear@25: MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, nuclear@25: LayoutParams.WRAP_CONTENT); nuclear@25: params.setMargins(0, 0, 0, 0); nuclear@25: act.setContentView(ad_main_layout, params); nuclear@25: nuclear@25: nuclear@25: nuclear@25: ad_view = new AdView(act); nuclear@25: ad_view.setAdSize(AdSize.BANNER); nuclear@25: ad_view.setAdUnitId(ad_id_test); nuclear@25: nuclear@25: ad_layout = new LinearLayout(act); nuclear@25: ad_layout.setPadding(-5, -5, -5, -5); nuclear@25: ad_layout.setOrientation(LinearLayout.VERTICAL); nuclear@25: ad_layout.addView(ad_view, params); nuclear@25: ad_win.setContentView(ad_layout); nuclear@25: nuclear@25: nuclear@25: ad_view.setAdListener(new AdListener() { nuclear@25: @Override nuclear@25: public void onAdLoaded() nuclear@25: { nuclear@25: super.onAdLoaded(); nuclear@25: Log.i(tag, "ad loaded"); nuclear@25: ad_ready = true; nuclear@25: waiting_for_ad = false; nuclear@25: show_ad(); nuclear@25: } nuclear@25: @Override nuclear@25: public void onAdFailedToLoad(int error_code) nuclear@25: { nuclear@25: super.onAdFailedToLoad(error_code); nuclear@25: Log.e(tag, "ad failed to load, error code: " + error_code); nuclear@25: ad_ready = false; nuclear@25: waiting_for_ad = false; nuclear@25: nuclear@25: request_ad(); nuclear@25: } nuclear@25: }); nuclear@25: nuclear@25: nuclear@29: if(!waiting_for_ad) { nuclear@29: request_ad(); nuclear@29: } nuclear@25: nuclear@25: Log.i(tag, "Done creating ad popup"); nuclear@25: } nuclear@25: }); nuclear@25: } nuclear@25: nuclear@25: public void destroy_ad_popup() nuclear@25: { nuclear@25: Log.i(tag, "destroy_ad_popup called"); nuclear@25: nuclear@25: if(ad_view != null) { nuclear@25: ad_view.destroy(); nuclear@25: ad_view = null; nuclear@25: } nuclear@25: if(ad_win != null) { nuclear@25: ad_win.dismiss(); nuclear@25: ad_win = null; nuclear@25: } nuclear@25: } nuclear@25: nuclear@25: public void request_ad() nuclear@25: { nuclear@25: Log.i(tag, "requesting ad"); nuclear@25: nuclear@25: AdRequest.Builder reqbuild = new AdRequest.Builder(); nuclear@25: reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); nuclear@25: reqbuild.addTestDevice(dev_id_gnexus); nuclear@28: reqbuild.addTestDevice(dev_id_nexus7); nuclear@25: ad_view.loadAd(reqbuild.build()); nuclear@25: nuclear@25: waiting_for_ad = true; nuclear@25: } nuclear@25: nuclear@25: private void show_ad() nuclear@25: { nuclear@25: Log.i(tag, "show_ad called"); nuclear@25: nuclear@25: if(ad_ready) { nuclear@29: if(ad_win == null) { nuclear@29: create_ad_popup(); nuclear@29: } nuclear@29: ad_view.setVisibility(View.VISIBLE); nuclear@25: ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0); nuclear@25: ad_win.update(); nuclear@29: Log.i(tag, "showing ad window: " + ad_win); nuclear@25: } else { nuclear@25: if(!waiting_for_ad) { nuclear@25: request_ad(); nuclear@25: } nuclear@25: } nuclear@25: } nuclear@25: nuclear@25: private void hide_ad() nuclear@25: { nuclear@25: Log.i(tag, "hide_ad called"); nuclear@25: //ad_view.setVisibility(View.GONE); nuclear@29: Log.i(tag, "hiding ad window: " + ad_win); nuclear@25: ad_win.dismiss(); nuclear@29: ad_win.update(); nuclear@29: //ad_win.update(); nuclear@29: //destroy_ad_popup(); nuclear@25: ad_ready = false; nuclear@25: waiting_for_ad = false; nuclear@25: } nuclear@25: nuclear@25: nuclear@25: /* nuclear@25: @Override nuclear@25: public void onAdOpened() nuclear@25: { nuclear@25: // Code to be executed when an ad opens an overlay that nuclear@25: // covers the screen. nuclear@25: } nuclear@25: nuclear@25: @Override nuclear@25: public void onAdLeftApplication() nuclear@25: { nuclear@25: // Code to be executed when the user has left the app. nuclear@25: } nuclear@25: nuclear@25: @Override nuclear@25: public void onAdClosed() nuclear@25: { nuclear@25: // Code to be executed when when the user is about to return nuclear@25: // to the app after tapping on an ad. nuclear@25: } nuclear@25: */ nuclear@24: }