/** * Copyright © 2013 Titan Framework. All Rights Reserved. * * Developed by Laboratory for Precision Livestock, Environment and Software Engineering (PLEASE Lab) * of Embrapa Beef Cattle at Campo Grande - MS - Brazil. * * @see http://please.cnpgc.embrapa.br * * @author Camilo Carromeu * @author Jairo Ricardes Rodrigues Filho * * @version -1-alpha */ package .task; import java.util.List; import android.app.ProgressDialog; import android.content.SharedPreferences; import android.os.AsyncTask; import .; import .ListActivity; import .converter.Converter; import .dao.DAO; import .model.; import .ws.WebService; import .util.Preferences; import .util.ScreenHelper; public class Task extends AsyncTask { private ProgressDialog progress; private ListActivity activity; private Exception exception; public Task (ListActivity a) { activity = a; } @Override protected void onPreExecute () { ScreenHelper.lock (activity); progress = ProgressDialog.show (activity, "Sincronizando...", "Aguarde! Se esta for a primeira vez que você faz esta sincronização, poderá demorar alguns minutos.", true, false); } @Override protected Boolean doInBackground (Void... params) { try { String active = WebService.active (); DAO.singleton ().deleteNonActive (active); SharedPreferences preferences = Preferences.singleton (); long time = preferences.getLong ("lastSyncFor", 0); WebService ws = new WebService (); String json = ws.list (time); List<> list = Converter.fromJsonString (json); if (list.size () == 0) return true; DAO.singleton ().insertOrUpdate (list); SharedPreferences.Editor editor = preferences.edit (); editor.putLong ("lastSyncFor", ws.getServerTime ()); editor.commit (); return true; } catch (Exception e) { exception = e; } return false; } @Override protected void onPostExecute (Boolean success) { progress.dismiss (); ScreenHelper.unlock (activity); if (!success && exception != null) activity.fail (exception.getMessage ()); (() activity.getApplication ()).checkSync (activity.getClass ().getName ()); activity.refresh (); } }