Search in sources :

Example 26 with LocationListener

use of android.location.LocationListener in project platform_frameworks_base by android.

the class ExternalSharedPermsDiffKeyTest method testRunBluetoothAndFineLocation.

/** The use of location manager and bluetooth below are simply to simulate an app that
     *  tries to use them, so we can verify whether permissions are granted and accessible.
     * */
public void testRunBluetoothAndFineLocation() {
    LocationManager locationManager = (LocationManager) getInstrumentation().getContext().getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

        public void onLocationChanged(Location location) {
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    });
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if ((mBluetoothAdapter != null) && (!mBluetoothAdapter.isEnabled())) {
        mBluetoothAdapter.getName();
    }
    fail("this app was signed by a different cert and should crash/fail to run by now");
}
Also used : LocationManager(android.location.LocationManager) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) BluetoothAdapter(android.bluetooth.BluetoothAdapter) Location(android.location.Location)

Example 27 with LocationListener

use of android.location.LocationListener in project platform_frameworks_base by android.

the class ExternalSharedPermsFLTest method testRunFineLocation.

/** The use of location manager below is simply to simulate an app that
     *  tries to use it, so we can verify whether permissions are granted and accessible.
     * */
public void testRunFineLocation() {
    LocationManager locationManager = (LocationManager) getInstrumentation().getContext().getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

        public void onLocationChanged(Location location) {
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    });
}
Also used : LocationManager(android.location.LocationManager) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) Location(android.location.Location)

Example 28 with LocationListener

use of android.location.LocationListener 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 29 with LocationListener

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

the class GPSTesterActivityController method setLocationListenerGPSProvider.

private void setLocationListenerGPSProvider(final Boolean isNetworkAvailable) {
    Log.d("GPSTester", "Starting up LocationListener for GPS_PROVIDER");
    _locationListenerGPSProvider = new LocationListener() {

        boolean initialLapNetwork = true;

        String elapsedTimeGPSProvider = "N/A";

        public void onLocationChanged(Location location) {
            if (initialLapNetwork == true) {
                //NOTE: You can also use location.getTime()
                elapsedTimeGPSProvider = _elapsedTimer.getMinutes() + ":" + _elapsedTimer.getSeconds() + ":" + _elapsedTimer.getMillis();
                initialLapNetwork = false;
            }
            writeResultToTable(location, _locationManager, elapsedTimeGPSProvider, isNetworkAvailable);
        //			    Preferences.setSharedPreferences(
        //			    		PreferenceKey.GPS_LATLON, location.getLatitude() + "," +
        //			    				location.getLongitude(),_activity 
        //			    );
        //			    String t = Preferences.getSharedPreferences(PreferenceKey.GPS_LATLON, _activity);
        //			    String x = t;
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch(status) {
                case LocationProvider.OUT_OF_SERVICE:
                    Log.d("GPSTester", "Location Status Changed: GPS Out of Service");
                    //		Toast.LENGTH_LONG).show();
                    break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    Log.d("GPSTester", "Location Status Changed: GPS Temporarily Unavailable");
                    //		Toast.LENGTH_LONG).show();
                    break;
                case LocationProvider.AVAILABLE:
                    Log.d("GPSTester", "Status Changed: GPS Available");
                    //		Toast.LENGTH_LONG).show();
                    break;
            }
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };
    try {
        final long minDistance = Long.valueOf(_preferences.getString("pref_key_updateGPSMinDistance", "0"));
        final long minTime = Long.valueOf(_preferences.getString("pref_key_updateGPSMinTime", "0"));
        // Register the listener with the Location Manager to receive location updates
        _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, _locationListenerGPSProvider);
    } catch (Exception exc) {
        Log.d("GPSTester", "Unable to start GPS provider. Bad value. " + exc.getMessage());
    }
}
Also used : Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) Point(com.esri.core.geometry.Point) IOException(java.io.IOException) Location(android.location.Location)

Example 30 with LocationListener

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

the class SatelliteDataActivityController method setLocationListenerGPSProvider.

