| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package com.ivalentin.margolariak;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.AsyncTask;
- import android.util.Log;
- import android.view.View;
- import android.widget.LinearLayout;
- import com.google.android.gms.maps.model.LatLng;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Locale;
- /**
- * Async task to download images on the fly.
- * The remote image file, the local path, and the ImageView where the image will be loaded are set on the constructor.
- *
- * @author Iñigo Valentin
- *
- */
- class HomeSectionLocation extends AsyncTask<Void, Void, Void> {
- private boolean isLocationReported;
- private LatLng coord;
- private final View view;
- /**
- * Constructor.
- *
- * @param v Application top view.
- *
- * @see android.widget.ImageView
- */
- public HomeSectionLocation(View v) {
- super();
- this.view = v;
- }
- /**
- * Downloading file in background thread.
- */
- @Override
- protected Void doInBackground(Void... v) {
- String o = "";
- try {
- FetchURL fu = new FetchURL();
- fu.Run(GM.API.SERVER + "/app/location.php");
- //All the info
- o = fu.getOutput().toString();
- } catch (Exception e) {
- Log.e("Location error: ", e.getMessage());
- }
- if (o.contains("<reporting>1</reporting>")){
- isLocationReported = true;
- String latString = o.substring(o.indexOf("<lat>") + 5, o.indexOf("</lat>"));
- String lonString = o.substring(o.indexOf("<lon>") + 5, o.indexOf("</lon>"));
- coord = new LatLng(Double.parseDouble(latString), Double.parseDouble(lonString));
- }
- return null;
- }
- /**
- * After completing background task, set the image on the ImageView.
- */
- @Override
- protected void onPostExecute(Void v) {
- if (isLocationReported){
- //TODO: Do this on a db table
- /*SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = preferences.edit();
- editor.putLong(GM.PREF_GM_LATITUDE, Double.doubleToLongBits(coord.latitude));
- editor.putLong(GM.PREF_GM_LONGITUDE, Double.doubleToLongBits(coord.longitude));
- editor.putString(GM.PREF_GM_LOCATION, new SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(Calendar.getInstance().getTime()));
- editor.apply();*/
- //Show menu entry
- //LinearLayout menuEntry = (LinearLayout) activity.findViewById(R.id.rl_menu_location);
- //menuEntry.setVisibility(View.VISIBLE);
- //Set up home view section
- LinearLayout section = (LinearLayout) view.findViewById(R.id.ll_home_section_location);
- section.setVisibility(View.VISIBLE);
- section.setOnClickListener(new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- ((MainActivity) v.getContext()).loadSection(GM.SECTION.LOCATION);
- }
- });
- }
- }
- }
|