# IntentSanitizer

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/content/IntentSanitizer.java+class:androidx.core.content.IntentSanitizer) Added in [1.9.0](https://developer.android.com/jetpack/androidx/releases/core#1.9.0)

*** ** * ** ***


```
public class IntentSanitizer
```

<br />

*** ** * ** ***

This class is used to make a sanitized copy of an `https://developer.android.com/reference/android/content/Intent.html`. This could be used when `https://developer.android.com/reference/android/os/strictmode/UnsafeIntentLaunchViolation.html` is detected. This class is thread safe and the object created is safe to be reused. Typical usage of the class:

```
Intent intent = new  IntentSanitizer.Builder()
     .allowComponent(“com.example.ActivityA”)
     .allowData(“com.example”)
     .allowType(“text/plain”)
     .build()
     .sanitizeByThrowing(intent);
```
At least one of the allowPackage, allowComponent must be called unless implicit intent is allowed. In which case, allowAnyComponent must be called and caution has to be taken to protect your private data.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/core/content/IntentSanitizer.Builder` General strategy of building is to only offer additive "or" operations that are chained together. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/content/IntentSanitizer#sanitize(android.content.Intent,androidx.core.util.Consumer<java.lang.String>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html in, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/util/Consumer<https://developer.android.com/reference/java/lang/String.html> penalty)` This method sanitizes the given intent. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/content/IntentSanitizer#sanitizeByFiltering(android.content.Intent)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html in)` Convenient method for filtering unwanted members from the input intent and log it. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/core/content/IntentSanitizer#sanitizeByThrowing(android.content.Intent)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html in)` Convenient method for throwing a SecurityException when unwanted members of the input intent is encountered. |

## Public methods

### sanitize

Added in [1.9.0](https://developer.android.com/jetpack/androidx/releases/core#1.9.0)

```
public @NonNull Intent sanitize(@NonNull Intent in, @NonNull Consumer<String> penalty)
```

This method sanitizes the given intent. If dirty members are found, the errors are consumed by the penalty object. The penalty action could be called multiple times if multiple issues exist.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html in` | the given intent. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/util/Consumer<https://developer.android.com/reference/java/lang/String.html> penalty` | consumer of the error message if dirty members are found. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html` | a sanitized copy of the given intent. |

### sanitizeByFiltering

Added in [1.9.0](https://developer.android.com/jetpack/androidx/releases/core#1.9.0)

```
public @NonNull Intent sanitizeByFiltering(@NonNull Intent in)
```

Convenient method for filtering unwanted members from the input intent and log it.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html in` | input intent |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html` | a copy of the input intent after filtering out unwanted members. |

### sanitizeByThrowing

Added in [1.9.0](https://developer.android.com/jetpack/androidx/releases/core#1.9.0)

```
public @NonNull Intent sanitizeByThrowing(@NonNull Intent in)
```

Convenient method for throwing a SecurityException when unwanted members of the input intent is encountered.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html in` | input intent |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Intent.html` | a copy of the input intent if the input intent does not contain any unwanted members. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException.html` | if the input intent contains any unwanted members. |