HomeSectionLocation.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.ivalentin.margolariak;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.os.AsyncTask;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.LinearLayout;
  9. import com.google.android.gms.maps.model.LatLng;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Calendar;
  12. import java.util.Locale;
  13. /**
  14. * Async task to download images on the fly.
  15. * The remote image file, the local path, and the ImageView where the image will be loaded are set on the constructor.
  16. *
  17. * @author Iñigo Valentin
  18. *
  19. */
  20. class HomeSectionLocation extends AsyncTask<Void, Void, Void> {
  21. private boolean isLocationReported;
  22. private LatLng coord;
  23. private final View view;
  24. /**
  25. * Constructor.
  26. *
  27. * @param v Application top view.
  28. *
  29. * @see android.widget.ImageView
  30. */
  31. public HomeSectionLocation(View v) {
  32. super();
  33. this.view = v;
  34. }
  35. /**
  36. * Downloading file in background thread.
  37. */
  38. @Override
  39. protected Void doInBackground(Void... v) {
  40. String o = "";
  41. try {
  42. FetchURL fu = new FetchURL();
  43. fu.Run(GM.API.SERVER + "/app/location.php");
  44. //All the info
  45. o = fu.getOutput().toString();
  46. } catch (Exception e) {
  47. Log.e("Location error: ", e.getMessage());
  48. }
  49. if (o.contains("<reporting>1</reporting>")){
  50. isLocationReported = true;
  51. String latString = o.substring(o.indexOf("<lat>") + 5, o.indexOf("</lat>"));
  52. String lonString = o.substring(o.indexOf("<lon>") + 5, o.indexOf("</lon>"));
  53. coord = new LatLng(Double.parseDouble(latString), Double.parseDouble(lonString));
  54. }
  55. return null;
  56. }
  57. /**
  58. * After completing background task, set the image on the ImageView.
  59. */
  60. @Override
  61. protected void onPostExecute(Void v) {
  62. if (isLocationReported){
  63. //TODO: Do this on a db table
  64. /*SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
  65. SharedPreferences.Editor editor = preferences.edit();
  66. editor.putLong(GM.PREF_GM_LATITUDE, Double.doubleToLongBits(coord.latitude));
  67. editor.putLong(GM.PREF_GM_LONGITUDE, Double.doubleToLongBits(coord.longitude));
  68. editor.putString(GM.PREF_GM_LOCATION, new SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(Calendar.getInstance().getTime()));
  69. editor.apply();*/
  70. //Show menu entry
  71. //LinearLayout menuEntry = (LinearLayout) activity.findViewById(R.id.rl_menu_location);
  72. //menuEntry.setVisibility(View.VISIBLE);
  73. //Set up home view section
  74. LinearLayout section = (LinearLayout) view.findViewById(R.id.ll_home_section_location);
  75. section.setVisibility(View.VISIBLE);
  76. section.setOnClickListener(new View.OnClickListener(){
  77. @Override
  78. public void onClick(View v) {
  79. ((MainActivity) v.getContext()).loadSection(GM.SECTION.LOCATION);
  80. }
  81. });
  82. }
  83. }
  84. }