Skip to content

Commit

Permalink
feat: change getFile method
Browse files Browse the repository at this point in the history
this commit prepares the server to allow multiple folders selection. The settings activity is not prepared yet (the next commit will change it).
  • Loading branch information
jhonathanc committed Sep 6, 2023
1 parent e33736a commit 06ca336
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 29 deletions.
17 changes: 14 additions & 3 deletions app/src/main/java/com/jhonju/ps3netsrv/app/FirstFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

public class FirstFragment extends Fragment {

Expand Down Expand Up @@ -77,13 +79,22 @@ public void onClick(View view) {
isServerRunning = !isServerRunning;
btnStartServer.setText(isServerRunning ? R.string.stop_server : R.string.start_server);

String folderPath = SettingsService.getFolder();
Set<String> folderPaths = SettingsService.getFolders();
Set<String> folderPathsAux = new HashSet<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
folderPath = URLDecoder.decode(folderPath, StandardCharsets.UTF_8.displayName());
for (String folderPath: folderPaths) {
folderPathsAux.add(URLDecoder.decode(folderPath, StandardCharsets.UTF_8.displayName()));
}
} else {
folderPathsAux = folderPaths;
}
int port = SettingsService.getPort();

String serverRunningMsg = isServerRunning ? String.format(getResources().getString(R.string.server_running), Utils.getIPAddress(true), port, folderPath) :
StringBuilder sb = new StringBuilder();
for (String item : folderPathsAux) {
sb.append(item).append("\n");
}
String serverRunningMsg = isServerRunning ? String.format(getResources().getString(R.string.server_running), Utils.getIPAddress(true), port, sb.toString()) :
getResources().getString(R.string.server_stopped);

tvServerState.setText(serverRunningMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void onCreate() {
executorService = Executors.newSingleThreadExecutor();
int idListType = SettingsService.getListType();
EListType eListType = idListType == R.id.rbNone ? EListType.LIST_TYPE_NONE : idListType == R.id.rbAllowed ? EListType.LIST_TYPE_ALLOWED : EListType.LIST_TYPE_BLOCKED;
task = new PS3NetSrvTask(SettingsService.getPort(), SettingsService.getFolder(), SettingsService.getMaxConnections(), SettingsService.isReadOnly(), SettingsService.getIps(), eListType, exceptionHandler);
task = new PS3NetSrvTask(SettingsService.getPort(), SettingsService.getFolders(), SettingsService.getMaxConnections(), SettingsService.isReadOnly(), SettingsService.getIps(), eListType, exceptionHandler);
}

@Override
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/jhonju/ps3netsrv/app/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;

Expand Down Expand Up @@ -73,9 +74,9 @@ private String saveMaxConnection() {

private void loadSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
((TextInputLayout) findViewById(R.id.tilFolder)).getEditText().setText(Uri.parse(SettingsService.getFolder()).getPath());
((TextInputLayout) findViewById(R.id.tilFolder)).getEditText().setText(Uri.parse(SettingsService.getFolders().iterator().next()).getPath());
} else {
((TextInputLayout) findViewById(R.id.tilFolder)).getEditText().setText(SettingsService.getFolder());
((TextInputLayout) findViewById(R.id.tilFolder)).getEditText().setText(SettingsService.getFolders().iterator().next());
}
((TextInputLayout) findViewById(R.id.tilPort)).getEditText().setText(SettingsService.getPort() + "");
((TextInputLayout) findViewById(R.id.tilMaximumClientsNumber)).getEditText().setText(SettingsService.getMaxConnections() + "");
Expand Down Expand Up @@ -234,7 +235,9 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
private final SimpleFileChooser.FileSelectedListener onFileSelectedListener = new SimpleFileChooser.FileSelectedListener() {
@Override
public void onFileSelected(File file) {
SettingsService.setFolder(file.getAbsolutePath());
Set<String> folderAux = new HashSet<>();
folderAux.add(file.getAbsolutePath());
SettingsService.setFolders(folderAux);
((TextInputLayout) findViewById(R.id.tilFolder)).getEditText().setText(file.getAbsolutePath());
}
};
Expand All @@ -245,7 +248,9 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE_PICK_FOLDER && resultCode == RESULT_OK && data != null) {
SettingsService.setFolder(data.getData().toString());
Set<String> folderAux = new HashSet<>();
folderAux.add(data.getData().toString());
SettingsService.setFolders(folderAux);
((TextInputLayout) findViewById(R.id.tilFolder)).getEditText().setText(data.getData().getPath());
}
}
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/com/jhonju/ps3netsrv/app/SettingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SettingsService {
private static final String settings = "settings";
private static SharedPreferences spPort = PS3NetSrvApp.getAppContext().getSharedPreferences("PORT",0);
private static SharedPreferences spFolder = PS3NetSrvApp.getAppContext().getSharedPreferences("FOLDER",0);
private static SharedPreferences spFolders = PS3NetSrvApp.getAppContext().getSharedPreferences("FOLDERS",0);
private static SharedPreferences spIps = PS3NetSrvApp.getAppContext().getSharedPreferences("IPS",0);
private static SharedPreferences spListType = PS3NetSrvApp.getAppContext().getSharedPreferences("LIST_TYPE",0);
private static SharedPreferences spMaxConnections = PS3NetSrvApp.getAppContext().getSharedPreferences("MAX_CONNECTIONS",0);
Expand All @@ -22,8 +23,10 @@ public class SettingsService {

public static Set<String> getIps() { return spIps.getStringSet(settings, new HashSet<String>()); }

public static String getFolder() {
return spFolder.getString(settings, getDefaultFolder());
public static Set<String> getFolders() {
HashSet<String> folders = new HashSet<String>();
folders.add(spFolder.getString(settings, getDefaultFolder())); //if user has updated to this new version, reuse the settings.
return spFolders.getStringSet(settings, folders);
}

public static int getListType() { return spListType.getInt(settings, 0); }
Expand Down Expand Up @@ -52,9 +55,9 @@ public static void setPort(int port) {
editor.apply();
}

public static void setFolder(String folder) {
SharedPreferences.Editor editor = spFolder.edit();
editor.putString(settings, folder);
public static void setFolders(Set<String> folders) {
SharedPreferences.Editor editor = spFolders.edit();
editor.putStringSet(settings, folders);
editor.apply();
}

Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/jhonju/ps3netsrv/server/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Set;

public class Context {
private Socket socket;
private final String rootDirectory;
private final Set<String> rootDirectorys;
private final boolean readOnly;
private IFile file;
private IRandomAccessFile readOnlyFile;
private CDSectorSize cdSectorSize;

public Context(Socket socket, String rootDirectory, boolean readOnly) {
this.rootDirectory = rootDirectory;
public Context(Socket socket, Set<String> rootDirectorys, boolean readOnly) {
this.rootDirectorys = rootDirectorys;
this.socket = socket;
this.cdSectorSize = CDSectorSize.CD_SECTOR_2352;
this.readOnly = readOnly;
}

public String getRootDirectory() { return rootDirectory; }
public Set<String> getRootDirectorys() { return rootDirectorys; }

public boolean isSocketConnected() { return socket.isConnected(); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
public class PS3NetSrvTask implements Runnable {
private final Thread.UncaughtExceptionHandler exceptionHandler;
private final int port;
private final String folderPath;
private final Set<String> folderPaths;
private final int maxConnections;
private final boolean readOnly;
private final EListType listType;
private final Set<String> filterAddresses;
private ServerSocket serverSocket;
private boolean isRunning = true;

public PS3NetSrvTask(int port, String folderPath, int maxConnections, boolean readOnly, Set<String> filterAddresses, EListType listType, Thread.UncaughtExceptionHandler exceptionHandler) {
public PS3NetSrvTask(int port, Set<String> folderPaths, int maxConnections, boolean readOnly, Set<String> filterAddresses, EListType listType, Thread.UncaughtExceptionHandler exceptionHandler) {
this.port = port;
this.folderPath = folderPath;
this.folderPaths = folderPaths;
this.maxConnections = maxConnections;
this.readOnly = readOnly;
this.filterAddresses = filterAddresses;
Expand All @@ -45,7 +45,7 @@ public void run() {
}
continue;
}
new ContextHandler(new Context(clientSocket, folderPath, readOnly), maxConnections, exceptionHandler).start();
new ContextHandler(new Context(clientSocket, folderPaths, readOnly), maxConnections, exceptionHandler).start();
}
} catch (IOException e) {
exceptionHandler.uncaughtException(null, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,32 @@ protected IFile getFile() throws IOException, PS3NetSrvException {
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return new File(new java.io.File(ctx.getRootDirectory(), new String(buffer.array(), StandardCharsets.UTF_8).replaceAll("\\x00+$", "")));
File file = null;
for (String rootDirectory : ctx.getRootDirectorys()) {
File fileAux = new File(new java.io.File(rootDirectory, new String(buffer.array(), StandardCharsets.UTF_8).replaceAll("\\x00+$", "")));
if (fileAux.exists()) {
file = fileAux;
break;
}
}
if (file == null) {
send(ERROR_CODE_BYTEARRAY);
throw new PS3NetSrvException("ERROR: file not found.");
}
return file;
}

androidx.documentfile.provider.DocumentFile documentFile = androidx.documentfile.provider.DocumentFile.fromTreeUri(PS3NetSrvApp.getAppContext(), Uri.parse(ctx.getRootDirectory()));
if (documentFile == null || !documentFile.exists()) {
androidx.documentfile.provider.DocumentFile documentFile = null;
for (String rootDirectory : ctx.getRootDirectorys()) {
androidx.documentfile.provider.DocumentFile documentFileAux = androidx.documentfile.provider.DocumentFile.fromTreeUri(PS3NetSrvApp.getAppContext(), Uri.parse(rootDirectory));
if (documentFileAux.exists()) {
documentFile = documentFileAux;
break;
}
}
if (documentFile == null) {
send(ERROR_CODE_BYTEARRAY);
throw new PS3NetSrvException("ERROR: wrong path configuration.");
throw new PS3NetSrvException("ERROR: file not found.");
}

String path = getFormattedPath(new String(buffer.array(), StandardCharsets.UTF_8));
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<string name="start_server">Iniciar servidor</string>
<string name="stop_server">Detener servidor</string>
<string name="server_stopped">Servidor detenido</string>
<string name="server_running">Servidor en ejecución en %s\npuerto %d\nRuta: %s</string>
<string name="server_running">Servidor en ejecución en %s\npuerto %d\nRutas: %s</string>
<string name="read_external_permission_error">Primero debe dar los permisos de lectura de almacenamiento</string>
<string name="listType">Tipo de lista:</string>
<string name="rbNone">Ninguna</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<string name="start_server">Avviare il server</string>
<string name="stop_server">Arrestare il server</string>
<string name="server_stopped">Il server è stato arrestato</string>
<string name="server_running">Il server è in esecuzione su %s\nporta %d\ncartella %s</string>
<string name="server_running">Il server è in esecuzione su %s\nporta %d\ncartelle: %s</string>
<string name="read_external_permission_error">È necessario concedere prima i permessi di lettura dello storage</string>
<string name="listType">Tipo di lista:</string>
<string name="rbNone">Nessuno</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<string name="start_server">Iniciar servidor</string>
<string name="stop_server">Parar servidor</string>
<string name="server_stopped">Servidor parado</string>
<string name="server_running">Servidor em execução na %s\nporta %d\npasta %s</string>
<string name="server_running">Servidor em execução na %s\nporta %d\npastas: %s</string>
<string name="read_external_permission_error">Você precisa conceder permissões de leitura de armazenamento primeiro</string>
<string name="listType">Tipo de lista:</string>
<string name="rbNone">Nenhum</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<string name="start_server">Start server</string>
<string name="stop_server">Stop server</string>
<string name="server_stopped">Server is stopped</string>
<string name="server_running">Server is running at %s\nport %d\nfolder %s</string>
<string name="server_running">Server is running at %s\nport %d\nfolders: %s</string>
<string name="read_external_permission_error">You need to grant permission to read storage first</string>
<string name="listType">List type:</string>
<string name="rbNone">None</string>
Expand Down

0 comments on commit 06ca336

Please sign in to comment.