Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #10626 from t895/glide
Android: Replace Picasso with Glide
  • Loading branch information
JosJuice committed Sep 13, 2022
2 parents 14f427d + c79b12a commit 7f450f1
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 293 deletions.
4 changes: 2 additions & 2 deletions Source/Android/app/build.gradle
Expand Up @@ -112,8 +112,8 @@ dependencies {
// For REST calls
implementation 'com.android.volley:volley:1.2.1'

// For loading huge screenshots from the disk.
implementation 'com.squareup.picasso:picasso:2.71828'
// For loading game covers from disk and GameTDB
implementation 'com.github.bumptech.glide:glide:4.13.1'

implementation 'com.nononsenseapps:filepicker:4.2.1'
}
Expand Down
Expand Up @@ -17,7 +17,7 @@
import org.dolphinemu.dolphinemu.dialogs.GamePropertiesDialog;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.services.GameFileCacheManager;
import org.dolphinemu.dolphinemu.utils.PicassoUtils;
import org.dolphinemu.dolphinemu.utils.GlideUtils;
import org.dolphinemu.dolphinemu.viewholders.GameViewHolder;

import java.util.ArrayList;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void onBindViewHolder(GameViewHolder holder, int position)
{
Context context = holder.itemView.getContext();
GameFile gameFile = mGameFiles.get(position);
PicassoUtils.loadGameCover(holder, holder.imageScreenshot, gameFile);
GlideUtils.loadGameCover(holder, holder.imageScreenshot, gameFile);

if (GameFileCacheManager.findSecondDisc(gameFile) != null)
{
Expand Down
Expand Up @@ -16,7 +16,7 @@
import org.dolphinemu.dolphinemu.dialogs.GamePropertiesDialog;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.services.GameFileCacheManager;
import org.dolphinemu.dolphinemu.utils.PicassoUtils;
import org.dolphinemu.dolphinemu.utils.GlideUtils;
import org.dolphinemu.dolphinemu.viewholders.TvGameViewHolder;

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ public void onBindViewHolder(ViewHolder viewHolder, Object item)
GameFile gameFile = (GameFile) item;

holder.imageScreenshot.setImageDrawable(null);
PicassoUtils.loadGameCover(null, holder.imageScreenshot, gameFile);
GlideUtils.loadGameCover(null, holder.imageScreenshot, gameFile);

holder.cardParent.setTitleText(gameFile.getTitle());

Expand Down
Expand Up @@ -17,7 +17,7 @@
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.services.GameFileCacheManager;
import org.dolphinemu.dolphinemu.utils.PicassoUtils;
import org.dolphinemu.dolphinemu.utils.GlideUtils;

public final class GameDetailsDialog extends DialogFragment
{
Expand Down Expand Up @@ -114,7 +114,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState)
}
}

PicassoUtils.loadGameBanner(banner, gameFile);
GlideUtils.loadGameBanner(banner, gameFile);

return new MaterialAlertDialogBuilder(requireActivity())
.setView(contents)
Expand Down

This file was deleted.

@@ -0,0 +1,220 @@
// SPDX-License-Identifier: GPL-2.0-or-later

package org.dolphinemu.dolphinemu.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;

