+ @Override
+ public String getSelfNumber() {
+ return m.getSelfNumber();
+ }
+
+ @Override
+ public void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException {
+ final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", "");
+
+ try {
+ m.submitRateLimitRecaptchaChallenge(challenge, captcha);
+ } catch (IOException e) {
+ throw new IOErrorException("Submit challenge error: " + e.getMessage(), e);
+ }
+
+ }
+
+ @Override
+ public void addDevice(String uri) {
+ try {
+ m.addDeviceLink(new URI(uri));
+ } catch (IOException | InvalidKeyException e) {
+ throw new Error.Failure(e.getClass().getSimpleName() + " Add device link failed. " + e.getMessage());
+ } catch (URISyntaxException e) {
+ throw new Error.InvalidUri(e.getClass().getSimpleName()
+ + " Device link uri has invalid format: "
+ + e.getMessage());
+ }
+ }
+
+ @Override
+ public DBusPath getDevice(long deviceId) {
+ updateDevices();
+ return new DBusPath(getDeviceObjectPath(objectPath, deviceId));
+ }
+
+ @Override
+ public List<DBusPath> listDevices() {
+ updateDevices();
+ return this.devices;
+ }
+
+ private void updateDevices() {
+ List<org.asamk.signal.manager.api.Device> linkedDevices;
+ try {
+ linkedDevices = m.getLinkedDevices();
+ } catch (IOException | Error.Failure e) {
+ throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
+ }
+
+ unExportDevices();
+
+ linkedDevices.forEach(d -> {
+ final var object = new DbusSignalDeviceImpl(d);
+ final var deviceObjectPath = object.getObjectPath();
+ try {
+ connection.exportObject(object);
+ } catch (DBusException e) {
+ e.printStackTrace();
+ }
+ if (d.isThisDevice()) {
+ thisDevice = new DBusPath(deviceObjectPath);
+ }
+ this.devices.add(new DBusPath(deviceObjectPath));
+ });
+ }
+
+ private void unExportDevices() {
+ this.devices.stream().map(DBusPath::getPath).forEach(connection::unExportObject);
+ this.devices.clear();
+ }
+
+ @Override
+ public DBusPath getThisDevice() {
+ updateDevices();
+ return thisDevice;
+ }
+