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:at.ac.tuwien.caa.docscan.ui.StartActivity.java

private void requestCameraPermission() {

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA }, PERMISSION_CAMERA);
    } else/*www.j ava 2s  . c o m*/
        startCamera();

}

From source file:com.adithya321.sharesanalysis.database.RealmBackupRestore.java

private void checkStoragePermissions(Activity activity) {
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    }/*w w w.  ja v  a  2 s . c o  m*/
}

From source file:com.amobletool.bluetooth.le.downexample.ui.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    // ???ble ?,???
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();/* w w w  . j a v  a 2 s  . c o m*/
    }

    // ? Bluetooth adapter, ?????(APIandroid4.3)
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

    // ???
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// API level  23(Android 6.0) 
        //???
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //??????
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION)) {
                //                    showToast("Android 6.0???????Ble");
            }
            //??
            ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                    REQUEST_CODE_ACCESS_COARSE_LOCATION);
        }
    }
}

From source file:br.gbizotto.customcamera.camera2.Camera2BasicFragment.java

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    if (requestCode == REQUEST_CAMERA_PERMISSION) {
        if (grantResults.length != 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            ErrorDialog.newInstance(getString(R.string.request_permission))
                    .show(getActivity().getFragmentManager(), ErrorDialog.FRAGMENT_DIALOG);
        }/*  ww  w  .ja v  a2s . com*/
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

From source file:cn.yibulz.caviewtest.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        mCameraView.start();//from ww w .j av a  2s.  c o  m
    } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        ConfirmationDialogFragment
                .newInstance(R.string.camera_permission_confirmation,
                        new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA_PERMISSION,
                        R.string.camera_permission_not_granted)
                .show(getSupportFragmentManager(), FRAGMENT_DIALOG);
    } else {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA },
                REQUEST_CAMERA_PERMISSION);
    }
}

From source file:com.andrasta.dashi.SplashActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    if (requestCode == requestId) {
        if (!checkPermissions()) {
            return;
        }/*from  www  .  j  a  v  a 2  s  .c  om*/

        if (grantResults.length != 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            MainActivity.ExitDialog.newInstance(getString(R.string.permission_discard))
                    .show(getFragmentManager(), "Dialog");
        } else {
            onAllPermissionsGranted();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

From source file:co.aurasphere.bluepair.bluetooth.BluetoothController.java

/**
 * Starts the discovery of new Bluetooth devices nearby.
 *//*from  w w w .  ja  v  a  2 s  .co m*/
public void startDiscovery() {
    broadcastReceiverDelegator.onDeviceDiscoveryStarted();

    // This line of code is very important. In Android >= 6.0 you have to ask for the runtime
    // permission as well in order for the discovery to get the devices ids. If you don't do
    // this, the discovery won't find any device.
    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(context, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                1);
    }

    // If another discovery is in progress, cancels it before starting the new one.
    if (bluetooth.isDiscovering()) {
        bluetooth.cancelDiscovery();
    }

    // Tries to start the discovery. If the discovery returns false, this means that the
    // bluetooth has not started yet.
    Log.d(TAG, "Bluetooth starting discovery.");
    if (!bluetooth.startDiscovery()) {
        Toast.makeText(context, "Error while starting device discovery!", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "StartDiscovery returned false. Maybe Bluetooth isn't on?");

        // Ends the discovery.
        broadcastReceiverDelegator.onDeviceDiscoveryEnd();
    }
}

From source file:cn.qqjlbsc.shopseller.utlis.EasyPermissions.java

/**
 *
 *//*from w ww . j  av a  2 s. c  om*/
public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults,
        Object object) {

    checkCallingObjectSuitability(object);

    // Make a collection of granted and denied permissions from the request.
    ArrayList<String> granted = new ArrayList<>();
    ArrayList<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);
        }
    }

    // Report granted permissions, if any.
    if (!granted.isEmpty()) {
        // Notify callbacks
        if (object instanceof PermissionCallbacks) {
            ((PermissionCallbacks) object).onPermissionsGranted(requestCode, granted);
        }
    }

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

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

From source file:butter.droid.activities.MainActivity.java

@SuppressLint("MissingSuperCall")
@Override//from  w  w w .  j  a v  a 2s .com
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.activity_main);

    if (!PrefUtils.contains(this, TermsActivity.TERMS_ACCEPTED)) {
        startActivity(new Intent(this, TermsActivity.class));
    }

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                PERMISSIONS_REQUEST);
    }

    String action = getIntent().getAction();
    Uri data = getIntent().getData();
    if (action != null && action.equals(Intent.ACTION_VIEW) && data != null) {
        String streamUrl = data.toString();
        try {
            streamUrl = URLDecoder.decode(streamUrl, "utf-8");
            StreamLoadingActivity.startActivity(this, new StreamInfo(streamUrl));
            finish();
            return;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    FragmentManager.enableDebugLogging(BuildConfig.DEBUG);

    setSupportActionBar(mToolbar);
    setShowCasting(true);

    ToolbarUtils.updateToolbarHeight(this, mToolbar);

    // Set up the drawer.
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark));

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer_fragment);

    mNavigationDrawerFragment.initialise(mNavigationDrawerContainer, drawerLayout);

    if (null != savedInstanceState)
        return;
    int providerId = PrefUtils.get(this, Prefs.DEFAULT_VIEW, 0);
    mNavigationDrawerFragment.selectItem(providerId);
}

From source file:ar.com.bestprice.buyitnow.barcodereader.BarcodeCaptureActivity.java

/**
 * Initializes the UI and creates the detector pipeline.
 *///from  w w w.  j  av  a2s . c  o  m
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.barcode_capture);

    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>) findViewById(R.id.graphicOverlay);

    // read parameters from the intent used to launch the activity.
    boolean autoFocus = getIntent().getBooleanExtra(AutoFocus, false);
    boolean useFlash = getIntent().getBooleanExtra(UseFlash, false);

    // 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) {
        createCameraSource(autoFocus, useFlash);
    } else {
        requestCameraPermission();
    }

    gestureDetector = new GestureDetector(this, new CaptureGestureListener());
    scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());

    Snackbar.make(mGraphicOverlay, "Tap to capture. Pinch/Stretch to zoom", Snackbar.LENGTH_LONG).show();
}