Pyjnius - implement a nested public interface

25 views
Skip to first unread message

Dominik Wenger

unread,
Nov 26, 2021, 12:23:24 PM11/26/21
to Kivy users support
Hello,

i am using kivy and pyjnius for an application on a raspberry pi. For a new device i want to integrate a java library to access the device. But his java library expects the user to register callbacks which whould be implemented from a nested interface in the class. And i can not find out how to declare this in Pyjnius. Can anybody help ?

The class looks like this:
////////////////////////////////////////////////////////////////////////////////////////////////////////
package CCVCH.OPI;
//many imports
public class FlowManager {

    //some private data
    //Access to the callbacks

  public synchronized void addCashierDisplayListener(CashierDisplayListener listener) {
    this.listeners.add(CashierDisplayListener.class, listener);
  }
 
  public synchronized void removeCashierDisplayListener(CashierDisplayListener listener) {
    this.listeners.remove(CashierDisplayListener.class, listener);
  }
 
  //more callback functions
 
  //constructors
  public FlowManager(String terminalIPAddress, int IPPort0, int IPPort1, int timeout, String logFileName) throws IOException {
    InitializeFlowManager(terminalIPAddress, IPPort0, IPPort1, IPPort0, IPPort1, timeout, 0, logFileName, 1, 2,
        "99999");
  }
 
  //many more constructors
  public FlowManager(String terminalIPAddress, int IPPort0, int IPPort1, int timeout, String logFileName, String merchantPassword) throws IOException {
    InitializeFlowManager(terminalIPAddress, IPPort0, IPPort1, IPPort0, IPPort1, timeout, 0, logFileName, 1, 2,
        merchantPassword);
  }
 
  //many private and public functions
 
  // declarations of the interfaces
  public static interface CashierDisplayListener extends EventListener {
    void handleCashierDisplay(FlowManagerEventArgs param1FlowManagerEventArgs);
  }
 
  //many more interfaces
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
So i want to somehow implement the CashierDisplayListener interface with pyjnius and use it with the addCashierDisplayListenerfunction.

I am gratefull for any hints on how i can do this.

kind regards,
Dominik Wenger

Robert

unread,
Nov 26, 2021, 3:20:46 PM11/26/21
to Kivy users support
I didn't try to figure out the Java in the post, but the pattern is known.

The hard part is passing the listener's event back to Python, 'explained' here:
It works because the CallbackWrapper interface is declared in Java, and the CallbackWrapper implementation is declared in Python via Pyjnius. 
I've only done this on Android, but nothing  is platform specific except the buildozer.spec comment at the end. I've used with JDK 8 and 13.

Here is an example (part of something else) with 3 listeners, the Java Listeners are added in Python and have Python callbacks. I'd recommend reading he previous link first. The key point is passing the CallbackWrapper to the Listener, and this Listener implements the Java Listener in the 3rd party Java .

Python file:
Java files:

Yes, it is a little convoluted.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages