istereo2

changeset 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 64ca672a79dc
files .hgignore android/Makefile android/README android/manifest.xml.in src/android/MainActivity.java
diffstat 5 files changed, 220 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Sun Oct 04 08:15:24 2015 +0300
     1.2 +++ b/.hgignore	Mon Oct 05 06:00:16 2015 +0300
     1.3 @@ -17,6 +17,7 @@
     1.4  ^android/src/
     1.5  ^android/assets/
     1.6  ^android/gen/
     1.7 +^android/google-play-services_lib/
     1.8  \.xml$
     1.9  \.properties$
    1.10  proguard-project\.txt$
     2.1 --- a/android/Makefile	Sun Oct 04 08:15:24 2015 +0300
     2.2 +++ b/android/Makefile	Mon Oct 05 06:00:16 2015 +0300
     2.3 @@ -1,4 +1,5 @@
     2.4  bin = stereotunnel
     2.5 +title = Stereoscopic Tunnel
     2.6  root = ..
     2.7  src = $(wildcard $(root)/src/*.c)
     2.8  ccsrc = $(wildcard $(root)/src/*.cc)
     2.9 @@ -97,11 +98,12 @@
    2.10  	android create project -p . -t $(android_platform) -k $(pkg) -a NativeActivity -n $(name)
    2.11  	cat manifest.xml.in | sed 's/$$APPNAME/$(name)/g' | sed 's/$$APPTITLE/$(title)/g' >$@
    2.12  	cd src && rm -f *.java && ln -s ../../src/android/*.java .
    2.13 +	$(MAKE) update-project
    2.14  
    2.15  .PHONY: update-project
    2.16  update-project: build.xml
    2.17 -	android update project -p . -t $(android_platform)
    2.18 +	android update project -p . -t $(android_platform) --library google-play-services_lib --subprojects
    2.19  
    2.20  .PHONY: logcat
    2.21  logcat:
    2.22 -	adb logcat $(name):V AndroidRuntime:V DEBUG:V '*:S'
    2.23 +	adb logcat $(name):V AndroidRuntime:V DEBUG:V Ads:V '*:S'
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android/README	Mon Oct 05 06:00:16 2015 +0300
     3.3 @@ -0,0 +1,1 @@
     3.4 +Before building on a new system, copy google-play-services_lib here!
     4.1 --- a/android/manifest.xml.in	Sun Oct 04 08:15:24 2015 +0300
     4.2 +++ b/android/manifest.xml.in	Mon Oct 05 06:00:16 2015 +0300
     4.3 @@ -4,12 +4,19 @@
     4.4  	android:versionCode="1"
     4.5  	android:versionName="1.0">
     4.6  
     4.7 +	<!-- required for AdMob -->
     4.8 +	<uses-permission android:name="android.permission.INTERNET"/>
     4.9 +	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    4.10 +
    4.11  	<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19"/>
    4.12  	<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    4.13  
    4.14  	<application android:label="$APPNAME" android:debuggable="true">
    4.15  		<!-- android:icon="@drawable/ic_launcher" -->
    4.16  
    4.17 +		<meta-data android:name="com.google.android.gms.version"
    4.18 +			android:value="@integer/google_play_services_version"/>
    4.19 +
    4.20  		<activity android:name="MainActivity"
    4.21  			android:configChanges="orientation"
    4.22  			android:label="$APPTITLE">
    4.23 @@ -21,5 +28,10 @@
    4.24  				<category android:name="android.intent.category.LAUNCHER" />
    4.25  			</intent-filter>
    4.26  		</activity>
    4.27 +
    4.28 +		<activity android:name="com.google.android.gms.ads.AdActivity"
    4.29 +			android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    4.30 +			android:theme="@android:style/Theme.Translucent"/>
    4.31 +
    4.32  	</application>
    4.33  </manifest>
     5.1 --- a/src/android/MainActivity.java	Sun Oct 04 08:15:24 2015 +0300
     5.2 +++ b/src/android/MainActivity.java	Mon Oct 05 06:00:16 2015 +0300
     5.3 @@ -4,11 +4,28 @@
     5.4  import android.app.NativeActivity;
     5.5  import android.view.*;
     5.6  import android.view.WindowManager.LayoutParams;
     5.7 -//import android.util.Log;
     5.8 +import android.view.ViewGroup.MarginLayoutParams;
     5.9 +import android.widget.*;
    5.10 +import com.google.android.gms.ads.*;
    5.11 +import android.util.Log;
    5.12 +import android.content.res.Configuration;
    5.13  
    5.14  public class MainActivity extends NativeActivity
    5.15  {
    5.16 -	//public static String tag = "stereotunnel";
    5.17 +	public static String tag = "stereotunnel";
    5.18 +	public static String ad_id_test = "ca-app-pub-3940256099942544/6300978111";
    5.19 +	public static String ad_id_prod = "ca-app-pub-3466324067850759/4861471428";
    5.20 +
    5.21 +	public static String dev_id_gnexus = "B6FBA93004067A8DA892B85127D9454C";
    5.22 +	public static String dev_id_nexus7 = "";
    5.23 +
    5.24 +	MainActivity act;
    5.25 +
    5.26 +	AdView ad_view;
    5.27 +	PopupWindow ad_win;
    5.28 +	LinearLayout ad_layout, ad_main_layout;
    5.29 +	boolean ad_ready = false;
    5.30 +	boolean waiting_for_ad = false;
    5.31  
    5.32  	@Override
    5.33  	protected void onCreate(Bundle saved_inst)
    5.34 @@ -17,12 +34,30 @@
    5.35  
    5.36  		// go fullscreen
    5.37  		int winflags = LayoutParams.FLAG_FULLSCREEN |
    5.38 -			LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    5.39 +			LayoutParams.FLAG_LAYOUT_NO_LIMITS |
    5.40 +			LayoutParams.FLAG_LAYOUT_IN_SCREEN |
    5.41 +			LayoutParams.FLAG_KEEP_SCREEN_ON;
    5.42 +
    5.43  		Window win = getWindow();
    5.44  		win.setFlags(winflags, winflags);
    5.45  	}
    5.46  
    5.47  	@Override
    5.48 +	public void onDestroy()
    5.49 +	{
    5.50 +		destroy_ad_popup();
    5.51 +		super.onDestroy();
    5.52 +	}
    5.53 +
    5.54 +	@Override
    5.55 +	public void onAttachedToWindow()
    5.56 +	{
    5.57 +		super.onAttachedToWindow();
    5.58 +
    5.59 +		create_ad_popup();
    5.60 +	}
    5.61 +
    5.62 +	@Override
    5.63  	public void onWindowFocusChanged(boolean focus)
    5.64  	{
    5.65  		super.onWindowFocusChanged(focus);
    5.66 @@ -34,9 +69,28 @@
    5.67  	protected void onResume()
    5.68  	{
    5.69  		super.onResume();
    5.70 +		if(ad_view != null) {
    5.71 +			ad_view.resume();
    5.72 +		}
    5.73  		set_fullscreen();
    5.74  	}
    5.75  
    5.76 +	protected void onPause()
    5.77 +	{
    5.78 +		super.onPause();
    5.79 +		if(ad_view != null) {
    5.80 +			ad_view.pause();
    5.81 +		}
    5.82 +	}
    5.83 +
    5.84 +	public void onConfigurationChanged(Configuration config)
    5.85 +	{
    5.86 +		super.onConfigurationChanged(config);
    5.87 +
    5.88 +		destroy_ad_popup();
    5.89 +		create_ad_popup();
    5.90 +	}
    5.91 +
    5.92  	public void set_fullscreen()
    5.93  	{
    5.94  		int uiflags = View.SYSTEM_UI_FLAG_FULLSCREEN |
    5.95 @@ -53,4 +107,149 @@
    5.96  		View decor = getWindow().getDecorView();
    5.97  		decor.setSystemUiVisibility(uiflags);
    5.98  	}
    5.99 +
   5.100 +	// ads ...
   5.101 +	public void create_ad_popup()
   5.102 +	{
   5.103 +		Log.i(tag, "create_ad_popup called");
   5.104 +		if(ad_view != null) return;
   5.105 +
   5.106 +		act = this;
   5.107 +
   5.108 +		this.runOnUiThread(new Runnable() {
   5.109 +			@Override
   5.110 +			public void run()
   5.111 +			{
   5.112 +				Log.i(tag, "Creating Ad popup");
   5.113 +
   5.114 +				ad_win = new PopupWindow(act);
   5.115 +				// set minimum size
   5.116 +				int xsz = 320;//AdSize.SMART_BANNER.getWidthInPixels(act);
   5.117 +				int ysz = 50;//AdSize.SMART_BANNER.getHeightInPixels(act);
   5.118 +				ad_win.setWidth(xsz);	// orig:320
   5.119 +				ad_win.setHeight(ysz);	// orig:50
   5.120 +				ad_win.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   5.121 +				ad_win.setClippingEnabled(false);
   5.122 +
   5.123 +				ad_main_layout = new LinearLayout(act);
   5.124 +				MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
   5.125 +						LayoutParams.WRAP_CONTENT);
   5.126 +				params.setMargins(0, 0, 0, 0);
   5.127 +				act.setContentView(ad_main_layout, params);
   5.128 +
   5.129 +
   5.130 +
   5.131 +				ad_view = new AdView(act);
   5.132 +				ad_view.setAdSize(AdSize.BANNER);
   5.133 +				ad_view.setAdUnitId(ad_id_test);
   5.134 +
   5.135 +				ad_layout = new LinearLayout(act);
   5.136 +				ad_layout.setPadding(-5, -5, -5, -5);
   5.137 +				ad_layout.setOrientation(LinearLayout.VERTICAL);
   5.138 +				ad_layout.addView(ad_view, params);
   5.139 +				ad_win.setContentView(ad_layout);
   5.140 +
   5.141 +
   5.142 +				ad_view.setAdListener(new AdListener() {
   5.143 +					@Override
   5.144 +					public void onAdLoaded()
   5.145 +					{
   5.146 +						super.onAdLoaded();
   5.147 +						Log.i(tag, "ad loaded");
   5.148 +						ad_ready = true;
   5.149 +						waiting_for_ad = false;
   5.150 +						show_ad();
   5.151 +					}
   5.152 +					@Override
   5.153 +					public void onAdFailedToLoad(int error_code)
   5.154 +					{
   5.155 +						super.onAdFailedToLoad(error_code);
   5.156 +						Log.e(tag, "ad failed to load, error code: " + error_code);
   5.157 +						ad_ready = false;
   5.158 +						waiting_for_ad = false;
   5.159 +
   5.160 +						request_ad();
   5.161 +					}
   5.162 +				});
   5.163 +
   5.164 +
   5.165 +				request_ad();
   5.166 +
   5.167 +				Log.i(tag, "Done creating ad popup");
   5.168 +			}
   5.169 +		});
   5.170 +	}
   5.171 +
   5.172 +	public void destroy_ad_popup()
   5.173 +	{
   5.174 +		Log.i(tag, "destroy_ad_popup called");
   5.175 +
   5.176 +		if(ad_view != null) {
   5.177 +			ad_view.destroy();
   5.178 +			ad_view = null;
   5.179 +		}
   5.180 +		if(ad_win != null) {
   5.181 +			ad_win.dismiss();
   5.182 +			ad_win = null;
   5.183 +		}
   5.184 +	}
   5.185 +
   5.186 +	public void request_ad()
   5.187 +	{
   5.188 +		Log.i(tag, "requesting ad");
   5.189 +
   5.190 +		AdRequest.Builder reqbuild = new AdRequest.Builder();
   5.191 +		reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
   5.192 +		reqbuild.addTestDevice(dev_id_gnexus);
   5.193 +		ad_view.loadAd(reqbuild.build());
   5.194 +
   5.195 +		waiting_for_ad = true;
   5.196 +	}
   5.197 +
   5.198 +	private void show_ad()
   5.199 +	{
   5.200 +		Log.i(tag, "show_ad called");
   5.201 +		//ad_view.setVisibility(View.VISIBLE);
   5.202 +
   5.203 +		if(ad_ready) {
   5.204 +			ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
   5.205 +			ad_win.update();
   5.206 +		} else {
   5.207 +			if(!waiting_for_ad) {
   5.208 +				request_ad();
   5.209 +			}
   5.210 +		}
   5.211 +	}
   5.212 +
   5.213 +	private void hide_ad()
   5.214 +	{
   5.215 +		Log.i(tag, "hide_ad called");
   5.216 +		//ad_view.setVisibility(View.GONE);
   5.217 +		ad_win.dismiss();
   5.218 +		ad_ready = false;
   5.219 +		waiting_for_ad = false;
   5.220 +	}
   5.221 +
   5.222 +
   5.223 +	/*
   5.224 +    @Override
   5.225 +    public void onAdOpened()
   5.226 +	{
   5.227 +        // Code to be executed when an ad opens an overlay that
   5.228 +        // covers the screen.
   5.229 +    }
   5.230 +
   5.231 +    @Override
   5.232 +    public void onAdLeftApplication()
   5.233 +	{
   5.234 +        // Code to be executed when the user has left the app.
   5.235 +    }
   5.236 +
   5.237 +    @Override
   5.238 +    public void onAdClosed()
   5.239 +	{
   5.240 +        // Code to be executed when when the user is about to return
   5.241 +        // to the app after tapping on an ad.
   5.242 +    }
   5.243 +	*/
   5.244  }