package com.example.location01;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;


// http://javapapers.com/android/get-current-location-in-android/    code source

//http://justsimpleinfo.blogspot.fr/2015/01/android-get-latitude-longitude-and.html

// https://stackoverflow.com/questions/17591147/how-to-get-current-location-in-android

// https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

// https://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

// https://gist.github.com/ertugrulozcan/70973144b1c582618a6c

// http://www.vogella.com/tutorials/AndroidLocationAPI/article.html

public class MainActivity extends Activity implements LocationListener
{
	protected LocationManager locationManager;
	protected LocationListener locationListener;
	protected Context context;
	
	TextView txtLat;
	TextView tv_time;
	
	String lat;
	String provider;
	protected String latitude,longitude; 
	protected boolean gps_enabled,network_enabled;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txtLat = (TextView) findViewById(R.id.textview1);
		
		tv_time = (TextView) findViewById(R.id.tv_time);
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
	}
	
	@Override
	public void onLocationChanged(Location location) 
	{
		txtLat = (TextView) findViewById(R.id.textview1);
		txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude() + "  Time  " +location.getTime());
		
		
		Calendar calendar = Calendar.getInstance();
		TimeZone tz = TimeZone.getDefault();
		calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
		java.util.Date currenTimeZone=new java.util.Date((long)1379487711*1000);
		
		String infos_date = sdf.format(currenTimeZone);
		
		//Toast.makeText(MainActivity.this, sdf.format(currenTimeZone), Toast.LENGTH_SHORT).show();
		
		/******* OK ********/
		// tient compte de l'heure été / hiver  - DST en anglais : Daylight Saving Time
		long timestamp =location.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		TimeZone timezone = TimeZone.getTimeZone("Europe/Paris");
		int t_offsetFromUTC = timezone.getOffset(timestamp);
		int t_decalage = timezone.getDSTSavings();
		int t_shift = t_offsetFromUTC + t_decalage + 3600000  ; // on rajoute 1 heures soit 3600 * 1000 ms
		timestamp = timestamp + t_shift;
		String valuex = formatter.format(timestamp);
		
		/******* FIN OK ********/
		tv_time.setText(valuex);
		
		Date d = new Date(timestamp);
		d.toLocaleString(); //to phone's locale
		d.toGMTString(); //in GMT

		
		 TimeZone obj = TimeZone.getTimeZone("CEST");
		SimpleDateFormat aformatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		formatter.setTimeZone(obj);
		String val = formatter.format(timestamp);
		
		
		 //long msFromEpochGmt = date.getTime();
		  timestamp =location.getTime();
		
		 int offsetFromUTC = tz.getOffset(timestamp);
		 int decalage = tz.getDSTSavings();
		
		//Calendar cal = Calendar.getInstance(Locale.getDefault());
		Calendar cal = Calendar.getInstance(Locale.FRANCE);
		
		int shift = offsetFromUTC + decalage ;
		
		// UTC
		//DST en anglais : Daylight Saving Time).
		//DateTimeZone zone = DateTimeZone.forID("Europe/Paris");
	
        cal.setTimeInMillis(timestamp+shift);
        String date = DateFormat.format("dd-MM-yyyy hh:mm:ss", cal).toString();
       
        
        TimeZone alpha = cal.getTimeZone();
        
        
        TimeZone timeZone = TimeZone.getDefault();
        String timeZoneInGMTFormat = timeZone.getDisplayName(false,TimeZone.SHORT);
        
        TimeZone txz = TimeZone.getDefault();
        int offset = txz.getRawOffset();
        
        Calendar calendar1 = Calendar.getInstance();       
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
        sdf1.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
        String toto = sdf.format(calendar1.getTime());
        
        
        TimeZone atz = TimeZone.getDefault();
        
        TimeZone zobi= calendar1.getTimeZone();
        
        Date now = new Date();
        int offsetFromUtc = atz.getOffset(now.getTime()) / 1000;
        
		
	}
	
	@Override
	public void onProviderDisabled(String provider) 
	{
		Log.d("Latitude","disable");
	}
	
	@Override
	public void onProviderEnabled(String provider) 
	{
		Log.d("Latitude","enable");
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) 
	{
		Log.d("Latitude","status");
	}
	
	
	 @Override
	 public void onDestroy()
	 {
		 super.onDestroy();
		 //locationManager.removeUpdates(locationListener);
		 
		 locationManager.removeUpdates(this);
		 locationManager = null;
		 
	 }
	
}