private void setLocationListenerGPSProvider() {
    _locationListenerGPSProvider = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        }
    };
    _nmeaListener = new NmeaListener() {

        @Override
        public void onNmeaReceived(long timestamp, String nmea) {
            String time = _elapsedTimer.convertMillisToMDYHMSS(timestamp);
            _gpsNMEAText = "<b><font color='yellow'>GPS NMEA</b></font>" + "<br><b>Timestamp:</b> " + time + "<br><b>NMEA code:</b> " + nmea;
            _gpsNMEATextView.setText(Html.fromHtml(_gpsNMEAText));
        }
    };
    _locationManager.addNmeaListener(_nmeaListener);
    _gpsStatusListener = new GpsStatus.Listener() {

        String seconds;

        String minutes;

        String hours;

        String ms;

        String satelliteHMS;

        String usedInFix = "false";

        int t;

        @Override
        public void onGpsStatusChanged(int event) {
            _satelliteList = "";
            satelliteHMS = "N/A";
            //Occasionally there may be null values if GPS hiccups
            try {
                t = _locationManager.getGpsStatus(null).getTimeToFirstFix();
                //String seconds = String.format(_format, t/1000 % 60);
                seconds = String.format(_format, TimeUnit.MILLISECONDS.toSeconds(t));
                minutes = String.format(_format, TimeUnit.MILLISECONDS.toMinutes(t));
                hours = String.format(_format, TimeUnit.MILLISECONDS.toHours(t));
                ms = String.format(_format, t % 1000);
                satelliteHMS = hours + ":" + minutes + ":" + seconds + ":" + ms;
                _satellites = _locationManager.getGpsStatus(null).getSatellites();
                if (_satellites != null) {
                    for (GpsSatellite sat : _satellites) {
                        if (sat.usedInFix() == true) {
                            usedInFix = "<font color='red'>true</font>";
                        } else {
                            usedInFix = "false";
                        }
                        _satelliteList = _satelliteList + "<br>" + sat.getPrn() + ", " + sat.getSnr() + ", " + usedInFix;
                    }
                }
            } catch (Exception exc) {
                Log.d("GPSTester", "GPS Status error (onGpsStatusChanged): " + exc.getMessage());
            }
            if (_satelliteList != "") {
                _gpsSatelliteTextView.setText(Html.fromHtml("<b><font color='yellow'>GPS Satellite Info (No., SNR, Used in fix)</b></font>" + "<br><b>Time to 1st fix:</b> " + satelliteHMS + _satelliteList));
            }
        }
    };
    _locationManager.addGpsStatusListener(_gpsStatusListener);
    try {
        long minDistance = Long.valueOf(_preferences.getString("pref_key_updateGPSMinDistance", "0"));
        long minTime = Long.valueOf(_preferences.getString("pref_key_updateGPSMinTime", "0"));
        // Register the listener with the Location Manager to receive location updates
        _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, _locationListenerGPSProvider);
    } catch (Exception exc) {
        Log.d("GPSTester", "Unable to start GPS provider. Bad value. " + exc.getMessage());
    }
}
Also used : NmeaListener(android.location.GpsStatus.NmeaListener) GpsStatus(android.location.GpsStatus) GpsSatellite(android.location.GpsSatellite) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) Location(android.location.Location)

Aggregations

LocationListener (android.location.LocationListener)41 Location (android.location.Location)34 Bundle (android.os.Bundle)34 LocationManager (android.location.LocationManager)22 BluetoothAdapter (android.bluetooth.BluetoothAdapter)12 Timer (java.util.Timer)6 TimerTask (java.util.TimerTask)6 Criteria (android.location.Criteria)5 IOException (java.io.IOException)3 Intent (android.content.Intent)2 Point (com.esri.core.geometry.Point)2 FacebookException (com.facebook.FacebookException)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 PendingIntent (android.app.PendingIntent)1 CanceledException (android.app.PendingIntent.CanceledException)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 GpsSatellite (android.location.GpsSatellite)1 GpsStatus (android.location.GpsStatus)1