Search in sources :

Example 1 with PushRegistrationOverall

use of com.nextcloud.talk.models.json.push.PushRegistrationOverall in project talk-android by nextcloud.

the class PushUtils method pushRegistrationToServer.

public void pushRegistrationToServer() {
    String token = appPreferences.getPushToken();
    if (!TextUtils.isEmpty(token)) {
        String pushTokenHash = generateSHA512Hash(token).toLowerCase();
        PublicKey devicePublicKey = (PublicKey) readKeyFromFile(true);
        if (devicePublicKey != null) {
            byte[] publicKeyBytes = Base64.encode(devicePublicKey.getEncoded(), Base64.NO_WRAP);
            String publicKey = new String(publicKeyBytes);
            publicKey = publicKey.replaceAll("(.{64})", "$1\n");
            publicKey = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-----END PUBLIC KEY-----\n";
            if (userUtils.anyUserExists()) {
                String providerValue;
                PushConfigurationState accountPushData = null;
                for (Object userEntityObject : userUtils.getUsers()) {
                    UserEntity userEntity = (UserEntity) userEntityObject;
                    providerValue = userEntity.getPushConfigurationState();
                    if (!TextUtils.isEmpty(providerValue)) {
                        try {
                            accountPushData = LoganSquare.parse(providerValue, PushConfigurationState.class);
                        } catch (IOException e) {
                            Log.d(TAG, "Failed to parse account push data");
                            accountPushData = null;
                        }
                    } else {
                        accountPushData = null;
                    }
                    if (accountPushData != null && !accountPushData.getPushToken().equals(token) && !userEntity.getScheduledForDeletion() || TextUtils.isEmpty(providerValue) && !userEntity.getScheduledForDeletion()) {
                        Map<String, String> queryMap = new HashMap<>();
                        queryMap.put("format", "json");
                        queryMap.put("pushTokenHash", pushTokenHash);
                        queryMap.put("devicePublicKey", publicKey);
                        queryMap.put("proxyServer", proxyServer);
                        ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
                        ncApi.registerDeviceForNotificationsWithNextcloud(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), queryMap).subscribeOn(Schedulers.newThread()).subscribe(new Observer<PushRegistrationOverall>() {

                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(PushRegistrationOverall pushRegistrationOverall) {
                                Map<String, String> proxyMap = new HashMap<>();
                                proxyMap.put("pushToken", token);
                                proxyMap.put("deviceIdentifier", pushRegistrationOverall.getOcs().getData().getDeviceIdentifier());
                                proxyMap.put("deviceIdentifierSignature", pushRegistrationOverall.getOcs().getData().getSignature());
                                proxyMap.put("userPublicKey", pushRegistrationOverall.getOcs().getData().getPublicKey());
                                ncApi.registerDeviceForNotificationsWithProxy(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlPushProxy(), proxyMap).subscribeOn(Schedulers.newThread()).subscribe(new Observer<Void>() {

                                    @Override
                                    public void onSubscribe(Disposable d) {
                                    }

                                    @Override
                                    public void onNext(Void aVoid) {
                                        PushConfigurationState pushConfigurationState = new PushConfigurationState();
                                        pushConfigurationState.setPushToken(token);
                                        pushConfigurationState.setDeviceIdentifier(pushRegistrationOverall.getOcs().getData().getDeviceIdentifier());
                                        pushConfigurationState.setDeviceIdentifierSignature(pushRegistrationOverall.getOcs().getData().getSignature());
                                        pushConfigurationState.setUserPublicKey(pushRegistrationOverall.getOcs().getData().getPublicKey());
                                        pushConfigurationState.setUsesRegularPass(false);
                                        try {
                                            userUtils.createOrUpdateUser(null, null, null, userEntity.getDisplayName(), LoganSquare.serialize(pushConfigurationState), null, null, userEntity.getId(), null).subscribe(new Observer<UserEntity>() {

                                                @Override
                                                public void onSubscribe(Disposable d) {
                                                }

                                                @Override
                                                public void onNext(UserEntity userEntity) {
                                                }

                                                @Override
                                                public void onError(Throwable e) {
                                                }

                                                @Override
                                                public void onComplete() {
                                                }
                                            });
                                        } catch (IOException e) {
                                            Log.e(TAG, "IOException while updating user");
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                    }

                                    @Override
                                    public void onComplete() {
                                    }
                                });
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) PublicKey(java.security.PublicKey) IOException(java.io.IOException) UserEntity(com.nextcloud.talk.models.database.UserEntity) PushRegistrationOverall(com.nextcloud.talk.models.json.push.PushRegistrationOverall) PushConfigurationState(com.nextcloud.talk.models.json.push.PushConfigurationState) Observer(io.reactivex.Observer) NcApi(com.nextcloud.talk.api.NcApi) HashMap(java.util.HashMap) Map(java.util.Map) CookieManager(java.net.CookieManager)

Aggregations

NcApi (com.nextcloud.talk.api.NcApi)1 UserEntity (com.nextcloud.talk.models.database.UserEntity)1 PushConfigurationState (com.nextcloud.talk.models.json.push.PushConfigurationState)1 PushRegistrationOverall (com.nextcloud.talk.models.json.push.PushRegistrationOverall)1 Observer (io.reactivex.Observer)1 Disposable (io.reactivex.disposables.Disposable)1 IOException (java.io.IOException)1 CookieManager (java.net.CookieManager)1 PublicKey (java.security.PublicKey)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JavaNetCookieJar (okhttp3.JavaNetCookieJar)1