Search in sources :

Example 1 with ToggleFavoriteOperation

use of com.owncloud.android.lib.resources.files.ToggleFavoriteOperation in project android by nextcloud.

the class OCFileListFragment method onMessageEvent.

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(FavoriteEvent event) {
    Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
    OwnCloudAccount ocAccount = null;
    AccountManager mAccountMgr = AccountManager.get(getActivity());
    try {
        ocAccount = new OwnCloudAccount(currentAccount, MainApp.getAppContext());
        OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
        String userId = mAccountMgr.getUserData(currentAccount, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
        if (TextUtils.isEmpty(userId)) {
            userId = mClient.getCredentials().getUsername();
        }
        ToggleFavoriteOperation toggleFavoriteOperation = new ToggleFavoriteOperation(event.shouldFavorite, event.remotePath, userId);
        RemoteOperationResult remoteOperationResult = toggleFavoriteOperation.execute(mClient);
        if (remoteOperationResult.isSuccess()) {
            mAdapter.setFavoriteAttributeForItemID(event.remoteId, event.shouldFavorite);
        }
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        Log_OC.e(TAG, "Account not found", e);
    } catch (AuthenticatorException e) {
        Log_OC.e(TAG, "Authentication failed", e);
    } catch (IOException e) {
        Log_OC.e(TAG, "IO error", e);
    } catch (OperationCanceledException e) {
        Log_OC.e(TAG, "Operation has been canceled", e);
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCanceledException(android.accounts.OperationCanceledException) AccountUtils(com.owncloud.android.authentication.AccountUtils) AuthenticatorException(android.accounts.AuthenticatorException) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) ToggleFavoriteOperation(com.owncloud.android.lib.resources.files.ToggleFavoriteOperation) IOException(java.io.IOException) AccountManager(android.accounts.AccountManager) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Subscribe(org.greenrobot.eventbus.Subscribe)

Aggregations

Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 AccountUtils (com.owncloud.android.authentication.AccountUtils)1 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)1 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 ToggleFavoriteOperation (com.owncloud.android.lib.resources.files.ToggleFavoriteOperation)1 IOException (java.io.IOException)1 Subscribe (org.greenrobot.eventbus.Subscribe)1