Search in sources :

Example 11 with UserEntity

use of com.nextcloud.talk.models.database.UserEntity in project talk-android by nextcloud.

the class AccountRemovalJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
    PushConfigurationState pushConfigurationState;
    for (Object userEntityObject : userUtils.getUsersScheduledForDeletion()) {
        UserEntity userEntity = (UserEntity) userEntityObject;
        try {
            if (!TextUtils.isEmpty(userEntity.getPushConfigurationState())) {
                pushConfigurationState = LoganSquare.parse(userEntity.getPushConfigurationState(), PushConfigurationState.class);
                PushConfigurationState finalPushConfigurationState = pushConfigurationState;
                ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
                ncApi.unregisterDeviceForNotificationsWithNextcloud(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl())).subscribe(new Observer<GenericOverall>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(GenericOverall genericOverall) {
                        if (genericOverall.getOcs().getMeta().getStatusCode() == 200 || genericOverall.getOcs().getMeta().getStatusCode() == 202) {
                            HashMap<String, String> queryMap = new HashMap<>();
                            queryMap.put("deviceIdentifier", finalPushConfigurationState.deviceIdentifier);
                            queryMap.put("userPublicKey", finalPushConfigurationState.getUserPublicKey());
                            queryMap.put("deviceIdentifierSignature", finalPushConfigurationState.getDeviceIdentifierSignature());
                            ncApi.unregisterDeviceForNotificationsWithProxy(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlPushProxy(), queryMap).subscribe(new Observer<Void>() {

                                @Override
                                public void onSubscribe(Disposable d) {
                                }

                                @Override
                                public void onNext(Void aVoid) {
                                    userUtils.deleteUser(userEntity.getUsername(), userEntity.getBaseUrl()).subscribe(new CompletableObserver() {

                                        @Override
                                        public void onSubscribe(Disposable d) {
                                        }

                                        @Override
                                        public void onComplete() {
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                        }
                                    });
                                }

                                @Override
                                public void onError(Throwable e) {
                                }

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

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
            } else {
                userUtils.deleteUser(userEntity.getUsername(), userEntity.getBaseUrl()).subscribe(new CompletableObserver() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                });
            }
        } catch (IOException e) {
            Log.d(TAG, "Something went wrong while removing job at parsing PushConfigurationState");
            userUtils.deleteUser(userEntity.getUsername(), userEntity.getBaseUrl()).subscribe(new CompletableObserver() {

                @Override
                public void onSubscribe(Disposable d) {
                }

                @Override
                public void onComplete() {
                }

                @Override
                public void onError(Throwable e) {
                }
            });
        }
    }
    return Result.SUCCESS;
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) IOException(java.io.IOException) UserEntity(com.nextcloud.talk.models.database.UserEntity) PushConfigurationState(com.nextcloud.talk.models.json.push.PushConfigurationState) CompletableObserver(io.reactivex.CompletableObserver) Observer(io.reactivex.Observer) CompletableObserver(io.reactivex.CompletableObserver) NcApi(com.nextcloud.talk.api.NcApi) GenericOverall(com.nextcloud.talk.models.json.generic.GenericOverall) CookieManager(java.net.CookieManager) NonNull(android.support.annotation.NonNull)

Example 12 with UserEntity

use of com.nextcloud.talk.models.database.UserEntity in project talk-android by nextcloud.

the class CapabilitiesJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
    long internalUserId = getParams().getExtras().getLong(BundleKeys.KEY_INTERNAL_USER_ID, -1);
    UserEntity userEntity;
    List userEntityObjectList = new ArrayList();
    if (internalUserId == -1 || (userEntity = userUtils.getUserWithInternalId(internalUserId)) == null) {
        userEntityObjectList = userUtils.getUsers();
    } else {
        userEntityObjectList.add(userEntity);
    }
    for (Object userEntityObject : userEntityObjectList) {
        UserEntity internalUserEntity = (UserEntity) userEntityObject;
        ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new JavaNetCookieJar(new java.net.CookieManager())).build()).build().create(NcApi.class);
        ncApi.getCapabilities(ApiUtils.getCredentials(internalUserEntity.getUsername(), internalUserEntity.getToken()), ApiUtils.getUrlForCapabilities(internalUserEntity.getBaseUrl())).subscribeOn(Schedulers.newThread()).subscribe(new Observer<CapabilitiesOverall>() {

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(CapabilitiesOverall capabilitiesOverall) {
                updateUser(capabilitiesOverall, internalUserEntity);
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });
    }
    return Result.SUCCESS;
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) Disposable(io.reactivex.disposables.Disposable) ArrayList(java.util.ArrayList) UserEntity(com.nextcloud.talk.models.database.UserEntity) CapabilitiesOverall(com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall) ArrayList(java.util.ArrayList) List(java.util.List) NcApi(com.nextcloud.talk.api.NcApi) NonNull(android.support.annotation.NonNull)

