Example usage for android.content Intent FLAG_GRANT_READ_URI_PERMISSION

List of usage examples for android.content Intent FLAG_GRANT_READ_URI_PERMISSION

Introduction

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

Prototype

int FLAG_GRANT_READ_URI_PERMISSION

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

Click Source Link

Document

If set, the recipient of this Intent will be granted permission to perform read operations on the URI in the Intent's data and any URIs specified in its ClipData.

Usage

From source file:com.android.gallery3d.ui.MenuExecutor.java

public void onMenuClicked(int action, ProgressListener listener, boolean waitOnStop, boolean showDialog) {
    int title;// w  ww.j  a  v a  2s  . c  o m
    switch (action) {
    case R.id.action_select_all:
        if (mSelectionManager.inSelectAllMode()) {
            mSelectionManager.deSelectAll();
        } else {
            mSelectionManager.selectAll();
        }
        return;
    case R.id.action_crop: {
        Intent intent = getIntentBySingleSelectedPath(CropActivity.CROP_ACTION);
        ((Activity) mActivity).startActivity(intent);
        return;
    }
    case R.id.action_edit: {
        Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_EDIT)
                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        ((Activity) mActivity).startActivity(Intent.createChooser(intent, null));
        return;
    }
    case R.id.action_setas: {
        Intent intent = getIntentBySingleSelectedPath(Intent.ACTION_ATTACH_DATA)
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra("mimeType", intent.getType());
        Activity activity = mActivity;
        activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.set_as)));
        return;
    }
    case R.id.action_delete:
        title = R.string.delete;
        break;
    case R.id.action_rotate_cw:
        title = R.string.rotate_right;
        break;
    case R.id.action_rotate_ccw:
        title = R.string.rotate_left;
        break;
    case R.id.action_show_on_map:
        title = R.string.show_on_map;
        break;
    default:
        return;
    }
    startAction(action, title, listener, waitOnStop, showDialog);
}

From source file:com.github.michalbednarski.intentslab.editor.IntentGeneralFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    View v = inflater.inflate(R.layout.intent_editor_general, container, false);

    // Prepare form
    mActionText = (AutoCompleteTextView) v.findViewById(R.id.action);
    mActionsSpinner = (Spinner) v.findViewById(R.id.action_spinner);
    mDataText = (AutoCompleteTextView) v.findViewById(R.id.data);
    mDataTextWrapper = v.findViewById(R.id.data_wrapper);
    mDataTextHeader = v.findViewById(R.id.data_header);
    mDataTypeHeader = v.findViewById(R.id.data_type_header);
    mDataTypeText = (TextView) v.findViewById(R.id.data_type);
    mDataTypeSpinner = (Spinner) v.findViewById(R.id.data_type_spinner);
    mDataTypeSpinnerWrapper = v.findViewById(R.id.data_type_spinner_wrapper);
    mDataTypeSlash = v.findViewById(R.id.data_type_slash);
    mDataSubtypeText = (TextView) v.findViewById(R.id.data_type_after_slash);
    mComponentText = (TextView) v.findViewById(R.id.component);

    mComponentTypeSpinner = (Spinner) v.findViewById(R.id.componenttype);
    mComponentTypeSpinner.setAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.componenttypes)));

    mMethodSpinner = (Spinner) v.findViewById(R.id.method);

    mCategoriesContainer = (ViewGroup) v.findViewById(R.id.categories);
    mAddCategoryButton = (Button) v.findViewById(R.id.category_add);
    mCategoriesHeader = v.findViewById(R.id.categories_header);
    mResponseCodeTextView = (TextView) v.findViewById(R.id.response_code);
    mPackageNameText = (AutoCompleteTextView) v.findViewById(R.id.package_name);

    mResultCodeWrapper = v.findViewById(R.id.result_intent_wrapper);
    mComponentTypeAndMethodSpinners = v.findViewById(R.id.component_and_method_spinners);
    mComponentHeader = v.findViewById(R.id.component_header);
    mComponentFieldWithButtons = v.findViewById(R.id.component_field_with_buttons);
    mPackageNameHeader = v.findViewById(R.id.package_name_header);
    mIntentTrackerSummary = (TextView) v.findViewById(R.id.intent_tracker_summary);
    mIntentTrackerSummary.setMovementMethod(LinkMovementMethod.getInstance());

    // Apparently using android:scrollHorizontally="true" does not work.
    // http://stackoverflow.com/questions/9011944/android-ice-cream-sandwich-edittext-disabling-spell-check-and-word-wrap
    mComponentText.setHorizontallyScrolling(true);

    // Bind button actions (android:onClick="" applies to hosting activity)
    mAddCategoryButton.setOnClickListener(new OnClickListener() {
        @Override/*from   w w  w .jav a 2 s  .  co  m*/
        public void onClick(View v) {
            addCategoryTextField("");
        }
    });
    v.findViewById(R.id.component_pick).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            pickComponent();
        }
    });
    v.findViewById(R.id.component_pick).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            findComponent();
            return true;
        }
    });
    v.findViewById(R.id.component_clear).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mComponentText.setText("");
        }
    });
    v.findViewById(R.id.data_query_button).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            getIntentEditor().updateIntent();
            startActivity(
                    new Intent(getActivity(), AdvancedQueryActivity.class).setData(mEditedIntent.getData()));
        }
    });
    v.findViewById(R.id.data_query_button)
            .setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View v,
                        ContextMenu.ContextMenuInfo menuInfo) {
                    getIntentEditor().updateIntent();
                    final Uri uri = mEditedIntent.getData();
                    if (uri != null) {
                        final String scheme = uri.getScheme();
                        final String authority = uri.getAuthority();
                        if ("content".equals(scheme) && authority != null) {
                            menu.add("Wrap").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                                @Override
                                public boolean onMenuItemClick(MenuItem item) {
                                    mDataText.setText("content://" + ((mEditedIntent.getFlags()
                                            & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                                                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) != 0
                                                            ? ProxyProviderForGrantUriPermission.AUTHORITY
                                                            : ProxyProvider.AUTHORITY)
                                            + "/" + authority + uri.getPath());
                                    return true;
                                }
                            });
                        }
                    }
                }
            });

    // Set up autocomplete
    mUriAutocompleteAdapter = new UriAutocompleteAdapter(getActivity());
    mDataText.setAdapter(mUriAutocompleteAdapter);
    mPackageNameText.setAdapter(new PackageNameAutocompleteAdapter(getActivity()));

    // Get edited intent for form filling
    mEditedIntent = getEditedIntent();

    // Component field, affects options menu
    if (mEditedIntent.getComponent() != null) {
        mComponentText.setText(mEditedIntent.getComponent().flattenToShortString());
    }
    mComponentText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            updateIntentComponent();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    // Fill the form
    setupActionSpinnerOrField();
    updateNonActionIntentFilter(true);
    mDataText.setText(mEditedIntent.getDataString());
    mPackageNameText.setText(mEditedIntent.getPackage());

    showOrHideFieldsForResultIntent();
    showOrHideAdvancedFields();
    if (getComponentType() == IntentEditorConstants.RESULT) {
        mResponseCodeTextView.setText(String.valueOf(getIntentEditor().getMethodId()));
        mResponseCodeTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    getIntentEditor().setMethodId(Integer.parseInt(s.toString()));
                    mResponseCodeTextView.setError(null);
                } catch (NumberFormatException e) {
                    mResponseCodeTextView.setError(getIntentEditor().getText(R.string.value_parse_error));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    } else {
        initComponentAndMethodSpinners();
    }

    setupActionAutocomplete();

    // Prepare intent tracker
    {
        IntentTracker tracker = getIntentEditor().getIntentTracker();
        if (tracker != null) {
            tracker.setUpdateListener(this, true);
        } else {
            onNoTracker();
        }
    }

    return v;
}

