Search in sources :

Example 31 with Criteria

use of android.location.Criteria in project glasquare by davidvavra.

the class LocationUtils method getRecentLocation.

public static void getRecentLocation(final LocationListener listener) {
    Location last = LocationUtils.getLastLocation();
    if (last == null || LocationUtils.getAgeInSeconds(last.getTime()) > 60) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);
        final LocationManager locationManager = (LocationManager) App.get().getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = locationManager.getProviders(criteria, true);
        if (providers.size() == 0) {
            listener.onLocationFailed();
            return;
        }
        locationSuccess = false;
        final android.location.LocationListener locationListener = new android.location.LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                locationSuccess = true;
                locationManager.removeUpdates(this);
                listener.onLocationAcquired(location);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }
        };
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 1000, 5, locationListener);
        }
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                if (!locationSuccess) {
                    locationManager.removeUpdates(locationListener);
                    listener.onLocationFailed();
                }
            }
        }, 5000);
    } else {
        listener.onLocationAcquired(last);
    }
}
Also used : LocationManager(android.location.LocationManager) Bundle(android.os.Bundle) Handler(android.os.Handler) Criteria(android.location.Criteria) Location(android.location.Location)

Example 32 with Criteria

use of android.location.Criteria in project robolectric by robolectric.

the class ShadowLocationManagerTest method shouldThrowExceptionWhenRequestingLocationUpdatesAndNoProviderIsFound.

@Test
public void shouldThrowExceptionWhenRequestingLocationUpdatesAndNoProviderIsFound() throws Exception {
    Intent someIntent = new Intent("some_action");
    PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(ShadowApplication.getInstance().getApplicationContext(), 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    try {
        shadowLocationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent);
        Assert.fail("When requesting location updates the intent must not be null!");
    } catch (Exception e) {
    // No worries, everything is fine...
    }
}
Also used : Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Criteria(android.location.Criteria) Test(org.junit.Test)

Example 33 with Criteria

use of android.location.Criteria in project robolectric by robolectric.

the class ShadowLocationManagerTest method getBestProvider_returnsProviderBasedOnCriteriaAndEnabledState.

@Test
public void getBestProvider_returnsProviderBasedOnCriteriaAndEnabledState() throws Exception {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    assertThat(locationManager.getBestProvider(null, false)).isEqualTo(LocationManager.GPS_PROVIDER);
    assertThat(locationManager.getBestProvider(null, true)).isNull();
    assertThat(locationManager.getBestProvider(criteria, false)).isEqualTo(LocationManager.NETWORK_PROVIDER);
    assertThat(locationManager.getBestProvider(criteria, true)).isNull();
}
Also used : Criteria(android.location.Criteria) Test(org.junit.Test)

Example 34 with Criteria

use of android.location.Criteria in project Gadgetbridge by Freeyourgadget.

