android.support.v13.app.ActivityCompat Java Examples

The following examples show how to use android.support.v13.app.ActivityCompat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeviceActivity.java    From M365-Power with GNU General Public License v3.0 6 votes vote down vote up
private void requestStoragePermission() {
    // Permission has not been granted and must be requested.
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // Display a SnackBar with cda button to request the missing permission.
        Snackbar.make(mRootView, R.string.permission_request,
                Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Request the permission
                ActivityCompat.requestPermissions(DeviceActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        PERMISSION_EXTERNAL_STORAGE);
            }
        }).show();

    } else {
        Snackbar.make(mRootView, R.string.permission_unavailable, Snackbar.LENGTH_SHORT).show();
        // Request the permission. The result will be received in onRequestPermissionResult().
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_EXTERNAL_STORAGE);
    }
}
 
Example #2
Source File: MainActivity.java    From APKMirror with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {

    if (isWritePermissionGranted()) {
        download(url, suggestedFilename);
    } else {
        new MaterialDialog.Builder(MainActivity.this)
                .title(R.string.write_permission)
                .content(R.string.storage_access)
                .positiveText(R.string.request_permission)
                .negativeText(android.R.string.cancel)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        //Request permission
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                    }
                })
                .show();
    }

}
 
Example #3
Source File: SplashActivity.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void requestStoragePermissions() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        ErrorDialog.newInstance(getString(R.string.storage_permission_explanation))
                .show(getFragmentManager(), "");
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE);
    }
}
 
Example #4
Source File: UserGuideActivity.java    From prevent with Do What The F*ck You Want To Public License 5 votes vote down vote up
private boolean checkPermission() {
    if (checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    }
    return false;
}
 
Example #5
Source File: MainActivity.java    From android-wallet-app with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("StatementWithEmptyBody")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case Constants.REQUEST_CAMERA_PERMISSION:
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted!

            } else {

                // permission denied!

                if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CAMERA)) {
                    Snackbar.make(findViewById(R.id.drawer_layout), R.string.messages_permission_camera,
                            Snackbar.LENGTH_LONG)
                            .setAction(R.string.buttons_ok, view -> {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
                                intent.addCategory(Intent.CATEGORY_DEFAULT);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivityForResult(intent, Constants.REQUEST_CAMERA_PERMISSION);
                            })
                            .show();
                }
            }
    }
}
 
Example #6
Source File: MapPickerActivity.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p/>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQ_LOCATION);
            im.actor.runtime.Log.d("Permissions", "MapPickerActivity.setUpMap - no permission :c");
            return;
        }
    }

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    for (String provider : locationManager.getAllProviders()) {
        currentLocation = locationManager.getLastKnownLocation(provider);
        if (currentLocation != null) {
            break;
        }
    }

    if (currentLocation != null) {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 14));
        fetchPlaces(null);
    }
    mMap.setOnMyLocationChangeListener(this);
    mMap.getUiSettings().setMyLocationButtonEnabled(false);
    mMap.getUiSettings().setZoomControlsEnabled(false);
    mMap.getUiSettings().setCompassEnabled(false);
    mMap.setMyLocationEnabled(true);
    mMap.setOnMapLongClickListener(this);
    mMap.setOnMarkerClickListener(this);
}
 
