Example usage for android.content.pm PackageManager PERMISSION_GRANTED

List of usage examples for android.content.pm PackageManager PERMISSION_GRANTED

Introduction

In this page you can find the example usage for android.content.pm PackageManager PERMISSION_GRANTED.

Prototype

int PERMISSION_GRANTED

To view the source code for android.content.pm PackageManager PERMISSION_GRANTED.

Click Source Link

Document

Permission check result: this is returned by #checkPermission if the permission has been granted to the given package.

Usage

From source file:com.aftabsikander.permissionassist.PermissionAssistant.java

/**
 * Handle the result of a permission request, should be called from the calling {@link
 * Activity}'s {@link ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int,
 * String[], int[])} method.// w  w w .  ja v  a 2s.c om
 * <p>
 * If any permissions were granted or denied, the {@code object} will receive the appropriate
 * callbacks through {@link PermissionCallbacks} or {@link PermissionCallback},
 * and methods annotated with {@link AfterPermissionGranted} will be run if appropriate.
 *
 * @param requestCode  requestCode argument to permission result callback.
 * @param permissions  permissions argument to permission result callback.
 * @param grantResults grantResults argument to permission result callback.
 * @param receivers    an array of objects that have a method annotated with {@link
 *                     AfterPermissionGranted} or implement {@link PermissionCallbacks}.
 */
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults, @NonNull Object... receivers) {
    // Make a collection of granted and denied permissions from the request.
    List<String> granted = new ArrayList<>();
    List<String> denied = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++) {
        String perm = permissions[i];
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
            granted.add(perm);
        } else {
            denied.add(perm);
        }
    }

    // iterate through all receivers
    for (Object object : receivers) {
        // Report granted permissions, if any.
        if (!granted.isEmpty() && object instanceof PermissionCallbacks) {
            //if (object instanceof PermissionCallbacks) {
            ((PermissionCallbacks) object).onPermissionsGranted(requestCode, granted);
            //}
        }

        // Report denied permissions, if any.
        if (!denied.isEmpty() && object instanceof PermissionCallbacks) {
            //if () {
            ((PermissionCallbacks) object).onPermissionsDenied(requestCode, denied);
            //}
        }

        // Report all requested permissions, granted or denied
        if (object instanceof PermissionCallback) {
            ((PermissionCallback) object).onPermissionsResults(requestCode, granted, denied);
        }

        // If 100% successful, call annotated methods
        if (!granted.isEmpty() && denied.isEmpty()) {
            runAnnotatedMethods(object, requestCode);
        }
    }
}

From source file:carsharing.starter.automotive.iot.ibm.com.mobilestarterapp.Home.CarBrowse.java

private void requestLocationUpdates(final long minTime, final float minDistance) {
    final FragmentActivity activity = getActivity();
    if (activity == null) {
        return;/*ww  w . j  av a  2s.c o m*/
    }
    if (ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    if (locationManager != null) {
        locationManager.removeUpdates(this);
        locationManager.requestLocationUpdates(provider, minTime, minDistance, this);
        Log.i("Location Data", "requestLocationUpdates: minTime=" + minTime + " minDistance=" + minDistance);
    }
}

From source file:android_network.hetnet.vpn_service.Util.java

public static boolean hasInternet(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    return (pm.checkPermission("android.permission.INTERNET",
            packageName) == PackageManager.PERMISSION_GRANTED);
}

From source file:co.tinode.tindroid.ContactsFragment.java

/**
 * A wrapper which ensures that run-time permission to access contacts is granted.
 *///from w w  w.j  a  v  a  2  s.c  om
private void assignAdapter() {
    // Check the SDK version and whether the permission is already granted or not.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getActivity()
            .checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] { Manifest.permission.READ_CONTACTS },
                PERMISSIONS_REQUEST_READ_CONTACTS);
        //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
    } else {
        getLoaderManager().initLoader(ContactsQuery.PHEMIM_QUERY_ID, null, mPhEmImLoaderCallback);

        // Create the main contacts adapter
        mAdapter = new ContactsAdapter(getActivity());
        setListAdapter(mAdapter);
        // mAdapter.notifyDataSetChanged();
    }
}