Example 13 with UserEntity

use of com.nextcloud.talk.models.database.UserEntity in project talk-android by nextcloud.

the class CallsListController method fetchData.

private void fetchData(boolean fromBottomSheet) {
    dispose(null);
    callItems = new ArrayList<>();
    roomsQueryDisposable = ncApi.getRooms(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()), ApiUtils.getUrlForGetRooms(userEntity.getBaseUrl())).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(roomsOverall -> {
        if (roomsOverall != null) {
            for (int i = 0; i < roomsOverall.getOcs().getData().size(); i++) {
                callItems.add(new CallItem(roomsOverall.getOcs().getData().get(i), userEntity));
            }
            adapter.updateDataSet(callItems, true);
            Collections.sort(callItems, (callItem, t1) -> Long.compare(t1.getModel().getLastPing(), callItem.getModel().getLastPing()));
            if (searchItem != null) {
                searchItem.setVisible(callItems.size() > 0);
            }
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }, throwable -> {
        if (searchItem != null) {
            searchItem.setVisible(false);
        }
        if (throwable instanceof HttpException) {
            HttpException exception = (HttpException) throwable;
            switch(exception.code()) {
                case 401:
                    if (getParentController() != null && getParentController().getRouter() != null) {
                        getParentController().getRouter().pushController((RouterTransaction.with(new WebViewLoginController(userEntity.getBaseUrl(), true)).pushChangeHandler(new VerticalChangeHandler()).popChangeHandler(new VerticalChangeHandler())));
                    }
                    break;
                default:
                    break;
            }
        }
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        dispose(roomsQueryDisposable);
    }, () -> {
        dispose(roomsQueryDisposable);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        if (fromBottomSheet) {
            new Handler().postDelayed(() -> {
                bottomSheet.setCancelable(true);
                if (bottomSheet.isShowing()) {
                    bottomSheet.cancel();
                }
            }, 2500);
        }
    });
}
Also used : AutoInjector(autodagger.AutoInjector) Bundle(android.os.Bundle) UserUtils(com.nextcloud.talk.utils.database.user.UserUtils) EntryMenuController(com.nextcloud.talk.controllers.bottomsheet.EntryMenuController) SearchView(android.support.v7.widget.SearchView) CallMenuController(com.nextcloud.talk.controllers.bottomsheet.CallMenuController) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) DividerItemDecoration(android.support.v7.widget.DividerItemDecoration) BindView(butterknife.BindView) ApiUtils(com.nextcloud.talk.utils.ApiUtils) BottomSheet(com.kennyc.bottomsheet.BottomSheet) Handler(android.os.Handler) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler) Participant(com.nextcloud.talk.models.json.participants.Participant) MenuItemCompat(android.support.v4.view.MenuItemCompat) HttpException(retrofit2.HttpException) InputType(android.text.InputType) FlexibleAdapter(eu.davidea.flexibleadapter.FlexibleAdapter) ThreadMode(org.greenrobot.eventbus.ThreadMode) ViewGroup(android.view.ViewGroup) NoOpControllerChangeHandler(com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler) BundleKeys(com.nextcloud.talk.utils.bundle.BundleKeys) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Parcels(org.parceler.Parcels) FastScroller(eu.davidea.fastscroller.FastScroller) CallItem(com.nextcloud.talk.adapters.items.CallItem) Nullable(android.support.annotation.Nullable) SearchManager(android.app.SearchManager) EditorInfo(android.view.inputmethod.EditorInfo) Context(android.content.Context) MoreMenuClickEvent(com.nextcloud.talk.events.MoreMenuClickEvent) UserEntity(com.nextcloud.talk.models.database.UserEntity) SmoothScrollLinearLayoutManager(eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) BottomSheetLockEvent(com.nextcloud.talk.events.BottomSheetLockEvent) Inject(javax.inject.Inject) BaseController(com.nextcloud.talk.controllers.base.BaseController) MenuInflater(android.view.MenuInflater) EventBus(org.greenrobot.eventbus.EventBus) Menu(android.view.Menu) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) NcApi(com.nextcloud.talk.api.NcApi) BottomNavigationView(android.support.design.widget.BottomNavigationView) R(com.nextcloud.talk.R) Room(com.nextcloud.talk.models.json.rooms.Room) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) RecyclerView(android.support.v7.widget.RecyclerView) Subscribe(org.greenrobot.eventbus.Subscribe) KeyboardUtils(com.nextcloud.talk.utils.KeyboardUtils) CallActivity(com.nextcloud.talk.activities.CallActivity) ViewTreeObserver(android.view.ViewTreeObserver) Collections(java.util.Collections) NextcloudTalkApplication(com.nextcloud.talk.application.NextcloudTalkApplication) RouterTransaction(com.bluelinelabs.conductor.RouterTransaction) Handler(android.os.Handler) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler) NoOpControllerChangeHandler(com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler) HorizontalChangeHandler(com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler) CallItem(com.nextcloud.talk.adapters.items.CallItem) HttpException(retrofit2.HttpException) VerticalChangeHandler(com.bluelinelabs.conductor.changehandler.VerticalChangeHandler)

