1 package com.example.furt.myapplication;
4 import android.content.Context;
5 import android.os.AsyncTask;
6 import android.os.Environment;
7 import android.util.Log;
8 import android.widget.Toast;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.HttpURLConnection;
15 import java.net.MalformedURLException;
17 import java.util.ArrayList;
18 import java.util.List;
20 public class aggiorna {
24 public String sincronizza(String sessionKey,String user,String pass) {
25 String ritorno = null;
26 String login = "1/" + user + "/" + pass;
27 while (ritorno == null || ritorno.contains("request-login"))
29 ritorno = serverSync(sessionKey, user);
30 if(ritorno == null || ritorno.contains("request-login"))
32 sessionKey= serverComunication.connessione(login);
34 else if(ritorno.contains("error"))
35 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
38 while (ritorno == null || ritorno.contains("request-login"))
40 ritorno = syncLocaldb(sessionKey, user);
41 if(ritorno == null || ritorno.contains("request-login"))
43 sessionKey= serverComunication.connessione(login);
45 else if(ritorno.contains("error"))
46 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
49 while (ritorno == null || ritorno.contains("request-login"))
51 ritorno=downloadEX(sessionKey);
52 if(ritorno == null || ritorno.contains("request-login"))
54 sessionKey= serverComunication.connessione(login);
56 else if(ritorno.contains("error"))
57 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
62 public String serverSync(String sessionKey, String user)
64 personalDBHelper connessione = new personalDBHelper(aggiornamento.t.getContext());
65 List<listElem> esercizi=connessione.getElem(user);
66 if(esercizi!=null && !esercizi.isEmpty()) {
67 for (listElem esercizio : esercizi) {
68 Log.e("",esercizio.getEsercizio());}
69 for (listElem esercizio : esercizi) {
70 String richiesta = "8/" + sessionKey + "/" + esercizio.getEsercizio() + "/" + esercizio.getMd5() + "/" + esercizio.getTime() + "/" + esercizio.getClick();
71 String ritorno = serverComunication.connessione(richiesta);
72 if (ritorno.contains("es-ok"))
73 connessione.updateElem(esercizio.getMd5(), user, esercizio.getTime());
74 else if (ritorno.contains("request-login") || ritorno.contains("es-err")){
75 if(ritorno.contains("es-err"))
76 connessione.remove(esercizio.getUtente(),esercizio.getTime());
84 public String syncLocaldb(String sessionKey, String utente)
86 String richiesta = "9/" + sessionKey;
87 String ritorno = serverComunication.connessione(richiesta);
88 if(ritorno.contains("request-login"))
92 personalDBHelper connessione=new personalDBHelper(aggiornamento.t.getContext().getApplicationContext());
93 ArrayList<String> allExercise=suString.stringToMultiArray(ritorno);
94 List<listElem> localExercise=connessione.getAllElem(utente);
95 if(localExercise!=null && !localExercise.isEmpty()) {
96 for (String ex : allExercise) {
97 ArrayList<String> tmp = suString.stringToArrayList(ex);
99 for (listElem elem : localExercise) {
100 if (tmp.get(0).equals(elem.getEsercizio()) && Double.valueOf(tmp.get(2)) == elem.getTime()) {
101 if (elem.getCheck() == 0)
102 connessione.updateElem(tmp.get(1), utente, Double.valueOf(tmp.get(2)));
108 connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1);
112 for (String ex : allExercise) {
113 ArrayList<String> tmp = suString.stringToArrayList(ex);
115 connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1);
119 return "local-sync-ok";
122 public String downloadEX(String sessionKey)
124 Context context = aggiornamento.t.getContext();
125 File SDCardRoot = Environment.getExternalStorageDirectory();
126 String[] names = SDCardRoot.list();
129 for (String name : names) {
130 if (name.compareTo("tesiEs") == 0) {
136 File dir = new File(global.esDir);
140 ritorno = serverComunication.connessione("7/" + sessionKey);
141 if (!ritorno.contains("error") && !ritorno.contains("request-login"))
143 ArrayList<String> esercizi= suString.stringToMultiArray(ritorno);
144 File dir = new File(global.esDir);
145 String[] presenti = dir.list();
147 for(String esercizio : esercizi)
149 ArrayList<String> es=suString.stringToArrayList(esercizio);
151 for (String nome : presenti) {
152 if (nome.contentEquals(es.get(0))) {
153 if ((MD5.digest(nome)).contentEquals(es.get(1))) {
157 File file = new File(global.esDir + nome);
163 String request = new download().execute(es.get(0)).get();
174 class download extends AsyncTask<String,String,String>
177 protected String doInBackground(String... params)
180 //set the download URL, a url that points to a file on the internet
181 //this is the file to be downloaded
182 URL url = new URL(global.httpHost+params[0]);
184 //create the new connection
185 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
187 //set up some things on the connection
188 urlConnection.setRequestMethod("GET");
189 urlConnection.setDoOutput(true);
192 urlConnection.connect();
194 //set the path where we want to save the file
195 //in this case, going to save it on the root directory of the
197 File SDCardRoot = Environment.getExternalStorageDirectory();
198 File esDir = new File(global.esDir);
199 //create a new file, specifying the path, and the filename
200 //which we want to save the file as.
203 File file = new File(esDir,params[0]);
204 //this will be used to write the downloaded data into the file we created
205 FileOutputStream fileOutput = new FileOutputStream(file);
207 //this will be used in reading the data from the internet
208 InputStream inputStream = urlConnection.getInputStream();
210 //this is the total size of the file
211 int totalSize = urlConnection.getContentLength();
212 //variable to store total downloaded bytes
213 int downloadedSize = 0;
216 byte[] buffer = new byte[1024];
217 int bufferLength = 0; //used to store a temporary size of the buffer
219 //now, read through the input buffer and write the contents to the file
220 while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
221 //add the data in the buffer to the file in the file output stream (the file on the sd card
222 fileOutput.write(buffer, 0, bufferLength);
223 //add up the size so we know how much is downloaded
224 downloadedSize += bufferLength;
225 //this is where you would do something to report the prgress, like this maybe
226 //updateProgress(downloadedSize, totalSize);
229 //close the output stream when done
232 //catch some possible errors...
233 } catch (MalformedURLException e) {
235 } catch (IOException e) {