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