Example usage for android.content UriPermission getUri

List of usage examples for android.content UriPermission getUri

Introduction

In this page you can find the example usage for android.content UriPermission getUri.

Prototype

public Uri getUri() 

Source Link

Document

Return the Uri this permission pertains to.

Usage

From source file:com.commonsware.android.diceware.PassphraseFragment.java

private static boolean obtainDurablePermission(ContentResolver resolver, Uri document) {
    boolean weHaveDurablePermission = false;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

        try {/*from  www  . j a  v a  2  s.c om*/
            resolver.takePersistableUriPermission(document, perms);

            for (UriPermission perm : resolver.getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHaveDurablePermission = true;
                }
            }
        } catch (SecurityException e) {
            // OK, we were not offered any persistable permissions
        }
    }

    return (weHaveDurablePermission);
}

From source file:com.commonsware.android.documents.consumer.DurablizerService.java

private boolean obtainDurablePermission(Uri document) {
    boolean weHaveDurablePermission = false;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

        try {/*from  ww w  .ja va2  s .c om*/
            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHaveDurablePermission = true;
                }
            }
        } catch (SecurityException e) {
            // OK, we were not offered any persistable permissions
        }
    }

    return (weHaveDurablePermission);
}

From source file:com.veniosg.dir.mvvm.model.storage.access.SafStorageAccessManager.java

private boolean permissionGrantedForParentOf(@NonNull File fileInStorage) {
    List<UriPermission> permissions = context.getContentResolver().getPersistedUriPermissions();

    for (UriPermission permission : permissions) {
        String storageRoot = getExternalStorageRoot(fileInStorage, context);
        DocumentFile grantedDocFile = fromTreeUri(context, permission.getUri());
        boolean grantedOnAncestor = areSameFile(storageRoot, grantedDocFile);
        if (permission.isWritePermission() && grantedOnAncestor)
            return true;
    }/*from www  .  j  ava 2 s  . com*/
    return false;
}

From source file:com.commonsware.android.tte.DocumentStorageService.java

private void load(Uri document) {
    try {//from   ww  w  . j  ava 2 s  . c o  m
        boolean weHavePermission = false;
        boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());

        if (isContent) {
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHavePermission = true;
                }
            }
        } else {
            weHavePermission = true;
        }

        if (weHavePermission) {
            try {
                InputStream is = getContentResolver().openInputStream(document);

                try {
                    String text = slurp(is);
                    DocumentFile docFile;

                    if (isContent) {
                        docFile = DocumentFile.fromSingleUri(this, document);
                    } else {
                        docFile = DocumentFile.fromFile(new File(document.getPath()));
                    }

                    EventBus.getDefault().post(
                            new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
                } finally {
                    is.close();
                }
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
                EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
            }
        } else {
            Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
            EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
        }
    } catch (SecurityException e) {
        Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
        EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
    }
}

From source file:com.almalence.opencam.SavingService.java

@TargetApi(19)
public static DocumentFile getSaveDirNew(boolean forceSaveToInternalMemory) {
    DocumentFile saveDir = null;//from  ww w . ja  v  a2  s.  c o  m
    boolean usePhoneMem = true;

    String abcDir = "Camera";
    if (sortByData) {
        Calendar rightNow = Calendar.getInstance();
        abcDir = String.format("%tF", rightNow);
    }

    int saveToValue = Integer.parseInt(saveToPreference);
    if (saveToValue == 1 || saveToValue == 2) {
        boolean canWrite = false;
        String uri = saveToPath;
        try {
            saveDir = DocumentFile.fromTreeUri(ApplicationScreen.instance, Uri.parse(uri));
        } catch (Exception e) {
            saveDir = null;
        }
        List<UriPermission> perms = ApplicationScreen.instance.getContentResolver()
                .getPersistedUriPermissions();
        for (UriPermission p : perms) {
            if (p.getUri().toString().equals(uri.toString()) && p.isWritePermission()) {
                canWrite = true;
                break;
            }
        }

        if (saveDir != null && canWrite && saveDir.exists()) {
            if (sortByData) {
                DocumentFile dateFolder = saveDir.findFile(abcDir);
                if (dateFolder == null) {
                    dateFolder = saveDir.createDirectory(abcDir);
                }
                saveDir = dateFolder;
            }
            usePhoneMem = false;
        }
    }

    if (usePhoneMem || forceSaveToInternalMemory) // phone memory (internal
    // sd card)
    {
        saveDir = DocumentFile
                .fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
        DocumentFile abcFolder = saveDir.findFile(abcDir);
        if (abcFolder == null || !abcFolder.exists()) {
            abcFolder = saveDir.createDirectory(abcDir);
        }
        saveDir = abcFolder;
    }

    return saveDir;
}

From source file:com.xperia64.timidityae.TimidityActivity.java

@SuppressLint("NewApi")
public void initCallback() {
    if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        List<UriPermission> permissions = getContentResolver().getPersistedUriPermissions();
        int trueExt = 0;
        for (File f : getExternalFilesDirs(null)) {
            if (f != null)
                trueExt++;// w  ww  .  j a v a  2  s .c o m
        }
        if ((permissions == null || permissions.isEmpty()) && Globals.shouldLolNag && trueExt > 1) {
            new AlertDialog.Builder(this).setTitle("SD Card Access").setCancelable(false).setMessage(
                    "Would you like to give Timidity AE write access to your external sd card? This is recommended if you're converting files or would like to place Timidity AE's data directory there. Problems may occur if a directory other than the root of your SD card is selected.")
                    .setPositiveButton("Yes", new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
                            startActivityForResult(intent, 42);
                        }

                    }).setNegativeButton("No, do not ask again", new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Globals.prefs.edit().putBoolean("shouldLolNag", Globals.shouldLolNag = false)
                                    .commit();
                            initCallback2();
                        }

                    }).setNeutralButton("No", new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            initCallback2();
                        }
                    }).show();
        } else {
            for (UriPermission permission : permissions) {
                if (permission.isReadPermission() && permission.isWritePermission()) {
                    Globals.theFold = permission.getUri();
                }
            }

            initCallback2();
        }
    } else {
        initCallback2();
    }
}