package de.donmanfred;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
@Version(1.11f)
@ShortName("GeoLocator")
@Author("DonManfred")
//@ActivityObject
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE","android.permission.ACCESS_FINE_LOCATION"})
@Events(values={"Location(success As Boolean, Lattitude As Double, Longitude As Double, fromMockProvider As Boolean, ismocklocation As Boolean)"})
//@DependsOn(values={"android-viewbadger"})
/**
* com.android.support:support-v4
* com.android.support:appcompat-v7
* com.android.support:cardview-v7
* com.android.support:gridlayout-v7
* com.android.support:mediarouter-v7
* com.android.support:palette-v7
* com.android.support:recyclerview-v
* com.android.support:preference-v7
* com.android.support:support-v13
*/
public class GeoLocatorwrapper {
private BA ba;
private String eventName;
private static double lattitude, longitude;
private static boolean fromMockProvider;
private String address, city, state, country, postalCode, knownName;
/**
* <link>...|https://www.b4x.com</link>
*/
public static void LIBRARY_DOC() {
}
public void Initialize(BA ba, String EventName) {
this.eventName = EventName.toLowerCase(BA.cul);
this.ba = ba;
fromMockProvider = false;
GetLocation(ba);
}
public void GetLocation(BA ba) {
LocationManager locationManager = (LocationManager) ba.context.getSystemService(ba.context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// showSettingsAlert();
//Toast.makeText(context, "Permission Denied", Toast.LENGTH_SHORT).show();
String info = "Permission Denied";
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(false),null,null,info});
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = latti;
longitude = longi;
fromMockProvider = location.isFromMockProvider();
String info = "NETWORK_PROVIDER";
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(true),lattitude,longitude, info, fromMockProvider});
} else if (location1 != null) {
double latti = location1.getLatitude();
double longi = location1.getLongitude();
lattitude = latti;
longitude = longi;
String info = "GPS_PROVIDER";
fromMockProvider = location1.isFromMockProvider();
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(true),lattitude,longitude, info, fromMockProvider});
} else if (location2 != null) {
double latti = location2.getLatitude();
double longi = location2.getLongitude();
lattitude = latti;
longitude = longi;
fromMockProvider = location2.isFromMockProvider();
String info = "PASSIVE_PROVIDER";
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(true),lattitude,longitude, info, fromMockProvider});
} else {
//Toast.makeText(context, "Unble to Trace your location", Toast.LENGTH_SHORT).show();
double value = 0;
fromMockProvider = false;
String info = "Unble to Trace your location";
ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(false),value,value, info, fromMockProvider});
}
}
}
public double getLattitude() {
return lattitude;
}
public double getLongitude() {
return longitude;
}
/*
* Make sure to call this method only
* after you got a valid Location.
*/
public void geoAddress(BA ba) throws IOException {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(ba.context, Locale.getDefault());
addresses = geocoder.getFromLocation(lattitude, longitude, 1);
BA.Log("addresses:"+addresses.size());
if(addresses.size() > -1){
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
knownName = addresses.get(0).getFeatureName();
} else {
city = "";
state = "";
country = "";
postalCode = "";
knownName = "";
}
}
public void geoAddress2(BA ba, double lattitude, double longitude) throws IOException {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(ba.context, Locale.getDefault());
addresses = geocoder.getFromLocation(lattitude, longitude, 1);
if(addresses.size() > -1){
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
postalCode = addresses.get(0).getPostalCode();
knownName = addresses.get(0).getFeatureName();
} else {
city = "";
state = "";
country = "";
postalCode = "";
knownName = "";
}
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getCountry() {
return country;
}
public String getPostalCode() {
return postalCode;
}
public String getKnownName() {
return knownName;
}
}