mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-07 22:55:40 +12:00
Resolve PathUtils.java
This commit is contained in:
parent
6757117e0d
commit
058530bd90
3 changed files with 47 additions and 1 deletions
|
@ -5,6 +5,7 @@ import android.os.Bundle;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.result.ActivityResultCallback;
|
import androidx.activity.result.ActivityResultCallback;
|
||||||
import androidx.activity.result.ActivityResultLauncher;
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
@ -50,6 +51,10 @@ public class GamesFragment extends Fragment implements ActivityResultCallback<Ur
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
String uri = result.toString();
|
String uri = result.toString();
|
||||||
if (GameUtils.findByRomPath(uri) == null) {
|
if (GameUtils.findByRomPath(uri) == null) {
|
||||||
|
if (FileUtils.obtainRealPath(uri) == null){
|
||||||
|
Toast.makeText(getContext(), "Invalid file path", Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
FileUtils.makeUriPermanent(uri, FileUtils.MODE_READ);
|
FileUtils.makeUriPermanent(uri, FileUtils.MODE_READ);
|
||||||
GameMetadata game = new GameMetadata(uri, FileUtils.getName(uri).split("\\.")[0],"Unknown");
|
GameMetadata game = new GameMetadata(uri, FileUtils.getName(uri).split("\\.")[0],"Unknown");
|
||||||
GameUtils.addGame(game);
|
GameUtils.addGame(game);
|
||||||
|
|
|
@ -3,6 +3,8 @@ package com.panda3ds.pandroid.utils;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
|
import android.system.Os;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.documentfile.provider.DocumentFile;
|
import androidx.documentfile.provider.DocumentFile;
|
||||||
|
@ -15,9 +17,11 @@ import java.io.FileNotFoundException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
public static final String MODE_READ = "r";
|
public static final String MODE_READ = "r";
|
||||||
|
public static final int CANONICAL_SEARCH_DEEP = 8;
|
||||||
|
|
||||||
private static DocumentFile parseFile(String path) {
|
private static DocumentFile parseFile(String path) {
|
||||||
if (path.startsWith("/")) {
|
if (path.startsWith("/")) {
|
||||||
|
@ -125,4 +129,41 @@ public class FileUtils {
|
||||||
|
|
||||||
getContext().getContentResolver().takePersistableUriPermission(Uri.parse(uri), flags);
|
getContext().getContentResolver().takePersistableUriPermission(Uri.parse(uri), flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When call ContentProvider.openFileDescriptor() android open a file descriptor
|
||||||
|
* on app process in /proc/self/fd/[file descriptor id] this is a link to real file path
|
||||||
|
* can use File.getCanonicalPath() for get a link origin, but in some android version
|
||||||
|
* need use Os.readlink(path) to get a real path.
|
||||||
|
*/
|
||||||
|
public static String obtainRealPath(String uri) {
|
||||||
|
try {
|
||||||
|
ParcelFileDescriptor parcelDescriptor = getContext().getContentResolver().openFileDescriptor(Uri.parse(uri), "r");
|
||||||
|
int fd = parcelDescriptor.getFd();
|
||||||
|
File file = new File("/proc/self/fd/" + fd).getAbsoluteFile();
|
||||||
|
for (int i = 0; i < CANONICAL_SEARCH_DEEP; i++) {
|
||||||
|
try {
|
||||||
|
String canonical = file.getCanonicalPath();
|
||||||
|
if (!Objects.equals(canonical, file.getAbsolutePath())) {
|
||||||
|
file = new File(canonical).getAbsoluteFile();
|
||||||
|
}
|
||||||
|
} catch (Exception x) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.getAbsolutePath().startsWith("/proc/self/")) {
|
||||||
|
parcelDescriptor.close();
|
||||||
|
return file.getAbsolutePath();
|
||||||
|
}
|
||||||
|
String path = Os.readlink(file.getAbsolutePath());
|
||||||
|
parcelDescriptor.close();
|
||||||
|
|
||||||
|
if (new File(path).exists())
|
||||||
|
return path;
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ public class GameUtils {
|
||||||
|
|
||||||
public static void launch(Context context, GameMetadata game) {
|
public static void launch(Context context, GameMetadata game) {
|
||||||
currentGame = game;
|
currentGame = game;
|
||||||
String path = PathUtils.getPath(Uri.parse(game.getRomPath()));
|
String path = FileUtils.obtainRealPath(game.getRomPath());
|
||||||
context.startActivity(new Intent(context, GameActivity.class).putExtra(Constants.ACTIVITY_PARAMETER_PATH, path));
|
context.startActivity(new Intent(context, GameActivity.class).putExtra(Constants.ACTIVITY_PARAMETER_PATH, path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue