private boolean setupLocationTest()

in niap-cc/Permissions/Companion/app/src/main/java/com/android/certifications/niap/permissions/companion/MainActivity.java [224:279]


    private boolean setupLocationTest() {
        final String ACCESS_LOCATION_ACTION = "ACCESS_LOCATION_ACTION";
        CountDownLatch[] latch = new CountDownLatch[1];
        latch[0] = new CountDownLatch(1);

        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "Location permission has not been granted; prompting for it now");
            ActivityCompat.requestPermissions(this, LOCATION_PERMISSIONS,
                    REQUEST_LOCATION_PERMISSION);
            return false;
        } else {
            Log.d(TAG, "ACCESS_FINE_LOCATION permission has been granted");
        }

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                LocationResult result = LocationResult.extractResult(intent);
                // The countdown on the latch should only occur if the provided intent includes
                // a valid location since updates can be sent without location.
                if (result != null) {
                    Location location = result.getLastLocation();
                    if (location != null) {
                        Log.d(TAG, "Received a lat,long of " + location.getLatitude() + ", "
                                + location.getLongitude());
                        latch[0].countDown();
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(ACCESS_LOCATION_ACTION));
        Intent intent = new Intent(ACCESS_LOCATION_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        FusedLocationProviderClient locationClient =
                LocationServices.getFusedLocationProviderClient(this);
        try {
            locationClient.requestLocationUpdates(mLocationRequest, pendingIntent);
        } catch (SecurityException e) {
            Log.e(TAG, "Caught a SecurityException requesting location updates: ", e);
            return false;
        }
        boolean locationReceived = false;
        try {
            locationReceived = latch[0].await(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Log.e(TAG, "Caught an InterruptedException: ", e);
        }
        locationClient.removeLocationUpdates(pendingIntent);
        unregisterReceiver(receiver);
        if (!locationReceived) {
            Log.d(TAG, "Location update not received within timeout window");
        }
        return locationReceived;
    }