17
17
import static android .view .View .GONE ;
18
18
import static android .view .View .VISIBLE ;
19
19
20
+ import android .app .Activity ;
21
+ import android .content .Intent ;
20
22
import android .graphics .Bitmap ;
21
23
import android .net .Uri ;
22
24
import android .os .Bundle ;
27
29
import android .widget .ImageView ;
28
30
import android .widget .TextView ;
29
31
import android .widget .Toast ;
32
+ import androidx .activity .result .ActivityResult ;
33
+ import androidx .activity .result .ActivityResultLauncher ;
34
+ import androidx .activity .result .contract .ActivityResultContracts .StartActivityForResult ;
30
35
import androidx .annotation .NonNull ;
31
36
import androidx .annotation .Nullable ;
32
37
import androidx .appcompat .app .AppCompatActivity ;
@@ -49,6 +54,9 @@ public class FeedbackActivity extends AppCompatActivity {
49
54
public static final String SCREENSHOT_URI_KEY =
50
55
"com.google.firebase.appdistribution.FeedbackActivity.SCREENSHOT_URI" ;
51
56
57
+ private final ActivityResultLauncher <Intent > chooseScreenshotLauncher =
58
+ registerForActivityResult (new StartActivityForResult (), this ::handleChooseScreenshotResult );
59
+
52
60
@ Inject FeedbackSender feedbackSender ;
53
61
@ Inject @ Blocking Executor blockingExecutor ;
54
62
@ Inject @ UiThread Executor uiThreadExecutor ;
@@ -78,6 +86,7 @@ protected void onCreate(Bundle savedInstanceState) {
78
86
screenshotUri = Uri .parse (getIntent ().getStringExtra (SCREENSHOT_URI_KEY ));
79
87
}
80
88
}
89
+
81
90
setupView ();
82
91
}
83
92
@@ -100,19 +109,29 @@ private void setupView() {
100
109
findViewById (R .id .backButton ).setOnClickListener (v -> finish ());
101
110
findViewById (R .id .sendButton ).setOnClickListener (this ::submitFeedback );
102
111
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
+
103
121
setupScreenshot ();
104
122
}
105
123
106
124
private void setupScreenshot () {
107
125
blockingExecutor .execute (
108
126
() -> {
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 );
111
129
if (screenshot != null ) {
112
130
runOnUiThread (
113
131
() -> {
114
132
ImageView imageView = findViewById (R .id .screenshotImageView );
115
133
imageView .setImageBitmap (screenshot );
134
+ imageView .setVisibility (VISIBLE );
116
135
CheckBox checkBox = findViewById (R .id .screenshotCheckBox );
117
136
checkBox .setChecked (true );
118
137
checkBox .setOnClickListener (
@@ -131,22 +150,35 @@ private void setupScreenshot() {
131
150
});
132
151
}
133
152
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
+
134
166
@ Nullable
135
- private Bitmap readScreenshot () {
167
+ private Bitmap readScreenshot (@ Nullable Uri uri ) {
168
+ if (uri == null ) {
169
+ return null ;
170
+ }
136
171
Bitmap bitmap ;
137
172
try {
138
173
bitmap =
139
174
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 );
144
176
} 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 );
146
178
return null ;
147
179
}
148
180
if (bitmap == null ) {
149
- LogWrapper .e (TAG , "Could not decode screenshot image: " + screenshotUri );
181
+ LogWrapper .e (TAG , "Could not decode screenshot image: " + uri );
150
182
}
151
183
return bitmap ;
152
184
}
0 commit comments