Example #7
Source File: PictureActivity.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
private void savePicture() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQ_MEDIA);
            Log.d("Permissions", "savePhoto - no permission :c");
            return;
        }
    }

    File externalFile = Environment.getExternalStorageDirectory();
    if (externalFile == null) {
        Toast.makeText(getActivity(), R.string.toast_no_sdcard, Toast.LENGTH_LONG).show();
    } else {
        boolean isGif = path.endsWith(".gif");
        String externalPath = externalFile.getAbsolutePath();
        String exportPathBase = externalPath + "/" + ActorSDK.sharedActor().getAppName() + "/" + ActorSDK.sharedActor().getAppName() + " images" + "/";
        new File(exportPathBase).mkdirs();
        try {
            String exportPath = exportPathBase + (fileName != null ? fileName : "exported") + "_" + Randoms.randomId() + (isGif ? ".gif" : ".jpg");
            Files.copy(new File(this.path), new File(exportPath));
            MediaScannerConnection.scanFile(getActivity(), new String[]{exportPath}, new String[]{"image/" + (isGif ? "gif" : "jpeg")}, null);
            Toast.makeText(getActivity(), getString(R.string.file_saved) + " " + exportPath, Toast.LENGTH_LONG).show();
            saveMenuItem.setEnabled(false);
            saveMenuItem.setTitle(R.string.menu_saved);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
Example #8
Source File: DropTargetFragment.java    From user-interface-samples with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean setImageUri(View view, DragEvent event, Uri uri) {
    // Read the string from the clip description extras.
    Log.d(TAG, "ClipDescription extra: " + getExtra(event));

    Log.d(TAG, "Setting image source to: " + uri.toString());
    mImageUri = uri;

    if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        // Accessing a "content" scheme Uri requires a permission grant.
        DragAndDropPermissionsCompat dropPermissions = ActivityCompat
                .requestDragAndDropPermissions(getActivity(), event);
        Log.d(TAG, "Requesting permissions.");

        if (dropPermissions == null) {
            // Permission could not be obtained.
            Log.d(TAG, "Drop permission request failed.");
            return false;
        }

        final boolean result = super.setImageUri(view, event, uri);

        if (mReleasePermissionCheckBox.isChecked()) {
            /* Release the permissions if you are done with the URI.
             Note that you may need to hold onto the permission until later if other
             operations are performed on the content. For instance, releasing the
             permissions here will prevent onCreateView from properly restoring the
             ImageView state.
             If permissions are not explicitly released, the permission grant will be
             revoked when the activity is destroyed.
             */
            dropPermissions.release();
            Log.d(TAG, "Permissions released.");
        }

        return result;
    } else {
        // Other schemes (such as "android.resource") do not require a permission grant.
        return super.setImageUri(view, event, uri);
    }
}
 
Example #9
Source File: DropTargetFragment.java    From android-DragAndDropAcrossApps with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean setImageUri(View view, DragEvent event, Uri uri) {
    // Read the string from the clip description extras.
    Log.d(TAG, "ClipDescription extra: " + getExtra(event));

    Log.d(TAG, "Setting image source to: " + uri.toString());
    mImageUri = uri;

    if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        // Accessing a "content" scheme Uri requires a permission grant.
        DragAndDropPermissionsCompat dropPermissions = ActivityCompat
                .requestDragAndDropPermissions(getActivity(), event);
        Log.d(TAG, "Requesting permissions.");

        if (dropPermissions == null) {
            // Permission could not be obtained.
            Log.d(TAG, "Drop permission request failed.");
            return false;
        }

        final boolean result = super.setImageUri(view, event, uri);

        if (mReleasePermissionCheckBox.isChecked()) {
            /* Release the permissions if you are done with the URI.
             Note that you may need to hold onto the permission until later if other
             operations are performed on the content. For instance, releasing the
             permissions here will prevent onCreateView from properly restoring the
             ImageView state.
             If permissions are not explicitly released, the permission grant will be
             revoked when the activity is destroyed.
             */
            dropPermissions.release();
            Log.d(TAG, "Permissions released.");
        }

        return result;
    } else {
        // Other schemes (such as "android.resource") do not require a permission grant.
        return super.setImageUri(view, event, uri);
    }
}
 
Example #10
Source File: UserGuideActivity.java    From prevent with Do What The F*ck You Want To Public License 4 votes vote down vote up
private boolean hasPermission() {
    return checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            || !ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
 
Example #11
Source File: MainActivity.java    From stockita-point-of-sale with MIT License 4 votes vote down vote up
/**
 * This callback coming from the {@link ItemMasterListFragmentUI.MyAdapter}
 * @param userUid The user UID
 * @param pushKey The server unique key
 * @param model   The model POJO
 */
@Override
public void onItemMasterEditDialogCallBacks(int requestCode, String userUid, String pushKey, Object model) {

    switch (requestCode) {

        /**
         * This case coming from {@link ItemMasterListFragmentUI.MyAdapter} to
         * show {@link ItemMasterEditFormDialogFragment}, so the user can edit the item master.
         */
        case Constants.REQUEST_CODE_DIALOG_ONE:

            // Instantiate the dialog fragment object and pass the encoded email, pushKey, and the itemModel as an argument
            ItemMasterEditFormDialogFragment itemMasterEditFormDialogFragment =
                    ItemMasterEditFormDialogFragment.newInstance(userUid, pushKey, (ItemModel) model);

            // Show the Dialog Fragment on the screen
            itemMasterEditFormDialogFragment.show(getFragmentManager(), FRAGMENT_DIALOG_ITEM_MASTER_EDIT);
            break;


        /**
         * If the user choose to add image from the gallery
         */
        case Constants.REQUEST_CODE_DIALOG_TWO:

            aaItemMasterUserUid = userUid;
            aaItemMasterPushKey = pushKey;

            // Check of permission
            if (android.support.v4.app.ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            // Verify that the intent will resolve to an activity */
            if (i.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(i, GET_FROM_GALLERY_REQUEST_CODE);
            }

            break;

        /**
         * If the user choose to take a photo from the camera
         */
        case Constants.REQUEST_CODE_DIALOG_THREE:

            aaItemMasterUserUid = userUid;
            aaItemMasterPushKey = pushKey;

            // Check for permission
            if (android.support.v4.app.ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
            startActivityForResult(startCustomCameraIntent, TAKE_PHOTO);

            break;
    }
}