Skip to content

Commit 9b9721a

Browse files
committed
Add FTP repository.
1 parent 653afa6 commit 9b9721a

8 files changed

Lines changed: 246 additions & 10 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ dependencies {
2424
testCompile 'junit:junit:4.12'
2525
compile 'com.android.support:appcompat-v7:23.1.1'
2626
compile 'com.android.support:design:23.1.1'
27+
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
2728
}

app/libs/ftp4j-1.7.2.jar

67.3 KB
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="com.paulds.simpleftp">
44

55
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.INTERNET"/>
67

78
<application
89
android:name=".presentation.AndroidApplication"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.paulds.simpleftp.data.entities;
2+
3+
/**
4+
* Entity representing a file.
5+
*
6+
* @author Paul-DS
7+
*/
8+
public class FileEntity {
9+
10+
/**
11+
* The file name.
12+
*/
13+
private String name;
14+
15+
/**
16+
* The file path.
17+
*/
18+
private String path;
19+
20+
/**
21+
* The file size.
22+
*/
23+
private Long size;
24+
25+
/**
26+
* Indicates whether this file is a directory.
27+
*/
28+
private boolean isDirectory;
29+
30+
/**
31+
* Gets the file name.
32+
* @return The file name.
33+
*/
34+
public String getName() {
35+
return name;
36+
}
37+
38+
/**
39+
* Sets the file name.
40+
* @param name The file name.
41+
*/
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
/**
47+
* Gets the file path.
48+
* @return The file path.
49+
*/
50+
public String getPath() {
51+
return path;
52+
}
53+
54+
/**
55+
* Sets the file path.
56+
* @param path The file path.
57+
*/
58+
public void setPath(String path) {
59+
this.path = path;
60+
}
61+
62+
/**
63+
* Gets the file size.
64+
* @return The file size.
65+
*/
66+
public Long getSize() {
67+
return size;
68+
}
69+
70+
/**
71+
* Sets the file size.
72+
* @param size The file size.
73+
*/
74+
public void setSize(long size) {
75+
this.size = size;
76+
}
77+
78+
/**
79+
* Indicates whether this file is a directory.
80+
* @return true if the file is a directory; false otherwise
81+
*/
82+
public boolean isDirectory() {
83+
return isDirectory;
84+
}
85+
86+
/**
87+
* Set a value indicating whether this file is a directory.
88+
* @param isDirectory true if the file is a directory; false otherwise
89+
*/
90+
public void setIsDirectory(boolean isDirectory) {
91+
this.isDirectory = isDirectory;
92+
}
93+
}

app/src/main/java/com/paulds/simpleftp/data/repository/ApplicationRepository.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public class ApplicationRepository {
2424
*/
2525
private FileRepository fileRepository;
2626

27+
/**
28+
* The FTP repository.
29+
*/
30+
private FtpRepository ftpRepository;
31+
2732
/**
2833
* Initialize a new instance of ApplicationRepository
2934
* @param context The context used for repositories initialization.
@@ -55,4 +60,16 @@ public FileRepository getFileRepository() {
5560

5661
return this.fileRepository;
5762
}
63+
64+
/**
65+
* Gets the FTP repository.
66+
* @return The FTP repository.
67+
*/
68+
public FtpRepository getFtpRepository() {
69+
if(this.ftpRepository == null) {
70+
this.ftpRepository = new FtpRepository();
71+
}
72+
73+
return this.ftpRepository;
74+
}
5875
}

app/src/main/java/com/paulds/simpleftp/data/repository/FileRepository.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import android.os.Environment;
44

5+
import com.paulds.simpleftp.data.entities.FileEntity;
6+
57
import java.io.File;
8+
import java.util.ArrayList;
9+
import java.util.List;
610

