| 17 | import java.util.List; |
| 18 | |
| 19 | public class Drive { |
| 20 | |
| 21 | @SerializedName("name") |
| 22 | private String name; |
| 23 | @SerializedName("server") |
| 24 | private String server; |
| 25 | |
| 26 | private Connection connection; |
| 27 | private SMBClient smbClient; |
| 28 | private DiskShare diskShare; |
| 29 | private Session session; |
| 30 | private String subPath; |
| 31 | |
| 32 | public static List<Drive> arrayFrom(String str) { |
| 33 | Type listType = TypeToken.getParameterized(List.class, Drive.class).getType(); |
| 34 | return new Gson().fromJson(str, listType); |
| 35 | } |
| 36 | |
| 37 | public Drive(String name) { |
| 38 | this.name = name; |
| 39 | } |
| 40 | |
| 41 | private String getName() { |
| 42 | return TextUtils.isEmpty(name) ? "" : name; |
| 43 | } |
| 44 | |
| 45 | public String getServer() { |
| 46 | return TextUtils.isEmpty(server) ? "" : server; |
| 47 | } |
| 48 | |
| 49 | public String getSubPath() { |
| 50 | return TextUtils.isEmpty(subPath) ? "" : subPath; |
| 51 | } |
| 52 | |
| 53 | public DiskShare getShare() { |
| 54 | if (diskShare == null) init(); |
| 55 | return diskShare; |
| 56 | } |
| 57 | |
| 58 | public Class toType() { |
| 59 | return new Class(getName(), getName(), "1"); |
| 60 | } |
| 61 | |
| 62 | private void init() { |
| 63 | try { |
| 64 | smbClient = new SMBClient(); |
| 65 | Uri uri = Uri.parse(getServer()); |
| 66 | String[] parts = uri.getPath().substring(1).split("/", 2); |
| 67 | connection = smbClient.connect(uri.getHost(), uri.getPort() != -1 ? uri.getPort() : SMBClient.DEFAULT_PORT); |
| 68 | session = connection.authenticate(getAuthentication(uri)); |
| 69 | diskShare = (DiskShare) session.connectShare(parts[0]); |
| 70 | subPath = parts.length > 1 ? parts[1] : ""; |
| 71 | } catch (Exception e) { |
| 72 | e.printStackTrace(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private AuthenticationContext getAuthentication(Uri uri) { |
nothing calls this directly
no outgoing calls
no test coverage detected