Search in sources :

Example 36 with Criteria

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);
    }
}
Also used : LocationManager(android.location.LocationManager) GregorianCalendar(java.util.GregorianCalendar) Criteria(android.location.Criteria) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) CalendarEventSpec(nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec) Location(android.location.Location)

Example 37 with Criteria

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);
    }
}
Also used : Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) Criteria(android.location.Criteria) Location(android.location.Location)

Example 38 with Criteria

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());
        }
    });
}
Also used : Criteria(android.location.Criteria) Location(android.location.Location)

Example 39 with Criteria

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);
}
Also used : Criteria(android.location.Criteria)

Example 40 with Criteria

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);
}
Also used : Criteria(android.location.Criteria)

Aggregations

Criteria (android.location.Criteria)52 Location (android.location.Location)20 Test (org.junit.Test)12 LocationManager (android.location.LocationManager)9 ArrayList (java.util.ArrayList)8 Bundle (android.os.Bundle)7 PendingIntent (android.app.PendingIntent)6 Intent (android.content.Intent)6 LocationListener (android.location.LocationListener)6 Handler (android.os.Handler)6 SuppressLint (android.annotation.SuppressLint)2 FacebookException (com.facebook.FacebookException)2 HashMap (java.util.HashMap)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 EditTextPreference (android.preference.EditTextPreference)1 ListPreference (android.preference.ListPreference)1 Preference (android.preference.Preference)1 PreferenceCategory (android.preference.PreferenceCategory)1