+
+ requestSyncGroups();
+ requestSyncContacts();
+
+ save();
+ }
+
+ public List<DeviceInfo> getLinkedDevices() throws IOException {
+ return accountManager.getDevices();
+ }
+
+ public void removeLinkedDevices(int deviceId) throws IOException {
+ accountManager.removeDevice(deviceId);
+ }
+
+ public static Map<String, String> getQueryMap(String query) {
+ String[] params = query.split("&");
+ Map<String, String> map = new HashMap<>();
+ for (String param : params) {
+ String name = null;
+ try {
+ name = URLDecoder.decode(param.split("=")[0], "utf-8");
+ } catch (UnsupportedEncodingException e) {
+ // Impossible
+ }
+ String value = null;
+ try {
+ value = URLDecoder.decode(param.split("=")[1], "utf-8");
+ } catch (UnsupportedEncodingException e) {
+ // Impossible
+ }
+ map.put(name, value);
+ }
+ return map;
+ }
+
+ public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
+ Map<String, String> query = getQueryMap(linkUri.getRawQuery());
+ String deviceIdentifier = query.get("uuid");
+ String publicKeyEncoded = query.get("pub_key");
+
+ if (TextUtils.isEmpty(deviceIdentifier) || TextUtils.isEmpty(publicKeyEncoded)) {
+ throw new RuntimeException("Invalid device link uri");
+ }
+
+ ECPublicKey deviceKey = Curve.decodePoint(Base64.decode(publicKeyEncoded), 0);
+
+ addDevice(deviceIdentifier, deviceKey);
+ }
+
+ private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
+ IdentityKeyPair identityKeyPair = signalProtocolStore.getIdentityKeyPair();
+ String verificationCode = accountManager.getNewDeviceVerificationCode();
+
+ accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, verificationCode);