From source file:com.affectiva.affdexme.MainActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == CAMERA_PERMISSIONS_REQUEST) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            int grantResult = grantResults[i];

            if (permission.equals(Manifest.permission.CAMERA)) {
                cameraPermissionsAvailable = (grantResult == PackageManager.PERMISSION_GRANTED);
            }/*w  w  w  .j  a  v  a2  s . co m*/
        }

        if (!cameraPermissionsAvailable) {
            permissionsUnavailableLayout.setVisibility(View.VISIBLE);
        } else {
            permissionsUnavailableLayout.setVisibility(View.GONE);
        }
    }

    if (requestCode == EXTERNAL_STORAGE_PERMISSIONS_REQUEST) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            int grantResult = grantResults[i];

            if (permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                storagePermissionsAvailable = (grantResult == PackageManager.PERMISSION_GRANTED);
            }
        }

        if (storagePermissionsAvailable) {
            // resume taking the screenshot
            takeScreenshot(screenshotButton);
        }
    }

}

From source file:fr.cph.chicago.util.Util.java

public static void centerMap(@NonNull final SupportMapFragment mapFragment, @NonNull final Activity activity,
        @Nullable final Position position) {
    mapFragment.getMapAsync(googleMap -> {
        if (ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(activity,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION }, 1);
            return;
        }/*from   w  w w .  j a v  a 2s .co  m*/
        googleMap.setMyLocationEnabled(true);
        if (position != null) {
            final LatLng latLng = new LatLng(position.getLatitude(), position.getLongitude());
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
        } else {
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(CHICAGO, 10));
        }
    });
}

From source file:io.kristal.locationplugin.LocationPlugin.java

private boolean checkLocationPermission(Context context) {
    return ActivityCompat.checkSelfPermission(context,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

From source file:com.alivenet.dmvtaxi.fragment.FragmentArriveDriver.java

public void getCurrentLocation(double latitude, double longitude) {
    googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.maparrive)).getMap();
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.setMyLocationEnabled(true);

    LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);
    Location l = null;//  w  w  w .j  a  va  2 s  .  co m

    if (googleMap != null) {

        googleMap.clear();
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("");

        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude))
                .zoom(10).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        googleMap.addMarker(marker);

        if (ActivityCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        }
        googleMap.setMyLocationEnabled(true);

    }

    if (latitude != 0.0d && longitude != 0.0d) {

        Intent intent = new Intent(getActivity(), BackgroundService.class);
        intent.putExtra("userid", mUserId);
        intent.putExtra("lat", String.valueOf(latitude));
        intent.putExtra("long", String.valueOf(longitude));
        getActivity().startService(intent);
    }

}

From source file:carsharing.starter.automotive.iot.ibm.com.mobilestarterapp.Home.CarBrowse.java

public void getLocation(final View view) {
    if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;// ww  w . j  a v a 2 s.  c o m
    }

    final Location location = locationManager.getLastKnownLocation(provider);

    onLocationChanged(location);
}

From source file:br.com.brolam.cloudvision.ui.MainActivity.java

/**
 * Adicionar a imagem de background ao NotVision e tambm verificar a permisso a cmera
 * fotogrfica.//from  ww  w. ja  v a2  s . co  m
 * @param noteVisionKey informar uma chave vlida
 * @param noteVision informar um NoteVision vlido.
 */
private void addNoteVisionBackground(String noteVisionKey, HashMap noteVision) {
    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        if (this.imagesHelper != null) {
            try {
                NoteVision.BackgroundOrigin backgroundOrigin = NoteVision.getBackground(noteVision);
                if (backgroundOrigin == NoteVision.BackgroundOrigin.LOCAL) {
                    Toast.makeText(this, R.string.note_vision_alert_background_image_in_processing,
                            Toast.LENGTH_LONG).show();
                } else {
                    this.imagesHelper.takeNoteVisionBackground(noteVisionKey);
                    this.appAnalyticsHelper.logNoteVisionAddBackground(TAG);
                    this.setItemSelectedKey(noteVisionKey);
                }
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
                Toast.makeText(this, String.format(getString(R.string.main_activity_request_error),
                        ImagesHelper.REQUEST_IMAGE_CAPTURE), Toast.LENGTH_LONG).show();
            }
        }

    } else {
        ActivityHelper.requestCameraPermission(TAG, this, fabAdd);
    }

}