Skip to content

Commit ac2871b

Browse files
committed
Add unstyled button for uploading a custom screenshot (#4550)
* Add unstyled button for uploading a custom screenshot * Bump compileSdkVersion to 31 * Use plain Activity Results API to avoid having to raise compile/target SDK version
1 parent 68261a0 commit ac2871b

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FeedbackActivity.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static android.view.View.GONE;
1818
import static android.view.View.VISIBLE;
1919

20+
import android.app.Activity;
21+
import android.content.Intent;
2022
import android.graphics.Bitmap;
2123
import android.net.Uri;
2224
import android.os.Bundle;
@@ -27,6 +29,9 @@
2729
import android.widget.ImageView;
2830
import android.widget.TextView;
2931
import android.widget.Toast;
32+
import androidx.activity.result.ActivityResult;
33+
import androidx.activity.result.ActivityResultLauncher;
34+
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
3035
import androidx.annotation.NonNull;
3136
import androidx.annotation.Nullable;
3237
import androidx.appcompat.app.AppCompatActivity;
@@ -49,6 +54,9 @@ public class FeedbackActivity extends AppCompatActivity {
4954
public static final String SCREENSHOT_URI_KEY =
5055
"com.google.firebase.appdistribution.FeedbackActivity.SCREENSHOT_URI";
5156

57+
private final ActivityResultLauncher<Intent> chooseScreenshotLauncher =
58+
registerForActivityResult(new StartActivityForResult(), this::handleChooseScreenshotResult);
59+
5260
@Inject FeedbackSender feedbackSender;
5361
@Inject @Blocking Executor blockingExecutor;
5462
@Inject @UiThread Executor uiThreadExecutor;
@@ -78,6 +86,7 @@ protected void onCreate(Bundle savedInstanceState) {
7886
screenshotUri = Uri.parse(getIntent().getStringExtra(SCREENSHOT_URI_KEY));
7987
}
8088
}
89+
8190
setupView();
8291
}
8392

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

112+
findViewById(R.id.chooseScreenshotButton)
113+
.setOnClickListener(
114+
v -> {
115+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
116+
intent.addCategory(Intent.CATEGORY_OPENABLE);
117+
intent.setType("image/png");
118+
chooseScreenshotLauncher.launch(intent);
119+
});
120+
103121
setupScreenshot();
104122
}
105123

106124
private void setupScreenshot() {
107125
blockingExecutor.execute(
108126
() -> {
109-
// do I/O on separate thread in order to not block the UI
110-
Bitmap screenshot = screenshotUri == null ? null : readScreenshot();
127+
// Do I/O on separate thread in order to not block the UI
128+
Bitmap screenshot = readScreenshot(screenshotUri);
111129
if (screenshot != null) {
112130
runOnUiThread(
113131
() -> {
114132
ImageView imageView = findViewById(R.id.screenshotImageView);
115133
imageView.setImageBitmap(screenshot);
134+
imageView.setVisibility(VISIBLE);
116135
CheckBox checkBox = findViewById(R.id.screenshotCheckBox);
117136
checkBox.setChecked(true);
118137
checkBox.setOnClickListener(
@@ -131,22 +150,35 @@ private void setupScreenshot() {
131150
});
132151
}
133152

153+
private void handleChooseScreenshotResult(ActivityResult activityResult) {
154+
int resultCode = activityResult.getResultCode();
155+
Intent intent = activityResult.getData();
156+
if (resultCode == Activity.RESULT_OK && intent != null && intent.getData() != null) {
157+
Uri uri = intent.getData();
158+
LogWrapper.d(TAG, "Selected custom screenshot URI: " + uri);
159+
screenshotUri = uri;
160+
setupScreenshot();
161+
} else {
162+
LogWrapper.d(TAG, "No custom screenshot selected. Not changing screenshot URI.");
163+
}
164+
}
165+
134166
@Nullable
135-
private Bitmap readScreenshot() {
167+
private Bitmap readScreenshot(@Nullable Uri uri) {
168+
if (uri == null) {
169+
return null;
170+
}
136171
Bitmap bitmap;
137172
try {
138173
bitmap =
139174
ImageUtils.readScaledImage(
140-
getContentResolver(),
141-
screenshotUri,
142-
SCREENSHOT_TARGET_WIDTH_PX,
143-
SCREENSHOT_TARGET_HEIGHT_PX);
175+
getContentResolver(), uri, SCREENSHOT_TARGET_WIDTH_PX, SCREENSHOT_TARGET_HEIGHT_PX);
144176
} catch (IOException | SecurityException e) {
145-
LogWrapper.e(TAG, "Could not read screenshot image from URI: " + screenshotUri, e);
177+
LogWrapper.e(TAG, "Could not read screenshot image from URI: " + uri, e);
146178
return null;
147179
}
148180
if (bitmap == null) {
149-
LogWrapper.e(TAG, "Could not decode screenshot image: " + screenshotUri);
181+
LogWrapper.e(TAG, "Could not decode screenshot image: " + uri);
150182
}
151183
return bitmap;
152184
}

firebase-appdistribution/src/main/res/layout/activity_feedback.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@
113113
android:scaleType="centerInside"
114114
android:adjustViewBounds="true"
115115
android:contentDescription="@string/screenshot_image_description" />
116+
117+
<Button
118+
android:id="@+id/chooseScreenshotButton"
119+
android:layout_width="wrap_content"
120+
android:layout_height="wrap_content"
121+
android:text="Gallery" />
116122
</LinearLayout>
117123
</LinearLayout>
118124
</ScrollView>

firebase-appdistribution/test-app/test-app.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ android {
2525
applicationId "com.googletest.firebase.appdistribution.testapp"
2626
minSdkVersion 23
2727
targetSdkVersion 33
28-
versionName "3.1"
29-
versionCode 6
28+
versionName "3.2"
29+
versionCode 7
3030

3131
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3232
}

0 commit comments

Comments
 (0)