X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=mainActivity%2Fsrc%2Fcom%2Fexample%2Ffurt%2Fmyapplication%2Faggiorna.java;fp=mainActivity%2Fsrc%2Fcom%2Fexample%2Ffurt%2Fmyapplication%2Faggiorna.java;h=a97220e983a57be932289b35a628719f4b9916a4;hb=92c1b7c5e38b8d6ffbeffb2c515bb99149841803;hp=0000000000000000000000000000000000000000;hpb=71021eb632e1429274a66cf4344e107b9c5fb0d5;p=logicplayer.git diff --git a/mainActivity/src/com/example/furt/myapplication/aggiorna.java b/mainActivity/src/com/example/furt/myapplication/aggiorna.java new file mode 100755 index 0000000..a97220e --- /dev/null +++ b/mainActivity/src/com/example/furt/myapplication/aggiorna.java @@ -0,0 +1,241 @@ +package com.example.furt.myapplication; + + +import android.content.Context; +import android.os.AsyncTask; +import android.os.Environment; +import android.util.Log; +import android.widget.Toast; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class aggiorna { + + aggiorna(){} + + public String sincronizza(String sessionKey,String user,String pass) { + String ritorno = null; + String login = "1/" + user + "/" + pass; + while (ritorno == null || ritorno.contains("request-login")) + { + ritorno = serverSync(sessionKey, user); + if(ritorno == null || ritorno.contains("request-login")) + { + sessionKey= serverComunication.connessione(login); + } + else if(ritorno.contains("error")) + Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show(); + } + ritorno=null; + while (ritorno == null || ritorno.contains("request-login")) + { + ritorno = syncLocaldb(sessionKey, user); + if(ritorno == null || ritorno.contains("request-login")) + { + sessionKey= serverComunication.connessione(login); + } + else if(ritorno.contains("error")) + Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show(); + } + ritorno=null; + while (ritorno == null || ritorno.contains("request-login")) + { + ritorno=downloadEX(sessionKey); + if(ritorno == null || ritorno.contains("request-login")) + { + sessionKey= serverComunication.connessione(login); + } + else if(ritorno.contains("error")) + Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show(); + } + return sessionKey; + } + + public String serverSync(String sessionKey, String user) + { + personalDBHelper connessione = new personalDBHelper(aggiornamento.t.getContext()); + List esercizi=connessione.getElem(user); + if(esercizi!=null && !esercizi.isEmpty()) { + for (listElem esercizio : esercizi) { + Log.e("",esercizio.getEsercizio());} + for (listElem esercizio : esercizi) { + String richiesta = "8/" + sessionKey + "/" + esercizio.getEsercizio() + "/" + esercizio.getMd5() + "/" + esercizio.getTime() + "/" + esercizio.getClick(); + String ritorno = serverComunication.connessione(richiesta); + if (ritorno.contains("es-ok")) + connessione.updateElem(esercizio.getMd5(), user, esercizio.getTime()); + else if (ritorno.contains("request-login") || ritorno.contains("es-err")){ + if(ritorno.contains("es-err")) + connessione.remove(esercizio.getUtente(),esercizio.getTime()); + return ritorno; + } + } + } + return "sync-ok"; + } + + public String syncLocaldb(String sessionKey, String utente) + { + String richiesta = "9/" + sessionKey; + String ritorno = serverComunication.connessione(richiesta); + if(ritorno.contains("request-login")) + return ritorno; + else + { + personalDBHelper connessione=new personalDBHelper(aggiornamento.t.getContext().getApplicationContext()); + ArrayList allExercise=suString.stringToMultiArray(ritorno); + List localExercise=connessione.getAllElem(utente); + if(localExercise!=null && !localExercise.isEmpty()) { + for (String ex : allExercise) { + ArrayList tmp = suString.stringToArrayList(ex); + boolean add = true; + for (listElem elem : localExercise) { + if (tmp.get(0).equals(elem.getEsercizio()) && Double.valueOf(tmp.get(2)) == elem.getTime()) { + if (elem.getCheck() == 0) + connessione.updateElem(tmp.get(1), utente, Double.valueOf(tmp.get(2))); + add = false; + } + } + if (add) + if(tmp.size()>3) + connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1); + } + } + else{ + for (String ex : allExercise) { + ArrayList tmp = suString.stringToArrayList(ex); + if(tmp.size()>3) + connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1); + } + } + } + return "local-sync-ok"; + } + + public String downloadEX(String sessionKey) + { + Context context = aggiornamento.t.getContext(); + File SDCardRoot = Environment.getExternalStorageDirectory(); + String[] names = SDCardRoot.list(); + String ritorno=null; + boolean go = true; + for (String name : names) { + if (name.compareTo("tesiEs") == 0) { + go = false; + break; + } + } + if (go) { + File dir = new File(global.esDir); + dir.mkdir(); + } + try { + ritorno = serverComunication.connessione("7/" + sessionKey); + if (!ritorno.contains("error") && !ritorno.contains("request-login")) + { + ArrayList esercizi= suString.stringToMultiArray(ritorno); + File dir = new File(global.esDir); + String[] presenti = dir.list(); + boolean tmp = true; + for(String esercizio : esercizi) + { + ArrayList es=suString.stringToArrayList(esercizio); + tmp = true; + for (String nome : presenti) { + if (nome.contentEquals(es.get(0))) { + if ((MD5.digest(nome)).contentEquals(es.get(1))) { + tmp = false; + } + else { + File file = new File(global.esDir + nome); + file.delete(); + } + } + } + if (tmp) { + String request = new download().execute(es.get(0)).get(); + } + } + } + }catch (Exception e) + { + e.printStackTrace(); + } + return ritorno; + } +} +class download extends AsyncTask +{ + @Override + protected String doInBackground(String... params) + { + try { + //set the download URL, a url that points to a file on the internet + //this is the file to be downloaded + URL url = new URL(global.httpHost+params[0]); + + //create the new connection + HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + + //set up some things on the connection + urlConnection.setRequestMethod("GET"); + urlConnection.setDoOutput(true); + + //and connect! + urlConnection.connect(); + + //set the path where we want to save the file + //in this case, going to save it on the root directory of the + //sd card. + File SDCardRoot = Environment.getExternalStorageDirectory(); + File esDir = new File(global.esDir); + //create a new file, specifying the path, and the filename + //which we want to save the file as. + + + File file = new File(esDir,params[0]); + //this will be used to write the downloaded data into the file we created + FileOutputStream fileOutput = new FileOutputStream(file); + + //this will be used in reading the data from the internet + InputStream inputStream = urlConnection.getInputStream(); + + //this is the total size of the file + int totalSize = urlConnection.getContentLength(); + //variable to store total downloaded bytes + int downloadedSize = 0; + + //create a buffer... + byte[] buffer = new byte[1024]; + int bufferLength = 0; //used to store a temporary size of the buffer + + //now, read through the input buffer and write the contents to the file + while ( (bufferLength = inputStream.read(buffer)) > 0 ) { + //add the data in the buffer to the file in the file output stream (the file on the sd card + fileOutput.write(buffer, 0, bufferLength); + //add up the size so we know how much is downloaded + downloadedSize += bufferLength; + //this is where you would do something to report the prgress, like this maybe + //updateProgress(downloadedSize, totalSize); + + } + //close the output stream when done + fileOutput.close(); + +//catch some possible errors... + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } +} +