From f9fb4c2d1642beb4a470c68b92cb1977fef66f81 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 17 Apr 2021 20:58:12 +1000 Subject: [PATCH] Android: Fix filenames for SAF paths showing incorrectly --- .../app/src/cpp/android_host_interface.cpp | 16 +++++-------- .../duckstation/EmulationActivity.java | 2 +- .../stenzek/duckstation/FileHelper.java | 23 +++++++++++++++++++ .../stenzek/duckstation/GameListEntry.java | 15 +----------- .../stenzek/duckstation/GameListFragment.java | 6 +---- .../duckstation/GamePropertiesActivity.java | 1 - 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index b66c3bc67..1aed1d7d0 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -1003,10 +1003,10 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) (s_EmulationActivity_method_setInputDeviceVibration = env->GetMethodID(s_EmulationActivity_class, "setInputDeviceVibration", "(IFF)V")) == nullptr || (s_PatchCode_constructor = env->GetMethodID(s_PatchCode_class, "", "(ILjava/lang/String;Z)V")) == nullptr || - (s_GameListEntry_constructor = env->GetMethodID( - s_GameListEntry_class, "", - "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/" - "String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) == nullptr || + (s_GameListEntry_constructor = + env->GetMethodID(s_GameListEntry_class, "", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/" + "String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) == nullptr || (s_SaveStateInfo_constructor = env->GetMethodID( s_SaveStateInfo_class, "", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IZII[B)V")) == @@ -1345,13 +1345,11 @@ static jobject CreateGameListEntry(JNIEnv* env, AndroidHostInterface* hi, const { const Timestamp modified_ts( Timestamp::FromUnixTimestamp(static_cast(entry.last_modified_time))); - const std::string file_title_str(FileSystem::GetFileTitleFromPath(entry.path)); const std::string cover_path_str(hi->GetGameList()->GetCoverImagePathForEntry(&entry)); jstring path = env->NewStringUTF(entry.path.c_str()); jstring code = env->NewStringUTF(entry.code.c_str()); jstring title = env->NewStringUTF(entry.title.c_str()); - jstring file_title = env->NewStringUTF(file_title_str.c_str()); jstring region = env->NewStringUTF(DiscRegionToString(entry.region)); jstring type = env->NewStringUTF(GameList::EntryTypeToString(entry.type)); jstring compatibility_rating = @@ -1360,9 +1358,8 @@ static jobject CreateGameListEntry(JNIEnv* env, AndroidHostInterface* hi, const jstring modified_time = env->NewStringUTF(modified_ts.ToString("%Y/%m/%d, %H:%M:%S")); jlong size = entry.total_size; - jobject entry_jobject = - env->NewObject(s_GameListEntry_class, s_GameListEntry_constructor, path, code, title, file_title, size, - modified_time, region, type, compatibility_rating, cover_path); + jobject entry_jobject = env->NewObject(s_GameListEntry_class, s_GameListEntry_constructor, path, code, title, size, + modified_time, region, type, compatibility_rating, cover_path); env->DeleteLocalRef(modified_time); if (cover_path) @@ -1370,7 +1367,6 @@ static jobject CreateGameListEntry(JNIEnv* env, AndroidHostInterface* hi, const env->DeleteLocalRef(compatibility_rating); env->DeleteLocalRef(type); env->DeleteLocalRef(region); - env->DeleteLocalRef(file_title); env->DeleteLocalRef(title); env->DeleteLocalRef(code); env->DeleteLocalRef(path); diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/EmulationActivity.java b/android/app/src/main/java/com/github/stenzek/duckstation/EmulationActivity.java index 5737f20d7..fcb8d19f2 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/EmulationActivity.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/EmulationActivity.java @@ -712,7 +712,7 @@ public class EmulationActivity extends AppCompatActivity implements SurfaceHolde CharSequence[] items = new CharSequence[numPaths + 1]; for (int i = 0; i < numPaths; i++) - items[i] = GameListEntry.getFileNameForPath(paths[i]); + items[i] = FileHelper.getFileNameForPath(paths[i]); items[numPaths] = getString(R.string.emulation_activity_change_disc_select_new_file); builder.setSingleChoiceItems(items, (currentPath < numPaths) ? currentPath : -1, (dialogInterface, i) -> { diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/FileHelper.java b/android/app/src/main/java/com/github/stenzek/duckstation/FileHelper.java index edab85e50..006e9601e 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/FileHelper.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/FileHelper.java @@ -242,6 +242,29 @@ public class FileHelper { } } + /** + * Returns the file name component of a path or URI. + * @param path Path/URI to examine. + * @return File name component of path/URI. + */ + public static String getFileNameForPath(String path) { + if (path.startsWith("content:/") || path.startsWith("file:/")) { + try { + final Uri uri = Uri.parse(path); + final String lastPathSegment = uri.getLastPathSegment(); + if (lastPathSegment != null) + path = lastPathSegment; + } catch (Exception e) { + } + } + + int lastSlash = path.lastIndexOf('/'); + if (lastSlash > 0 && lastSlash < path.length() - 1) + return path.substring(lastSlash + 1); + else + return path; + } + /** * Retrieves a file descriptor for a content URI string. Called by native code. * diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/GameListEntry.java b/android/app/src/main/java/com/github/stenzek/duckstation/GameListEntry.java index 22e69cb4d..95e5674b5 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/GameListEntry.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/GameListEntry.java @@ -27,7 +27,6 @@ public class GameListEntry { private String mPath; private String mCode; private String mTitle; - private String mFileTitle; private long mSize; private String mModifiedTime; private DiscRegion mRegion; @@ -35,12 +34,11 @@ public class GameListEntry { private CompatibilityRating mCompatibilityRating; private String mCoverPath; - public GameListEntry(String path, String code, String title, String fileTitle, long size, String modifiedTime, String region, + public GameListEntry(String path, String code, String title, long size, String modifiedTime, String region, String type, String compatibilityRating, String coverPath) { mPath = path; mCode = code; mTitle = title; - mFileTitle = fileTitle; mSize = size; mModifiedTime = modifiedTime; mCoverPath = coverPath; @@ -76,10 +74,6 @@ public class GameListEntry { return mTitle; } - public String getFileTitle() { - return mFileTitle; - } - public long getSize() { return mSize; } public String getModifiedTime() { @@ -102,11 +96,4 @@ public class GameListEntry { public void setCoverPath(String coverPath) { mCoverPath = coverPath; } - public static String getFileNameForPath(String path) { - int lastSlash = path.lastIndexOf('/'); - if (lastSlash > 0 && lastSlash < path.length() - 1) - return path.substring(lastSlash + 1); - else - return path; - } } diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/GameListFragment.java b/android/app/src/main/java/com/github/stenzek/duckstation/GameListFragment.java index bef38bd95..01635d7b5 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/GameListFragment.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/GameListFragment.java @@ -2,14 +2,10 @@ package com.github.stenzek.duckstation; import android.os.AsyncTask; import android.os.Bundle; -import android.view.Gravity; import android.view.LayoutInflater; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; import android.widget.ImageView; -import android.widget.PopupMenu; import android.widget.TextView; import androidx.annotation.NonNull; @@ -74,7 +70,7 @@ public class GameListFragment extends Fragment implements GameList.OnRefreshList } private String getSubTitle() { - String fileName = GameListEntry.getFileNameForPath(mEntry.getPath()); + String fileName = FileHelper.getFileNameForPath(mEntry.getPath()); String sizeString = String.format("%.2f MB", (double) mEntry.getSize() / 1048576.0); return String.format("%s (%s)", fileName, sizeString); } diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/GamePropertiesActivity.java b/android/app/src/main/java/com/github/stenzek/duckstation/GamePropertiesActivity.java index cd0847d2f..d9c11fb97 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/GamePropertiesActivity.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/GamePropertiesActivity.java @@ -32,7 +32,6 @@ public class GamePropertiesActivity extends AppCompatActivity { mPropertiesListAdapter = new PropertyListAdapter(this); mPropertiesListAdapter.addItem("title", "Title", mGameListEntry.getTitle()); - mPropertiesListAdapter.addItem("filetitle", "File Title", mGameListEntry.getFileTitle()); mPropertiesListAdapter.addItem("serial", "Serial", mGameListEntry.getCode()); mPropertiesListAdapter.addItem("type", "Type", mGameListEntry.getType().toString()); mPropertiesListAdapter.addItem("path", "Path", mGameListEntry.getPath());