从接口继承的Delphi Android Java类

时间:2018-06-29 13:11:50

标签: java android delphi

我正在尝试在Delphi中实施最新版本的Google Play服务(15.01)。我将相关的库合并到一个jar文件中,并使用java2op提取了一个桥接文件。一切运行都非常顺利,直到我碰上了这个展示排行榜:

我上了这个课:

JRoomConfigClass = interface(JObjectClass)
['{88F39239-F51D-4BA0-8131-24EBF070A584}']
{class} function builder(P1: JRoomUpdateListener): JRoomConfig_Builder; 
cdecl; overload;
{class} function builder(P1: JRoomUpdateCallback): JRoomConfig_Builder; 
cdecl; overload;
{class} function createAutoMatchCriteria(P1: Integer; P2: Integer; P3: 
Int64): JBundle; cdecl;
end;

[JavaSignature('com/google/android/gms/games/multiplayer/realtime/
RoomConfig')]
JRoomConfig = interface(JObject)
['{EF764B81-BF25-490C-8562-D9DC6BB337AD}']
function getAutoMatchCriteria: JBundle; cdecl;
function getInvitationId: JString; cdecl;
function getInvitedPlayerIds: TJavaObjectArray<JString>; cdecl;
function getMessageReceivedListener: JRealTimeMessageReceivedListener; cdecl;
function getOnMessageReceivedListener: JOnRealTimeMessageReceivedListener; 
cdecl;
function getRoomStatusUpdateCallback: JRoomStatusUpdateCallback; cdecl;
function getRoomStatusUpdateListener: JRoomStatusUpdateListener; cdecl;
function getRoomUpdateCallback: JRoomUpdateCallback; cdecl;
function getRoomUpdateListener: JRoomUpdateListener; cdecl;
function getVariant: Integer; cdecl;
function zzch: Jrealtime_zzh; cdecl;
end;
TJRoomConfig = class(TJavaGenericImport<JRoomConfigClass, JRoomConfig>) end;

我将方法称为“生成器”:

rtmConfigBuilder := TJRoomConfig.JavaClass.builder(RoomUpdateCallback);

但是RoomUpdateCallback的定义如下:

JRoomUpdateCallbackClass = interface(JRoomUpdateListenerClass)
['{B7973699-780A-4D76-9585-D113DD245CC5}']
{class} function init: JRoomUpdateCallback; cdecl;
end;
[JavaSignature('com/google/android/gms/games/multiplayer/realtime/
RoomUpdateCallback')]

JRoomUpdateCallback = interface(JRoomUpdateListener)
['{0CA35AB2-8AA9-4604-909B-68DE93112867}']
procedure onJoinedRoom(P1: Integer; P2: JRoom); cdecl;
procedure onLeftRoom(P1: Integer; P2: JString); cdecl;
procedure onRoomConnected(P1: Integer; P2: JRoom); cdecl;
procedure onRoomCreated(P1: Integer; P2: JRoom); cdecl;
end;
TJRoomUpdateCallback = class(TJavaGenericImport<JRoomUpdateCallbackClass, 
JRoomUpdateCallback>) end;

所以,这不是一个接口,这意味着我不能使用通常的侦听器方法:

TRoomUpdateListener = class(TJavaLocal, JRoomUpdateCallback)
  procedure onJoinedRoom(P1: Integer; P2: JRoom); cdecl;
  procedure onLeftRoom(P1: Integer; P2: JString); cdecl;
  procedure onRoomConnected(P1: Integer; P2: JRoom); cdecl;
  procedure onRoomCreated(P1: Integer; P2: JRoom); cdecl;
 end;

但是JRoomUpdateCallback是作为接口的JRoomUpdateListener的后代:

JRoomUpdateListenerClass = interface(IJavaClass)
['{62795DB4-DDD0-48F2-AABA-644CC96D5E6E}']
end;

[JavaSignature('com/google/android/gms/games/multiplayer/realtime/
RoomUpdateListener')]
JRoomUpdateListener = interface(IJavaInstance)
['{236212AF-A72A-4DEC-BB73-E0B42E80F7A1}']
procedure onJoinedRoom(P1: Integer; P2: JRoom); cdecl;
procedure onLeftRoom(P1: Integer; P2: JString); cdecl;
procedure onRoomConnected(P1: Integer; P2: JRoom); cdecl;
procedure onRoomCreated(P1: Integer; P2: JRoom); cdecl;
end;
TJRoomUpdateListener = class(TJavaGenericImport<JRoomUpdateListenerClass, 
JRoomUpdateListener>) end;

我该如何实施?有可能吗?

1 个答案:

答案 0 :(得分:1)

没有办法解决。我必须编写一些Java代码。如果有人可以从中受益,这就是我放在Java库中的内容。

package cometsoft.com.roomconfiglib;

import 
com.google.android.gms.games.multiplayer.realtime.OnRealTimeMessageReceivedListener;
import com.google.android.gms.games.multiplayer.realtime.RealTimeMessage;
import com.google.android.gms.games.multiplayer.realtime.Room;
import com.google.android.gms.games.multiplayer.realtime.RoomConfig;
import com.google.android.gms.games.multiplayer.realtime.RoomStatusUpdateCallback;
import com.google.android.gms.games.multiplayer.realtime.RoomUpdateCallback;

import android.os.Bundle;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

interface RoomConfigListener {
    public abstract void onRoomConnecting(@android.support.annotation.Nullable 
com.google.android.gms.games.multiplayer.realtime.Room room);

public abstract void onRoomAutoMatching(@android.support.annotation.Nullable 
com.google.android.gms.games.multiplayer.realtime.Room room);

public abstract void onPeerInvitedToRoom(@android.support.annotation.Nullable 
com.google.android.gms.games.multiplayer.realtime.Room room, 
   @android.support.annotation.NonNull java.util.List<java.lang.String> list);

public abstract void onPeerDeclined(@android.support.annotation.Nullable 
com.google.android.gms.games.multiplayer.realtime.Room room, 
@android.support.annotation.NonNull java.util.List<java.lang.String> list);

public abstract void onPeerJoined(@android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room, 
   @android.support.annotation.NonNull java.util.List<java.lang.String> list);

public abstract void onPeerLeft(@android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room, 
   @android.support.annotation.NonNull java.util.List<java.lang.String> list);

public abstract void onConnectedToRoom(@android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room);

public abstract void onDisconnectedFromRoom(@android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room);

public abstract void onPeersConnected(@android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room, 
   @android.support.annotation.NonNull java.util.List<java.lang.String> list);

public abstract void onPeersDisconnected(@android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room, 
   @android.support.annotation.NonNull java.util.List<java.lang.String> list);

public abstract void onP2PConnected(@android.support.annotation.NonNull 
   java.lang.String s);

public abstract void onP2PDisconnected(@android.support.annotation.NonNull 
   java.lang.String s);

public abstract void onRoomCreated(int i, @android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room);

public abstract void onJoinedRoom(int i, @android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room);

public abstract void onLeftRoom(int i, @android.support.annotation.NonNull 
   java.lang.String s);

public abstract void onRoomConnected(int i, @android.support.annotation.Nullable 
   com.google.android.gms.games.multiplayer.realtime.Room room);

void onRealTimeMessageReceived(@android.support.annotation.NonNull 
   com.google.android.gms.games.multiplayer.realtime.RealTimeMessage 
   realTimeMessage);

}

public class RoomConfigBuilder {

    private RoomConfigListener mRoomConfigListener;
    private Bundle mautoMatchCriteria;

    private RoomUpdateCallback mRoomUpdateCallback = new RoomUpdateCallback() {

        @Override
        public void onRoomCreated(int statusCode, Room room) {
            mRoomConfigListener.onRoomCreated(statusCode, room);
        }

        @Override
        public void onRoomConnected(int statusCode, Room room) {
            mRoomConfigListener.onRoomConnected(statusCode, room);
        }

        @Override
        public void onJoinedRoom(int statusCode, Room room) {
            mRoomConfigListener.onJoinedRoom(statusCode, room);
        }

        @Override
        public void onLeftRoom(int statusCode, @NonNull String roomId) {
            mRoomConfigListener.onLeftRoom(statusCode, roomId);
        }

    };

    private RoomStatusUpdateCallback mRoomStatusUpdateCallback = new 
       RoomStatusUpdateCallback() {

        @Override
        public void onConnectedToRoom(Room room) {
            mRoomConfigListener.onConnectedToRoom(room);
        }

        @Override
        public void onDisconnectedFromRoom(Room room) {
            mRoomConfigListener.onDisconnectedFromRoom(room);
        }

        @Override
        public void onPeerDeclined(Room room, @NonNull List<String> arg1) {
            mRoomConfigListener.onPeerDeclined(room, arg1);
        }

        @Override
        public void onPeerInvitedToRoom(Room room, @NonNull List<String> arg1) {
            mRoomConfigListener.onPeerInvitedToRoom(room, arg1);
        }

        @Override
        public void onP2PDisconnected(@NonNull String participant) {
            mRoomConfigListener.onP2PDisconnected(participant);
        }

        @Override
        public void onP2PConnected(@NonNull String participant) {
            mRoomConfigListener.onP2PConnected(participant);
        }

        @Override
        public void onPeerJoined(Room room, @NonNull List<String> arg1) {
            mRoomConfigListener.onPeerJoined(room, arg1);
        }

        @Override
        public void onPeerLeft(Room room, @NonNull List<String> peersWhoLeft) {
            mRoomConfigListener.onPeerLeft(room, peersWhoLeft);
        }

        @Override
        public void onRoomAutoMatching(Room room) {
            mRoomConfigListener.onRoomAutoMatching(room);
        }

        @Override
        public void onRoomConnecting(Room room) {
            mRoomConfigListener.onRoomConnecting(room);
        }

        @Override
        public void onPeersConnected(Room room, @NonNull List<String> peers) {
            mRoomConfigListener.onPeersConnected(room, peers);
        }

        @Override
        public void onPeersDisconnected(Room room, @NonNull List<String> peers) {
            mRoomConfigListener.onPeersDisconnected(room, peers);
        }
    };

    private OnRealTimeMessageReceivedListener mOnRealTimeMessageReceivedListener = 
       new OnRealTimeMessageReceivedListener() {
        @Override
        public void onRealTimeMessageReceived(@NonNull RealTimeMessage 
           realTimeMessage) {
            mRoomConfigListener.onRealTimeMessageReceived(realTimeMessage);
        }
    };

    public RoomConfig BuildRoomConfig(RoomConfigListener listener, Bundle 
           autoMatchCriteria, ArrayList<String> invitees, String invitationId) {
        mRoomConfigListener = listener;
        mautoMatchCriteria = autoMatchCriteria;
        RoomConfig.Builder mRoomConfigBuilder;

        mRoomConfigBuilder = 
           RoomConfig.builder(mRoomUpdateCallback)
           .setRoomStatusUpdateCallback(mRoomStatusUpdateCallback)
           .setOnMessageReceivedListener(mOnRealTimeMessageReceivedListener);

        if (invitees != null){
           mRoomConfigBuilder.addPlayersToInvite(invitees);
        }

        if (autoMatchCriteria != null){
            mRoomConfigBuilder.setAutoMatchCriteria(mautoMatchCriteria);
        }

        if (invitationId != null){
            mRoomConfigBuilder.setInvitationIdToAccept(invitationId);
        }

        return mRoomConfigBuilder.build();
    }
}