3737import android .text .Editable ;
3838import android .text .TextUtils ;
3939import android .text .TextWatcher ;
40+ import android .util .ArrayMap ;
4041import android .util .Log ;
4142import android .view .View ;
4243import android .view .WindowManager ;
5455import androidx .recyclerview .widget .RecyclerView ;
5556import butterknife .BindView ;
5657import 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 ;
6558import com .box .androidsdk .content .BoxApiFile ;
6659import com .box .androidsdk .content .BoxConfig ;
6760import com .box .androidsdk .content .BoxException ;
7871import com .dropbox .core .v2 .DbxClientV2 ;
7972import com .dropbox .core .v2 .files .WriteMode ;
8073import com .google .android .material .snackbar .Snackbar ;
74+ import com .google .gson .JsonElement ;
8175import com .mikepenz .community_material_typeface_library .CommunityMaterial ;
8276import com .mikepenz .iconics .view .IconicsImageView ;
8377import com .owncloud .android .lib .common .OwnCloudClient ;
109103import java .text .SimpleDateFormat ;
110104import java .util .ArrayList ;
111105import java .util .Date ;
112- import java .util .HashMap ;
113- import java .util .Map ;
114106import org .fossasia .phimpme .R ;
115107import org .fossasia .phimpme .base .PhimpmeProgressBarHandler ;
116108import org .fossasia .phimpme .base .RecyclerItemClickListner ;
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 .ImgurApiInterface ;
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 ;
132126import org .json .JSONException ;
133127import org .json .JSONObject ;
128+ import retrofit2 .Call ;
129+ import retrofit2 .Callback ;
130+ import retrofit2 .Response ;
134131
135132/**
136133 * Class which deals with Sharing images to multiple Account logged in by the user in the app. If
@@ -155,6 +152,7 @@ public class SharingActivity extends ThemedActivity
155152 OnRemoteOperationListener ,
156153 RecyclerItemClickListner .OnItemClickListener {
157154
155+ private static final String IMGUR_BASE_URL = "https://api.imgur.com/3/" ;
158156 public static final String EXTRA_OUTPUT = "extra_output" ;
159157 private static String LOG_TAG = SharingActivity .class .getCanonicalName ();
160158 public String saveFilePath ;
@@ -202,6 +200,7 @@ public class SharingActivity extends ThemedActivity
202200 public String uploadName ;
203201 private int positionShareOption ;
204202 private boolean triedUploading = false ;
203+ private ImgurApiInterface imgurApiInterface ;
205204
206205 public static String getClientAuth () {
207206 return Constants .IMGUR_HEADER_CLIENt + " " + Constants .MY_IMGUR_CLIENT_ID ;
@@ -1015,6 +1014,10 @@ private void shareToWhatsapp() {
10151014 }
10161015
10171016 private void shareToImgur () {
1017+ if (imgurApiInterface == null ) {
1018+ imgurApiInterface =
1019+ RetrofitClient .getRetrofitClient (IMGUR_BASE_URL ).create (ImgurApiInterface .class );
1020+ }
10181021 final AlertDialog .Builder dialogBuilder =
10191022 new AlertDialog .Builder (SharingActivity .this , getDialogStyle ());
10201023 RealmQuery <AccountDatabase > query = realm .where (AccountDatabase .class );
@@ -1081,107 +1084,93 @@ void uploadImgur() {
10811084 Bitmap bitmap = getBitmapFromPath (saveFilePath );
10821085 final String imageString = getStringImage (bitmap );
10831086 // 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 >() {
1087+ ArrayMap <String , String > body = new ArrayMap <>();
1088+ body .put ("image" , imageString );
1089+ if (caption != null && !caption .isEmpty ()) {
1090+ body .put ("title" , caption );
1091+ }
1092+ String authorization ;
1093+ if (isPersonal && imgurAuth != null ) {
1094+ authorization = imgurAuth ;
1095+ } else {
1096+ authorization = getClientAuth ();
1097+ }
1098+ imgurApiInterface
1099+ .uploadImageToImgur (authorization , body )
1100+ .enqueue (
1101+ new Callback <JsonElement >() {
10891102 @ 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" );
1097- if (success ) {
1098- final String url = jsonObject .getJSONObject ("data" ).getString ("link" );
1103+ public void onResponse (Call <JsonElement > call , Response <JsonElement > response ) {
1104+ if (response .body () != null && response .isSuccessful ()) {
1105+ dialog .dismiss ();
1106+ JSONObject jsonObject = null ;
1107+ try {
1108+ jsonObject = new JSONObject (response .body ().toString ());
1109+ boolean success = jsonObject .getBoolean ("success" );
1110+ if (success ) {
1111+ final String url = jsonObject .getJSONObject ("data" ).getString ("link" );
1112+
1113+ if (isPersonal ) {
1114+ imgurString = getString (R .string .upload_personal ) + "\n " + url ;
1115+ } else {
1116+ imgurString = getString (R .string .upload_anonymous ) + "\n " + url ;
1117+ }
10991118
1100- if (isPersonal ) {
1101- imgurString = getString (R .string .upload_personal ) + "\n " + url ;
1119+ AlertDialogsHelper .getTextDialog (
1120+ SharingActivity .this ,
1121+ dialogBuilder ,
1122+ R .string .imgur_uplaoded_dialog_title ,
1123+ 0 ,
1124+ imgurString );
1125+ dialogBuilder .setPositiveButton (
1126+ getString (R .string .share ).toUpperCase (),
1127+ new DialogInterface .OnClickListener () {
1128+ @ Override
1129+ public void onClick (DialogInterface dialogInterface , int i ) {
1130+ shareMsgOnIntent (SharingActivity .this , url );
1131+ sendResult (Constants .SUCCESS );
1132+ }
1133+ });
1134+
1135+ dialogBuilder .setNeutralButton (
1136+ getString (R .string .copy_action ).toUpperCase (),
1137+ new DialogInterface .OnClickListener () {
1138+ @ Override
1139+ public void onClick (DialogInterface dialogInterface , int i ) {
1140+ copyToClipBoard (SharingActivity .this , url );
1141+ sendResult (Constants .SUCCESS );
1142+ }
1143+ });
1144+ dialogBuilder .setNegativeButton (getString (R .string .exit ).toUpperCase (), null );
1145+ AlertDialog alertDialog = dialogBuilder .create ();
1146+ alertDialog .show ();
1147+ AlertDialogsHelper .setButtonTextColor (
1148+ new int [] {
1149+ DialogInterface .BUTTON_POSITIVE ,
1150+ DialogInterface .BUTTON_NEGATIVE ,
1151+ DialogInterface .BUTTON_NEUTRAL
1152+ },
1153+ getAccentColor (),
1154+ alertDialog );
11021155 } else {
1103- imgurString = getString (R .string .upload_anonymous ) + "\n " + url ;
1156+ SnackBarHandler .create (parent , getString (R .string .error_on_imgur )).show ();
1157+ sendResult (FAIL );
11041158 }
1105-
1106- AlertDialogsHelper .getTextDialog (
1107- SharingActivity .this ,
1108- dialogBuilder ,
1109- R .string .imgur_uplaoded_dialog_title ,
1110- 0 ,
1111- imgurString );
1112- dialogBuilder .setPositiveButton (
1113- getString (R .string .share ).toUpperCase (),
1114- new DialogInterface .OnClickListener () {
1115- @ Override
1116- public void onClick (DialogInterface dialogInterface , int i ) {
1117- shareMsgOnIntent (SharingActivity .this , url );
1118- sendResult (Constants .SUCCESS );
1119- }
1120- });
1121-
1122- dialogBuilder .setNeutralButton (
1123- getString (R .string .copy_action ).toUpperCase (),
1124- new DialogInterface .OnClickListener () {
1125- @ Override
1126- public void onClick (DialogInterface dialogInterface , int i ) {
1127- copyToClipBoard (SharingActivity .this , url );
1128- sendResult (Constants .SUCCESS );
1129- }
1130- });
1131- dialogBuilder .setNegativeButton (getString (R .string .exit ).toUpperCase (), null );
1132- AlertDialog alertDialog = dialogBuilder .create ();
1133- alertDialog .show ();
1134- AlertDialogsHelper .setButtonTextColor (
1135- new int [] {
1136- DialogInterface .BUTTON_POSITIVE ,
1137- DialogInterface .BUTTON_NEGATIVE ,
1138- DialogInterface .BUTTON_NEUTRAL
1139- },
1140- getAccentColor (),
1141- alertDialog );
1142- } else {
1143- SnackBarHandler .create (parent , getString (R .string .error_on_imgur )).show ();
1144- sendResult (FAIL );
1159+ } catch (JSONException e ) {
1160+ e .printStackTrace ();
11451161 }
1146- } catch (JSONException e ) {
1147- e .printStackTrace ();
1162+ } else {
1163+ dialog .dismiss ();
1164+ SnackBarHandler .create (parent , getString (R .string .error_volly )).show ();
11481165 }
11491166 }
1150- },
1151- new Response .ErrorListener () {
1167+
11521168 @ Override
1153- public void onErrorResponse ( VolleyError volleyError ) {
1169+ public void onFailure ( Call < JsonElement > call , Throwable t ) {
11541170 dialog .dismiss ();
1155- SnackBarHandler .create (parent , getString (R .string .error_volly ))
1156- .show (); // add volleyError to check error
1171+ SnackBarHandler .create (parent , getString (R .string .error_volly )).show ();
11571172 }
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 );
1173- }
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 );
1173+ });
11851174 }
11861175
11871176 /**
0 commit comments