From source file:com.doplgangr.secrecy.views.FileViewer.java

private Intent generateCustomChooserIntent(Intent prototype, ArrayList<Uri> uris) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
    Intent chooserIntent;//from   w w w  .ja  v  a  2  s.  c  om

    Intent dummy = new Intent(prototype.getAction());
    dummy.setType(prototype.getType());
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(dummy, 0);

    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            if (resolveInfo.activityInfo == null
                    || resolveInfo.activityInfo.packageName.equalsIgnoreCase("com.doplgangr.secrecy"))
                continue;

            HashMap<String, String> info = new HashMap<String, String>();
            info.put("packageName", resolveInfo.activityInfo.packageName);
            info.put("className", resolveInfo.activityInfo.name);
            info.put("simpleName",
                    String.valueOf(resolveInfo.activityInfo.loadLabel(context.getPackageManager())));
            intentMetaInfo.add(info);
            for (Uri uri : uris)
                context.grantUriPermission(resolveInfo.activityInfo.packageName, uri,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        if (!intentMetaInfo.isEmpty()) {
            // sorting for nice readability
            Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() {
                @Override
                public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
                    return map.get("simpleName").compareTo(map2.get("simpleName"));
                }
            });

            // create the custom intent list
            for (HashMap<String, String> metaInfo : intentMetaInfo) {
                Intent targetedShareIntent = (Intent) prototype.clone();
                targetedShareIntent.setPackage(metaInfo.get("packageName"));
                targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className"));
                targetedShareIntents.add(targetedShareIntent);
            }
            chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1),
                    CustomApp.context.getString(R.string.Dialog__send_file));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
            return chooserIntent;
        }
    }

    return new Intent(Intent.ACTION_SEND); //Unable to do anything. Duh.
}

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * Open a View intent for given file//from   w  w  w .  ja v a  2  s .  c  om
 *
 * @param file The file to share
 */