Example 14 with UserEntity

use of com.nextcloud.talk.models.database.UserEntity in project talk-android by nextcloud.

the class SwitchAccountController method onViewBound.

@Override
protected void onViewBound(@NonNull View view) {
    super.onViewBound(view);
    NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
    swipeRefreshLayout.setEnabled(false);
    if (adapter == null) {
        adapter = new FlexibleAdapter<>(userItems, getActivity(), false);
        UserEntity userEntity;
        Participant participant;
        if (!isAccountImport) {
            for (Object userEntityObject : userUtils.getUsers()) {
                userEntity = (UserEntity) userEntityObject;
                if (!userEntity.getCurrent()) {
                    participant = new Participant();
                    participant.setName(userEntity.getDisplayName());
                    String userId;
                    if (userEntity.getUserId() != null) {
                        userId = userEntity.getUserId();
                    } else {
                        userId = userEntity.getUsername();
                    }
                    participant.setUserId(userId);
                    userItems.add(new AdvancedUserItem(participant, userEntity, null));
                }
            }
            adapter.addListener(onSwitchItemClickListener);
            adapter.updateDataSet(userItems, false);
        } else {
            if (getActionBar() != null) {
                getActionBar().show();
            }
            Account account;
            ImportAccount importAccount;
            for (Object accountObject : AccountUtils.findAccounts(userUtils.getUsers())) {
                account = (Account) accountObject;
                importAccount = AccountUtils.getInformationFromAccount(account);
                participant = new Participant();
                participant.setName(importAccount.getUsername());
                participant.setUserId(importAccount.getUsername());
                userEntity = new UserEntity();
                userEntity.setBaseUrl(importAccount.getBaseUrl());
                userItems.add(new AdvancedUserItem(participant, userEntity, account));
            }
            adapter.addListener(onImportItemClickListener);
            adapter.updateDataSet(userItems, false);
        }
    }
    prepareViews();
}
Also used : ImportAccount(com.nextcloud.talk.models.ImportAccount) Account(android.accounts.Account) Participant(com.nextcloud.talk.models.json.participants.Participant) ImportAccount(com.nextcloud.talk.models.ImportAccount) AdvancedUserItem(com.nextcloud.talk.adapters.items.AdvancedUserItem) UserEntity(com.nextcloud.talk.models.database.UserEntity)

Example 15 with UserEntity

use of com.nextcloud.talk.models.database.UserEntity 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

UserEntity (com.nextcloud.talk.models.database.UserEntity)17 Disposable (io.reactivex.disposables.Disposable)7 NonNull (android.support.annotation.NonNull)6 NcApi (com.nextcloud.talk.api.NcApi)6 Result (io.requery.query.Result)5 ArrayList (java.util.ArrayList)5 TextUtils (android.text.TextUtils)4 LayoutInflater (android.view.LayoutInflater)4 View (android.view.View)4 ViewGroup (android.view.ViewGroup)4 AutoInjector (autodagger.AutoInjector)4 BindView (butterknife.BindView)4 RouterTransaction (com.bluelinelabs.conductor.RouterTransaction)4 R (com.nextcloud.talk.R)4 Intent (android.content.Intent)3 Bundle (android.os.Bundle)3 Nullable (android.support.annotation.Nullable)3 HorizontalChangeHandler (com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler)3 VerticalChangeHandler (com.bluelinelabs.conductor.changehandler.VerticalChangeHandler)3 NextcloudTalkApplication (com.nextcloud.talk.application.NextcloudTalkApplication)3