the class SettingsActivity method onPostCreate.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    Preference pref = findPreference("notifications_generic");
    pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            startActivity(enableIntent);
            return true;
        }
    });
    pref = findPreference("pref_key_miband");
    pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            Intent enableIntent = new Intent(SettingsActivity.this, MiBandPreferencesActivity.class);
            startActivity(enableIntent);
            return true;
        }
    });
    pref = findPreference("pref_key_blacklist");
    pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            Intent enableIntent = new Intent(SettingsActivity.this, AppBlacklistActivity.class);
            startActivity(enableIntent);
            return true;
        }
    });
    pref = findPreference("pebble_emu_addr");
    pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newVal) {
            Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent);
            preference.setSummary(newVal.toString());
            return true;
        }
    });
    pref = findPreference("pebble_emu_port");
    pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newVal) {
            Intent refreshIntent = new Intent(DeviceManager.ACTION_REFRESH_DEVICELIST);
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(refreshIntent);
            preference.setSummary(newVal.toString());
            return true;
        }
    });
    pref = findPreference("log_to_file");
    pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newVal) {
            boolean doEnable = Boolean.TRUE.equals(newVal);
            try {
                if (doEnable) {
                    // ensures that it is created
                    FileUtils.getExternalFilesDir();
                }
                GBApplication.setupLogging(doEnable);
            } catch (IOException ex) {
                GB.toast(getApplicationContext(), getString(R.string.error_creating_directory_for_logfiles, ex.getLocalizedMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
            }
            return true;
        }
    });
    if (!GBApplication.isRunningMarshmallowOrLater()) {
        pref = findPreference("notification_filter");
        PreferenceCategory category = (PreferenceCategory) findPreference("pref_key_notifications");
        category.removePreference(pref);
    }
    pref = findPreference("location_aquire");
    pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SettingsActivity.this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, 0);
            }
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, false);
            if (provider != null) {
                Location location = locationManager.getLastKnownLocation(provider);
                if (location != null) {
                    setLocationPreferences(location);
                } else {
                    locationManager.requestSingleUpdate(provider, new LocationListener() {

                        @Override
                        public void onLocationChanged(Location location) {
                            setLocationPreferences(location);
                        }

                        @Override
                        public void onStatusChanged(String provider, int status, Bundle extras) {
                            LOG.info("provider status changed to " + status + " (" + provider + ")");
                        }

                        @Override
                        public void onProviderEnabled(String provider) {
                            LOG.info("provider enabled (" + provider + ")");
                        }

                        @Override
                        public void onProviderDisabled(String provider) {
                            LOG.info("provider disabled (" + provider + ")");
                            GB.toast(SettingsActivity.this, getString(R.string.toast_enable_networklocationprovider), 3000, 0);
                        }
                    }, null);
                }
            } else {
                LOG.warn("No location provider found, did you deny location permission?");
            }
            return true;
        }
    });
    pref = findPreference("canned_messages_dismisscall_send");
    pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            Prefs prefs = GBApplication.getPrefs();
            ArrayList<String> messages = new ArrayList<>();
            for (int i = 1; i <= 16; i++) {
                String message = prefs.getString("canned_message_dismisscall_" + i, null);
                if (message != null && !message.equals("")) {
                    messages.add(message);
                }
            }
            CannedMessagesSpec cannedMessagesSpec = new CannedMessagesSpec();
            cannedMessagesSpec.type = CannedMessagesSpec.TYPE_MISSEDCALLS;
            cannedMessagesSpec.cannedMessages = messages.toArray(new String[messages.size()]);
            GBApplication.deviceService().onSetCannedMessages(cannedMessagesSpec);
            return true;
        }
    });
    // Get all receivers of Media Buttons
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> mediaReceivers = pm.queryBroadcastReceivers(mediaButtonIntent, PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RESOLVED_FILTER);
    CharSequence[] newEntries = new CharSequence[mediaReceivers.size() + 1];
    CharSequence[] newValues = new CharSequence[mediaReceivers.size() + 1];
    newEntries[0] = getString(R.string.pref_default);
    newValues[0] = "default";
    int i = 1;
    for (ResolveInfo resolveInfo : mediaReceivers) {
        newEntries[i] = resolveInfo.activityInfo.loadLabel(pm);
        newValues[i] = resolveInfo.activityInfo.packageName;
        i++;
    }
    final ListPreference audioPlayer = (ListPreference) findPreference("audio_player");
    audioPlayer.setEntries(newEntries);
    audioPlayer.setEntryValues(newValues);
    audioPlayer.setDefaultValue(newValues[0]);
}
Also used : MiBandPreferencesActivity(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity) ArrayList(java.util.ArrayList) Criteria(android.location.Criteria) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) LocationListener(android.location.LocationListener) LocationManager(android.location.LocationManager) Bundle(android.os.Bundle) Intent(android.content.Intent) IOException(java.io.IOException) ListPreference(android.preference.ListPreference) EditTextPreference(android.preference.EditTextPreference) ListPreference(android.preference.ListPreference) Preference(android.preference.Preference) PreferenceCategory(android.preference.PreferenceCategory) CannedMessagesSpec(nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec) Location(android.location.Location)

Example 35 with Criteria

use of android.location.Criteria in project android-gps-test-tool by Esri.

the class GPSTesterActivityController method getBestProviderNameViaCriteria.

/**
	 * Let's you test scenarios based on various criteria settings. Does <b>not</b> currently include
	 * all criteria from: http://developer.android.com/reference/android/location/Criteria.html. 
	 * You can view the results of your settings changes in the Best Provider window at the
	 * bottom of the main activity screen.<br><br>
	 * <b>NOTE:</b> This does <b>override</b> how the default application operates.  
	 */
private String getBestProviderNameViaCriteria() {
    final int power = Integer.parseInt(_preferences.getString("pref_key_setPower", "1"));
    final int accuracy = Integer.parseInt(_preferences.getString("pref_key_setAccuracy", "1"));
    final boolean cost = Boolean.valueOf(_preferences.getString("pref_key_setCost", "true"));
    Criteria criteria = new Criteria();
    criteria.setAccuracy(accuracy);
    criteria.setCostAllowed(cost);
    criteria.setPowerRequirement(power);
    String finalBestProvider = "<b><font color='yellow'>Best Provider (via Criteria)</font></b><br>";
    String bestProviderName = _locationManager.getBestProvider(criteria, true);
    if (bestProviderName != null) {
        _bestLocationProviderTextView.setText(Html.fromHtml(finalBestProvider + bestProviderName));
    } else {
        _bestLocationProviderTextView.setText(Html.fromHtml(finalBestProvider + "N/A"));
    }
    return bestProviderName;
}
Also used : Criteria(android.location.Criteria) Point(com.esri.core.geometry.Point)

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