All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.agora.rtm.internal.RtmPresenceImpl Maven / Gradle / Ivy

package io.agora.rtm.internal;

import io.agora.rtm.ChannelInfo;
import io.agora.rtm.ErrorInfo;
import io.agora.rtm.GetOnlineUsersOptions;
import io.agora.rtm.GetOnlineUsersResult;
import io.agora.rtm.PresenceOptions;
import io.agora.rtm.ResultCallback;
import io.agora.rtm.RtmConstants;
import io.agora.rtm.RtmConstants.RtmChannelType;
import io.agora.rtm.RtmConstants.RtmErrorCode;
import io.agora.rtm.RtmPresence;
import io.agora.rtm.StateItem;
import io.agora.rtm.UserState;
import io.agora.rtm.WhoNowResult;
import java.util.ArrayList;
import java.util.List;

class RtmPresenceImpl extends RtmPresence {
  private static final String TAG = RtmPresenceImpl.class.getSimpleName();

  private long mNativePresence = 0;
  private RtmClientImpl mRtmClient;

  public RtmPresenceImpl(long nativeHandle, RtmClientImpl rtmClient) {
    mNativePresence = nativeHandle;
    mRtmClient = rtmClient;
  }

  @Override
  public synchronized void whoNow(String channelName, RtmChannelType channelType,
      PresenceOptions options, ResultCallback resultCallback) {
    if (resultCallback == null) {
      return;
    }
    if (mNativePresence == 0) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.NOT_INITIALIZED,
          mRtmClient.getErrorReason(RtmErrorCode.NOT_INITIALIZED), RtmConstants.WHO_NOW_API_STR));
      return;
    }

    if (options == null) {
      options = new PresenceOptions();
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeWhoNow(
          mNativePresence, channelName, RtmChannelType.getValue(channelType), options, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mWhoNowCallback.put(requestInfo.requestId, resultCallback);
      } else {
        resultCallback.onFailure(
            new ErrorInfo(errorCode, mRtmClient.getErrorReason(ret), RtmConstants.WHO_NOW_API_STR));
      }
    }
  }

  @Override
  public synchronized void whereNow(
      String userId, ResultCallback> resultCallback) {
    if (resultCallback == null) {
      return;
    }
    if (mNativePresence == 0) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.NOT_INITIALIZED,
          mRtmClient.getErrorReason(RtmErrorCode.NOT_INITIALIZED), RtmConstants.WHERE_NOW_API_STR));
      return;
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeWhereNow(mNativePresence, userId, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mWhereNowCallback.put(requestInfo.requestId, resultCallback);
      } else {
        resultCallback.onFailure(new ErrorInfo(
            errorCode, mRtmClient.getErrorReason(ret), RtmConstants.WHERE_NOW_API_STR));
      }
    }
  }

  @Override
  public synchronized void getOnlineUsers(String channelName, RtmChannelType channelType,
      GetOnlineUsersOptions options, ResultCallback resultCallback) {
    if (resultCallback == null) {
      return;
    }
    if (mNativePresence == 0) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.NOT_INITIALIZED,
          mRtmClient.getErrorReason(RtmErrorCode.NOT_INITIALIZED),
          RtmConstants.GET_ONLINE_USERS_API_STR));
      return;
    }
    if (options == null) {
      options = new GetOnlineUsersOptions();
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeGetOnlineUsers(
          mNativePresence, channelName, RtmChannelType.getValue(channelType), options, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mGetOnlineUsersCallback.put(requestInfo.requestId, resultCallback);
      } else {
        resultCallback.onFailure(new ErrorInfo(
            errorCode, mRtmClient.getErrorReason(ret), RtmConstants.GET_ONLINE_USERS_API_STR));
      }
    }
  }

  @Override
  public synchronized void getUserChannels(
      String userId, ResultCallback> resultCallback) {
    if (resultCallback == null) {
      return;
    }
    if (mNativePresence == 0) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.NOT_INITIALIZED,
          mRtmClient.getErrorReason(RtmErrorCode.NOT_INITIALIZED),
          RtmConstants.GET_USER_CHANNELS_API_STR));
      return;
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeGetUserChannels(mNativePresence, userId, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mGetUserChannelsCallback.put(requestInfo.requestId, resultCallback);
      } else {
        resultCallback.onFailure(new ErrorInfo(
            errorCode, mRtmClient.getErrorReason(ret), RtmConstants.GET_USER_CHANNELS_API_STR));
      }
    }
  }

  @Override
  public synchronized void setState(String channelName, RtmChannelType channelType,
      ArrayList items, ResultCallback resultCallback) {
    if (mNativePresence == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.NOT_INITIALIZED, RtmConstants.SET_STATE_API_STR);
      return;
    }

    if (items == null) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.PRESENCE_INVALID_ARGUMENT, RtmConstants.SET_STATE_API_STR);
      return;
    }
    for (StateItem stateItem : items) {
      if (stateItem == null || stateItem.getKey() == null || stateItem.getValue() == null) {
        mRtmClient.processFailureCallback(
            resultCallback, RtmErrorCode.PRESENCE_INVALID_ARGUMENT, RtmConstants.SET_STATE_API_STR);
        return;
      }
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeSetState(mNativePresence, channelName, RtmChannelType.getValue(channelType),
          items.toArray(new StateItem[0]), requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mModifyStateCallback.put(requestInfo.requestId, resultCallback);
      } else {
        mRtmClient.processFailureCallback(
            resultCallback, errorCode, RtmConstants.SET_STATE_API_STR);
      }
    }
  }

  @Override
  public synchronized void removeState(String channelName, RtmChannelType channelType,
      ArrayList keys, ResultCallback resultCallback) {
    if (mNativePresence == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.NOT_INITIALIZED, RtmConstants.REMOVE_STATE_API_STR);
      return;
    }
    if (keys == null) {
      mRtmClient.processFailureCallback(resultCallback, RtmErrorCode.PRESENCE_INVALID_ARGUMENT,
          RtmConstants.REMOVE_STATE_API_STR);
      return;
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeRemoveState(
          mNativePresence, channelName, RtmChannelType.getValue(channelType), keys, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mModifyStateCallback.put(requestInfo.requestId, resultCallback);
      } else {
        mRtmClient.processFailureCallback(
            resultCallback, errorCode, RtmConstants.REMOVE_STATE_API_STR);
      }
    }
  }

  @Override
  public synchronized void getState(String channelName, RtmChannelType channelType, String userId,
      ResultCallback resultCallback) {
    if (resultCallback == null) {
      return;
    }
    if (mNativePresence == 0) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.NOT_INITIALIZED,
          mRtmClient.getErrorReason(RtmErrorCode.NOT_INITIALIZED), RtmConstants.GET_STATE_API_STR));
      return;
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeGetState(
          mNativePresence, channelName, RtmChannelType.getValue(channelType), userId, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mGetStateCallback.put(requestInfo.requestId, resultCallback);
      } else {
        resultCallback.onFailure(new ErrorInfo(
            errorCode, mRtmClient.getErrorReason(ret), RtmConstants.GET_STATE_API_STR));
      }
    }
  }

  private native int nativeWhoNow(long nativeRtmPresenceAndroid, String channelName,
      int channelType, PresenceOptions options, RequestInfo requestInfo);

  private native int nativeWhereNow(
      long nativeRtmPresenceAndroid, String userId, RequestInfo requestInfo);

  private native int nativeGetOnlineUsers(long nativeRtmPresenceAndroid, String channelName,
      int channelType, GetOnlineUsersOptions options, RequestInfo requestInfo);

  private native int nativeGetUserChannels(
      long nativeRtmPresenceAndroid, String userId, RequestInfo requestInfo);

  private native int nativeSetState(long nativeRtmPresenceAndroid, String channelName,
      int channelType, StateItem[] items, RequestInfo requestInfo);

  private native int nativeRemoveState(long nativeRtmPresenceAndroid, String channelName,
      int channelType, ArrayList keys, RequestInfo requestInfo);

  private native int nativeGetState(long nativeRtmPresenceAndroid, String channelName,
      int channelType, String userId, RequestInfo requestInfo);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy