Pixxl
Pixxl

Reputation: 996

How to access native functionality from an included Java dependency

I'm looking to create a Nativescript wrapper for a Java library so I can utilize functionalities of it for a Nativescript app. There does not seem to be too many articles that go into this in detail, and there does not seem to be a clear way to do this within the Nativescript app itself which is why I am now making this a plugin wrapper.

The specific Java library I am working to include is Libsignal-protocol-java. I've gone ahead and cloned the Nativescript Plugin Seed and added this Java library as a dependency:

src/platforms/android/include.gradle

android {   
}

dependencies {
  compile 'org.whispersystems:signal-protocol-android:2.3.0+'
}

I then found the particular package that contains the method I am trying ro access within the Java source: KeyHelper.generateRegistrationId(); source. One article mentioned this is required as you'll have to specify the package when instantiating the class and method.

I then setup my libsignal-protocol.common.ts as follows to attempt and use the native method:

src/libsignal-protocol.common.ts

import { Observable } from 'tns-core-modules/data/observable';

export class Common extends Observable {

    constructor() {
      // does not work
      let test1 = new org.whispersystems.libsignal.util.KeyHelper.generateRegistrationId();

      // does not work
      let test2 = org.whispersystems.libsignal.util.KeyHelper.generateRegistrationId();

      console.log(test1);
      console.log(test2);
    }
}

To my dismay, the logger returned this error:

System.err: Error: java.lang.Exception: Failed resolving method generateRegistrationId on class org.whispersystems.libsignal.util.KeyHelper

I am not sure where else to go here now, I wanted to go this route as it seemed safer/cleaner to create a wrapper for this awesome Java library than trying to browserify their javascript library as it requires certain features not available within Nativescript.

Any help or suggestions would be appreciated! For sanity, I will include some articles I have found on this matter that has helped lead me to where I am now.


Sources

Upvotes: 0

Views: 198

Answers (1)

Manoj
Manoj

Reputation: 21908

As you see in the source code generateRegistrationId method expects one boolean argument.

public static int generateRegistrationId(boolean extendedRange) {
    try {
      SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
      if (extendedRange) return secureRandom.nextInt(Integer.MAX_VALUE - 1) + 1;
      else               return secureRandom.nextInt(16380) + 1;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    }
  }

So you must pass a boolean to the method,

let test2 = org.whispersystems.libsignal.util.KeyHelper.generateRegistrationId(false);

Upvotes: 1

Related Questions