istereo2

diff src/android/MainActivity.java @ 25:a9f2d30f7e8e

First hack at implementing banners on android. JNI interface to hide/show ads with the menu remains to be done.
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 05 Oct 2015 06:00:16 +0300
parents 9d53a4938ce8
children 74b50b538858
line diff
     1.1 --- a/src/android/MainActivity.java	Sun Oct 04 08:15:24 2015 +0300
     1.2 +++ b/src/android/MainActivity.java	Mon Oct 05 06:00:16 2015 +0300
     1.3 @@ -4,11 +4,28 @@
     1.4  import android.app.NativeActivity;
     1.5  import android.view.*;
     1.6  import android.view.WindowManager.LayoutParams;
     1.7 -//import android.util.Log;
     1.8 +import android.view.ViewGroup.MarginLayoutParams;
     1.9 +import android.widget.*;
    1.10 +import com.google.android.gms.ads.*;
    1.11 +import android.util.Log;
    1.12 +import android.content.res.Configuration;
    1.13  
    1.14  public class MainActivity extends NativeActivity
    1.15  {
    1.16 -	//public static String tag = "stereotunnel";
    1.17 +	public static String tag = "stereotunnel";
    1.18 +	public static String ad_id_test = "ca-app-pub-3940256099942544/6300978111";
    1.19 +	public static String ad_id_prod = "ca-app-pub-3466324067850759/4861471428";
    1.20 +
    1.21 +	public static String dev_id_gnexus = "B6FBA93004067A8DA892B85127D9454C";
    1.22 +	public static String dev_id_nexus7 = "";
    1.23 +
    1.24 +	MainActivity act;
    1.25 +
    1.26 +	AdView ad_view;
    1.27 +	PopupWindow ad_win;
    1.28 +	LinearLayout ad_layout, ad_main_layout;
    1.29 +	boolean ad_ready = false;
    1.30 +	boolean waiting_for_ad = false;
    1.31  
    1.32  	@Override
    1.33  	protected void onCreate(Bundle saved_inst)
    1.34 @@ -17,12 +34,30 @@
    1.35  
    1.36  		// go fullscreen
    1.37  		int winflags = LayoutParams.FLAG_FULLSCREEN |
    1.38 -			LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    1.39 +			LayoutParams.FLAG_LAYOUT_NO_LIMITS |
    1.40 +			LayoutParams.FLAG_LAYOUT_IN_SCREEN |
    1.41 +			LayoutParams.FLAG_KEEP_SCREEN_ON;
    1.42 +
    1.43  		Window win = getWindow();
    1.44  		win.setFlags(winflags, winflags);
    1.45  	}
    1.46  
    1.47  	@Override
    1.48 +	public void onDestroy()
    1.49 +	{
    1.50 +		destroy_ad_popup();
    1.51 +		super.onDestroy();
    1.52 +	}
    1.53 +
    1.54 +	@Override
    1.55 +	public void onAttachedToWindow()
    1.56 +	{
    1.57 +		super.onAttachedToWindow();
    1.58 +
    1.59 +		create_ad_popup();
    1.60 +	}
    1.61 +
    1.62 +	@Override
    1.63  	public void onWindowFocusChanged(boolean focus)
    1.64  	{
    1.65  		super.onWindowFocusChanged(focus);
    1.66 @@ -34,9 +69,28 @@
    1.67  	protected void onResume()
    1.68  	{
    1.69  		super.onResume();
    1.70 +		if(ad_view != null) {
    1.71 +			ad_view.resume();
    1.72 +		}
    1.73  		set_fullscreen();
    1.74  	}
    1.75  
    1.76 +	protected void onPause()
    1.77 +	{
    1.78 +		super.onPause();
    1.79 +		if(ad_view != null) {
    1.80 +			ad_view.pause();
    1.81 +		}
    1.82 +	}
    1.83 +
    1.84 +	public void onConfigurationChanged(Configuration config)
    1.85 +	{
    1.86 +		super.onConfigurationChanged(config);
    1.87 +
    1.88 +		destroy_ad_popup();
    1.89 +		create_ad_popup();
    1.90 +	}
    1.91 +
    1.92  	public void set_fullscreen()
    1.93  	{
    1.94  		int uiflags = View.SYSTEM_UI_FLAG_FULLSCREEN |
    1.95 @@ -53,4 +107,149 @@
    1.96  		View decor = getWindow().getDecorView();
    1.97  		decor.setSystemUiVisibility(uiflags);
    1.98  	}
    1.99 +
   1.100 +	// ads ...
   1.101 +	public void create_ad_popup()
   1.102 +	{
   1.103 +		Log.i(tag, "create_ad_popup called");
   1.104 +		if(ad_view != null) return;
   1.105 +
   1.106 +		act = this;
   1.107 +
   1.108 +		this.runOnUiThread(new Runnable() {
   1.109 +			@Override
   1.110 +			public void run()
   1.111 +			{
   1.112 +				Log.i(tag, "Creating Ad popup");
   1.113 +
   1.114 +				ad_win = new PopupWindow(act);
   1.115 +				// set minimum size
   1.116 +				int xsz = 320;//AdSize.SMART_BANNER.getWidthInPixels(act);
   1.117 +				int ysz = 50;//AdSize.SMART_BANNER.getHeightInPixels(act);
   1.118 +				ad_win.setWidth(xsz);	// orig:320
   1.119 +				ad_win.setHeight(ysz);	// orig:50
   1.120 +				ad_win.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   1.121 +				ad_win.setClippingEnabled(false);
   1.122 +
   1.123 +				ad_main_layout = new LinearLayout(act);
   1.124 +				MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
   1.125 +						LayoutParams.WRAP_CONTENT);
   1.126 +				params.setMargins(0, 0, 0, 0);
   1.127 +				act.setContentView(ad_main_layout, params);
   1.128 +
   1.129 +
   1.130 +
   1.131 +				ad_view = new AdView(act);
   1.132 +				ad_view.setAdSize(AdSize.BANNER);
   1.133 +				ad_view.setAdUnitId(ad_id_test);
   1.134 +
   1.135 +				ad_layout = new LinearLayout(act);
   1.136 +				ad_layout.setPadding(-5, -5, -5, -5);
   1.137 +				ad_layout.setOrientation(LinearLayout.VERTICAL);
   1.138 +				ad_layout.addView(ad_view, params);
   1.139 +				ad_win.setContentView(ad_layout);
   1.140 +
   1.141 +
   1.142 +				ad_view.setAdListener(new AdListener() {
   1.143 +					@Override
   1.144 +					public void onAdLoaded()
   1.145 +					{
   1.146 +						super.onAdLoaded();
   1.147 +						Log.i(tag, "ad loaded");
   1.148 +						ad_ready = true;
   1.149 +						waiting_for_ad = false;
   1.150 +						show_ad();
   1.151 +					}
   1.152 +					@Override
   1.153 +					public void onAdFailedToLoad(int error_code)
   1.154 +					{
   1.155 +						super.onAdFailedToLoad(error_code);
   1.156 +						Log.e(tag, "ad failed to load, error code: " + error_code);
   1.157 +						ad_ready = false;
   1.158 +						waiting_for_ad = false;
   1.159 +
   1.160 +						request_ad();
   1.161 +					}
   1.162 +				});
   1.163 +
   1.164 +
   1.165 +				request_ad();
   1.166 +
   1.167 +				Log.i(tag, "Done creating ad popup");
   1.168 +			}
   1.169 +		});
   1.170 +	}
   1.171 +
   1.172 +	public void destroy_ad_popup()
   1.173 +	{
   1.174 +		Log.i(tag, "destroy_ad_popup called");
   1.175 +
   1.176 +		if(ad_view != null) {
   1.177 +			ad_view.destroy();
   1.178 +			ad_view = null;
   1.179 +		}
   1.180 +		if(ad_win != null) {
   1.181 +			ad_win.dismiss();
   1.182 +			ad_win = null;
   1.183 +		}
   1.184 +	}
   1.185 +
   1.186 +	public void request_ad()
   1.187 +	{
   1.188 +		Log.i(tag, "requesting ad");
   1.189 +
   1.190 +		AdRequest.Builder reqbuild = new AdRequest.Builder();
   1.191 +		reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
   1.192 +		reqbuild.addTestDevice(dev_id_gnexus);
   1.193 +		ad_view.loadAd(reqbuild.build());
   1.194 +
   1.195 +		waiting_for_ad = true;
   1.196 +	}
   1.197 +
   1.198 +	private void show_ad()
   1.199 +	{
   1.200 +		Log.i(tag, "show_ad called");
   1.201 +		//ad_view.setVisibility(View.VISIBLE);
   1.202 +
   1.203 +		if(ad_ready) {
   1.204 +			ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
   1.205 +			ad_win.update();
   1.206 +		} else {
   1.207 +			if(!waiting_for_ad) {
   1.208 +				request_ad();
   1.209 +			}
   1.210 +		}
   1.211 +	}
   1.212 +
   1.213 +	private void hide_ad()
   1.214 +	{
   1.215 +		Log.i(tag, "hide_ad called");
   1.216 +		//ad_view.setVisibility(View.GONE);
   1.217 +		ad_win.dismiss();
   1.218 +		ad_ready = false;
   1.219 +		waiting_for_ad = false;
   1.220 +	}
   1.221 +
   1.222 +
   1.223 +	/*
   1.224 +    @Override
   1.225 +    public void onAdOpened()
   1.226 +	{
   1.227 +        // Code to be executed when an ad opens an overlay that
   1.228 +        // covers the screen.
   1.229 +    }
   1.230 +
   1.231 +    @Override
   1.232 +    public void onAdLeftApplication()
   1.233 +	{
   1.234 +        // Code to be executed when the user has left the app.
   1.235 +    }
   1.236 +
   1.237 +    @Override
   1.238 +    public void onAdClosed()
   1.239 +	{
   1.240 +        // Code to be executed when when the user is about to return
   1.241 +        // to the app after tapping on an ad.
   1.242 +    }
   1.243 +	*/
   1.244  }