Эх сурвалжийг харах

use authenticated glide loader for svg

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 5 жил өмнө
parent
commit
aa8fc2e5ce

+ 5 - 1
src/main/java/com/nextcloud/client/di/ComponentsModule.java

@@ -20,6 +20,7 @@
 
 package com.nextcloud.client.di;
 
+import com.nextcloud.client.whatsnew.WhatsNewActivity;
 import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.authentication.DeepLinkLoginActivity;
 import com.owncloud.android.files.BootupBroadcastReceiver;
@@ -56,7 +57,6 @@ import com.owncloud.android.ui.activity.UploadFilesActivity;
 import com.owncloud.android.ui.activity.UploadListActivity;
 import com.owncloud.android.ui.activity.UploadPathActivity;
 import com.owncloud.android.ui.activity.UserInfoActivity;
-import com.nextcloud.client.whatsnew.WhatsNewActivity;
 import com.owncloud.android.ui.dialog.ChooseTemplateDialogFragment;
 import com.owncloud.android.ui.errorhandling.ErrorShowActivity;
 import com.owncloud.android.ui.fragment.ExtendedListFragment;
@@ -64,6 +64,7 @@ import com.owncloud.android.ui.fragment.FileDetailActivitiesFragment;
 import com.owncloud.android.ui.fragment.FileDetailFragment;
 import com.owncloud.android.ui.fragment.LocalFileListFragment;
 import com.owncloud.android.ui.fragment.OCFileListFragment;
+import com.owncloud.android.ui.fragment.contactsbackup.ContactListFragment;
 import com.owncloud.android.ui.preview.PreviewImageActivity;
 import com.owncloud.android.ui.preview.PreviewVideoActivity;
 import com.owncloud.android.ui.trashbin.TrashbinActivity;
@@ -121,6 +122,9 @@ abstract class ComponentsModule {
     @ContributesAndroidInjector abstract FileDetailActivitiesFragment fileDetailActivitiesFragment();
     @ContributesAndroidInjector abstract ChooseTemplateDialogFragment chooseTemplateDialogFragment();
 
+    @ContributesAndroidInjector
+    abstract ContactListFragment chooseContactListFragment();
+
     @ContributesAndroidInjector abstract FileUploader fileUploader();
 
     @ContributesAndroidInjector abstract BootupBroadcastReceiver bootupBroadcastReceiver();

+ 14 - 2
src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -924,7 +924,13 @@ public abstract class DrawerActivity extends ToolbarActivity
                         }
                     };
 
-                    DisplayUtils.downloadIcon(this, firstQuota.iconUrl, target, R.drawable.ic_link, size, size);
+                    DisplayUtils.downloadIcon(getUserAccountManager(),
+                                              this,
+                                              firstQuota.iconUrl,
+                                              target,
+                                              R.drawable.ic_link,
+                                              size,
+                                              size);
 
                 } else {
                     mQuotaTextLink.setVisibility(View.GONE);
@@ -1051,7 +1057,13 @@ public abstract class DrawerActivity extends ToolbarActivity
                     }
                 };
 
-                DisplayUtils.downloadIcon(this, link.iconUrl, target, R.drawable.ic_link, size, size);
+                DisplayUtils.downloadIcon(getUserAccountManager(),
+                                          this,
+                                          link.iconUrl,
+                                          target,
+                                          R.drawable.ic_link,
+                                          size,
+                                          size);
             }
 
             setDrawerMenuItemChecked(mCheckedMenuItem);

+ 25 - 7
src/main/java/com/owncloud/android/ui/fragment/contactsbackup/ContactListFragment.java

@@ -59,6 +59,8 @@ import com.bumptech.glide.request.target.SimpleTarget;
 import com.evernote.android.job.JobRequest;
 import com.evernote.android.job.util.support.PersistableBundleCompat;
 import com.google.android.material.snackbar.Snackbar;
+import com.nextcloud.client.account.UserAccountManager;
+import com.nextcloud.client.di.Injectable;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
@@ -77,6 +79,7 @@ import com.owncloud.android.utils.ThemeUtils;
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
+import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
 import java.io.IOException;
@@ -87,6 +90,8 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import javax.inject.Inject;
+
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.ActionBar;
 import androidx.appcompat.app.AlertDialog;
@@ -104,7 +109,7 @@ import static com.owncloud.android.ui.fragment.contactsbackup.ContactListFragmen
 /**
  * This fragment shows all contacts from a file and allows to import them.
  */
