1 package com.example.furt.myapplication;
4 import android.content.Context;
5 import android.os.AsyncTask;
6 import android.os.Environment;
7 import android.widget.Toast;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.HttpURLConnection;
14 import java.net.MalformedURLException;
16 import java.util.ArrayList;
17 import java.util.List;
19 public class aggiorna {
23 public String sincronizza(String sessionKey,String user,String pass) {
24 String ritorno = null;
25 String login = "1/" + user + "/" + pass;
26 while (ritorno == null || ritorno.contains("request-login"))
28 ritorno = serverSync(sessionKey, user);
29 if(ritorno == null || ritorno.contains("request-login"))
31 sessionKey= serverComunication.connessione(login);
33 else if(ritorno.contains("error"))
34 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
37 while (ritorno == null || ritorno.contains("request-login"))
39 ritorno = syncLocaldb(sessionKey, user);
40 if(ritorno == null || ritorno.contains("request-login"))
42 sessionKey= serverComunication.connessione(login);
44 else if(ritorno.contains("error"))
45 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
48 while (ritorno == null || ritorno.contains("request-login"))
50 ritorno=downloadEX(sessionKey);
51 if(ritorno == null || ritorno.contains("request-login"))
53 sessionKey= serverComunication.connessione(login);
55 else if(ritorno.contains("error"))
56 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
61 public String serverSync(String sessionKey, String user)
63 personalDBHelper connessione = new personalDBHelper(aggiornamento.t.getContext());
64 List<listElem> esercizi=connessione.getElem(user);
65 if(esercizi!=null && !esercizi.isEmpty()) {
67 for (listElem esercizio : esercizi) {
68 String richiesta = "8/" + sessionKey + "/" + esercizio.getEsercizio() + "/" + esercizio.getMd5() + "/" + esercizio.getTime() + "/" + esercizio.getClick();
69 String ritorno = serverComunication.connessione(richiesta);
70 if (ritorno.contains("es-ok"))
71 connessione.updateElem(esercizio.getMd5(), user, esercizio.getTime());
72 else if (ritorno.contains("request-login") || ritorno.contains("es-err")){
73 if(ritorno.contains("es-err"))
74 connessione.remove(esercizio.getUtente(),esercizio.getTime());
82 public String syncLocaldb(String sessionKey, String utente)
84 String richiesta = "9/" + sessionKey;
85 String ritorno = serverComunication.connessione(richiesta);
86 if(ritorno.contains("request-login"))
90 personalDBHelper connessione=new personalDBHelper(aggiornamento.t.getContext().getApplicationContext());
91 ArrayList<String> allExercise=suString.stringToMultiArray(ritorno);
92 List<listElem> localExercise=connessione.getAllElem(utente);
93 if(localExercise!=null && !localExercise.isEmpty()) {
94 for (String ex : allExercise) {
95 ArrayList<String> tmp = suString.stringToArrayList(ex);
97 for (listElem elem : localExercise) {
98 if (tmp.get(0).equals(elem.getEsercizio()) && Double.valueOf(tmp.get(2)) == elem.getTime()) {
99 if (elem.getCheck() == 0)
100 connessione.updateElem(tmp.get(1), utente, Double.valueOf(tmp.get(2)));
106 connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1);
110 for (String ex : allExercise) {
111 ArrayList<String> tmp = suString.stringToArrayList(ex);
113 connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1);
117 return "local-sync-ok";
120 public String downloadEX(String sessionKey)
122 Context context = aggiornamento.t.getContext();
123 File SDCardRoot = Environment.getExternalStorageDirectory();
124 String[] names = SDCardRoot.list();
127 for (String name : names) {
128 if (name.compareTo("tesiEs") == 0) {
134 File dir = new File(global.esDir);
138 ritorno = serverComunication.connessione("7/" + sessionKey);
139 if (!ritorno.contains("error") && !ritorno.contains("request-login"))
141 ArrayList<String> esercizi= suString.stringToMultiArray(ritorno);
142 File dir = new File(global.esDir);
143 String[] presenti = dir.list();
145 for(String esercizio : esercizi)
147 ArrayList<String> es=suString.stringToArrayList(esercizio);
149 for (String nome : presenti) {
150 if (nome.contentEquals(es.get(0))) {
151 if ((MD5.digest(nome)).contentEquals(es.get(1))) {
155 File file = new File(global.esDir + nome);
161 String request = new download().execute(es.get(0)).get();
172 class download extends AsyncTask<String,String,String>
175 protected String doInBackground(String... params)
178 //set the download URL, a url that points to a file on the internet
179 //this is the file to be downloaded
180 URL url = new URL(global.httpHost+params[0]);
182 //create the new connection
183 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
185 //set up some things on the connection
186 urlConnection.setRequestMethod("GET");
187 urlConnection.setDoOutput(true);
190 urlConnection.connect();
192 //set the path where we want to save the file
193 //in this case, going to save it on the root directory of the
195 File SDCardRoot = Environment.getExternalStorageDirectory();
196 File esDir = new File(global.esDir);
197 //create a new file, specifying the path, and the filename
198 //which we want to save the file as.
201 File file = new File(esDir,params[0]);
202 //this will be used to write the downloaded data into the file we created
203 FileOutputStream fileOutput = new FileOutputStream(file);
205 //this will be used in reading the data from the internet
206 InputStream inputStream = urlConnection.getInputStream();
208 //this is the total size of the file
209 int totalSize = urlConnection.getContentLength();
210 //variable to store total downloaded bytes
211 int downloadedSize = 0;
214 byte[] buffer = new byte[1024];
215 int bufferLength = 0; //used to store a temporary size of the buffer
217 //now, read through the input buffer and write the contents to the file
218 while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
219 //add the data in the buffer to the file in the file output stream (the file on the sd card
220 fileOutput.write(buffer, 0, bufferLength);
221 //add up the size so we know how much is downloaded
222 downloadedSize += bufferLength;
223 //this is where you would do something to report the prgress, like this maybe
224 //updateProgress(downloadedSize, totalSize);
227 //close the output stream when done
230 //catch some possible errors...
231 } catch (MalformedURLException e) {
233 } catch (IOException e) {