5454import androidx .recyclerview .widget .RecyclerView ;
5555import butterknife .BindView ;
5656import butterknife .ButterKnife ;
57- import com .android .volley .AuthFailureError ;
58- import com .android .volley .DefaultRetryPolicy ;
59- import com .android .volley .Request ;
60- import com .android .volley .RequestQueue ;
61- import com .android .volley .Response ;
62- import com .android .volley .VolleyError ;
63- import com .android .volley .toolbox .StringRequest ;
64- import com .android .volley .toolbox .Volley ;
6557import com .box .androidsdk .content .BoxApiFile ;
6658import com .box .androidsdk .content .BoxConfig ;
6759import com .box .androidsdk .content .BoxException ;
109101import java .text .SimpleDateFormat ;
110102import java .util .ArrayList ;
111103import java .util .Date ;
112- import java .util .HashMap ;
113- import java .util .Map ;
114104import org .fossasia .phimpme .R ;
115105import org .fossasia .phimpme .base .PhimpmeProgressBarHandler ;
116106import org .fossasia .phimpme .base .RecyclerItemClickListner ;
122112import org .fossasia .phimpme .gallery .util .AlertDialogsHelper ;
123113import org .fossasia .phimpme .gallery .util .ThemeHelper ;
124114import org .fossasia .phimpme .share .flickr .FlickrHelper ;
115+ import org .fossasia .phimpme .share .imgur .ImgurPicUploadReq ;
116+ import org .fossasia .phimpme .share .imgur .ImgurPicUploadResp ;
125117import org .fossasia .phimpme .share .tumblr .TumblrClient ;
126118import org .fossasia .phimpme .share .twitter .HelperMethods ;
127119import org .fossasia .phimpme .utilities .ActivitySwitchHelper ;
128120import org .fossasia .phimpme .utilities .Constants ;
121+ import org .fossasia .phimpme .utilities .ImgurApi ;
129122import org .fossasia .phimpme .utilities .NotificationHandler ;
123+ import org .fossasia .phimpme .utilities .RetrofitClient ;
130124import org .fossasia .phimpme .utilities .SnackBarHandler ;
131125import org .fossasia .phimpme .utilities .Utils ;
132- import org .json .JSONException ;
133- import org .json .JSONObject ;
126+ import retrofit2 .Call ;
127+ import retrofit2 .Callback ;
128+ import retrofit2 .Response ;
134129
135130/**
136131 * Class which deals with Sharing images to multiple Account logged in by the user in the app. If
@@ -155,6 +150,9 @@ public class SharingActivity extends ThemedActivity
155150 OnRemoteOperationListener ,
156151 RecyclerItemClickListner .OnItemClickListener {
157152
153+ private static final String IMGUR_BASE_URL = "https://api.imgur.com/3/" ;
154+ private static final String IMGUR_HEADER_CLIENT = "Client-ID" ;
155+ private static final String IMGUR_HEADER_USER = "Bearer" ;
158156 public static final String EXTRA_OUTPUT = "extra_output" ;
159157 private static String LOG_TAG = SharingActivity .class .getCanonicalName ();
160158 public String saveFilePath ;
@@ -202,9 +200,10 @@ public class SharingActivity extends ThemedActivity
202200 public String uploadName ;
203201 private int positionShareOption ;
204202 private boolean triedUploading = false ;
203+ private ImgurApi imgurApiInterface ;
205204
206205 public static String getClientAuth () {
207- return Constants . IMGUR_HEADER_CLIENt + " " + Constants .MY_IMGUR_CLIENT_ID ;
206+ return IMGUR_HEADER_CLIENT + " " + Constants .MY_IMGUR_CLIENT_ID ;
208207 }
209208
210209 @ Override
@@ -1015,14 +1014,17 @@ private void shareToWhatsapp() {
10151014 }
10161015
10171016 private void shareToImgur () {
1017+ if (imgurApiInterface == null ) {
1018+ imgurApiInterface = RetrofitClient .getRetrofitClient (IMGUR_BASE_URL ).create (ImgurApi .class );
1019+ }
10181020 final AlertDialog .Builder dialogBuilder =
10191021 new AlertDialog .Builder (SharingActivity .this , getDialogStyle ());
10201022 RealmQuery <AccountDatabase > query = realm .where (AccountDatabase .class );
10211023 query .equalTo ("name" , getString (R .string .imgur ));
10221024 final RealmResults <AccountDatabase > result = query .findAll ();
10231025 if (result .size () != 0 ) {
10241026 isPersonal = true ;
1025- imgurAuth = Constants . IMGUR_HEADER_USER + " " + result .get (0 ).getToken ();
1027+ imgurAuth = IMGUR_HEADER_USER + " " + result .get (0 ).getToken ();
10261028 }
10271029 AlertDialogsHelper .getTextDialog (
10281030 SharingActivity .this , dialogBuilder , R .string .choose , R .string .imgur_select_mode , null );
@@ -1081,21 +1083,30 @@ void uploadImgur() {
10811083 Bitmap bitmap = getBitmapFromPath (saveFilePath );
10821084 final String imageString = getStringImage (bitmap );
10831085 // sending image to server
1084- StringRequest request =
1085- new StringRequest (
1086- Request .Method .POST ,
1087- Constants .IMGUR_IMAGE_UPLOAD_URL ,
1088- new Response .Listener <String >() {
1086+ ImgurPicUploadReq imgurPicUpload = new ImgurPicUploadReq ();
1087+ imgurPicUpload .setImage (imageString );
1088+ if (caption != null && !caption .isEmpty ()) {
1089+ imgurPicUpload .setCaption (caption );
1090+ }
1091+ String authorization ;
1092+ if (isPersonal && imgurAuth != null ) {
1093+ authorization = imgurAuth ;
1094+ } else {
1095+ authorization = getClientAuth ();
1096+ }
1097+ imgurApiInterface
1098+ .uploadImageToImgur (authorization , imgurPicUpload )
1099+ .enqueue (
1100+ new Callback <ImgurPicUploadResp >() {
10891101 @ Override
1090- public void onResponse (String s ) {
1091- dialog .dismiss ();
1092- JSONObject jsonObject = null ;
1093-
1094- try {
1095- jsonObject = new JSONObject (s );
1096- Boolean success = jsonObject .getBoolean ("success" );
1102+ public void onResponse (
1103+ Call <ImgurPicUploadResp > call , Response <ImgurPicUploadResp > response ) {
1104+ if (response .body () != null && response .isSuccessful ()) {
1105+ dialog .dismiss ();
1106+ ImgurPicUploadResp imgurPicUploadResp = response .body ();
1107+ boolean success = imgurPicUploadResp .isSuccess ();
10971108 if (success ) {
1098- final String url = jsonObject . getJSONObject ( "data" ). getString ( "link" );
1109+ final String url = imgurPicUploadResp . getData (). getLink ( );
10991110
11001111 if (isPersonal ) {
11011112 imgurString = getString (R .string .upload_personal ) + "\n " + url ;
@@ -1143,45 +1154,18 @@ public void onClick(DialogInterface dialogInterface, int i) {
11431154 SnackBarHandler .create (parent , getString (R .string .error_on_imgur )).show ();
11441155 sendResult (FAIL );
11451156 }
1146- } catch (JSONException e ) {
1147- e .printStackTrace ();
1157+ } else {
1158+ dialog .dismiss ();
1159+ SnackBarHandler .create (parent , getString (R .string .error_volly )).show ();
11481160 }
11491161 }
1150- },
1151- new Response .ErrorListener () {
1162+
11521163 @ Override
1153- public void onErrorResponse ( VolleyError volleyError ) {
1164+ public void onFailure ( Call < ImgurPicUploadResp > call , Throwable t ) {
11541165 dialog .dismiss ();
1155- SnackBarHandler .create (parent , getString (R .string .error_volly ))
1156- .show (); // add volleyError to check error
1157- }
1158- }) {
1159- @ Override
1160- protected Map <String , String > getParams () throws AuthFailureError {
1161- Map <String , String > parameters = new HashMap <String , String >();
1162- parameters .put ("image" , imageString );
1163- if (caption != null && !caption .isEmpty ()) parameters .put ("title" , caption );
1164- return parameters ;
1165- }
1166-
1167- @ Override
1168- public Map <String , String > getHeaders () throws AuthFailureError {
1169- Map <String , String > headers = new HashMap <String , String >();
1170- if (isPersonal ) {
1171- if (imgurAuth != null ) {
1172- headers .put (getString (R .string .header_auth ), imgurAuth );
1166+ SnackBarHandler .create (parent , getString (R .string .error_volly )).show ();
11731167 }
1174- } else {
1175- headers .put (getString (R .string .header_auth ), getClientAuth ());
1176- }
1177-
1178- return headers ;
1179- }
1180- };
1181- request .setRetryPolicy (
1182- new DefaultRetryPolicy (50000 , 5 , DefaultRetryPolicy .DEFAULT_BACKOFF_MULT ));
1183- RequestQueue rQueue = Volley .newRequestQueue (SharingActivity .this );
1184- rQueue .add (request );
1168+ });
11851169 }
11861170
11871171 /**
0 commit comments