Compare commits
4 Commits
e6e4ef7df5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5faa970b79 | |||
| 0c3b7054bd | |||
| 3357fe76d4 | |||
| 12f27e9f13 |
@@ -32,6 +32,7 @@ dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.3.0'
|
||||
implementation 'com.google.android.material:material:1.4.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
|
||||
implementation 'androidx.security:security-crypto:1.1.0-alpha06'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
|
||||
@@ -31,6 +31,15 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".UserManagementActivity"
|
||||
android:label="Manage Users"
|
||||
android:parentActivityName=".MainActivity">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value=".MainActivity" />
|
||||
</activity>
|
||||
|
||||
<service
|
||||
android:name=".FTPService"
|
||||
android:enabled="true"
|
||||
|
||||
@@ -9,8 +9,12 @@ public class FTPConfig {
|
||||
private static final String KEY_PORT = "ftp_port";
|
||||
private static final String KEY_ROOT_DIR_URI = "root_directory_uri";
|
||||
private static final String KEY_ROOT_DIR_PATH = "root_directory_path";
|
||||
private static final String KEY_MIN_DATA_PORT = "min_data_port";
|
||||
private static final String KEY_MAX_DATA_PORT = "max_data_port";
|
||||
|
||||
private static final int DEFAULT_PORT = 2121;
|
||||
private static final int DEFAULT_MIN_DATA_PORT = 50000;
|
||||
private static final int DEFAULT_MAX_DATA_PORT = 50100;
|
||||
|
||||
private final SharedPreferences preferences;
|
||||
|
||||
@@ -60,4 +64,27 @@ public class FTPConfig {
|
||||
.putString(KEY_ROOT_DIR_PATH, rootDirPath)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public int getMinDataPort() {
|
||||
return preferences.getInt(KEY_MIN_DATA_PORT, DEFAULT_MIN_DATA_PORT);
|
||||
}
|
||||
|
||||
public void setMinDataPort(int port) {
|
||||
preferences.edit().putInt(KEY_MIN_DATA_PORT, port).apply();
|
||||
}
|
||||
|
||||
public int getMaxDataPort() {
|
||||
return preferences.getInt(KEY_MAX_DATA_PORT, DEFAULT_MAX_DATA_PORT);
|
||||
}
|
||||
|
||||
public void setMaxDataPort(int port) {
|
||||
preferences.edit().putInt(KEY_MAX_DATA_PORT, port).apply();
|
||||
}
|
||||
|
||||
public void setDataPortRange(int minPort, int maxPort) {
|
||||
preferences.edit()
|
||||
.putInt(KEY_MIN_DATA_PORT, minPort)
|
||||
.putInt(KEY_MAX_DATA_PORT, maxPort)
|
||||
.apply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,41 @@ public class FTPDataConnection {
|
||||
private int passivePort;
|
||||
|
||||
public boolean openPassiveMode(InetAddress bindAddress) {
|
||||
return openPassiveMode(bindAddress, 0, 0);
|
||||
}
|
||||
|
||||
public boolean openPassiveMode(InetAddress bindAddress, int minPort, int maxPort) {
|
||||
try {
|
||||
if (minPort <= 0 || maxPort <= 0 || minPort > maxPort) {
|
||||
// Use port 0 to get a random available port
|
||||
passiveSocket = new ServerSocket(0, 1, bindAddress);
|
||||
passiveSocket.setSoTimeout(DATA_CONNECTION_TIMEOUT);
|
||||
passivePort = passiveSocket.getLocalPort();
|
||||
|
||||
Log.i(TAG, "Passive mode enabled on port: " + passivePort);
|
||||
Log.i(TAG, "Passive mode enabled on random port: " + passivePort);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try to find an available port in the specified range
|
||||
IOException lastException = null;
|
||||
for (int port = minPort; port <= maxPort; port++) {
|
||||
try {
|
||||
passiveSocket = new ServerSocket(port, 1, bindAddress);
|
||||
passiveSocket.setSoTimeout(DATA_CONNECTION_TIMEOUT);
|
||||
passivePort = passiveSocket.getLocalPort();
|
||||
Log.i(TAG, "Passive mode enabled on port: " + passivePort + " (range: " + minPort + "-" + maxPort + ")");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
lastException = e;
|
||||
// Port is in use, try next port
|
||||
}
|
||||
}
|
||||
|
||||
// No available port found in range
|
||||
Log.e(TAG, "No available port in range " + minPort + "-" + maxPort);
|
||||
if (lastException != null) {
|
||||
Log.e(TAG, "Last error: " + lastException.getMessage());
|
||||
}
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Error opening passive mode: " + e.getMessage());
|
||||
return false;
|
||||
|
||||
@@ -23,20 +23,30 @@ public class FTPServer {
|
||||
private int port;
|
||||
private Context context;
|
||||
private Uri rootDirectoryUri;
|
||||
private int minDataPort = 0;
|
||||
private int maxDataPort = 0;
|
||||
private FTPUserManager userManager;
|
||||
|
||||
public FTPServer(Context context) {
|
||||
this(context, DEFAULT_PORT, null);
|
||||
this(context, DEFAULT_PORT, null, 0, 0);
|
||||
}
|
||||
|
||||
public FTPServer(Context context, int port) {
|
||||
this(context, port, null);
|
||||
this(context, port, null, 0, 0);
|
||||
}
|
||||
|
||||
public FTPServer(Context context, int port, Uri rootDirectoryUri) {
|
||||
this(context, port, rootDirectoryUri, 0, 0);
|
||||
}
|
||||
|
||||
public FTPServer(Context context, int port, Uri rootDirectoryUri, int minDataPort, int maxDataPort) {
|
||||
this.context = context;
|
||||
this.port = port;
|
||||
this.rootDirectoryUri = rootDirectoryUri;
|
||||
this.minDataPort = minDataPort;
|
||||
this.maxDataPort = maxDataPort;
|
||||
this.executorService = Executors.newFixedThreadPool(MAX_CONNECTIONS);
|
||||
this.userManager = FTPUserManager.getInstance(context);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
@@ -56,7 +66,7 @@ public class FTPServer {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
Log.i(TAG, "New client connection from: " + clientSocket.getInetAddress());
|
||||
|
||||
FTPSession session = new FTPSession(clientSocket, context, rootDirectoryUri);
|
||||
FTPSession session = new FTPSession(clientSocket, context, rootDirectoryUri, minDataPort, maxDataPort, userManager);
|
||||
executorService.execute(session);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -37,11 +37,13 @@ public class FTPService extends Service {
|
||||
if (ACTION_START.equals(action)) {
|
||||
int port = intent.getIntExtra("port", 2121);
|
||||
String rootDirUriString = intent.getStringExtra("rootDirUri");
|
||||
int minDataPort = intent.getIntExtra("minDataPort", 0);
|
||||
int maxDataPort = intent.getIntExtra("maxDataPort", 0);
|
||||
android.net.Uri rootDirUri = null;
|
||||
if (rootDirUriString != null && !rootDirUriString.isEmpty()) {
|
||||
rootDirUri = android.net.Uri.parse(rootDirUriString);
|
||||
}
|
||||
startFTPServer(port, rootDirUri);
|
||||
startFTPServer(port, rootDirUri, minDataPort, maxDataPort);
|
||||
} else if (ACTION_STOP.equals(action)) {
|
||||
stopFTPServer();
|
||||
}
|
||||
@@ -50,19 +52,24 @@ public class FTPService extends Service {
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
private void startFTPServer(int port, android.net.Uri rootDirUri) {
|
||||
private void startFTPServer(int port, android.net.Uri rootDirUri, int minDataPort, int maxDataPort) {
|
||||
if (ftpServer != null && ftpServer.isRunning()) {
|
||||
Log.w(TAG, "FTP Server is already running");
|
||||
return;
|
||||
}
|
||||
|
||||
ftpServer = new FTPServer(this, port, rootDirUri);
|
||||
ftpServer = new FTPServer(this, port, rootDirUri, minDataPort, maxDataPort);
|
||||
ftpServer.start();
|
||||
|
||||
Notification notification = createNotification("FTP Server is running on port " + ftpServer.getPort());
|
||||
String notificationText = "FTP Server is running on port " + ftpServer.getPort();
|
||||
if (minDataPort > 0 && maxDataPort > 0) {
|
||||
notificationText += " (Data ports: " + minDataPort + "-" + maxDataPort + ")";
|
||||
}
|
||||
Notification notification = createNotification(notificationText);
|
||||
startForeground(NOTIFICATION_ID, notification);
|
||||
|
||||
Log.i(TAG, "FTP Server started on port " + port + " with root URI: " + rootDirUri);
|
||||
Log.i(TAG, "FTP Server started on port " + port + " with root URI: " + rootDirUri +
|
||||
", data port range: " + minDataPort + "-" + maxDataPort);
|
||||
}
|
||||
|
||||
private void stopFTPServer() {
|
||||
|
||||
@@ -31,15 +31,29 @@ public class FTPSession implements Runnable {
|
||||
private FTPDataConnection dataConnection;
|
||||
private String transferType = "A"; // A = ASCII, I = Binary
|
||||
private boolean useUtf8 = true; // UTF-8 enabled by default for better compatibility
|
||||
private int minDataPort = 0;
|
||||
private int maxDataPort = 0;
|
||||
private FTPUserManager userManager;
|
||||
|
||||
public FTPSession(Socket socket, Context context) {
|
||||
this(socket, context, null);
|
||||
this(socket, context, null, 0, 0, null);
|
||||
}
|
||||
|
||||
public FTPSession(Socket socket, Context context, Uri rootDirectoryUri) {
|
||||
this(socket, context, rootDirectoryUri, 0, 0, null);
|
||||
}
|
||||
|
||||
public FTPSession(Socket socket, Context context, Uri rootDirectoryUri, int minDataPort, int maxDataPort) {
|
||||
this(socket, context, rootDirectoryUri, minDataPort, maxDataPort, null);
|
||||
}
|
||||
|
||||
public FTPSession(Socket socket, Context context, Uri rootDirectoryUri, int minDataPort, int maxDataPort, FTPUserManager userManager) {
|
||||
this.controlSocket = socket;
|
||||
this.fileSystem = new FTPFileSystem(context, rootDirectoryUri);
|
||||
this.dataConnection = null;
|
||||
this.minDataPort = minDataPort;
|
||||
this.maxDataPort = maxDataPort;
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -153,10 +167,16 @@ public class FTPSession implements Runnable {
|
||||
return;
|
||||
}
|
||||
|
||||
// Simple authentication - accept any password for now
|
||||
// In production, implement proper authentication
|
||||
// Authenticate using FTPUserManager
|
||||
if (userManager != null && userManager.authenticate(username, password)) {
|
||||
isAuthenticated = true;
|
||||
sendResponse(FTPResponse.USER_LOGGED_IN, "User logged in");
|
||||
Log.i(TAG, "User authenticated: " + username);
|
||||
} else {
|
||||
isAuthenticated = false;
|
||||
sendResponse(FTPResponse.NOT_LOGGED_IN, "Authentication failed");
|
||||
Log.w(TAG, "Authentication failed for user: " + username);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleQuit() throws IOException {
|
||||
@@ -174,8 +194,6 @@ public class FTPSession implements Runnable {
|
||||
return;
|
||||
}
|
||||
String path = fileSystem.getCurrentPath();
|
||||
// Convert path to NFD format for MacOS compatibility
|
||||
path = denormalizeFilename(path);
|
||||
sendResponse(FTPResponse.PATHNAME_CREATED, "\"" + path + "\" is current directory");
|
||||
}
|
||||
|
||||
@@ -195,8 +213,6 @@ public class FTPSession implements Runnable {
|
||||
|
||||
if (fileSystem.changeDirectory(path)) {
|
||||
String currentPath = fileSystem.getCurrentPath();
|
||||
// Convert path to NFD format for MacOS compatibility
|
||||
currentPath = denormalizeFilename(currentPath);
|
||||
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory changed to " + currentPath);
|
||||
} else {
|
||||
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Failed to change directory");
|
||||
@@ -211,8 +227,6 @@ public class FTPSession implements Runnable {
|
||||
|
||||
if (fileSystem.changeToParentDirectory()) {
|
||||
String currentPath = fileSystem.getCurrentPath();
|
||||
// Convert path to NFD format for MacOS compatibility
|
||||
currentPath = denormalizeFilename(currentPath);
|
||||
sendResponse(FTPResponse.REQUESTED_FILE_ACTION_OK, "Directory changed to " + currentPath);
|
||||
} else {
|
||||
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Already at root directory");
|
||||
@@ -231,8 +245,6 @@ public class FTPSession implements Runnable {
|
||||
}
|
||||
|
||||
String fileList = fileSystem.formatFileList(true);
|
||||
// Convert filenames to NFD format for MacOS compatibility
|
||||
fileList = convertFileListToNFD(fileList);
|
||||
|
||||
sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for directory list");
|
||||
|
||||
@@ -264,8 +276,6 @@ public class FTPSession implements Runnable {
|
||||
}
|
||||
|
||||
String fileList = fileSystem.formatFileList(false);
|
||||
// Convert filenames to NFD format for MacOS compatibility
|
||||
fileList = convertFileListToNFD(fileList);
|
||||
|
||||
sendResponse(FTPResponse.FILE_STATUS_OK, "Opening data connection for name list");
|
||||
|
||||
@@ -301,8 +311,6 @@ public class FTPSession implements Runnable {
|
||||
|
||||
if (fileSystem.makeDirectory(dirName)) {
|
||||
String newPath = fileSystem.getCurrentPath() + "/" + dirName;
|
||||
// Convert path to NFD format for MacOS compatibility (NFC -> NFD for display)
|
||||
newPath = denormalizeFilename(newPath);
|
||||
sendResponse(FTPResponse.PATHNAME_CREATED, "\"" + newPath + "\" directory created");
|
||||
} else {
|
||||
sendResponse(FTPResponse.FILE_UNAVAILABLE, "Failed to create directory");
|
||||
@@ -405,7 +413,7 @@ public class FTPSession implements Runnable {
|
||||
// Get server address from control socket
|
||||
String serverAddress = controlSocket.getLocalAddress().getHostAddress();
|
||||
|
||||
if (dataConnection.openPassiveMode(controlSocket.getLocalAddress())) {
|
||||
if (dataConnection.openPassiveMode(controlSocket.getLocalAddress(), minDataPort, maxDataPort)) {
|
||||
int port = dataConnection.getPassivePort();
|
||||
|
||||
// Format: h1,h2,h3,h4,p1,p2
|
||||
|
||||
53
app/src/main/java/be/gyu/android/server/ftp/FTPUser.java
Normal file
53
app/src/main/java/be/gyu/android/server/ftp/FTPUser.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package be.gyu.android.server.ftp;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class FTPUser {
|
||||
private String username;
|
||||
private String passwordHash;
|
||||
private long createdTimestamp;
|
||||
|
||||
public FTPUser(String username, String passwordHash) {
|
||||
this.username = username;
|
||||
this.passwordHash = passwordHash;
|
||||
this.createdTimestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public FTPUser(String username, String passwordHash, long createdTimestamp) {
|
||||
this.username = username;
|
||||
this.passwordHash = passwordHash;
|
||||
this.createdTimestamp = createdTimestamp;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPasswordHash() {
|
||||
return passwordHash;
|
||||
}
|
||||
|
||||
public long getCreatedTimestamp() {
|
||||
return createdTimestamp;
|
||||
}
|
||||
|
||||
public void setPasswordHash(String passwordHash) {
|
||||
this.passwordHash = passwordHash;
|
||||
}
|
||||
|
||||
public JSONObject toJson() throws JSONException {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("username", username);
|
||||
json.put("passwordHash", passwordHash);
|
||||
json.put("createdTimestamp", createdTimestamp);
|
||||
return json;
|
||||
}
|
||||
|
||||
public static FTPUser fromJson(JSONObject json) throws JSONException {
|
||||
String username = json.getString("username");
|
||||
String passwordHash = json.getString("passwordHash");
|
||||
long createdTimestamp = json.optLong("createdTimestamp", System.currentTimeMillis());
|
||||
return new FTPUser(username, passwordHash, createdTimestamp);
|
||||
}
|
||||
}
|
||||
211
app/src/main/java/be/gyu/android/server/ftp/FTPUserManager.java
Normal file
211
app/src/main/java/be/gyu/android/server/ftp/FTPUserManager.java
Normal file
@@ -0,0 +1,211 @@
|
||||
package be.gyu.android.server.ftp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.security.crypto.EncryptedSharedPreferences;
|
||||
import androidx.security.crypto.MasterKey;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FTPUserManager {
|
||||
private static final String TAG = "FTPUserManager";
|
||||
private static final String PREF_NAME = "ftp_users_encrypted";
|
||||
private static final String KEY_USERS = "ftp_users";
|
||||
|
||||
private static FTPUserManager instance;
|
||||
private SharedPreferences encryptedPrefs;
|
||||
|
||||
private FTPUserManager(Context context) {
|
||||
try {
|
||||
MasterKey masterKey = new MasterKey.Builder(context)
|
||||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
|
||||
.build();
|
||||
|
||||
encryptedPrefs = EncryptedSharedPreferences.create(
|
||||
context,
|
||||
PREF_NAME,
|
||||
masterKey,
|
||||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
|
||||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
|
||||
);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to create encrypted preferences: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized FTPUserManager getInstance(Context context) {
|
||||
if (instance == null) {
|
||||
instance = new FTPUserManager(context.getApplicationContext());
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public synchronized boolean addUser(String username, String password) {
|
||||
if (!isValidUsername(username) || !isValidPassword(password)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (userExists(username)) {
|
||||
Log.w(TAG, "User already exists: " + username);
|
||||
return false;
|
||||
}
|
||||
|
||||
List<FTPUser> users = loadUsers();
|
||||
String passwordHash = hashPassword(password, username);
|
||||
users.add(new FTPUser(username, passwordHash));
|
||||
saveUsers(users);
|
||||
|
||||
Log.i(TAG, "User added: " + username);
|
||||
return true;
|
||||
}
|
||||
|
||||
public synchronized boolean updateUser(String username, String newPassword) {
|
||||
if (!isValidPassword(newPassword)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<FTPUser> users = loadUsers();
|
||||
for (FTPUser user : users) {
|
||||
if (user.getUsername().equals(username)) {
|
||||
String newPasswordHash = hashPassword(newPassword, username);
|
||||
user.setPasswordHash(newPasswordHash);
|
||||
saveUsers(users);
|
||||
Log.i(TAG, "User updated: " + username);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Log.w(TAG, "User not found for update: " + username);
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean deleteUser(String username) {
|
||||
List<FTPUser> users = loadUsers();
|
||||
|
||||
// Prevent deleting the last user
|
||||
if (users.size() <= 1) {
|
||||
Log.w(TAG, "Cannot delete last user");
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean removed = users.removeIf(user -> user.getUsername().equals(username));
|
||||
if (removed) {
|
||||
saveUsers(users);
|
||||
Log.i(TAG, "User deleted: " + username);
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
public synchronized List<FTPUser> getAllUsers() {
|
||||
return new ArrayList<>(loadUsers());
|
||||
}
|
||||
|
||||
public synchronized FTPUser getUser(String username) {
|
||||
List<FTPUser> users = loadUsers();
|
||||
for (FTPUser user : users) {
|
||||
if (user.getUsername().equals(username)) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized boolean authenticate(String username, String password) {
|
||||
FTPUser user = getUser(username);
|
||||
if (user == null) {
|
||||
Log.w(TAG, "Authentication failed - user not found: " + username);
|
||||
return false;
|
||||
}
|
||||
|
||||
String passwordHash = hashPassword(password, username);
|
||||
boolean authenticated = passwordHash.equals(user.getPasswordHash());
|
||||
|
||||
if (authenticated) {
|
||||
Log.i(TAG, "User authenticated: " + username);
|
||||
} else {
|
||||
Log.w(TAG, "Authentication failed - wrong password: " + username);
|
||||
}
|
||||
|
||||
return authenticated;
|
||||
}
|
||||
|
||||
public boolean userExists(String username) {
|
||||
return getUser(username) != null;
|
||||
}
|
||||
|
||||
public boolean isValidUsername(String username) {
|
||||
if (username == null || username.trim().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
username = username.trim();
|
||||
return username.length() >= 3 &&
|
||||
username.length() <= 20 &&
|
||||
username.matches("^[a-zA-Z0-9_]+$");
|
||||
}
|
||||
|
||||
public boolean isValidPassword(String password) {
|
||||
return password != null && password.length() >= 4;
|
||||
}
|
||||
|
||||
private String hashPassword(String password, String salt) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
String saltedPassword = salt + password;
|
||||
byte[] hash = digest.digest(saltedPassword.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// Convert to hex string
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
Log.e(TAG, "SHA-256 not available: " + e.getMessage());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private List<FTPUser> loadUsers() {
|
||||
List<FTPUser> users = new ArrayList<>();
|
||||
String usersJson = encryptedPrefs.getString(KEY_USERS, "[]");
|
||||
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(usersJson);
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
JSONObject userJson = jsonArray.getJSONObject(i);
|
||||
users.add(FTPUser.fromJson(userJson));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error loading users: " + e.getMessage());
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
private void saveUsers(List<FTPUser> users) {
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (FTPUser user : users) {
|
||||
jsonArray.put(user.toJson());
|
||||
}
|
||||
|
||||
encryptedPrefs.edit()
|
||||
.putString(KEY_USERS, jsonArray.toString())
|
||||
.apply();
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error saving users: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import androidx.core.content.ContextCompat;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
@@ -33,6 +35,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
private TextView rootDirPathTextView;
|
||||
private Button selectDirButton;
|
||||
private Button saveSettingsButton;
|
||||
private EditText minDataPortEditText;
|
||||
private EditText maxDataPortEditText;
|
||||
private Button manageUsersButton;
|
||||
|
||||
private FTPConfig config;
|
||||
private ActivityResultLauncher<Uri> directoryPickerLauncher;
|
||||
@@ -50,6 +55,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
loadSettings();
|
||||
setupListeners();
|
||||
requestPermissions();
|
||||
initializeDefaultUser();
|
||||
}
|
||||
|
||||
private void setupDirectoryPicker() {
|
||||
@@ -89,11 +95,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
rootDirPathTextView = findViewById(R.id.rootDirPathTextView);
|
||||
selectDirButton = findViewById(R.id.selectDirButton);
|
||||
saveSettingsButton = findViewById(R.id.saveSettingsButton);
|
||||
minDataPortEditText = findViewById(R.id.minDataPortEditText);
|
||||
maxDataPortEditText = findViewById(R.id.maxDataPortEditText);
|
||||
manageUsersButton = findViewById(R.id.manageUsersButton);
|
||||
}
|
||||
|
||||
private void loadSettings() {
|
||||
int port = config.getPort();
|
||||
String rootPath = config.getRootDirectoryPath();
|
||||
int minDataPort = config.getMinDataPort();
|
||||
int maxDataPort = config.getMaxDataPort();
|
||||
|
||||
portEditText.setText(String.valueOf(port));
|
||||
portTextView.setText("Port: " + port);
|
||||
@@ -103,6 +114,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
} else {
|
||||
rootDirPathTextView.setText("Not selected");
|
||||
}
|
||||
|
||||
minDataPortEditText.setText(String.valueOf(minDataPort));
|
||||
maxDataPortEditText.setText(String.valueOf(maxDataPort));
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
@@ -110,6 +124,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
stopButton.setOnClickListener(v -> stopFTPServer());
|
||||
selectDirButton.setOnClickListener(v -> selectDirectory());
|
||||
saveSettingsButton.setOnClickListener(v -> saveSettings());
|
||||
manageUsersButton.setOnClickListener(v -> openUserManagement());
|
||||
}
|
||||
|
||||
private void selectDirectory() {
|
||||
@@ -140,7 +155,45 @@ public class MainActivity extends AppCompatActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate data port range
|
||||
String minPortStr = minDataPortEditText.getText().toString().trim();
|
||||
String maxPortStr = maxDataPortEditText.getText().toString().trim();
|
||||
|
||||
int minDataPort = 0;
|
||||
int maxDataPort = 0;
|
||||
|
||||
if (!minPortStr.isEmpty() || !maxPortStr.isEmpty()) {
|
||||
if (minPortStr.isEmpty() || maxPortStr.isEmpty()) {
|
||||
Toast.makeText(this, "Please enter both min and max data ports or leave both empty", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
minDataPort = Integer.parseInt(minPortStr);
|
||||
maxDataPort = Integer.parseInt(maxPortStr);
|
||||
|
||||
if (minDataPort < 1024 || minDataPort > 65535 || maxDataPort < 1024 || maxDataPort > 65535) {
|
||||
Toast.makeText(this, "Data ports must be between 1024 and 65535", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (minDataPort >= maxDataPort) {
|
||||
Toast.makeText(this, "Min data port must be less than max data port", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (maxDataPort - minDataPort < 10) {
|
||||
Toast.makeText(this, "Data port range should be at least 10 ports", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
Toast.makeText(this, "Invalid data port number", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
config.setPort(port);
|
||||
config.setDataPortRange(minDataPort, maxDataPort);
|
||||
portTextView.setText("Port: " + port);
|
||||
|
||||
Toast.makeText(this, "Settings saved", Toast.LENGTH_SHORT).show();
|
||||
@@ -194,6 +247,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
serviceIntent.setAction(FTPService.ACTION_START);
|
||||
serviceIntent.putExtra("port", config.getPort());
|
||||
serviceIntent.putExtra("rootDirUri", rootDirUri);
|
||||
serviceIntent.putExtra("minDataPort", config.getMinDataPort());
|
||||
serviceIntent.putExtra("maxDataPort", config.getMaxDataPort());
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(serviceIntent);
|
||||
@@ -239,4 +294,40 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if FTPService is currently running in the background
|
||||
*/
|
||||
private boolean isServiceRunning() {
|
||||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
if (manager != null) {
|
||||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
if (FTPService.class.getName().equals(service.service.getClassName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
// Sync UI with actual service state when activity resumes
|
||||
isServerRunning = isServiceRunning();
|
||||
updateUI();
|
||||
}
|
||||
|
||||
private void openUserManagement() {
|
||||
Intent intent = new Intent(this, UserManagementActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
private void initializeDefaultUser() {
|
||||
FTPUserManager userManager = FTPUserManager.getInstance(this);
|
||||
if (userManager.getAllUsers().isEmpty()) {
|
||||
userManager.addUser("admin", "admin");
|
||||
Toast.makeText(this, "Default user created: admin/admin\nPlease change the password in User Management", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
package be.gyu.android.server.ftp;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UserManagementActivity extends AppCompatActivity {
|
||||
private FTPUserManager userManager;
|
||||
private ListView userListView;
|
||||
private UserListAdapter userAdapter;
|
||||
private List<FTPUser> userList;
|
||||
private TextView emptyView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_user_management);
|
||||
|
||||
userManager = FTPUserManager.getInstance(this);
|
||||
|
||||
initViews();
|
||||
setupListeners();
|
||||
refreshUserList();
|
||||
}
|
||||
|
||||
private void initViews() {
|
||||
userListView = findViewById(R.id.userListView);
|
||||
emptyView = findViewById(R.id.emptyView);
|
||||
Button addUserButton = findViewById(R.id.addUserButton);
|
||||
|
||||
userList = new ArrayList<>();
|
||||
userAdapter = new UserListAdapter();
|
||||
userListView.setAdapter(userAdapter);
|
||||
userListView.setEmptyView(emptyView);
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
findViewById(R.id.addUserButton).setOnClickListener(v -> showAddUserDialog());
|
||||
}
|
||||
|
||||
private void showAddUserDialog() {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_user, null);
|
||||
builder.setView(dialogView);
|
||||
|
||||
TextView dialogTitle = dialogView.findViewById(R.id.dialogTitle);
|
||||
EditText usernameEditText = dialogView.findViewById(R.id.usernameEditText);
|
||||
EditText passwordEditText = dialogView.findViewById(R.id.passwordEditText);
|
||||
EditText confirmPasswordEditText = dialogView.findViewById(R.id.confirmPasswordEditText);
|
||||
TextView errorTextView = dialogView.findViewById(R.id.errorTextView);
|
||||
Button saveButton = dialogView.findViewById(R.id.saveButton);
|
||||
Button cancelButton = dialogView.findViewById(R.id.cancelButton);
|
||||
|
||||
dialogTitle.setText("Add User");
|
||||
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
saveButton.setOnClickListener(v -> {
|
||||
String username = usernameEditText.getText().toString().trim();
|
||||
String password = passwordEditText.getText().toString();
|
||||
String confirmPassword = confirmPasswordEditText.getText().toString();
|
||||
|
||||
// Validate username
|
||||
if (username.isEmpty()) {
|
||||
errorTextView.setText("Username is required");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!userManager.isValidUsername(username)) {
|
||||
errorTextView.setText("Username must be 3-20 characters, alphanumeric + underscore");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (userManager.userExists(username)) {
|
||||
errorTextView.setText("Username already exists");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate password
|
||||
if (password.isEmpty()) {
|
||||
errorTextView.setText("Password is required");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!userManager.isValidPassword(password)) {
|
||||
errorTextView.setText("Password must be at least 4 characters");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password.equals(confirmPassword)) {
|
||||
errorTextView.setText("Passwords do not match");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
// Add user
|
||||
if (userManager.addUser(username, password)) {
|
||||
Toast.makeText(this, "User added successfully", Toast.LENGTH_SHORT).show();
|
||||
refreshUserList();
|
||||
dialog.dismiss();
|
||||
} else {
|
||||
errorTextView.setText("Failed to add user");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
cancelButton.setOnClickListener(v -> dialog.dismiss());
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void showEditUserDialog(FTPUser user) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_user, null);
|
||||
builder.setView(dialogView);
|
||||
|
||||
TextView dialogTitle = dialogView.findViewById(R.id.dialogTitle);
|
||||
EditText usernameEditText = dialogView.findViewById(R.id.usernameEditText);
|
||||
EditText passwordEditText = dialogView.findViewById(R.id.passwordEditText);
|
||||
EditText confirmPasswordEditText = dialogView.findViewById(R.id.confirmPasswordEditText);
|
||||
TextView errorTextView = dialogView.findViewById(R.id.errorTextView);
|
||||
Button saveButton = dialogView.findViewById(R.id.saveButton);
|
||||
Button cancelButton = dialogView.findViewById(R.id.cancelButton);
|
||||
|
||||
dialogTitle.setText("Edit User");
|
||||
usernameEditText.setText(user.getUsername());
|
||||
usernameEditText.setEnabled(false);
|
||||
|
||||
AlertDialog dialog = builder.create();
|
||||
|
||||
saveButton.setOnClickListener(v -> {
|
||||
String password = passwordEditText.getText().toString();
|
||||
String confirmPassword = confirmPasswordEditText.getText().toString();
|
||||
|
||||
// Validate password
|
||||
if (password.isEmpty()) {
|
||||
errorTextView.setText("Password is required");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!userManager.isValidPassword(password)) {
|
||||
errorTextView.setText("Password must be at least 4 characters");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password.equals(confirmPassword)) {
|
||||
errorTextView.setText("Passwords do not match");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
return;
|
||||
}
|
||||
|
||||
// Update user
|
||||
if (userManager.updateUser(user.getUsername(), password)) {
|
||||
Toast.makeText(this, "User updated successfully", Toast.LENGTH_SHORT).show();
|
||||
refreshUserList();
|
||||
dialog.dismiss();
|
||||
} else {
|
||||
errorTextView.setText("Failed to update user");
|
||||
errorTextView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
|
||||
cancelButton.setOnClickListener(v -> dialog.dismiss());
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void showDeleteConfirmation(FTPUser user) {
|
||||
// Check if this is the last user
|
||||
if (userList.size() <= 1) {
|
||||
Toast.makeText(this, "Cannot delete the last user", Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle("Delete User")
|
||||
.setMessage("Are you sure you want to delete user '" + user.getUsername() + "'?")
|
||||
.setPositiveButton("Delete", (dialog, which) -> {
|
||||
if (userManager.deleteUser(user.getUsername())) {
|
||||
Toast.makeText(this, "User deleted successfully", Toast.LENGTH_SHORT).show();
|
||||
refreshUserList();
|
||||
} else {
|
||||
Toast.makeText(this, "Failed to delete user", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
private void refreshUserList() {
|
||||
userList.clear();
|
||||
userList.addAll(userManager.getAllUsers());
|
||||
userAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
private class UserListAdapter extends BaseAdapter {
|
||||
@Override
|
||||
public int getCount() {
|
||||
return userList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int position) {
|
||||
return userList.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if (convertView == null) {
|
||||
convertView = LayoutInflater.from(UserManagementActivity.this)
|
||||
.inflate(R.layout.item_user, parent, false);
|
||||
}
|
||||
|
||||
FTPUser user = userList.get(position);
|
||||
|
||||
TextView usernameView = convertView.findViewById(R.id.usernameTextView);
|
||||
Button editButton = convertView.findViewById(R.id.editButton);
|
||||
Button deleteButton = convertView.findViewById(R.id.deleteButton);
|
||||
|
||||
usernameView.setText(user.getUsername());
|
||||
editButton.setOnClickListener(v -> showEditUserDialog(user));
|
||||
deleteButton.setOnClickListener(v -> showDeleteConfirmation(user));
|
||||
|
||||
return convertView;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,13 +68,24 @@
|
||||
app:layout_constraintTop_toBottomOf="@id/startButton"
|
||||
android:layout_marginTop="16dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/manageUsersButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manage Users"
|
||||
android:minWidth="150dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/stopButton"
|
||||
android:layout_marginTop="16dp" />
|
||||
|
||||
<View
|
||||
android:id="@+id/divider"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="#CCCCCC"
|
||||
android:layout_marginTop="32dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/stopButton"
|
||||
app:layout_constraintTop_toBottomOf="@id/manageUsersButton"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
@@ -144,6 +155,62 @@
|
||||
app:layout_constraintTop_toBottomOf="@id/rootDirPathTextView"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/dataPortRangeLabel"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Data Port Range:"
|
||||
android:textSize="16sp"
|
||||
android:layout_marginTop="16dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/selectDirButton"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/minDataPortLabel"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Min:"
|
||||
android:textSize="14sp"
|
||||
android:layout_marginTop="8dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/dataPortRangeLabel"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/minDataPortEditText"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="number"
|
||||
android:hint="50000"
|
||||
android:layout_marginStart="16dp"
|
||||
app:layout_constraintBaseline_toBaselineOf="@id/minDataPortLabel"
|
||||
app:layout_constraintStart_toEndOf="@id/minDataPortLabel"
|
||||
app:layout_constraintEnd_toStartOf="@id/maxDataPortLabel"
|
||||
app:layout_constraintWidth_default="percent"
|
||||
app:layout_constraintHorizontal_weight="1" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/maxDataPortLabel"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Max:"
|
||||
android:textSize="14sp"
|
||||
android:layout_marginStart="16dp"
|
||||
app:layout_constraintBaseline_toBaselineOf="@id/minDataPortLabel"
|
||||
app:layout_constraintStart_toEndOf="@id/minDataPortEditText" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/maxDataPortEditText"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="number"
|
||||
android:hint="50100"
|
||||
android:layout_marginStart="16dp"
|
||||
app:layout_constraintBaseline_toBaselineOf="@id/minDataPortLabel"
|
||||
app:layout_constraintStart_toEndOf="@id/maxDataPortLabel"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintWidth_default="percent"
|
||||
app:layout_constraintHorizontal_weight="1" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/saveSettingsButton"
|
||||
android:layout_width="wrap_content"
|
||||
@@ -151,7 +218,7 @@
|
||||
android:text="Save Settings"
|
||||
android:minWidth="150dp"
|
||||
android:layout_marginTop="24dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/selectDirButton"
|
||||
app:layout_constraintTop_toBottomOf="@id/minDataPortLabel"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
|
||||
57
app/src/main/res/layout/activity_user_management.xml
Normal file
57
app/src/main/res/layout/activity_user_management.xml
Normal file
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="16dp"
|
||||
tools:context=".UserManagementActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/titleTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="User Management"
|
||||
android:textSize="24sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:layout_marginTop="16dp" />
|
||||
|
||||
<ListView
|
||||
android:id="@+id/userListView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/titleTextView"
|
||||
app:layout_constraintBottom_toTopOf="@id/addUserButton"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/emptyView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="No users. Add your first user."
|
||||
android:textSize="16sp"
|
||||
android:textColor="#999999"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintTop_toTopOf="@id/userListView"
|
||||
app:layout_constraintBottom_toBottomOf="@id/userListView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/addUserButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Add User"
|
||||
android:minWidth="150dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
94
app/src/main/res/layout/dialog_add_user.xml
Normal file
94
app/src/main/res/layout/dialog_add_user.xml
Normal file
@@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="24dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/dialogTitle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Add User"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Username:"
|
||||
android:textSize="14sp"
|
||||
android:layout_marginBottom="4dp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/usernameEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="3-20 characters, alphanumeric + underscore"
|
||||
android:inputType="text"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Password:"
|
||||
android:textSize="14sp"
|
||||
android:layout_marginBottom="4dp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/passwordEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Minimum 4 characters"
|
||||
android:inputType="textPassword"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Confirm Password:"
|
||||
android:textSize="14sp"
|
||||
android:layout_marginBottom="4dp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/confirmPasswordEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Re-enter password"
|
||||
android:inputType="textPassword"
|
||||
android:layout_marginBottom="8dp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/errorTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Error message"
|
||||
android:textColor="#FF0000"
|
||||
android:textSize="14sp"
|
||||
android:visibility="gone"
|
||||
android:layout_marginBottom="16dp" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="end">
|
||||
|
||||
<Button
|
||||
android:id="@+id/cancelButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Cancel"
|
||||
style="?android:attr/borderlessButtonStyle"
|
||||
android:layout_marginEnd="8dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/saveButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Save" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
34
app/src/main/res/layout/item_user.xml
Normal file
34
app/src/main/res/layout/item_user.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:padding="16dp"
|
||||
android:gravity="center_vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/usernameTextView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
android:text="Username" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/editButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Edit"
|
||||
android:layout_marginStart="8dp"
|
||||
style="?android:attr/borderlessButtonStyle" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/deleteButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Delete"
|
||||
android:layout_marginStart="8dp"
|
||||
style="?android:attr/borderlessButtonStyle" />
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user