Example usage for android.content Intent FLAG_GRANT_PREFIX_URI_PERMISSION

List of usage examples for android.content Intent FLAG_GRANT_PREFIX_URI_PERMISSION

Introduction

In this page you can find the example usage for android.content Intent FLAG_GRANT_PREFIX_URI_PERMISSION.

Prototype

int FLAG_GRANT_PREFIX_URI_PERMISSION

To view the source code for android.content Intent FLAG_GRANT_PREFIX_URI_PERMISSION.

Click Source Link

Document

When combined with #FLAG_GRANT_READ_URI_PERMISSION and/or #FLAG_GRANT_WRITE_URI_PERMISSION , the URI permission grant applies to any URI that is a prefix match against the original granted URI.

Usage

From source file:org.mariotaku.twidere.activity.MediaViewerActivity.java

protected void shareMedia() {
    if (mShareMediaPosition == -1)
        return;/*from  w  w  w  .j a  v  a2 s .co m*/
    final ViewPager viewPager = findViewPager();
    final PagerAdapter adapter = viewPager.getAdapter();
    final Object object = adapter.instantiateItem(viewPager, mShareMediaPosition);
    if (!(object instanceof CacheDownloadMediaViewerFragment))
        return;
    CacheDownloadLoader.Result result = ((CacheDownloadMediaViewerFragment) object).getDownloadResult();
    if (result == null || result.cacheUri == null) {
        // TODO show error
        return;
    }
    final File destination = ShareProvider.getFilesDir(this);
    if (destination == null)
        return;
    String type;
    if (object instanceof VideoPageFragment) {
        type = CacheProvider.Type.VIDEO;
    } else if (object instanceof ImagePageFragment) {
        type = CacheProvider.Type.IMAGE;
    } else if (object instanceof GifPageFragment) {
        type = CacheProvider.Type.IMAGE;
    } else {
        throw new UnsupportedOperationException("Unsupported fragment " + object);
    }
    final SaveFileTask task = new SaveFileTask(this, result.cacheUri, destination,
            new CacheProvider.CacheFileTypeCallback(this, type)) {
        private static final String PROGRESS_FRAGMENT_TAG = "progress";

        @Override
        protected void dismissProgress() {
            final IExtendedActivity activity = (IExtendedActivity) getContext();
            if (activity == null)
                return;
            activity.executeAfterFragmentResumed(new IExtendedActivity.Action() {
                @Override
                public void execute(IExtendedActivity activity) {
                    final FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
                    final DialogFragment fragment = (DialogFragment) fm
                            .findFragmentByTag(PROGRESS_FRAGMENT_TAG);
                    if (fragment != null) {
                        fragment.dismiss();
                    }
                }
            });
        }

        @Override
        protected void showProgress() {
            final IExtendedActivity activity = (IExtendedActivity) getContext();
            if (activity == null)
                return;
            activity.executeAfterFragmentResumed(new IExtendedActivity.Action() {
                @Override
                public void execute(IExtendedActivity activity) {
                    final DialogFragment fragment = new ProgressDialogFragment();
                    fragment.setCancelable(false);
                    fragment.show(((FragmentActivity) activity).getSupportFragmentManager(),
                            PROGRESS_FRAGMENT_TAG);
                }
            });
        }

        @Override
        protected void onFileSaved(@NonNull File savedFile, @Nullable String mimeType) {
            final MediaViewerActivity activity = (MediaViewerActivity) getContext();
            if (activity == null)
                return;

            final Uri fileUri = ShareProvider.getUriForFile(activity, AUTHORITY_TWIDERE_SHARE, savedFile);

            final Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setDataAndType(fileUri, mimeType);
            intent.putExtra(Intent.EXTRA_STREAM, fileUri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
            }
            activity.processShareIntent(intent);
            startActivityForResult(Intent.createChooser(intent, activity.getString(R.string.share)),
                    REQUEST_SHARE_MEDIA);
        }

        @Override
        protected void onFileSaveFailed() {
            final MediaViewerActivity activity = (MediaViewerActivity) getContext();
            if (activity == null)
                return;
            Toast.makeText(activity, R.string.error_occurred, Toast.LENGTH_SHORT).show();
        }
    };
    AsyncTaskUtils.executeTask(task);
}