711
/**
812
* This class provide an access to local files.
@@ -16,12 +20,22 @@ public class FileRepository {
1620
* @param path The specified path.
1721
* @return The list of files.
1822
*/
19-
public File[] listFiles(String path) {
23+
public List<FileEntity> listFiles(String path) {
2024
String fullPath = path;
2125

2226
File folder = new File(fullPath);
2327

24-
return folder.listFiles();
28+
File[] files = folder.listFiles();
29+
30+
List<FileEntity> results = new ArrayList<FileEntity>();
31+
32+
if(files != null) {
33+
for (File file : files) {
34+
results.add(this.fileToEntity(file));
35+
}
36+
}
37+
38+
return results;
2539
}
2640

2741

@@ -51,4 +65,19 @@ private boolean isExternalStorageWritable() {
5165
}
5266
return false;
5367
}
68+
69+
/**
70+
* Convert a file to a file entity.
71+
* @param file The fil to convert.
72+
* @return The converted file entity.
73+
*/
74+
private FileEntity fileToEntity(File file)
75+
{
76+
FileEntity entity = new FileEntity();
77+
entity.setPath(file.getPath());
78+
entity.setName(file.getName());
79+
entity.setSize(file.length());
80+
entity.setIsDirectory(file.isDirectory());
81+
return entity;
82+
}
5483
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.paulds.simpleftp.data.repository;
2+
3+
import android.os.Environment;
4+
5+
import com.paulds.simpleftp.data.entities.FileEntity;
6+
import com.paulds.simpleftp.data.entities.FtpServer;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import it.sauronsoftware.ftp4j.FTPAbortedException;
14+
import it.sauronsoftware.ftp4j.FTPClient;
15+
import it.sauronsoftware.ftp4j.FTPDataTransferException;
16+
import it.sauronsoftware.ftp4j.FTPException;
17+
import it.sauronsoftware.ftp4j.FTPFile;
18+
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
19+
import it.sauronsoftware.ftp4j.FTPListParseException;
20+
21+
/**
22+
* This class provide an access to remote files.
23+
*
24+
* @author Paul-DS
25+
*/
26+
public class FtpRepository {
27+
28+
/**
29+
* Lists the files present at the specified path.
30+
* @param path The specified path.
31+
* @return The list of files.
32+
*/
33+
public List<FileEntity> listFiles(FtpServer server, String path) throws FTPException, IOException, FTPIllegalReplyException, FTPAbortedException, FTPDataTransferException, FTPListParseException {
34+
FTPClient client = new FTPClient();
35+
client.connect(server.getHost(), server.getPort());
36+
client.login(server.isAnonymous() ? "anonymous" : server.getLogin(), server.getPassword());
37+
38+
//client.changeDirectory(path);
39+
40+
FTPFile[] list = client.list();
41+
42+
List<FileEntity> results = new ArrayList<FileEntity>();
43+
44+
if(list != null) {
45+
for (FTPFile file : list) {
46+
results.add(this.ftpFileToEntity(file));
47+
}
48+
}
49+
50+
return results;
51+
}
52+
53+
54+
/**
55+
* Create a folder at the specified path.
56+
* @param path The specified path.
57+
* @param name The name of the folder to create.
58+
*/
59+
public void createFolder(String path, String name) {
60+
String fullPath = Environment.getExternalStorageDirectory().toString() + path + "/" + name;
61+
62+
File folder = new File(fullPath);
63+
64+
if(!folder.exists()) {
65+
folder.mkdir();
66+
}
67+
}
68+
69+
/**
70+
* Convert a FTP file to a file entity.
71+
* @param file The FTP file to convert.
72+
* @return The converted file entity.
73+
*/
74+
private FileEntity ftpFileToEntity(FTPFile file)
75+
{
76+
FileEntity entity = new FileEntity();
77+
entity.setPath(file.getLink());
78+
entity.setName(file.getName());
79+
entity.setSize(file.getSize());
80+
entity.setIsDirectory(file.getType() == FTPFile.TYPE_DIRECTORY);
81+
return entity;
82+
}
83+
}

app/src/main/java/com/paulds/simpleftp/presentation/activities/ListFileActivity.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.AlertDialog;
44
import android.content.DialogInterface;
55
import android.content.Intent;
6+
import android.net.Uri;
67
import android.support.design.widget.FloatingActionButton;
78
import android.support.v7.app.AppCompatActivity;
89
import android.os.Bundle;
@@ -13,15 +14,27 @@
1314
import android.widget.ListView;
1415
import android.widget.Toast;
1516

17+
import com.google.android.gms.appindexing.Action;
18+
import com.google.android.gms.appindexing.AppIndex;
19+
import com.google.android.gms.common.api.GoogleApiClient;
1620
import com.paulds.simpleftp.R;
21+
import com.paulds.simpleftp.data.entities.FileEntity;
22+
import com.paulds.simpleftp.data.entities.FtpServer;
1723
import com.paulds.simpleftp.presentation.AndroidApplication;
1824
import com.paulds.simpleftp.presentation.adapters.FileListAdapter;
1925
import com.paulds.simpleftp.presentation.model.FileViewModel;
2026

2127
import java.io.File;
28+
import java.io.IOException;
2229
import java.util.ArrayList;
2330
import java.util.List;
2431

32+
import it.sauronsoftware.ftp4j.FTPAbortedException;
33+
import it.sauronsoftware.ftp4j.FTPDataTransferException;
34+
import it.sauronsoftware.ftp4j.FTPException;
35+
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
36+
import it.sauronsoftware.ftp4j.FTPListParseException;
37+
2538
/**
2639
* Activity which displays a list of files.
2740
*
@@ -121,31 +134,30 @@ public void onClick(DialogInterface dialog, int which) {
121134
* Update the list view with a new path.
122135
* @param path The new path.
123136
*/
124-
private void updateList(String path)
125-
{
137+
private void updateList(String path) {
126138
this.currentPath = path;
127139
filesAdapter.clear();
128140

129-
File[] files = AndroidApplication.getRepository().getFileRepository().listFiles(path);
141+
List<FileEntity> files = AndroidApplication.getRepository().getFileRepository().listFiles(path);
130142

131143
List<FileViewModel> viewModels = new ArrayList<FileViewModel>();
132144

133-
if(path != "/") {
145+
if (path != "/") {
134146
FileViewModel viewModel = new FileViewModel();
135147
viewModel.setFilename("...");
136148
viewModel.setFilepath(path.substring(0, path.lastIndexOf("/") + 1));
137149

138150
viewModels.add(viewModel);
139151
}
140152

141-
if(files != null) {
142-
for (File f : files) {
153+
if (files != null) {
154+
for (FileEntity f : files) {
143155
FileViewModel viewModel = new FileViewModel();
144156
viewModel.setFilename(f.getName());
145157
viewModel.setFilepath(f.getPath());
146158

147-
if(!f.isDirectory()) {
148-
viewModel.setSize(f.length());
159+
if (!f.isDirectory()) {
160+
viewModel.setSize(f.getSize());
149161
}
150162

151163
viewModels.add(viewModel);

0 commit comments

Comments
 (0)