Search in sources :

Example 26 with LocationManager

use of android.location.LocationManager in project Space-Station-Tracker by Kiarasht.

the class Locations method Connected.

/**
     * Lets find user's location.
     */
private void Connected() {
    // Check if we have the right permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, R.string.errorPermissionLocation, Toast.LENGTH_LONG).show();
        return;
    }
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);
    Location location = null;
    for (int i = providers.size() - 1; i >= 0; i--) {
        location = locationManager.getLastKnownLocation(providers.get(i));
        if (location != null)
            break;
    }
    // If we got something back start parsing
    if (location != null) {
        String url = "https://maps.googleapis.com/maps/api/staticmap?" + "center=LAT,LNG&" + "zoom=13&" + "scale=1&" + "size=640x640&" + "markers=color:red%7CLAT,LNG&" + "key=AIzaSyAtpWPhzhbtqTgofnQhAHjiG12MmrY2AAE";
        mLatitude = String.valueOf(location.getLatitude());
        mLongitude = String.valueOf(location.getLongitude());
        url = url.replace("LAT", mLatitude);
        url = url.replace("LNG", mLongitude);
        try {
            List<Address> matches = new Geocoder(this).getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            final Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
            if (bestMatch != null) {
                String locationFormat = "";
                if (!"null".equals(bestMatch.getLocality())) {
                    locationFormat += bestMatch.getLocality() + ", ";
                }
                if (!"null".equals(bestMatch.getAdminArea())) {
                    locationFormat += bestMatch.getAdminArea() + " ";
                }
                if (!"null".equals(bestMatch.getCountryCode())) {
                    locationFormat += bestMatch.getCountryCode() + " ";
                }
                if (!"null".equals(bestMatch.getPostalCode())) {
                    locationFormat += bestMatch.getPostalCode();
                }
                mTitleView.setText(locationFormat);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Picasso.with(mActivity).load(url).into(mImageView);
        displayResults();
        displayPasses(null, null, null);
    } else {
        Toast.makeText(this, R.string.errorLocation, Toast.LENGTH_LONG).show();
    }
}
Also used : LocationManager(android.location.LocationManager) Address(android.location.Address) IOException(java.io.IOException) Geocoder(android.location.Geocoder) Location(android.location.Location)

Example 27 with LocationManager

use of android.location.LocationManager in project android_frameworks_base by ResurrectionRemix.

the class ExternalSharedPermsTest method testRunLocationAndBluetooth.

/** 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 testRunLocationAndBluetooth() {
    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();
    }
}
Also used : LocationManager(android.location.LocationManager) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) BluetoothAdapter(android.bluetooth.BluetoothAdapter) Location(android.location.Location)

Example 28 with LocationManager

use of android.location.LocationManager in project android_frameworks_base by ResurrectionRemix.

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 29 with LocationManager

use of android.location.LocationManager in project android_frameworks_base by ResurrectionRemix.

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 30 with LocationManager

use of android.location.LocationManager in project android_frameworks_base by ResurrectionRemix.

the class TrackerService method stopListeners.

/**
     * De-registers all location listeners, closes persistent storage
     */
protected synchronized void stopListeners() {
    LocationManager lm = getLocationManager();
    if (mListeners != null) {
        for (LocationTrackingListener listener : mListeners) {
            lm.removeUpdates(listener);
        }
        mListeners.clear();
    }
    mListeners = null;
    // stop cell state listener
    if (mTelephonyManager != null) {
        mTelephonyManager.listen(mPhoneStateListener, 0);
    }
    // stop network/wifi listener
    if (mNetwork != null) {
        unregisterReceiver(mNetwork);
    }
    mNetwork = null;
    mTrackerData = null;
    if (mPrefListener != null) {
        getPreferences().unregisterOnSharedPreferenceChangeListener(mPrefListener);
        mPrefListener = null;
    }
}
Also used : LocationManager(android.location.LocationManager)

Aggregations

LocationManager (android.location.LocationManager)70 Location (android.location.Location)31 Bundle (android.os.Bundle)23 LocationListener (android.location.LocationListener)22 BluetoothAdapter (android.bluetooth.BluetoothAdapter)12 Criteria (android.location.Criteria)8 IntentFilter (android.content.IntentFilter)5 TrackerDataHelper (com.android.locationtracker.data.TrackerDataHelper)5 IOException (java.io.IOException)3 SuppressLint (android.annotation.SuppressLint)2 Intent (android.content.Intent)2 SensorManager (android.hardware.SensorManager)2 FacebookException (com.facebook.FacebookException)2 ArrayList (java.util.ArrayList)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 ActivityManager (android.app.ActivityManager)1 AlarmManager (android.app.AlarmManager)1 KeyguardManager (android.app.KeyguardManager)1 NotificationManager (android.app.NotificationManager)1 Context (android.content.Context)1