]> matita.cs.unibo.it Git - logicplayer.git/blobdiff - mainActivity/app/src/main/java/com/example/furt/myapplication/aggiorna.java
Ported to latest version of Android SDK
[logicplayer.git] / mainActivity / app / src / main / java / com / example / furt / myapplication / aggiorna.java
diff --git a/mainActivity/app/src/main/java/com/example/furt/myapplication/aggiorna.java b/mainActivity/app/src/main/java/com/example/furt/myapplication/aggiorna.java
new file mode 100644 (file)
index 0000000..7ea276d
--- /dev/null
@@ -0,0 +1,239 @@
+package com.example.furt.myapplication;
+
+
+import android.content.Context;
+import android.os.AsyncTask;
+import android.os.Environment;
+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<listElem> esercizi=connessione.getElem(user);
+        if(esercizi!=null && !esercizi.isEmpty()) {
+
+            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<String> allExercise=suString.stringToMultiArray(ritorno);
+            List<listElem> localExercise=connessione.getAllElem(utente);
+            if(localExercise!=null && !localExercise.isEmpty()) {
+                for (String ex : allExercise) {
+                    ArrayList<String> 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<String> 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<String> esercizi= suString.stringToMultiArray(ritorno);
+                File dir = new File(global.esDir);
+                String[] presenti = dir.list();
+                boolean tmp = true;
+                for(String esercizio : esercizi)
+                {
+                    ArrayList<String> 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<String,String,String>
+{
+    @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;
+    }
+}
+