]> matita.cs.unibo.it Git - logicplayer.git/blob - mainActivity/src/com/example/furt/myapplication/aggiorna.java
New version (to be tested).
[logicplayer.git] / mainActivity / src / com / example / furt / myapplication / aggiorna.java
1 package com.example.furt.myapplication;
2
3
4 import android.content.Context;
5 import android.os.AsyncTask;
6 import android.os.Environment;
7 import android.widget.Toast;
8
9 import java.io.File;
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;
15 import java.net.URL;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 public class aggiorna {
20
21     aggiorna(){}
22
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"))
27         {
28             ritorno = serverSync(sessionKey, user);
29             if(ritorno == null || ritorno.contains("request-login"))
30             {
31                 sessionKey= serverComunication.connessione(login);
32             }
33             else if(ritorno.contains("error"))
34                 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
35         }
36         ritorno=null;
37         while (ritorno == null || ritorno.contains("request-login"))
38         {
39             ritorno = syncLocaldb(sessionKey, user);
40             if(ritorno == null || ritorno.contains("request-login"))
41             {
42                 sessionKey= serverComunication.connessione(login);
43             }
44             else if(ritorno.contains("error"))
45                 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
46         }
47         ritorno=null;
48         while (ritorno == null || ritorno.contains("request-login"))
49         {
50             ritorno=downloadEX(sessionKey);
51             if(ritorno == null || ritorno.contains("request-login"))
52             {
53                 sessionKey= serverComunication.connessione(login);
54             }
55             else if(ritorno.contains("error"))
56                 Toast.makeText(aggiornamento.t.getContext().getApplicationContext(), "errore di connessione", Toast.LENGTH_SHORT).show();
57         }
58         return sessionKey;
59     }
60
61     public String serverSync(String sessionKey, String user)
62     {
63         personalDBHelper connessione = new personalDBHelper(aggiornamento.t.getContext());
64         List<listElem> esercizi=connessione.getElem(user);
65         if(esercizi!=null && !esercizi.isEmpty()) {
66
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());
75                     return ritorno;
76                  }
77             }
78         }
79         return "sync-ok";
80     }
81
82     public String syncLocaldb(String sessionKey, String utente)
83     {
84         String richiesta = "9/" + sessionKey;
85         String ritorno = serverComunication.connessione(richiesta);
86         if(ritorno.contains("request-login"))
87             return ritorno;
88         else
89         {
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);
96                     boolean add = true;
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)));
101                             add = false;
102                         }
103                     }
104                     if (add)
105                         if(tmp.size()>3)
106                             connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1);
107                 }
108             }
109             else{
110                 for (String ex : allExercise) {
111                     ArrayList<String> tmp = suString.stringToArrayList(ex);
112                     if(tmp.size()>3)
113                         connessione.add(utente, tmp.get(0), tmp.get(1), Double.valueOf(tmp.get(2)), Integer.valueOf(tmp.get(3)), 1);
114                 }
115             }
116         }
117         return "local-sync-ok";
118     }
119
120     public String downloadEX(String sessionKey)
121     {
122         Context context = aggiornamento.t.getContext();
123         File SDCardRoot = Environment.getExternalStorageDirectory();
124         String[] names = SDCardRoot.list();
125         String ritorno=null;
126         boolean go = true;
127         for (String name : names) {
128             if (name.compareTo("tesiEs") == 0) {
129                 go = false;
130                 break;
131             }
132         }
133         if (go) {
134             File dir = new File(global.esDir);
135             dir.mkdir();
136         }
137         try {
138             ritorno = serverComunication.connessione("7/" + sessionKey);
139             if (!ritorno.contains("error") && !ritorno.contains("request-login"))
140             {
141                 ArrayList<String> esercizi= suString.stringToMultiArray(ritorno);
142                 File dir = new File(global.esDir);
143                 String[] presenti = dir.list();
144                 boolean tmp = true;
145                 for(String esercizio : esercizi)
146                 {
147                     ArrayList<String> es=suString.stringToArrayList(esercizio);
148                     tmp = true;
149                     for (String nome : presenti) {
150                         if (nome.contentEquals(es.get(0))) {
151                             if ((MD5.digest(nome)).contentEquals(es.get(1))) {
152                                 tmp = false;
153                             }
154                             else {
155                                 File file = new File(global.esDir + nome);
156                                 file.delete();
157                             }
158                         }
159                     }
160                     if (tmp) {
161                         String request = new download().execute(es.get(0)).get();
162                     }
163                 }
164             }
165         }catch (Exception e)
166         {
167             e.printStackTrace();
168         }
169         return ritorno;
170     }
171 }
172 class download extends AsyncTask<String,String,String>
173 {
174     @Override
175     protected String doInBackground(String... params)
176     {
177         try {
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]);
181
182             //create the new connection
183             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
184
185             //set up some things on the connection
186             urlConnection.setRequestMethod("GET");
187             urlConnection.setDoOutput(true);
188
189             //and connect!
190             urlConnection.connect();
191
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
194             //sd card.
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.
199
200
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);
204
205             //this will be used in reading the data from the internet
206             InputStream inputStream = urlConnection.getInputStream();
207
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;
212
213             //create a buffer...
214             byte[] buffer = new byte[1024];
215             int bufferLength = 0; //used to store a temporary size of the buffer
216
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);
225
226             }
227             //close the output stream when done
228             fileOutput.close();
229
230 //catch some possible errors...
231         } catch (MalformedURLException e) {
232             e.printStackTrace();
233         } catch (IOException e) {
234             e.printStackTrace();
235         }
236         return null;
237     }
238 }
239