public boolean viewFileInOtherApp(File file, @Nullable String type) {
    // On some specific devices the first won't work
    Uri fileUri = null;
    try {
        fileUri = FileProvider.getUriForFile(_context, getFileProviderAuthority(), file);
    } catch (Exception ignored) {
        try {
            fileUri = Uri.fromFile(file);
        } catch (Exception ignored2) {
        }
    }

    if (fileUri != null) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra(Intent.EXTRA_STREAM, fileUri);
        intent.setData(fileUri);
        intent.putExtra(EXTRA_FILEPATH, file.getAbsolutePath());
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(fileUri, type);
        showChooser(intent, null);
        return true;
    }
    return false;
}

From source file:rosmi.acagild.alarmclock.ringing.ShareFragment.java

public void share() {
    Loggable.UserAction userAction = new Loggable.UserAction(Loggable.Key.ACTION_SHARE);
    Logger.track(userAction);/*from www .  j  av  a  2 s  .  co  m*/

    mCallback.onRequestLaunchShareAction();

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    // this causes issues with gmail
    //shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    File file = new File(mShareableUri);
    Uri uri = Uri.fromFile(file);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject_template));
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    startActivityForResult(
            Intent.createChooser(shareIntent, getResources().getString(R.string.share_action_description)),
            SHARE_REQUEST_CODE);
}

From source file:com.microsoft.mimickeralarm.ringing.ShareFragment.java

public void share() {
    Loggable.UserAction userAction = new Loggable.UserAction(Loggable.Key.ACTION_SHARE);
    Logger.track(userAction);/*from  w ww.j ava  2s. c  o m*/

    mCallback.onRequestLaunchShareAction();

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    // this causes issues with gmail
    //shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    File file = new File(mShareableUri);
    Uri uri = Uri.fromFile(file);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.share_text_template));
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject_template));
    shareIntent.setType("image/*");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    startActivityForResult(
            Intent.createChooser(shareIntent, getResources().getString(R.string.share_action_description)),
            SHARE_REQUEST_CODE);
}

From source file:com.jefftharris.passwdsafe.StorageFileListFragment.java

/** Open a password file URI from an intent */
private void openUri(Intent openIntent) {
    Uri uri = openIntent.getData();//  w w  w .  ja v  a  2  s . c om
    int flags = openIntent.getFlags()
            & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    Context ctx = getContext();
    String title = RecentFilesDb.getSafDisplayName(uri, ctx);
    RecentFilesDb.updateOpenedSafFile(uri, flags, ctx);
    if (title != null) {
        openUri(uri, title);
    }
}

From source file:org.gnucash.android.ui.common.BaseDrawerActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_CANCELED) {
        super.onActivityResult(requestCode, resultCode, data);
        return;/*from  w  w  w. j  av  a2  s  .com*/
    }

    switch (requestCode) {
    case AccountsActivity.REQUEST_PICK_ACCOUNTS_FILE:
        AccountsActivity.importXmlFileFromIntent(this, data, null);
        break;
    case BaseDrawerActivity.REQUEST_OPEN_DOCUMENT: //this uses the Storage Access Framework
        final int takeFlags = data.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        AccountsActivity.importXmlFileFromIntent(this, data, null);
        getContentResolver().takePersistableUriPermission(data.getData(), takeFlags);
        break;
    default:
        super.onActivityResult(requestCode, resultCode, data);
        break;
    }
}

From source file:io.nuclei.cyto.share.PackageTargetManager.java

/**
 * Set default intent data// ww w. j a va  2s  .com
 */
protected void onSetDefault(Context context, String packageName, String authority, Intent intent, String text) {
    intent.putExtra(Intent.EXTRA_TEXT, text);
    if (!TextUtils.isEmpty(mSubject))
        intent.putExtra(Intent.EXTRA_SUBJECT, mSubject);
    if (mUri != null || mFile != null) {
        Uri uri = mUri;
        String type = "*/*";
        if (mFile != null) {
            uri = FileProvider.getUriForFile(context, authority, mFile);
            final int lastDot = mFile.getName().lastIndexOf('.');
            if (lastDot >= 0) {
                String extension = mFile.getName().substring(lastDot + 1);
                String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                if (mimeType != null)
                    type = mimeType;
            }
        }
        intent.setDataAndType(intent.getData(), type);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        if (packageName != null) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    } else {
        intent.setType("text/plain");
    }
    if (!TextUtils.isEmpty(mEmail)) {
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { mEmail });
        intent.setData(Uri.parse("mailto:"));
    }
}

From source file:org.rm3l.ddwrt.tiles.status.wireless.WirelessIfaceQrCodeActivity.java

private void setShareFile(File file) {
    if (mShareActionProvider == null) {
        return;//from w  w  w. j  ava2s . com
    }

    final Uri uriForFile = FileProvider.getUriForFile(this, "org.rm3l.fileprovider", file);

    mShareActionProvider
            .setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
                @Override
                public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
                    grantUriPermission(intent.getComponent().getPackageName(), uriForFile,
                            Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    return true;
                }
            });

    final Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uriForFile);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, String.format("QR Code for Wireless Network '%s'", mSsid));

    sendIntent.setData(uriForFile);
    sendIntent.setType("image/png");
    sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    setShareIntent(sendIntent);

}