use of android.location.Criteria in project Gadgetbridge by Freeyourgadget.
the class AlarmReceiver method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
if (!GBApplication.getPrefs().getBoolean("send_sunrise_sunset", false)) {
LOG.info("won't send sunrise and sunset events (disabled in preferences)");
return;
}
LOG.info("will resend sunrise and sunset events");
final GregorianCalendar dateTimeTomorrow = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
dateTimeTomorrow.set(Calendar.HOUR, 0);
dateTimeTomorrow.set(Calendar.MINUTE, 0);
dateTimeTomorrow.set(Calendar.SECOND, 0);
dateTimeTomorrow.set(Calendar.MILLISECOND, 0);
dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1);
/*
* rotate ids ud reuse the id from two days ago for tomorrow, this way we will have
* sunrise /sunset for 3 days while sending only sunrise/sunset per day
*/
byte id_tomorrow = (byte) ((dateTimeTomorrow.getTimeInMillis() / (1000L * 60L * 60L * 24L)) % 3);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, id_tomorrow);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, id_tomorrow);
Prefs prefs = GBApplication.getPrefs();
float latitude = prefs.getFloat("location_latitude", 0);
float longitude = prefs.getFloat("location_longitude", 0);
LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && prefs.getBoolean("use_updated_location_if_available", false)) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
if (provider != null) {
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
if (lastKnownLocation != null) {
latitude = (float) lastKnownLocation.getLatitude();
longitude = (float) lastKnownLocation.getLongitude();
LOG.info("got longitude/latitude from last known location: " + latitude + "/" + longitude);
}
}
}
GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeTomorrow));
CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
calendarEventSpec.durationInSeconds = 0;
calendarEventSpec.description = null;
calendarEventSpec.type = CalendarEventSpec.TYPE_SUNRISE;
calendarEventSpec.title = "Sunrise";
if (sunriseTransitSetTomorrow[0] != null) {
calendarEventSpec.id = id_tomorrow;
calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[0].getTimeInMillis() / 1000);
GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
}
calendarEventSpec.type = CalendarEventSpec.TYPE_SUNSET;
calendarEventSpec.title = "Sunset";
if (sunriseTransitSetTomorrow[2] != null) {
calendarEventSpec.id = id_tomorrow;
calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[2].getTimeInMillis() / 1000);
GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
}
}
use of android.location.Criteria in project HomeMirror by HannahMitt.
the class SetUpActivity method setUpLocationMonitoring.
private void setUpLocationMonitoring() {
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = mLocationManager.getBestProvider(criteria, true);
try {
mLocation = mLocationManager.getLastKnownLocation(provider);
if (mLocation == null) {
mLocationView.setVisibility(View.VISIBLE);
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(SetUpActivity.this, R.string.found_location, Toast.LENGTH_SHORT).show();
mLocation = location;
mConfigSettings.setLatLon(String.valueOf(mLocation.getLatitude()), String.valueOf(mLocation.getLongitude()));
mLocationManager.removeUpdates(this);
if (mLocationView != null) {
mLocationView.setVisibility(View.GONE);
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
mLocationManager.requestLocationUpdates(provider, HOUR_MILLIS, METERS_MIN, mLocationListener);
} else {
mLocationView.setVisibility(View.GONE);
}
} catch (IllegalArgumentException e) {
Log.e("SetUpActivity", "Location manager could not use provider", e);
}
}
use of android.location.Criteria in project Parse-SDK-Android by ParsePlatform.
the class ParseGeoPoint method getCurrentLocationInBackground.
/**
* Asynchronously fetches the current location of the device.
*
* This will use a default {@link Criteria} with no accuracy or power requirements, which will
* generally result in slower, but more accurate location fixes.
* <p/>
* <strong>Note:</strong> If GPS is the best provider, it might not be able to locate the device
* at all and timeout.
*
* @param timeout
* The number of milliseconds to allow before timing out.
* @return A Task that is resolved when a location is found.
*
* @see android.location.LocationManager#getBestProvider(android.location.Criteria, boolean)
* @see android.location.LocationManager#requestLocationUpdates(String, long, float, android.location.LocationListener)
*/
public static Task<ParseGeoPoint> getCurrentLocationInBackground(long timeout) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
return LocationNotifier.getCurrentLocationAsync(Parse.getApplicationContext(), timeout, criteria).onSuccess(new Continuation<Location, ParseGeoPoint>() {
@Override
public ParseGeoPoint then(Task<Location> task) throws Exception {
Location location = task.getResult();
return new ParseGeoPoint(location.getLatitude(), location.getLongitude());
}
});
}
use of android.location.Criteria in project android_frameworks_base by ParanoidAndroid.
the class LocationManagerTest method testGetBestProviderEmptyCriteria.
public void testGetBestProviderEmptyCriteria() {
String p = manager.getBestProvider(new Criteria(), true);
assertNotNull(p);
}
use of android.location.Criteria in project android_frameworks_base by ParanoidAndroid.
the class LocationManagerTest method testGetBestProviderPowerCriteria.
public void testGetBestProviderPowerCriteria() {
Criteria c = new Criteria();
c.setPowerRequirement(Criteria.POWER_HIGH);
String p = manager.getBestProvider(c, true);
assertNotNull(p);
c.setPowerRequirement(Criteria.POWER_MEDIUM);
p = manager.getBestProvider(c, true);
assertNotNull(p);
c.setPowerRequirement(Criteria.POWER_LOW);
p = manager.getBestProvider(c, true);
assertNotNull(p);
c.setPowerRequirement(Criteria.NO_REQUIREMENT);
p = manager.getBestProvider(c, true);
assertNotNull(p);
}
Aggregations