mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Replace bitmap overlay uri EditText with local file picker
PiperOrigin-RevId: 515283537
This commit is contained in:
parent
2f01f9c53b
commit
2c852ea7bb
@ -206,7 +206,9 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
private static final String SAME_AS_INPUT_OPTION = "same as input";
|
||||
private static final float HALF_DIAGONAL = 1f / (float) Math.sqrt(2);
|
||||
|
||||
private @MonotonicNonNull ActivityResultLauncher<Intent> localFilePickerLauncher;
|
||||
private @MonotonicNonNull Runnable onPermissionsGranted;
|
||||
private @MonotonicNonNull ActivityResultLauncher<Intent> videoLocalFilePickerLauncher;
|
||||
private @MonotonicNonNull ActivityResultLauncher<Intent> overlayLocalFilePickerLauncher;
|
||||
private @MonotonicNonNull Button selectPresetFileButton;
|
||||
private @MonotonicNonNull Button selectLocalFileButton;
|
||||
private @MonotonicNonNull TextView selectedFileTextView;
|
||||
@ -257,11 +259,23 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
|
||||
findViewById(R.id.export_button).setOnClickListener(this::startExport);
|
||||
|
||||
videoLocalFilePickerLauncher =
|
||||
registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
this::videoLocalFilePickerLauncherResult);
|
||||
overlayLocalFilePickerLauncher =
|
||||
registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
this::overlayLocalFilePickerLauncherResult);
|
||||
|
||||
selectPresetFileButton = findViewById(R.id.select_preset_file_button);
|
||||
selectPresetFileButton.setOnClickListener(this::selectPresetFile);
|
||||
|
||||
selectLocalFileButton = findViewById(R.id.select_local_file_button);
|
||||
selectLocalFileButton.setOnClickListener(this::selectLocalFile);
|
||||
selectLocalFileButton.setOnClickListener(
|
||||
view ->
|
||||
selectLocalFile(
|
||||
view, checkNotNull(videoLocalFilePickerLauncher), /* mimeType= */ "video/*"));
|
||||
|
||||
selectedFileTextView = findViewById(R.id.selected_file_text_view);
|
||||
selectedFileTextView.setText(PRESET_FILE_URI_DESCRIPTIONS[inputUriPosition]);
|
||||
@ -341,11 +355,6 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
videoEffectsSelections = new boolean[VIDEO_EFFECTS.length];
|
||||
selectVideoEffectsButton = findViewById(R.id.select_video_effects_button);
|
||||
selectVideoEffectsButton.setOnClickListener(this::selectVideoEffects);
|
||||
|
||||
localFilePickerLauncher =
|
||||
registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
this::localFilePickerLauncherResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -356,7 +365,7 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
if (requestCode == FILE_PERMISSION_REQUEST_CODE
|
||||
&& grantResults.length == 1
|
||||
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
launchLocalFilePicker();
|
||||
checkNotNull(onPermissionsGranted).run();
|
||||
} else {
|
||||
Toast.makeText(
|
||||
getApplicationContext(), getString(R.string.permission_denied), Toast.LENGTH_LONG)
|
||||
@ -489,29 +498,51 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
selectedFileTextView.setText(PRESET_FILE_URI_DESCRIPTIONS[inputUriPosition]);
|
||||
}
|
||||
|
||||
private void selectLocalFile(View view) {
|
||||
private void selectLocalFile(
|
||||
View view, ActivityResultLauncher<Intent> localFilePickerLauncher, String mimeType) {
|
||||
String permission = SDK_INT >= 33 ? READ_MEDIA_VIDEO : READ_EXTERNAL_STORAGE;
|
||||
if (ActivityCompat.checkSelfPermission(/* context= */ this, permission)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
onPermissionsGranted = () -> launchLocalFilePicker(localFilePickerLauncher, mimeType);
|
||||
ActivityCompat.requestPermissions(
|
||||
/* activity= */ this, new String[] {permission}, FILE_PERMISSION_REQUEST_CODE);
|
||||
} else {
|
||||
launchLocalFilePicker();
|
||||
launchLocalFilePicker(localFilePickerLauncher, mimeType);
|
||||
}
|
||||
}
|
||||
|
||||
private void launchLocalFilePicker() {
|
||||
private void launchLocalFilePicker(
|
||||
ActivityResultLauncher<Intent> localFilePickerLauncher, String mimeType) {
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.setType("video/*");
|
||||
intent.setType(mimeType);
|
||||
checkNotNull(localFilePickerLauncher).launch(intent);
|
||||
}
|
||||
|
||||
@RequiresNonNull("selectedFileTextView")
|
||||
private void localFilePickerLauncherResult(ActivityResult result) {
|
||||
private void videoLocalFilePickerLauncherResult(ActivityResult result) {
|
||||
Intent data = result.getData();
|
||||
if (data != null) {
|
||||
localFileUri = checkNotNull(data.getData());
|
||||
selectedFileTextView.setText(localFileUri.toString());
|
||||
} else {
|
||||
Toast.makeText(
|
||||
getApplicationContext(),
|
||||
getString(R.string.local_file_picker_failed),
|
||||
Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
||||
private void overlayLocalFilePickerLauncherResult(ActivityResult result) {
|
||||
Intent data = result.getData();
|
||||
if (data != null) {
|
||||
bitmapOverlayUri = checkNotNull(data.getData()).toString();
|
||||
} else {
|
||||
Toast.makeText(
|
||||
getApplicationContext(),
|
||||
getString(R.string.local_file_picker_failed),
|
||||
Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
||||
@ -695,7 +726,11 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
private void controlBitmapOverlaySettings() {
|
||||
View dialogView =
|
||||
getLayoutInflater().inflate(R.layout.bitmap_overlay_options, /* root= */ null);
|
||||
EditText uriEditText = checkNotNull(dialogView.findViewById(R.id.bitmap_overlay_uri));
|
||||
Button uriButton = checkNotNull(dialogView.findViewById(R.id.bitmap_overlay_uri));
|
||||
uriButton.setOnClickListener(
|
||||
(view ->
|
||||
selectLocalFile(
|
||||
view, checkNotNull(overlayLocalFilePickerLauncher), /* mimeType= */ "image/*")));
|
||||
Slider alphaSlider = checkNotNull(dialogView.findViewById(R.id.bitmap_overlay_alpha_slider));
|
||||
new AlertDialog.Builder(/* context= */ this)
|
||||
.setTitle(R.string.bitmap_overlay_settings)
|
||||
@ -703,7 +738,6 @@ public final class ConfigurationActivity extends AppCompatActivity {
|
||||
.setPositiveButton(
|
||||
android.R.string.ok,
|
||||
(DialogInterface dialogInterface, int i) -> {
|
||||
bitmapOverlayUri = uriEditText.getText().toString();
|
||||
bitmapOverlayAlpha = alphaSlider.getValue();
|
||||
})
|
||||
.create()
|
||||
|
@ -38,9 +38,9 @@
|
||||
android:gravity="center_vertical" >
|
||||
<TextView
|
||||
android:text="@string/overlay_uri" />
|
||||
<EditText
|
||||
<Button
|
||||
android:id="@+id/bitmap_overlay_uri"
|
||||
android:inputType="textUri|textMultiLine"
|
||||
android:text="@string/select_local_image"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content" />
|
||||
</TableRow>
|
||||
|
@ -19,6 +19,7 @@
|
||||
<string name="configuration" translatable="false">Configuration</string>
|
||||
<string name="select_preset_file_title" translatable="false">Choose preset file</string>
|
||||
<string name="select_local_file_title">Choose local file</string>
|
||||
<string name="local_file_picker_failed">File couldn\'t be opened. Please try again.</string>
|
||||
<string name="remove_audio" translatable="false">Remove audio</string>
|
||||
<string name="remove_video" translatable="false">Remove video</string>
|
||||
<string name="flatten_for_slow_motion" translatable="false">Flatten for slow motion</string>
|
||||
@ -71,6 +72,7 @@
|
||||
<string name="overlay_alpha">Alpha</string>
|
||||
<string name="overlay_uri">Uri</string>
|
||||
<string name="bitmap_overlay_settings">Specify bitmap overlay settings</string>
|
||||
<string name="select_local_image">Select local image</string>
|
||||
<string name="overlay_text">Text</string>
|
||||
<string name="overlay_text_color">Text color</string>
|
||||
<string name="text_overlay_settings">Specify text overlay settings</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user