Skip to content

Commit 1ba745d

Browse files
Use Intent.createChooser()
1 parent c1bdffd commit 1ba745d

1 file changed

Lines changed: 6 additions & 34 deletions

File tree

  • app/src/main/java/org/schabi/newpipe/util/external_communication

app/src/main/java/org/schabi/newpipe/util/external_communication/ShareUtils.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,11 @@ public static void openIntentInApp(@NonNull final Context context,
174174
/**
175175
* Open the system chooser to launch an intent.
176176
* <p>
177-
* This method opens an {@link android.content.Intent#ACTION_CHOOSER} of the intent putted
178-
* as the intent param. If the setTitleChooser boolean is true, the string "Open with" will be
179-
* set as the title of the system chooser.
177+
* This method creates a chooser intent using
178+
* {@link Intent#createChooser(Intent, CharSequence)}. If the setTitleChooser boolean is true,
179+
* the string "Open with" will be set as the title of the system chooser.
180180
* For Android P and higher, title for {@link android.content.Intent#ACTION_SEND} system
181-
* choosers must be set on this intent, not on the
182-
* {@link android.content.Intent#ACTION_CHOOSER} intent.
181+
* choosers must be set on this intent, not on the {@link Intent#ACTION_CHOOSER} intent.
183182
*
184183
* @param context the context to use
185184
* @param intent the intent to open
@@ -188,35 +187,8 @@ public static void openIntentInApp(@NonNull final Context context,
188187
private static void openAppChooser(@NonNull final Context context,
189188
@NonNull final Intent intent,
190189
final boolean setTitleChooser) {
191-
final Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
192-
chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
193-
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
194-
if (setTitleChooser) {
195-
chooserIntent.putExtra(Intent.EXTRA_TITLE, context.getString(R.string.open_with));
196-
}
197-
198-
// Migrate any clip data and flags from the original intent.
199-
final int permFlags = intent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION
200-
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
201-
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
202-
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
203-
if (permFlags != 0) {
204-
ClipData targetClipData = intent.getClipData();
205-
if (targetClipData == null && intent.getData() != null) {
206-
final ClipData.Item item = new ClipData.Item(intent.getData());
207-
final String[] mimeTypes;
208-
if (intent.getType() != null) {
209-
mimeTypes = new String[] {intent.getType()};
210-
} else {
211-
mimeTypes = new String[] {};
212-
}
213-
targetClipData = new ClipData(null, mimeTypes, item);
214-
}
215-
if (targetClipData != null) {
216-
chooserIntent.setClipData(targetClipData);
217-
chooserIntent.addFlags(permFlags);
218-
}
219-
}
190+
final String title = setTitleChooser ? context.getString(R.string.open_with) : null;
191+
final Intent chooserIntent = Intent.createChooser(intent, title);
220192

221193
try {
222194
context.startActivity(chooserIntent);

0 commit comments

Comments
 (0)