7
7
import android .content .Context ;
8
8
import android .content .Intent ;
9
9
import android .widget .Toast ;
10
+ import androidx .activity .result .ActivityResult ;
10
11
import androidx .activity .result .ActivityResultLauncher ;
11
12
import androidx .annotation .NonNull ;
12
13
import androidx .lifecycle .LiveData ;
@@ -64,23 +65,24 @@ public ContributionController(@Named("default_preferences") JsonKvStore defaultK
64
65
* Check for permissions and initiate camera click
65
66
*/
66
67
public void initiateCameraPick (Activity activity ,
67
- ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ) {
68
+ ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ,
69
+ ActivityResultLauncher <Intent > resultLauncher ) {
68
70
boolean useExtStorage = defaultKvStore .getBoolean ("useExternalStorage" , true );
69
71
if (!useExtStorage ) {
70
- initiateCameraUpload (activity );
72
+ initiateCameraUpload (activity , resultLauncher );
71
73
return ;
72
74
}
73
75
74
76
PermissionUtils .checkPermissionsAndPerformAction (activity ,
75
77
() -> {
76
78
if (defaultKvStore .getBoolean ("inAppCameraFirstRun" )) {
77
79
defaultKvStore .putBoolean ("inAppCameraFirstRun" , false );
78
- askUserToAllowLocationAccess (activity , inAppCameraLocationPermissionLauncher );
80
+ askUserToAllowLocationAccess (activity , inAppCameraLocationPermissionLauncher , resultLauncher );
79
81
} else if (defaultKvStore .getBoolean ("inAppCameraLocationPref" )) {
80
82
createDialogsAndHandleLocationPermissions (activity ,
81
- inAppCameraLocationPermissionLauncher );
83
+ inAppCameraLocationPermissionLauncher , resultLauncher );
82
84
} else {
83
- initiateCameraUpload (activity );
85
+ initiateCameraUpload (activity , resultLauncher );
84
86
}
85
87
},
86
88
R .string .storage_permission_title ,
@@ -94,7 +96,8 @@ public void initiateCameraPick(Activity activity,
94
96
* @param activity
95
97
*/
96
98
private void createDialogsAndHandleLocationPermissions (Activity activity ,
97
- ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ) {
99
+ ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ,
100
+ ActivityResultLauncher <Intent > resultLauncher ) {
98
101
locationPermissionCallback = new LocationPermissionCallback () {
99
102
@ Override
100
103
public void onLocationPermissionDenied (String toastMessage ) {
@@ -103,16 +106,16 @@ public void onLocationPermissionDenied(String toastMessage) {
103
106
toastMessage ,
104
107
Toast .LENGTH_LONG
105
108
).show ();
106
- initiateCameraUpload (activity );
109
+ initiateCameraUpload (activity , resultLauncher );
107
110
}
108
111
109
112
@ Override
110
113
public void onLocationPermissionGranted () {
111
114
if (!locationPermissionsHelper .isLocationAccessToAppsTurnedOn ()) {
112
115
showLocationOffDialog (activity , R .string .in_app_camera_needs_location ,
113
- R .string .in_app_camera_location_unavailable );
116
+ R .string .in_app_camera_location_unavailable , resultLauncher );
114
117
} else {
115
- initiateCameraUpload (activity );
118
+ initiateCameraUpload (activity , resultLauncher );
116
119
}
117
120
}
118
121
};
@@ -135,9 +138,10 @@ public void onLocationPermissionGranted() {
135
138
* @param activity Activity reference
136
139
* @param dialogTextResource Resource id of text to be shown in dialog
137
140
* @param toastTextResource Resource id of text to be shown in toast
141
+ * @param resultLauncher
138
142
*/
139
143
private void showLocationOffDialog (Activity activity , int dialogTextResource ,
140
- int toastTextResource ) {
144
+ int toastTextResource , ActivityResultLauncher < Intent > resultLauncher ) {
141
145
DialogUtil
142
146
.showAlertDialog (activity ,
143
147
activity .getString (R .string .ask_to_turn_location_on ),
@@ -148,20 +152,21 @@ private void showLocationOffDialog(Activity activity, int dialogTextResource,
148
152
() -> {
149
153
Toast .makeText (activity , activity .getString (toastTextResource ),
150
154
Toast .LENGTH_LONG ).show ();
151
- initiateCameraUpload (activity );
155
+ initiateCameraUpload (activity , resultLauncher );
152
156
}
153
157
);
154
158
}
155
159
156
160
public void handleShowRationaleFlowCameraLocation (Activity activity ,
157
- ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ) {
161
+ ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ,
162
+ ActivityResultLauncher <Intent > resultLauncher ) {
158
163
DialogUtil .showAlertDialog (activity , activity .getString (R .string .location_permission_title ),
159
164
activity .getString (R .string .in_app_camera_location_permission_rationale ),
160
165
activity .getString (android .R .string .ok ),
161
166
activity .getString (android .R .string .cancel ),
162
167
() -> {
163
168
createDialogsAndHandleLocationPermissions (activity ,
164
- inAppCameraLocationPermissionLauncher );
169
+ inAppCameraLocationPermissionLauncher , resultLauncher );
165
170
},
166
171
() -> locationPermissionCallback .onLocationPermissionDenied (
167
172
activity .getString (R .string .in_app_camera_location_permission_denied )),
@@ -181,7 +186,8 @@ public void handleShowRationaleFlowCameraLocation(Activity activity,
181
186
* @param activity
182
187
*/
183
188
private void askUserToAllowLocationAccess (Activity activity ,
184
- ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ) {
189
+ ActivityResultLauncher <String []> inAppCameraLocationPermissionLauncher ,
190
+ ActivityResultLauncher <Intent > resultLauncher ) {
185
191
DialogUtil .showAlertDialog (activity ,
186
192
activity .getString (R .string .in_app_camera_location_permission_title ),
187
193
activity .getString (R .string .in_app_camera_location_access_explanation ),
@@ -190,12 +196,12 @@ private void askUserToAllowLocationAccess(Activity activity,
190
196
() -> {
191
197
defaultKvStore .putBoolean ("inAppCameraLocationPref" , true );
192
198
createDialogsAndHandleLocationPermissions (activity ,
193
- inAppCameraLocationPermissionLauncher );
199
+ inAppCameraLocationPermissionLauncher , resultLauncher );
194
200
},
195
201
() -> {
196
202
ViewUtil .showLongToast (activity , R .string .in_app_camera_location_permission_denied );
197
203
defaultKvStore .putBoolean ("inAppCameraLocationPref" , false );
198
- initiateCameraUpload (activity );
204
+ initiateCameraUpload (activity , resultLauncher );
199
205
},
200
206
null ,
201
207
true );
@@ -204,18 +210,18 @@ private void askUserToAllowLocationAccess(Activity activity,
204
210
/**
205
211
* Initiate gallery picker
206
212
*/
207
- public void initiateGalleryPick (final Activity activity , final boolean allowMultipleUploads ) {
208
- initiateGalleryUpload (activity , allowMultipleUploads );
213
+ public void initiateGalleryPick (final Activity activity , ActivityResultLauncher < Intent > resultLauncher , final boolean allowMultipleUploads ) {
214
+ initiateGalleryUpload (activity , resultLauncher , allowMultipleUploads );
209
215
}
210
216
211
217
/**
212
218
* Initiate gallery picker with permission
213
219
*/
214
- public void initiateCustomGalleryPickWithPermission (final Activity activity ) {
220
+ public void initiateCustomGalleryPickWithPermission (final Activity activity , ActivityResultLauncher < Intent > resultLauncher ) {
215
221
setPickerConfiguration (activity , true );
216
222
217
223
PermissionUtils .checkPermissionsAndPerformAction (activity ,
218
- () -> FilePicker .openCustomSelector (activity , 0 ),
224
+ () -> FilePicker .openCustomSelector (activity , resultLauncher , 0 ),
219
225
R .string .storage_permission_title ,
220
226
R .string .write_storage_permission_rationale ,
221
227
PermissionUtils .PERMISSIONS_STORAGE );
@@ -225,12 +231,10 @@ public void initiateCustomGalleryPickWithPermission(final Activity activity) {
225
231
/**
226
232
* Open chooser for gallery uploads
227
233
*/
228
- private void initiateGalleryUpload (final Activity activity ,
234
+ private void initiateGalleryUpload (final Activity activity , ActivityResultLauncher < Intent > resultLauncher ,
229
235
final boolean allowMultipleUploads ) {
230
236
setPickerConfiguration (activity , allowMultipleUploads );
231
- boolean openDocumentIntentPreferred = defaultKvStore .getBoolean (
232
- "openDocumentPhotoPickerPref" , true );
233
- FilePicker .openGallery (activity , 0 , openDocumentIntentPreferred );
237
+ FilePicker .openGallery (activity , resultLauncher , 0 , isDocumentPhotoPickerPreferred ());
234
238
}
235
239
236
240
/**
@@ -247,22 +251,43 @@ private void setPickerConfiguration(Activity activity,
247
251
/**
248
252
* Initiate camera upload by opening camera
249
253
*/
250
- private void initiateCameraUpload (Activity activity ) {
254
+ private void initiateCameraUpload (Activity activity , ActivityResultLauncher < Intent > resultLauncher ) {
251
255
setPickerConfiguration (activity , false );
252
256
if (defaultKvStore .getBoolean ("inAppCameraLocationPref" , false )) {
253
257
locationBeforeImageCapture = locationManager .getLastLocation ();
254
258
}
255
259
isInAppCameraUpload = true ;
256
- FilePicker .openCameraForImage (activity , 0 );
260
+ FilePicker .openCameraForImage (activity , resultLauncher , 0 );
261
+ }
262
+
263
+ private boolean isDocumentPhotoPickerPreferred (){
264
+ return defaultKvStore .getBoolean (
265
+ "openDocumentPhotoPickerPref" , true );
266
+ }
267
+
268
+ public void onPictureReturnedFromGallery (ActivityResult result , Activity activity , FilePicker .Callbacks callbacks ){
269
+
270
+ if (isDocumentPhotoPickerPreferred ()){
271
+ FilePicker .onPictureReturnedFromDocuments (result , activity , callbacks );
272
+ } else {
273
+ FilePicker .onPictureReturnedFromGallery (result , activity , callbacks );
274
+ }
275
+ }
276
+
277
+ public void onPictureReturnedFromCustomSelector (ActivityResult result , Activity activity , @ NonNull FilePicker .Callbacks callbacks ) {
278
+ FilePicker .onPictureReturnedFromCustomSelector (result , activity , callbacks );
279
+ }
280
+
281
+ public void onPictureReturnedFromCamera (ActivityResult result , Activity activity , @ NonNull FilePicker .Callbacks callbacks ) {
282
+ FilePicker .onPictureReturnedFromCamera (result , activity , callbacks );
257
283
}
258
284
259
285
/**
260
286
* Attaches callback for file picker.
261
287
*/
262
- public void handleActivityResult (Activity activity , int requestCode , int resultCode ,
263
- Intent data ) {
264
- FilePicker .handleActivityResult (requestCode , resultCode , data , activity ,
265
- new DefaultCallback () {
288
+ public void handleActivityResultWithCallback (Activity activity , FilePicker .HandleActivityResult handleActivityResult ) {
289
+
290
+ handleActivityResult .onHandleActivityResult (new DefaultCallback () {
266
291
267
292
@ Override
268
293
public void onCanceled (final ImageSource source , final int type ) {
0 commit comments