Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11017 from JosJuice/saf-custom-cover
Android: Fix reading custom covers with SAF
  • Loading branch information
lioncash committed Sep 1, 2022
2 parents 9195e1a + 40d6d61 commit 11281b5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
Expand Up @@ -61,7 +61,7 @@ private static LinkedHashSet<String> getPathSet(boolean removeNonExistentFolders
String path = dolphinIni.getString(Settings.SECTION_INI_GENERAL,
SettingsFile.KEY_ISO_PATH_BASE + i, "");

if (path.startsWith("content://") ? ContentHandler.exists(path) : new File(path).exists())
if (ContentHandler.isContentUri(path) ? ContentHandler.exists(path) : new File(path).exists())
{
pathSet.add(path);
}
Expand Down
Expand Up @@ -37,6 +37,11 @@

public class ContentHandler
{
public static boolean isContentUri(@NonNull String pathOrUri)
{
return pathOrUri.startsWith("content://");
}

@Keep
public static int openFd(@NonNull String uri, @NonNull String mode)
{
Expand Down Expand Up @@ -336,7 +341,7 @@ private static Uri getChild(@NonNull Uri parentUri, @NonNull String childName)
* provider to use URIs without any % characters.
*/
@NonNull
private static Uri unmangle(@NonNull String uri) throws FileNotFoundException, SecurityException
public static Uri unmangle(@NonNull String uri) throws FileNotFoundException, SecurityException
{
int lastComponentEnd = getLastComponentEnd(uri);
int lastComponentStart = getLastComponentStart(uri, lastComponentEnd);
Expand Down
Expand Up @@ -82,9 +82,16 @@ public static boolean isPathEmptyOrValid(StringSetting path)
return isPathEmptyOrValid(path.getStringGlobal());
}

/**
* Returns true if at least one of the following applies:
*
* 1. The input is empty.
* 2. The input is something which is not a content URI.
* 3. The input is a content URI that points to a file that exists and we're allowed to access.
*/
public static boolean isPathEmptyOrValid(String path)
{
return !path.startsWith("content://") || ContentHandler.exists(path);
return !ContentHandler.isContentUri(path) || ContentHandler.exists(path);
}

public static void runAfterExtensionCheck(Context context, Uri uri, Set<String> validExtensions,
Expand Down
Expand Up @@ -18,6 +18,7 @@
import org.dolphinemu.dolphinemu.viewholders.GameViewHolder;

import java.io.File;
import java.io.FileNotFoundException;

public class PicassoUtils
{
Expand Down Expand Up @@ -54,12 +55,33 @@ else if (gameViewHolder != null)
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 = new File(gameFile.getCustomCoverPath());
if (cover.exists())
File cover;
if (customCoverExists)
{
Picasso.get()
.load(cover)
.load(customCoverUri)
.noFade()
.noPlaceholder()
.fit()
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.dolphinemu.dolphinemu.ui.platform.Platform;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -155,26 +156,43 @@ public static Bitmap convertToBitmap(Context context, int resourceId)
}

/**
* Leanback lanucher requires a uri for poster art so we create a contentUri and
* Leanback launcher requires a uri for poster art so we create a contentUri and
* pass that to LEANBACK_PACKAGE
*/
public static Uri buildBanner(GameFile game, Context context)
{
Uri contentUri = null;
File cover;

try
{
File cover = new File(game.getCustomCoverPath());
if (cover.exists())
String customCoverPath = game.getCustomCoverPath();

if (ContentHandler.isContentUri(customCoverPath))
{
contentUri = getUriForFile(context, getFileProvider(context), cover);
try
{
contentUri = ContentHandler.unmangle(customCoverPath);
}
catch (FileNotFoundException | SecurityException ignored)
{
// Let contentUri remain null
}
}
else
{
if ((cover = new File(customCoverPath)).exists())
{
contentUri = getUriForFile(context, getFileProvider(context), cover);
}
}
else if ((cover = new File(game.getCoverPath(context))).exists())

if (contentUri == null && (cover = new File(game.getCoverPath(context))).exists())
{
contentUri = getUriForFile(context, getFileProvider(context), cover);
}
context.grantUriPermission(LEANBACK_PACKAGE, contentUri,
FLAG_GRANT_READ_URI_PERMISSION);

context.grantUriPermission(LEANBACK_PACKAGE, contentUri, FLAG_GRANT_READ_URI_PERMISSION);
}
catch (Exception e)
{
Expand Down

0 comments on commit 11281b5

Please sign in to comment.