Network Management, get binaries list and a specific binary from the

registred servers
This commit is contained in:
Schoumi 2016-04-24 18:03:42 +02:00
parent a0c752d341
commit ed85e6ae49
9 ha cambiato i file con 312 aggiunte e 2 eliminazioni

Vedi File

@ -5,6 +5,8 @@
<uses-feature
android:name="android.hardware.usb.host"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"

Vedi File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.lpcprog.listener;
import java.util.EventListener;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
public interface NetworkListener extends EventListener{
void startBinaries();
void startServer(Server server);
void endServer(Server server);
void endBinaries();
void onError(String string);
void downloadCanceled(Server server, Binary binary);
void downloadSuccessful(Server server, Binary binary);
void downloadFailed(Server server, Binary binary);
void downloadError(Server server, Binary binary, String string);
void progress(Server server, Binary binary, int progress);
}

Vedi File

@ -1,3 +1,21 @@
/*
* Copyright (C) 2015 Anthony Chomienne, anthony@mob-dev.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package fr.mobdev.lpcprog.managers;
import android.content.ContentValues;
@ -101,4 +119,7 @@ public class DatabaseManager extends SQLiteOpenHelper {
cursor.close();
return list;
}
public void serverFail(Server server) {
}
}

Vedi File

@ -1,4 +1,241 @@
package fr.mobdev.lpcprog.managers;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import fr.mobdev.lpcprog.R;
import fr.mobdev.lpcprog.listener.NetworkListener;
import fr.mobdev.lpcprog.objects.Binary;
import fr.mobdev.lpcprog.objects.Server;
public class NetworkManager {
private NetworkListener listener;
private static NetworkManager instance;
private Context context;
private boolean isCanceled;
private boolean downloadInProgress;
private NetworkManager(NetworkListener listener, Context context)
{
this.listener = listener;
this.context = context;
this.isCanceled = false;
}
public static NetworkManager getInstance(NetworkListener listener, Context context){
if (instance == null)
instance = new NetworkManager(listener,context);
if(listener != null && listener != instance.getListener())
instance.setListener(listener);
return instance;
}
private NetworkListener getListener()
{
return listener;
}
private void setListener(NetworkListener listener)
{
this.listener = listener;
}
public void browseBinaries(){
if(isConnectedToInternet(context)) {
new Thread(new Runnable() {
@Override
public void run() {
getBinariesList();
}
}).start();
}
else
listener.onError(context.getString(R.string.network_error));
}
public void retrieveBinary(final Server server, final Binary binary){
if(isConnectedToInternet(context)){
new Thread(new Runnable() {
@Override
public void run() {
getBinary(server, binary);
}
}).start();
}
else
listener.onError(context.getString(R.string.network_error));
}
private void getBinariesList(){
List<List<Server>> servers = DatabaseManager.getInstance(context).getServers();
List<Server> 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;
}
}

Vedi File

@ -10,4 +10,8 @@ public class Server {
public boolean isActive;
public int attempted;
public List<Binary> binaries = new ArrayList<>();
public void addBinary(Binary b) {
binaries.add(b);
}
}

Vedi File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>

Vedi File

@ -12,6 +12,9 @@
<string name="add_server">Add a Server</string>
<string name="device_informations">Device Informations</string>
<string name="server_management">Manage Servers</string>
<string name="network_error">Network Unavailable</string>
<string name="sha1_error">Verification on file Failed</string>
<string name="cant_create_file">Can\\\'t create file</string>
<array name="https">
<item>http://</item>
<item>https://</item>

Vedi File

@ -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

Vedi File

@ -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