Android: Fix filenames for SAF paths showing incorrectly

This commit is contained in:
Connor McLaughlin
2021-04-17 20:58:12 +10:00
parent c3f914565f
commit f9fb4c2d16
6 changed files with 32 additions and 31 deletions

View File

@ -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) -> {

View File

@ -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.
*

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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());