packageorg.marcinzelent.liberavem;importandroid.app.Activity;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentManager;importandroid.view.View;importandroid.widget.Toast;importcom.android.volley.Request;importcom.android.volley.Response;importcom.android.volley.VolleyError;importcom.android.volley.toolbox.StringRequest;importcom.android.volley.toolbox.Volley;importcom.google.gson.Gson;importcom.google.gson.GsonBuilder;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.Locale;publicclassDataKeeper{privatestaticfinalDataKeeperinstance=newDataKeeper();publicstaticDataKeepergetInstance(){returninstance;}privateDataKeeper(){}privateBird[]birds;privateObservation[]observations;publicBird[]getBirds(){returnbirds;}publicvoidsetBirds(Bird[]birds){this.birds=birds;}publicObservation[]getObservations(){returnobservations;}publicvoidsetObservations(Observation[]observations){this.observations=observations;}publicvoiddownloadData(finalActivityactivity){downloadBirds(activity);downloadObservations(activity);}privatevoiddownloadBirds(finalActivityactivity){StringbirdsUrl="http://birdobservationservice.azurewebsites.net/Service1.svc/birds";finalStringRequestbirdsRequest=newStringRequest(Request.Method.GET,birdsUrl,newResponse.Listener<String>(){@OverridepublicvoidonResponse(Stringresponse){Gsongson=newGsonBuilder().create();birds=gson.fromJson(response,Bird[].class);callPopulator(activity);}},newcom.android.volley.Response.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){Toast.makeText(activity,"Couldn't connect to the database!",Toast.LENGTH_LONG).show();}});Volley.newRequestQueue(activity).add(birdsRequest);}privatevoiddownloadObservations(finalActivityactivity){StringobservationsUrl="http://birdobservationservice.azurewebsites.net/Service1.svc/observations";finalStringRequestobservationsRequest=newStringRequest(Request.Method.GET,observationsUrl,newResponse.Listener<String>(){@OverridepublicvoidonResponse(Stringresponse){Gsongson=newGsonBuilder().create();observations=gson.fromJson(response,Observation[].class);for(Observationo:observations){intp=o.getCreated().indexOf('+');longepoch=Long.parseLong(o.getCreated().substring(6,p));Datedate=newDate(epoch);Stringformatted=newSimpleDateFormat("dd.MM.yyyy HH:mm",Locale.ENGLISH).format(date);o.setCreated(formatted);}callPopulator(activity);}},newcom.android.volley.Response.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){Toast.makeText(activity,"Couldn't connect to the database!",Toast.LENGTH_LONG).show();}});Volley.newRequestQueue(activity).add(observationsRequest);}privatevoidcallPopulator(Activityactivity){if(birds!=null&&observations!=null){MyObservationsFragmentmo=(MyObservationsFragment)((MainActivity)activity).getAllFragments().get(0);List<Observation>myObservationsList=newArrayList<>();for(Observationo:observations)if(o.getUserId().equals("Nancy"))myObservationsList.add(o);Observation[]myObservations=newObservation[myObservationsList.size()];myObservations=myObservationsList.toArray(myObservations);mo.populateList(myObservations,birds);AllObservationsFragmentaof=(AllObservationsFragment)((MainActivity)activity).getAllFragments().get(1);aof.populateList(observations,birds);}}}