Search in sources :

Example 1 with OwnCloudCredentials

use of com.owncloud.android.lib.common.authentication.OwnCloudCredentials in project android by owncloud.

the class PrepareVideoPlayerAsyncTask method buildHttpDataSourceFactory.

/**
 * Returns a new HttpDataSource factory.
 *
 * @param bandwidthMeter Whether to set {@link #BANDWIDTH_METER} as a listener to the new
 *                       DataSource factory.
 * @return A new HttpDataSource factory.
 */
private HttpDataSource.Factory buildHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter, OCFile file, Account account) {
    if (file.isDown()) {
        return new DefaultHttpDataSourceFactory(MainApp.Companion.getUserAgent(), bandwidthMeter);
    } else {
        try {
            OwnCloudCredentials credentials = AccountUtils.getCredentialsForAccount(MainApp.Companion.getAppContext(), account);
            String login = credentials.getUsername();
            String password = credentials.getAuthToken();
            Map<String, String> params = new HashMap<String, String>(1);
            if (credentials instanceof OwnCloudBasicCredentials) {
                // Basic auth
                String cred = login + ":" + password;
                String auth = "Basic " + Base64.encodeToString(cred.getBytes(), Base64.URL_SAFE);
                params.put("Authorization", auth);
            } else if (credentials instanceof OwnCloudBearerCredentials) {
                // OAuth
                String bearerToken = credentials.getAuthToken();
                String auth = "Bearer " + bearerToken;
                params.put("Authorization", auth);
            }
            return new CustomHttpDataSourceFactory(MainApp.Companion.getUserAgent(), bandwidthMeter, params);
        } catch (AuthenticatorException | IOException | OperationCanceledException e) {
            Timber.e(e);
        }
    }
    return null;
}
Also used : OwnCloudBearerCredentials(com.owncloud.android.lib.common.authentication.OwnCloudBearerCredentials) OwnCloudBasicCredentials(com.owncloud.android.lib.common.authentication.OwnCloudBasicCredentials) DefaultHttpDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory) HashMap(java.util.HashMap) OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) OwnCloudCredentials(com.owncloud.android.lib.common.authentication.OwnCloudCredentials)

Aggregations

AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 DefaultHttpDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory)1 OwnCloudBasicCredentials (com.owncloud.android.lib.common.authentication.OwnCloudBasicCredentials)1 OwnCloudBearerCredentials (com.owncloud.android.lib.common.authentication.OwnCloudBearerCredentials)1 OwnCloudCredentials (com.owncloud.android.lib.common.authentication.OwnCloudCredentials)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1