import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.viewholders.GameViewHolder;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GlideUtils
{
private static final ExecutorService executor = Executors.newSingleThreadExecutor();

public static void loadGameBanner(ImageView imageView, GameFile gameFile)
{
Context context = imageView.getContext();
int[] vector = gameFile.getBanner();
int width = gameFile.getBannerWidth();
int height = gameFile.getBannerHeight();
if (width > 0 && height > 0)
{
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(vector, 0, width, 0, 0, width, height);
Glide.with(context)
.load(bitmap)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.into(imageView);
}
else
{
Glide.with(context)
.load(R.drawable.no_banner)
.into(imageView);
}
}

public static void loadGameCover(GameViewHolder gameViewHolder, ImageView imageView,
GameFile gameFile)
{
if (BooleanSetting.MAIN_SHOW_GAME_TITLES.getBooleanGlobal() && gameViewHolder != null)
{
gameViewHolder.textGameTitle.setText(gameFile.getTitle());
gameViewHolder.textGameTitle.setVisibility(View.VISIBLE);
gameViewHolder.textGameTitleInner.setVisibility(View.GONE);
gameViewHolder.textGameCaption.setVisibility(View.VISIBLE);
}
else if (gameViewHolder != null)
{
gameViewHolder.textGameTitleInner.setText(gameFile.getTitle());
gameViewHolder.textGameTitle.setVisibility(View.GONE);
gameViewHolder.textGameCaption.setVisibility(View.GONE);
}

String customCoverPath = gameFile.getCustomCoverPath();
Uri customCoverUri = null;
boolean customCoverExists = false;
if (ContentHandler.isContentUri(customCoverPath))
{
try
{
customCoverUri = ContentHandler.unmangle(customCoverPath);
customCoverExists = true;
}
catch (FileNotFoundException | SecurityException ignored)
{
// Let customCoverExists remain false
}
}
else
{
customCoverUri = Uri.parse(customCoverPath);
customCoverExists = new File(customCoverPath).exists();
}

Context context = imageView.getContext();
File cover;
if (customCoverExists)
{
Glide.with(context)
.load(customCoverUri)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.listener(new RequestListener<Drawable>()
{
@Override public boolean onLoadFailed(@Nullable GlideException e, Object model,
Target<Drawable> target, boolean isFirstResource)
{
GlideUtils.enableInnerTitle(gameViewHolder, imageView);
return false;
}

@Override public boolean onResourceReady(Drawable resource, Object model,
Target<Drawable> target, DataSource dataSource, boolean isFirstResource)
{
GlideUtils.disableInnerTitle(gameViewHolder);
return false;
}
})
.into(imageView);
}
else if ((cover = new File(gameFile.getCoverPath(context))).exists())
{
Glide.with(context)
.load(cover)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.listener(new RequestListener<Drawable>()
{
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model,
Target<Drawable> target, boolean isFirstResource)
{
GlideUtils.enableInnerTitle(gameViewHolder, imageView);
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model,
Target<Drawable> target, DataSource dataSource, boolean isFirstResource)
{
GlideUtils.disableInnerTitle(gameViewHolder);
return false;
}
})
.into(imageView);
}
else if (BooleanSetting.MAIN_USE_GAME_COVERS.getBooleanGlobal())
{
Glide.with(context)
.load(CoverHelper.buildGameTDBUrl(gameFile, CoverHelper.getRegion(gameFile)))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.centerCrop()
.listener(new RequestListener<Drawable>()
{
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model,
Target<Drawable> target, boolean isFirstResource)
{
GlideUtils.enableInnerTitle(gameViewHolder, imageView);
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model,
Target<Drawable> target, DataSource dataSource, boolean isFirstResource)
{
GlideUtils.disableInnerTitle(gameViewHolder);
return false;
}
})
.into(new CustomTarget<Drawable>()
{
@Override
public void onResourceReady(@NonNull Drawable resource,
@Nullable Transition<? super Drawable> transition)
{
Bitmap cover = ((BitmapDrawable) resource).getBitmap();
executor.execute(
() -> CoverHelper.saveCover(cover, gameFile.getCoverPath(context)));
imageView.setImageBitmap(cover);
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder)
{
}
});
}
else
{
enableInnerTitle(gameViewHolder, imageView);
}
}

private static void enableInnerTitle(GameViewHolder gameViewHolder, ImageView imageView)
{
Glide.with(imageView.getContext())
.load(R.drawable.no_banner)
.into(imageView);

if (gameViewHolder != null && !BooleanSetting.MAIN_SHOW_GAME_TITLES.getBooleanGlobal())
{
gameViewHolder.textGameTitleInner.setVisibility(View.VISIBLE);
}
}

private static void disableInnerTitle(GameViewHolder gameViewHolder)
{
if (gameViewHolder != null && !BooleanSetting.MAIN_SHOW_GAME_TITLES.getBooleanGlobal())
{
gameViewHolder.textGameTitleInner.setVisibility(View.GONE);
}
}
}

0 comments on commit 7f450f1

Please sign in to comment.