11package fr .free .nrw .commons .upload ;
22
3+ import android .app .Activity ;
34import android .content .Context ;
5+ import android .content .Intent ;
46import android .graphics .BitmapRegionDecoder ;
57import android .net .Uri ;
68import android .os .AsyncTask ;
9+ import android .support .v7 .app .AlertDialog ;
710
811import java .io .IOException ;
12+ import java .lang .ref .WeakReference ;
913
14+ import fr .free .nrw .commons .R ;
15+ import fr .free .nrw .commons .contributions .ContributionsActivity ;
1016import fr .free .nrw .commons .utils .ImageUtils ;
1117import timber .log .Timber ;
1218
2127
2228public class DetectUnwantedPicturesAsync extends AsyncTask <Void , Void , ImageUtils .Result > {
2329
24- interface Callback {
25- void onResult (ImageUtils .Result result );
26- }
27-
28- private final Callback callback ;
2930 private final String imageMediaFilePath ;
31+ public final WeakReference <Activity > activityWeakReference ;
3032
31- DetectUnwantedPicturesAsync (String imageMediaFilePath , Callback callback ) {
32- this .callback = callback ;
33+ DetectUnwantedPicturesAsync (WeakReference < Activity > activityWeakReference , String imageMediaFilePath ) {
34+ // this.callback = callback;
3335 this .imageMediaFilePath = imageMediaFilePath ;
36+ this .activityWeakReference = activityWeakReference ;
3437 }
3538
3639 @ Override
@@ -53,7 +56,29 @@ protected ImageUtils.Result doInBackground(Void... voids) {
5356 @ Override
5457 protected void onPostExecute (ImageUtils .Result result ) {
5558 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+ }
5883 }
5984}
0 commit comments