1
1
package fr .free .nrw .commons .upload ;
2
2
3
+ import android .app .Activity ;
3
4
import android .content .Context ;
5
+ import android .content .Intent ;
4
6
import android .graphics .BitmapRegionDecoder ;
5
7
import android .net .Uri ;
6
8
import android .os .AsyncTask ;
9
+ import android .support .v7 .app .AlertDialog ;
7
10
8
11
import java .io .IOException ;
12
+ import java .lang .ref .WeakReference ;
9
13
14
+ import fr .free .nrw .commons .R ;
15
+ import fr .free .nrw .commons .contributions .ContributionsActivity ;
10
16
import fr .free .nrw .commons .utils .ImageUtils ;
11
17
import timber .log .Timber ;
12
18
21
27
22
28
public class DetectUnwantedPicturesAsync extends AsyncTask <Void , Void , ImageUtils .Result > {
23
29
24
- interface Callback {
25
- void onResult (ImageUtils .Result result );
26
- }
27
-
28
- private final Callback callback ;
29
30
private final String imageMediaFilePath ;
31
+ public final WeakReference <Activity > activityWeakReference ;
30
32
31
- DetectUnwantedPicturesAsync (String imageMediaFilePath , Callback callback ) {
32
- this .callback = callback ;
33
+ DetectUnwantedPicturesAsync (WeakReference < Activity > activityWeakReference , String imageMediaFilePath ) {
34
+ // this.callback = callback;
33
35
this .imageMediaFilePath = imageMediaFilePath ;
36
+ this .activityWeakReference = activityWeakReference ;
34
37
}
35
38
36
39
@ Override
@@ -53,7 +56,29 @@ protected ImageUtils.Result doInBackground(Void... voids) {
53
56
@ Override
54
57
protected void onPostExecute (ImageUtils .Result result ) {
55
58
super .onPostExecute (result );
56
- //callback to UI so that it can take necessary decision based on the result obtained
57
- callback .onResult (result );
59
+ Activity activity = activityWeakReference .get ();
60
+
61
+ if (result != ImageUtils .Result .IMAGE_OK ) {
62
+ //show appropriate error message
63
+ String errorMessage = result == ImageUtils .Result .IMAGE_DARK ? activity .getString (R .string .upload_image_too_dark ) : activity .getString (R .string .upload_image_blurry );
64
+ AlertDialog .Builder errorDialogBuilder = new AlertDialog .Builder (activity );
65
+ errorDialogBuilder .setMessage (errorMessage );
66
+ errorDialogBuilder .setTitle (activity .getString (R .string .warning ));
67
+ errorDialogBuilder .setPositiveButton (activity .getString (R .string .no ), (dialogInterface , i ) -> {
68
+ //user does not wish to upload the picture, take them back to ContributionsActivity
69
+ Intent intent = new Intent (activity , ContributionsActivity .class );
70
+ dialogInterface .dismiss ();
71
+ activity .startActivity (intent );
72
+ });
73
+ errorDialogBuilder .setNegativeButton (activity .getString (R .string .yes ), (dialogInterface , i ) -> {
74
+ //user wishes to go ahead with the upload of this picture, just dismiss this dialog
75
+ dialogInterface .dismiss ();
76
+ });
77
+
78
+ AlertDialog errorDialog = errorDialogBuilder .create ();
79
+ if (!activity .isFinishing ()) {
80
+ errorDialog .show ();
81
+ }
82
+ }
58
83
}
59
84
}
0 commit comments