HomeSectionLocation.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. private final Activity activity;
  25. /**
  26. * Constructor.
  27. *
  28. * @param v Application top view.
  29. *
  30. * @see android.widget.ImageView
  31. */
  32. public HomeSectionLocation(View v, Activity a) {
  33. super();
  34. this.activity = a;
  35. this.view = v;
  36. }
  37. /**
  38. * Downloading file in background thread.
  39. */
  40. @Override
  41. protected Void doInBackground(Void... v) {
  42. String o = "";
  43. try {
  44. FetchURL fu = new FetchURL();
  45. fu.Run(GM.SERVER + "/app/location.php");
  46. //All the info
  47. o = fu.getOutput().toString();
  48. } catch (Exception e) {
  49. Log.e("Location error: ", e.getMessage());
  50. }
  51. if (o.contains("<reporting>1</reporting>")){
  52. isLocationReported = true;
  53. String latString = o.substring(o.indexOf("<lat>") + 5, o.indexOf("</lat>"));
  54. String lonString = o.substring(o.indexOf("<lon>") + 5, o.indexOf("</lon>"));
  55. coord = new LatLng(Double.parseDouble(latString), Double.parseDouble(lonString));
  56. }
  57. return null;
  58. }
  59. /**
  60. * After completing background task, set the image on the ImageView.
  61. */
  62. @Override
  63. protected void onPostExecute(Void v) {
  64. if (isLocationReported){
  65. SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  66. SharedPreferences.Editor editor = preferences.edit();
  67. editor.putLong(GM.PREF_GM_LATITUDE, Double.doubleToLongBits(coord.latitude));
  68. editor.putLong(GM.PREF_GM_LONGITUDE, Double.doubleToLongBits(coord.longitude));
  69. editor.putString(GM.PREF_GM_LOCATION, new SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(Calendar.getInstance().getTime()));
  70. editor.apply();
  71. //Show menu entry
  72. LinearLayout menuEntry = (LinearLayout) activity.findViewById(R.id.ll_menu_location);
  73. menuEntry.setVisibility(View.VISIBLE);
  74. //Set up home view section
  75. LinearLayout section = (LinearLayout) view.findViewById(R.id.ll_home_section_location);
  76. section.setVisibility(View.VISIBLE);
  77. section.setOnClickListener(new View.OnClickListener(){
  78. @Override
  79. public void onClick(View v) {
  80. ((MainActivity) v.getContext()).loadSection(GM.SECTION_LOCATION, false);
  81. }
  82. });
  83. }
  84. }
  85. }