Fix wrong avatars in multi user setup

This commit is contained in:
Stefan Niedermann 2020-06-17 18:20:14 +02:00
parent e6e45b9530
commit 60f614c9bd
5 changed files with 61 additions and 7 deletions

View file

@ -8,10 +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.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemAccountChooseBinding;
import it.niedermann.owncloud.notes.glide.SingleSignOnOriginHeader;
import it.niedermann.owncloud.notes.model.LocalAccount;
public class AccountSwitcherViewHolder extends RecyclerView.ViewHolder {
@ -27,7 +29,7 @@ public class AccountSwitcherViewHolder extends RecyclerView.ViewHolder {
binding.accountName.setText(localAccount.getUserName());
binding.accountHost.setText(Uri.parse(localAccount.getUrl()).getHost());
Glide.with(itemView.getContext())
.load(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64")
.load(new GlideUrl(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64", new SingleSignOnOriginHeader(localAccount)))
.error(R.drawable.ic_account_circle_grey_24dp)
.apply(RequestOptions.circleCropTransform())
.into(binding.accountItemAvatar);

View file

@ -13,12 +13,14 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.request.RequestOptions;
import java.util.List;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemAccountChooseBinding;
import it.niedermann.owncloud.notes.glide.SingleSignOnOriginHeader;
import it.niedermann.owncloud.notes.model.LocalAccount;
import static it.niedermann.owncloud.notes.android.fragment.AccountChooserAdapter.AccountChooserViewHolder;
@ -64,7 +66,7 @@ public class AccountChooserAdapter extends RecyclerView.Adapter<AccountChooserVi
public void bind(LocalAccount localAccount, MoveAccountListener moveAccountListener) {
Glide
.with(binding.accountItemAvatar.getContext())
.load(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64")
.load(new GlideUrl(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64", new SingleSignOnOriginHeader(localAccount)))
.error(R.drawable.ic_account_circle_grey_24dp)
.apply(RequestOptions.circleCropTransform())
.into(binding.accountItemAvatar);

View file

@ -0,0 +1,38 @@
package it.niedermann.owncloud.notes.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.owncloud.notes.model.LocalAccount;
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 LocalAccount account) {
this.headers = new LazyHeaders.Builder().addHeader(X_HEADER_SSO_ACCOUNT_NAME, account.getAccountName()).build();
}
@Override
public Map<String, String> getHeaders() {
return this.headers.getHeaders();
}
}

View file

@ -10,6 +10,7 @@ import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.google.gson.GsonBuilder;
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;
@ -26,9 +27,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static it.niedermann.owncloud.notes.glide.SingleSignOnOriginHeader.X_HEADER_SSO_ACCOUNT_NAME;
/**
* Fetches an {@link InputStream} using the sso library.
* Fetches an {@link InputStream} using the Nextcloud SSO library.
*/
public class SingleSignOnStreamFetcher implements DataFetcher<InputStream> {
@ -51,11 +54,16 @@ public class SingleSignOnStreamFetcher implements DataFetcher<InputStream> {
public void loadData(@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) {
NextcloudAPI client;
try {
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
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, SingleAccountHelper.getCurrentSingleSignOnAccount(context), new GsonBuilder().create(), new NextcloudAPI.ApiConnectedListener() {
client = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), new NextcloudAPI.ApiConnectedListener() {
@Override
public void onConnected() {
Log.v(TAG, "SSO API successfully initialized");
@ -77,7 +85,9 @@ public class SingleSignOnStreamFetcher implements DataFetcher<InputStream> {
.setUrl(url.toURL().getPath());
Map<String, List<String>> header = new HashMap<>();
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
header.put(headerEntry.getKey(), Collections.singletonList(headerEntry.getValue()));
if(!X_HEADER_SSO_ACCOUNT_NAME.equals(headerEntry.getKey())) {
header.put(headerEntry.getKey(), Collections.singletonList(headerEntry.getValue()));
}
}
requestBuilder.setHeader(header);
NextcloudRequest nextcloudRequest = requestBuilder.build();

View file

@ -10,10 +10,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.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemAccountChooseBinding;
import it.niedermann.owncloud.notes.glide.SingleSignOnOriginHeader;
import it.niedermann.owncloud.notes.model.LocalAccount;
import static android.view.View.GONE;
@ -33,7 +35,7 @@ public class ManageAccountViewHolder extends RecyclerView.ViewHolder {
binding.accountName.setText(localAccount.getUserName());
binding.accountHost.setText(Uri.parse(localAccount.getUrl()).getHost());
Glide.with(itemView.getContext())
.load(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64")
.load(new GlideUrl(localAccount.getUrl() + "/index.php/avatar/" + Uri.encode(localAccount.getUserName()) + "/64", new SingleSignOnOriginHeader(localAccount)))
.error(R.drawable.ic_account_circle_grey_24dp)
.apply(RequestOptions.circleCropTransform())
.into(binding.accountItemAvatar);