diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-06-20 13:55:36 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-06-20 13:55:36 +0300 |
commit | 7ca8fe14a3d63c0ab0f733a79ba1a3c87a47dd59 (patch) | |
tree | 03bbaf67ba097d27fa55c2238e2f42d2b526e4a3 /app/src/main | |
parent | a914ff3a32f2c5e81178fc60bd29b4c04053609b (diff) |
Move SSO Glide integration into own module
Diffstat (limited to 'app/src/main')
7 files changed, 22 insertions, 279 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java index 86c857576..9de4185b1 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/accountswitcher/AccountSwitcherViewHolder.java @@ -8,13 +8,12 @@ import androidx.core.util.Consumer; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; -import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.request.RequestOptions; +import it.niedermann.android.glidesso.SingleSignOnUrl; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.ItemAccountChooseBinding; import it.niedermann.nextcloud.deck.model.Account; -import it.niedermann.nextcloud.deck.util.glide.SingleSignOnOriginHeader; import static it.niedermann.nextcloud.deck.util.DimensionUtil.dpToPx; @@ -31,7 +30,7 @@ public class AccountSwitcherViewHolder extends RecyclerView.ViewHolder { binding.accountName.setText(account.getUserName()); binding.accountHost.setText(Uri.parse(account.getUrl()).getHost()); Glide.with(itemView.getContext()) - .load(new GlideUrl(account.getAvatarUrl(dpToPx(binding.accountItemAvatar.getContext(), R.dimen.avatar_size)), new SingleSignOnOriginHeader(account))) + .load(new SingleSignOnUrl(account.getName(), account.getAvatarUrl(dpToPx(binding.accountItemAvatar.getContext(), R.dimen.avatar_size)))) .error(R.drawable.ic_person_grey600_24dp) .apply(RequestOptions.circleCropTransform()) .into(binding.accountItemAvatar); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/manageaccounts/ManageAccountViewHolder.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/manageaccounts/ManageAccountViewHolder.java index 0208d57d7..231e3f124 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/manageaccounts/ManageAccountViewHolder.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/manageaccounts/ManageAccountViewHolder.java @@ -9,13 +9,12 @@ import androidx.core.util.Consumer; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; -import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.request.RequestOptions; +import it.niedermann.android.glidesso.SingleSignOnUrl; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.databinding.ItemAccountChooseBinding; import it.niedermann.nextcloud.deck.model.Account; -import it.niedermann.nextcloud.deck.util.glide.SingleSignOnOriginHeader; import static android.view.View.GONE; import static android.view.View.VISIBLE; @@ -34,7 +33,7 @@ public class ManageAccountViewHolder extends RecyclerView.ViewHolder { binding.accountName.setText(account.getUserName()); binding.accountHost.setText(Uri.parse(account.getUrl()).getHost()); Glide.with(itemView.getContext()) - .load(new GlideUrl(account.getAvatarUrl(dpToPx(binding.accountItemAvatar.getContext(), R.dimen.avatar_size)), new SingleSignOnOriginHeader(account))) + .load(new SingleSignOnUrl(account.getName(), account.getAvatarUrl(dpToPx(binding.accountItemAvatar.getContext(), R.dimen.avatar_size)))) .error(R.drawable.ic_person_grey600_24dp) .apply(RequestOptions.circleCropTransform()) .into(binding.accountItemAvatar); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/CustomAppGlideModule.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/CustomAppGlideModule.java new file mode 100644 index 000000000..c8b5365f8 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/CustomAppGlideModule.java @@ -0,0 +1,18 @@ +package it.niedermann.nextcloud.deck.util; + +import android.content.Context; + +import androidx.annotation.NonNull; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.Registry; +import com.bumptech.glide.annotation.GlideModule; +import com.bumptech.glide.module.AppGlideModule; + +@GlideModule +public class CustomAppGlideModule extends AppGlideModule { + @Override + public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { + super.registerComponents(context, glide, registry); + } +}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnLibraryGlideModule.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnLibraryGlideModule.java deleted file mode 100644 index 389d31871..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnLibraryGlideModule.java +++ /dev/null @@ -1,28 +0,0 @@ -package it.niedermann.nextcloud.deck.util.glide; - -import android.content.Context; - -import androidx.annotation.NonNull; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.Registry; -import com.bumptech.glide.annotation.GlideModule; -import com.bumptech.glide.load.model.GlideUrl; -import com.bumptech.glide.module.AppGlideModule; - -import java.io.InputStream; - -/** - * Registers Nextcloud Single-Sign-On related classes via Glide's annotation processor. - * - * <p>For Applications that depend on this library and include an {@link AppGlideModule} and Glide's - * annotation processor, this class will be automatically included. - */ -@GlideModule -public final class SingleSignOnLibraryGlideModule extends AppGlideModule { - @Override - public void registerComponents( - @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { - registry.replace(GlideUrl.class, InputStream.class, new SingleSignOnUrlLoader.Factory(context)); - } -} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnOriginHeader.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnOriginHeader.java deleted file mode 100644 index 0c7d1e5a2..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnOriginHeader.java +++ /dev/null @@ -1,38 +0,0 @@ -package it.niedermann.nextcloud.deck.util.glide; - -import androidx.annotation.NonNull; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.load.model.Headers; -import com.bumptech.glide.load.model.LazyHeaders; -import com.nextcloud.android.sso.helper.SingleAccountHelper; -import com.nextcloud.android.sso.model.SingleSignOnAccount; - -import java.util.Map; - -import it.niedermann.nextcloud.deck.model.Account; - -public class SingleSignOnOriginHeader implements Headers { - - private LazyHeaders headers; - - /** - * Use this header and set the {@link SingleSignOnAccount} name property as value - * Format of the value needs to be - */ - public static final String X_HEADER_SSO_ACCOUNT_NAME = "X-SSO-Account-Name"; - - /** - * Use this as {@link Headers} if you want to do a {@link Glide} request for an {@link SingleSignOnAccount} which is not set by {@link SingleAccountHelper} as current {@link SingleSignOnAccount}. - * - * @param account Account from which host the request should be fired - */ - public SingleSignOnOriginHeader(@NonNull Account account) { - this.headers = new LazyHeaders.Builder().addHeader(X_HEADER_SSO_ACCOUNT_NAME, account.getName()).build(); - } - - @Override - public Map<String, String> getHeaders() { - return this.headers.getHeaders(); - } -} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnStreamFetcher.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnStreamFetcher.java deleted file mode 100644 index b83461e73..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnStreamFetcher.java +++ /dev/null @@ -1,140 +0,0 @@ -package it.niedermann.nextcloud.deck.util.glide; - -import android.content.Context; - -import androidx.annotation.NonNull; - -import com.bumptech.glide.Priority; -import com.bumptech.glide.load.DataSource; -import com.bumptech.glide.load.data.DataFetcher; -import com.bumptech.glide.load.model.GlideUrl; -import com.nextcloud.android.sso.AccountImporter; -import com.nextcloud.android.sso.aidl.NextcloudRequest; -import com.nextcloud.android.sso.api.NextcloudAPI; -import com.nextcloud.android.sso.api.Response; -import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; -import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; -import com.nextcloud.android.sso.exceptions.TokenMismatchException; -import com.nextcloud.android.sso.helper.SingleAccountHelper; -import com.nextcloud.android.sso.model.SingleSignOnAccount; - -import java.io.InputStream; -import java.net.MalformedURLException; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import it.niedermann.nextcloud.deck.DeckLog; -import it.niedermann.nextcloud.deck.api.GsonConfig; - -import static it.niedermann.nextcloud.deck.util.glide.SingleSignOnOriginHeader.X_HEADER_SSO_ACCOUNT_NAME; - - -/** - * Fetches an {@link InputStream} using the Nextcloud SSO library. - */ -public class SingleSignOnStreamFetcher implements DataFetcher<InputStream> { - private static final String METHOD_GET = "GET"; - - private static final Map<String, NextcloudAPI> INITIALIZED_APIs = new HashMap<>(); - - private final Context context; - private final GlideUrl url; - - // Public API. - @SuppressWarnings("WeakerAccess") - public SingleSignOnStreamFetcher(Context context, GlideUrl url) { - this.context = context; - this.url = url; - } - - @Override - public void loadData(@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) { - NextcloudAPI client; - try { - final SingleSignOnAccount ssoAccount; - if (url.getHeaders().containsKey(X_HEADER_SSO_ACCOUNT_NAME)) { - ssoAccount = AccountImporter.getSingleSignOnAccount(context, url.getHeaders().get(X_HEADER_SSO_ACCOUNT_NAME)); - } else { - ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context); - } - client = INITIALIZED_APIs.get(ssoAccount.name); - boolean didInitialize = false; - if (client == null) { - client = new NextcloudAPI(context, ssoAccount, GsonConfig.getGson(), new NextcloudAPI.ApiConnectedListener() { - @Override - public void onConnected() { - DeckLog.log("success: init SSO-Api"); - } - - @Override - public void onError(Exception e) { - DeckLog.logError(e); - } - }); - INITIALIZED_APIs.put(ssoAccount.name, client); - didInitialize = true; - } - - // FIXME shouldn't this go into the onConnected() callback if client is null? - NextcloudRequest.Builder requestBuilder; - try { - requestBuilder = new NextcloudRequest.Builder() - .setMethod(METHOD_GET) - .setUrl(url.toURL().getPath()); - Map<String, List<String>> header = new HashMap<>(); - for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { - if (!X_HEADER_SSO_ACCOUNT_NAME.equals(headerEntry.getKey())) { - header.put(headerEntry.getKey(), Collections.singletonList(headerEntry.getValue())); - } - } - requestBuilder.setHeader(header); - NextcloudRequest nextcloudRequest = requestBuilder.build(); - Response response = client.performNetworkRequestV2(nextcloudRequest); - callback.onDataReady(response.getBody()); - } catch (MalformedURLException e) { - callback.onLoadFailed(e); - } catch (TokenMismatchException e) { - if (!didInitialize) { - DeckLog.warn("SSO Glide loader failed with TokenMismatchException, trying to re-initialize..."); - client.stop(); - INITIALIZED_APIs.remove(ssoAccount.name); - loadData(priority, callback); - } else { - DeckLog.logError(e); - callback.onLoadFailed(e); - } - } catch (Exception e) { - callback.onLoadFailed(e); - } - - } catch (NextcloudFilesAppAccountNotFoundException e) { - e.printStackTrace(); - } catch (NoCurrentAccountSelectedException e) { - e.printStackTrace(); - } - } - - @Override - public void cleanup() { - - } - - @Override - public void cancel() { - - } - - @NonNull - @Override - public Class<InputStream> getDataClass() { - return InputStream.class; - } - - @NonNull - @Override - public DataSource getDataSource() { - return DataSource.REMOTE; - } -} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnUrlLoader.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnUrlLoader.java deleted file mode 100644 index 41ab323fc..000000000 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/glide/SingleSignOnUrlLoader.java +++ /dev/null @@ -1,67 +0,0 @@ -package it.niedermann.nextcloud.deck.util.glide; - -import android.content.Context; - -import androidx.annotation.NonNull; - -import com.bumptech.glide.load.Options; -import com.bumptech.glide.load.model.GlideUrl; -import com.bumptech.glide.load.model.ModelLoader; -import com.bumptech.glide.load.model.ModelLoaderFactory; -import com.bumptech.glide.load.model.MultiModelLoaderFactory; - -import org.jetbrains.annotations.NotNull; - -import java.io.InputStream; - -/** - * A simple model loader for fetching media over http/https using Nextcloud SSO. - */ -public class SingleSignOnUrlLoader implements ModelLoader<GlideUrl, InputStream> { - - private final Context context; - - // Public API. - @SuppressWarnings("WeakerAccess") - public SingleSignOnUrlLoader(@NonNull Context context) { - this.context = context; - } - - @Override - public boolean handles(@NonNull GlideUrl url) { - return true; - } - - @Override - public LoadData<InputStream> buildLoadData( - @NonNull GlideUrl model, int width, int height, @NonNull Options options) { - return new LoadData<>(model, new SingleSignOnStreamFetcher(context, model)); - } - - /** - * The default factory for {@link SingleSignOnUrlLoader}s. - */ - // Public API. - @SuppressWarnings("WeakerAccess") - public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> { - private SingleSignOnUrlLoader loader; - - /** - * Constructor for a new Factory that runs requests using given client. - */ - public Factory(@NonNull Context context) { - loader = new SingleSignOnUrlLoader(context); - } - - @NonNull - @Override - public ModelLoader<GlideUrl, InputStream> build(@NotNull MultiModelLoaderFactory multiFactory) { - return loader; - } - - @Override - public void teardown() { - // Do nothing, this instance doesn't own the client. - } - } -} |