-public class ContactListFragment extends FileFragment {
+public class ContactListFragment extends FileFragment implements Injectable {
     public static final String TAG = ContactListFragment.class.getSimpleName();
 
     public static final String FILE_NAME = "FILE_NAME";
@@ -142,6 +147,7 @@ public class ContactListFragment extends FileFragment {
     private Account account;
     private List<VCard> vCards = new ArrayList<>();
     private OCFile ocFile;
+    @Inject UserAccountManager accountManager;
 
     public static ContactListFragment newInstance(OCFile file, Account account) {
         ContactListFragment frag = new ContactListFragment();
@@ -184,7 +190,7 @@ public class ContactListFragment extends FileFragment {
         recyclerView = view.findViewById(R.id.contactlist_recyclerview);
 
         if (savedInstanceState == null) {
-            contactListAdapter = new ContactListAdapter(getContext(), vCards);
+            contactListAdapter = new ContactListAdapter(accountManager, getContext(), vCards);
         } else {
             Set<Integer> checkedItems = new HashSet<>();
             int[] itemsArray = savedInstanceState.getIntArray(CHECKED_ITEMS_ARRAY_KEY);
@@ -196,7 +202,7 @@ public class ContactListFragment extends FileFragment {
             if (checkedItems.size() > 0) {
                 onMessageEvent(new VCardToggleEvent(true));
             }
-            contactListAdapter = new ContactListAdapter(getContext(), vCards, checkedItems);
+            contactListAdapter = new ContactListAdapter(accountManager, getContext(), vCards, checkedItems);
         }
         recyclerView.setAdapter(contactListAdapter);
         recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@@ -489,6 +495,7 @@ public class ContactListFragment extends FileFragment {
             }
         }
 
+        @NotNull
         @Override
         public String toString() {
             return displayName;
@@ -575,17 +582,23 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
 
     private Context context;
 
-    ContactListAdapter(Context context, List<VCard> vCards) {
+    private UserAccountManager accountManager;
+
+    ContactListAdapter(UserAccountManager accountManager, Context context, List<VCard> vCards) {
         this.vCards = vCards;
         this.context = context;
         this.checkedVCards = new HashSet<>();
+        this.accountManager = accountManager;
     }
 
-    ContactListAdapter(Context context, List<VCard> vCards,
+    ContactListAdapter(UserAccountManager accountManager,
+                       Context context,
+                       List<VCard> vCards,
                        Set<Integer> checkedVCards) {
         this.vCards = vCards;
         this.context = context;
         this.checkedVCards = checkedVCards;
+        this.accountManager = accountManager;
     }
 
     public int getCheckedCount() {
@@ -679,8 +692,13 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
                     imageView.setImageDrawable(errorDrawable);
                 }
             };
-            DisplayUtils.downloadIcon(context, url, target, R.drawable.ic_user, imageView.getWidth(),
-                    imageView.getHeight());
+            DisplayUtils.downloadIcon(accountManager,
+                                      context,
+                                      url,
+                                      target,
+                                      R.drawable.ic_user,
+                                      imageView.getWidth(),
+                                      imageView.getHeight());
         }
     }
 

+ 18 - 6
src/main/java/com/owncloud/android/utils/DisplayUtils.java

@@ -57,6 +57,7 @@ import com.bumptech.glide.request.target.Target;
 import com.caverock.androidsvg.SVG;
 import com.google.android.material.bottomnavigation.BottomNavigationView;
 import com.google.android.material.snackbar.Snackbar;
+import com.nextcloud.client.account.CurrentAccountProvider;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
@@ -71,6 +72,7 @@ import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.events.MenuItemClickEvent;
 import com.owncloud.android.ui.events.SearchEvent;
 import com.owncloud.android.ui.fragment.OCFileListFragment;
+import com.owncloud.android.utils.glide.CustomGlideUriLoader;
 import com.owncloud.android.utils.svg.SvgDecoder;
 import com.owncloud.android.utils.svg.SvgDrawableTranscoder;
 
@@ -497,11 +499,16 @@ public final class DisplayUtils {
         }
     }
 
-    public static void downloadIcon(Context context, String iconUrl, SimpleTarget imageView, int placeholder,
-                                    int width, int height) {
+    public static void downloadIcon(CurrentAccountProvider currentAccountProvider,
+                                    Context context,
+                                    String iconUrl,
+                                    SimpleTarget imageView,
+                                    int placeholder,
+                                    int width,
+                                    int height) {
         try {
             if (iconUrl.endsWith(".svg")) {
-                downloadSVGIcon(context, iconUrl, imageView, placeholder, width, height);
+                downloadSVGIcon(currentAccountProvider, context, iconUrl, imageView, placeholder, width, height);
             } else {
                 downloadPNGIcon(context, iconUrl, imageView, placeholder);
             }
@@ -521,10 +528,15 @@ public final class DisplayUtils {
                 .into(imageView);
     }
 
-    private static void downloadSVGIcon(Context context, String iconUrl, SimpleTarget imageView, int placeholder,
-                                        int width, int height) {
+    private static void downloadSVGIcon(CurrentAccountProvider currentAccountProvider,
+                                        Context context,
+                                        String iconUrl,
+                                        SimpleTarget imageView,
+                                        int placeholder,
+                                        int width,
+                                        int height) {
         GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder = Glide.with(context)
-                .using(Glide.buildStreamModelLoader(Uri.class, context), InputStream.class)
+            .using(new CustomGlideUriLoader(currentAccountProvider), InputStream.class)
                 .from(Uri.class)
                 .as(SVG.class)
                 .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)

+ 46 - 0
src/main/java/com/owncloud/android/utils/glide/CustomGlideUriLoader.java

@@ -0,0 +1,46 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Tobias Kaminsky
+ * Copyright (C) 2019 Tobias Kaminsky
+ * Copyright (C) 2019 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.utils.glide;
+
+import android.net.Uri;
+
+import com.bumptech.glide.load.data.DataFetcher;
+import com.bumptech.glide.load.model.stream.StreamModelLoader;
+import com.nextcloud.client.account.CurrentAccountProvider;
+
+import java.io.InputStream;
+
+/**
+ * Custom Model for authenticated fetching from Uri
+ */
+public class CustomGlideUriLoader implements StreamModelLoader<Uri> {
+
+    private final CurrentAccountProvider currentAccount;
+
+    public CustomGlideUriLoader(CurrentAccountProvider currentAccount) {
+        this.currentAccount = currentAccount;
+    }
+
+    @Override
+    public DataFetcher<InputStream> getResourceFetcher(Uri url, int width, int height) {
+        return new HttpStreamFetcher(currentAccount, url.toString());
+    }
+}