From ed85e6ae49f3f1bb0f1c9d160d466b1500e69679 Mon Sep 17 00:00:00 2001 From: Schoumi Date: Sun, 24 Apr 2016 18:03:42 +0200 Subject: [PATCH] Network Management, get binaries list and a specific binary from the registred servers --- app/src/main/AndroidManifest.xml | 2 + .../lpcprog/listener/NetworkListener.java | 37 +++ .../lpcprog/managers/DatabaseManager.java | 21 ++ .../lpcprog/managers/NetworkManager.java | 237 ++++++++++++++++++ .../fr/mobdev/lpcprog/objects/Server.java | 4 + app/src/main/res/layout/binary_item.xml | 6 + app/src/main/res/values/strings.xml | 3 + build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 9 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/fr/mobdev/lpcprog/listener/NetworkListener.java create mode 100644 app/src/main/res/layout/binary_item.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e817e2f..6de4084 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,8 @@ + + > servers = DatabaseManager.getInstance(context).getServers(); + List serversAvailable = new ArrayList<>(); + for(Server server : servers.get(0)){ + serversAvailable.add(server); + } + for(Server server : servers.get(1)){ + serversAvailable.add(server); + } + + listener.startBinaries(); + + for(Server server : serversAvailable) { + listener.startServer(server); + try { + URL url = new URL(server.url.toString()+"Binaries"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + InputStream stream = connection.getInputStream(); + if(stream != null){ + InputStreamReader isr = new InputStreamReader(stream); + BufferedReader reader = new BufferedReader(isr); + String line = reader.readLine(); + while(line != null){ + Binary b = parseLine(line); + if(b != null) + server.addBinary(b); + line = reader.readLine(); + } + } + } catch (IOException e) { + e.printStackTrace(); + DatabaseManager.getInstance(context).serverFail(server); + } finally { + listener.endServer(server); + } + } + listener.endBinaries(); + } + + private Binary parseLine(String line){ + if(!line.matches("(.*;){5}")){ + System.out.println("Line not match regex"); + return null; + } + Binary b = new Binary(); + b.name = line.substring(0,line.indexOf(";")); + line = line.substring(line.indexOf(";")+1); + b.filename = line.substring(0,line.indexOf(";")); + line = line.substring(line.indexOf(";")+1); + b.version = Integer.valueOf(line.substring(0,line.indexOf(";"))); + line = line.substring(line.indexOf(";")+1); + b.path = line.substring(0,line.indexOf(";")); + line = line.substring(line.indexOf(";")+1); + b.sha1 = line.substring(0,line.indexOf(";")); + return b; + } + + private void getBinary(Server server, Binary binary){ + try { + URL url = new URL(server.url.toString() + binary.path + binary.filename); + downloadInProgress = true; + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + InputStream stream = connection.getInputStream(); + File file = new File(context.getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename); + file.getParentFile().mkdirs(); + if (!file.createNewFile() && !file.canWrite()){ + listener.downloadError(server,binary,context.getString(R.string.cant_create_file)); + return; + } + FileOutputStream output = new FileOutputStream(file); + int fileLength = connection.getContentLength(); + if (stream != null) { + byte[] data = new byte[4096]; + long totalSizeReaded = 0; + int sizeReaded ; + while((sizeReaded = stream.read(data)) != -1){ + if(isCanceled) { + isCanceled = false; + downloadInProgress = false; + listener.downloadCanceled(server,binary); + stream.close(); + output.close(); + cleanupFile(server,binary); + return; + } + totalSizeReaded += sizeReaded; + if(fileLength > 0){ + listener.progress(server,binary, (int) totalSizeReaded * 100 / fileLength); + } + output.write(data,0,sizeReaded); + } + output.close(); + if(checkFileSha1(server,binary)) + listener.downloadSuccessful(server,binary); + else + listener.downloadError(server,binary,context.getString(R.string.sha1_error)); + } + }catch (IOException e) { + e.printStackTrace(); + cleanupFile(server,binary); + listener.downloadFailed(server,binary); + } + downloadInProgress = false; + isCanceled = false; + } + + public boolean checkFileSha1(Server server, Binary binary){ + String sha1 = ""; + try { + File f = new File(context.getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename); + FileInputStream fis = new FileInputStream(f); + MessageDigest md = MessageDigest.getInstance("SHA-1"); + byte[] data = new byte[4096]; + int sizeReaded; + while((sizeReaded = fis.read(data)) >0){ + md.update(data,0,sizeReaded); + } + byte[] output = md.digest(); + for(byte b : output){ + sha1 += String.format("%02x",b); + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("Sha1 "+sha1); + return sha1.compareToIgnoreCase(binary.sha1) == 0; + } + + public boolean cleanupFile(Server server, Binary binary){ + File f = new File(context.getApplicationInfo().dataDir+"/"+server.id+"/"+binary.path+binary.filename); + return f.delete(); + } + + public void cancelDownload(){ + if(downloadInProgress) + isCanceled = true; + } + + private boolean isConnectedToInternet(Context context) + { + //verify the connectivity + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); + if (networkInfo != null) + { + NetworkInfo.State networkState = networkInfo.getState(); + if (networkState.equals(NetworkInfo.State.CONNECTED)) + { + return true; + } + } + return false; + } } diff --git a/app/src/main/java/fr/mobdev/lpcprog/objects/Server.java b/app/src/main/java/fr/mobdev/lpcprog/objects/Server.java index 44eba04..6e54ecf 100644 --- a/app/src/main/java/fr/mobdev/lpcprog/objects/Server.java +++ b/app/src/main/java/fr/mobdev/lpcprog/objects/Server.java @@ -10,4 +10,8 @@ public class Server { public boolean isActive; public int attempted; public List binaries = new ArrayList<>(); + + public void addBinary(Binary b) { + binaries.add(b); + } } diff --git a/app/src/main/res/layout/binary_item.xml b/app/src/main/res/layout/binary_item.xml new file mode 100644 index 0000000..3509b84 --- /dev/null +++ b/app/src/main/res/layout/binary_item.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9a66216..4019401 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,6 +12,9 @@ Add a Server Device Informations Manage Servers + Network Unavailable + Verification on file Failed + Can\\\'t create file http:// https:// diff --git a/build.gradle b/build.gradle index a6fcfe1..cb98401 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c71e76..d570517 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip