Skip to content

Add unstyled button for uploading a custom screenshot #4550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -27,6 +29,9 @@
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -49,6 +54,9 @@ public class FeedbackActivity extends AppCompatActivity {
public static final String SCREENSHOT_URI_KEY =
"com.google.firebase.appdistribution.FeedbackActivity.SCREENSHOT_URI";

private final ActivityResultLauncher<Intent> chooseScreenshotLauncher =
registerForActivityResult(new StartActivityForResult(), this::handleChooseScreenshotResult);

@Inject FeedbackSender feedbackSender;
@Inject @Blocking Executor blockingExecutor;
@Inject @UiThread Executor uiThreadExecutor;
Expand Down Expand Up @@ -78,6 +86,7 @@ protected void onCreate(Bundle savedInstanceState) {
screenshotUri = Uri.parse(getIntent().getStringExtra(SCREENSHOT_URI_KEY));
}
}

setupView();
}

Expand All @@ -100,19 +109,29 @@ private void setupView() {
findViewById(R.id.backButton).setOnClickListener(v -> finish());
findViewById(R.id.sendButton).setOnClickListener(this::submitFeedback);

findViewById(R.id.chooseScreenshotButton)
.setOnClickListener(
v -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/png");
chooseScreenshotLauncher.launch(intent);
});

setupScreenshot();
}

private void setupScreenshot() {
blockingExecutor.execute(
() -> {
// do I/O on separate thread in order to not block the UI
Bitmap screenshot = screenshotUri == null ? null : readScreenshot();
// Do I/O on separate thread in order to not block the UI
Bitmap screenshot = readScreenshot(screenshotUri);
Copy link
Contributor Author

@lfkellogg lfkellogg Jan 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that screenshotUri could technically be modified on another thread I don't want to rely on its value being the same for the entirety of the execution of readScreenshot()

if (screenshot != null) {
runOnUiThread(
() -> {
ImageView imageView = findViewById(R.id.screenshotImageView);
imageView.setImageBitmap(screenshot);
imageView.setVisibility(VISIBLE);
CheckBox checkBox = findViewById(R.id.screenshotCheckBox);
checkBox.setChecked(true);
checkBox.setOnClickListener(
Expand All @@ -131,22 +150,35 @@ private void setupScreenshot() {
});
}

private void handleChooseScreenshotResult(ActivityResult activityResult) {
int resultCode = activityResult.getResultCode();
Intent intent = activityResult.getData();
if (resultCode == Activity.RESULT_OK && intent != null && intent.getData() != null) {
Uri uri = intent.getData();
LogWrapper.d(TAG, "Selected custom screenshot URI: " + uri);
screenshotUri = uri;
setupScreenshot();
} else {
LogWrapper.d(TAG, "No custom screenshot selected. Not changing screenshot URI.");
}
}

@Nullable
private Bitmap readScreenshot() {
private Bitmap readScreenshot(@Nullable Uri uri) {
if (uri == null) {
return null;
}
Bitmap bitmap;
try {
bitmap =
ImageUtils.readScaledImage(
getContentResolver(),
screenshotUri,
SCREENSHOT_TARGET_WIDTH_PX,
SCREENSHOT_TARGET_HEIGHT_PX);
getContentResolver(), uri, SCREENSHOT_TARGET_WIDTH_PX, SCREENSHOT_TARGET_HEIGHT_PX);
} catch (IOException | SecurityException e) {
LogWrapper.e(TAG, "Could not read screenshot image from URI: " + screenshotUri, e);
LogWrapper.e(TAG, "Could not read screenshot image from URI: " + uri, e);
return null;
}
if (bitmap == null) {
LogWrapper.e(TAG, "Could not decode screenshot image: " + screenshotUri);
LogWrapper.e(TAG, "Could not decode screenshot image: " + uri);
}
return bitmap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:contentDescription="@string/screenshot_image_description" />

<Button
android:id="@+id/chooseScreenshotButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Expand Down
4 changes: 2 additions & 2 deletions firebase-appdistribution/test-app/test-app.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
applicationId "com.googletest.firebase.appdistribution.testapp"
minSdkVersion 23
targetSdkVersion 33
versionName "3.1"
versionCode 6
versionName "3.2"
versionCode 7

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down