54
54
import androidx .recyclerview .widget .RecyclerView ;
55
55
import butterknife .BindView ;
56
56
import 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 ;
65
57
import com .box .androidsdk .content .BoxApiFile ;
66
58
import com .box .androidsdk .content .BoxConfig ;
67
59
import com .box .androidsdk .content .BoxException ;
109
101
import java .text .SimpleDateFormat ;
110
102
import java .util .ArrayList ;
111
103
import java .util .Date ;
112
- import java .util .HashMap ;
113
- import java .util .Map ;
114
104
import org .fossasia .phimpme .R ;
115
105
import org .fossasia .phimpme .base .PhimpmeProgressBarHandler ;
116
106
import org .fossasia .phimpme .base .RecyclerItemClickListner ;
122
112
import org .fossasia .phimpme .gallery .util .AlertDialogsHelper ;
123
113
import org .fossasia .phimpme .gallery .util .ThemeHelper ;
124
114
import org .fossasia .phimpme .share .flickr .FlickrHelper ;
115
+ import org .fossasia .phimpme .share .imgur .ImgurPicUploadReq ;
116
+ import org .fossasia .phimpme .share .imgur .ImgurPicUploadResp ;
125
117
import org .fossasia .phimpme .share .tumblr .TumblrClient ;
126
118
import org .fossasia .phimpme .share .twitter .HelperMethods ;
127
119
import org .fossasia .phimpme .utilities .ActivitySwitchHelper ;
128
120
import org .fossasia .phimpme .utilities .Constants ;
121
+ import org .fossasia .phimpme .utilities .ImgurApi ;
129
122
import org .fossasia .phimpme .utilities .NotificationHandler ;
123
+ import org .fossasia .phimpme .utilities .RetrofitClient ;
130
124
import org .fossasia .phimpme .utilities .SnackBarHandler ;
131
125
import 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 ;
134
129
135
130
/**
136
131
* 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
155
150
OnRemoteOperationListener ,
156
151
RecyclerItemClickListner .OnItemClickListener {
157
152
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" ;
158
156
public static final String EXTRA_OUTPUT = "extra_output" ;
159
157
private static String LOG_TAG = SharingActivity .class .getCanonicalName ();
160
158
public String saveFilePath ;
@@ -202,9 +200,10 @@ public class SharingActivity extends ThemedActivity
202
200
public String uploadName ;
203
201
private int positionShareOption ;
204
202
private boolean triedUploading = false ;
203
+ private ImgurApi imgurApiInterface ;
205
204
206
205
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 ;
208
207
}
209
208
210
209
@ Override
@@ -1015,14 +1014,17 @@ private void shareToWhatsapp() {
1015
1014
}
1016
1015
1017
1016
private void shareToImgur () {
1017
+ if (imgurApiInterface == null ) {
1018
+ imgurApiInterface = RetrofitClient .getRetrofitClient (IMGUR_BASE_URL ).create (ImgurApi .class );
1019
+ }
1018
1020
final AlertDialog .Builder dialogBuilder =
1019
1021
new AlertDialog .Builder (SharingActivity .this , getDialogStyle ());
1020
1022
RealmQuery <AccountDatabase > query = realm .where (AccountDatabase .class );
1021
1023
query .equalTo ("name" , getString (R .string .imgur ));
1022
1024
final RealmResults <AccountDatabase > result = query .findAll ();
1023
1025
if (result .size () != 0 ) {
1024
1026
isPersonal = true ;
1025
- imgurAuth = Constants . IMGUR_HEADER_USER + " " + result .get (0 ).getToken ();
1027
+ imgurAuth = IMGUR_HEADER_USER + " " + result .get (0 ).getToken ();
1026
1028
}
1027
1029
AlertDialogsHelper .getTextDialog (
1028
1030
SharingActivity .this , dialogBuilder , R .string .choose , R .string .imgur_select_mode , null );
@@ -1081,21 +1083,30 @@ void uploadImgur() {
1081
1083
Bitmap bitmap = getBitmapFromPath (saveFilePath );
1082
1084
final String imageString = getStringImage (bitmap );
1083
1085
// 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 >() {
1089
1101
@ 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 ();
1097
1108
if (success ) {
1098
- final String url = jsonObject . getJSONObject ( "data" ). getString ( "link" );
1109
+ final String url = imgurPicUploadResp . getData (). getLink ( );
1099
1110
1100
1111
if (isPersonal ) {
1101
1112
imgurString = getString (R .string .upload_personal ) + "\n " + url ;
@@ -1143,45 +1154,18 @@ public void onClick(DialogInterface dialogInterface, int i) {
1143
1154
SnackBarHandler .create (parent , getString (R .string .error_on_imgur )).show ();
1144
1155
sendResult (FAIL );
1145
1156
}
1146
- } catch (JSONException e ) {
1147
- e .printStackTrace ();
1157
+ } else {
1158
+ dialog .dismiss ();
1159
+ SnackBarHandler .create (parent , getString (R .string .error_volly )).show ();
1148
1160
}
1149
1161
}
1150
- },
1151
- new Response .ErrorListener () {
1162
+
1152
1163
@ Override
1153
- public void onErrorResponse ( VolleyError volleyError ) {
1164
+ public void onFailure ( Call < ImgurPicUploadResp > call , Throwable t ) {
1154
1165
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 ();
1173
1167
}
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
+ });
1185
1169
}
1186
1170
1187
1171
/**
0 commit comments