|
37 | 37 | import android.text.Editable; |
38 | 38 | import android.text.TextUtils; |
39 | 39 | import android.text.TextWatcher; |
| 40 | +import android.util.ArrayMap; |
40 | 41 | import android.util.Log; |
41 | 42 | import android.view.View; |
42 | 43 | import android.view.WindowManager; |
|
54 | 55 | import androidx.recyclerview.widget.RecyclerView; |
55 | 56 | import butterknife.BindView; |
56 | 57 | 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 | 58 | import com.box.androidsdk.content.BoxApiFile; |
66 | 59 | import com.box.androidsdk.content.BoxConfig; |
67 | 60 | import com.box.androidsdk.content.BoxException; |
|
78 | 71 | import com.dropbox.core.v2.DbxClientV2; |
79 | 72 | import com.dropbox.core.v2.files.WriteMode; |
80 | 73 | import com.google.android.material.snackbar.Snackbar; |
| 74 | +import com.google.gson.JsonElement; |
81 | 75 | import com.mikepenz.community_material_typeface_library.CommunityMaterial; |
82 | 76 | import com.mikepenz.iconics.view.IconicsImageView; |
83 | 77 | import com.owncloud.android.lib.common.OwnCloudClient; |
|
109 | 103 | import java.text.SimpleDateFormat; |
110 | 104 | import java.util.ArrayList; |
111 | 105 | import java.util.Date; |
112 | | -import java.util.HashMap; |
113 | | -import java.util.Map; |
114 | 106 | import org.fossasia.phimpme.R; |
115 | 107 | import org.fossasia.phimpme.base.PhimpmeProgressBarHandler; |
116 | 108 | import org.fossasia.phimpme.base.RecyclerItemClickListner; |
|
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.ImgurApiInterface; |
| 122 | +import org.fossasia.phimpme.utilities.ImgurRetrofitClient; |
129 | 123 | import org.fossasia.phimpme.utilities.NotificationHandler; |
130 | 124 | import org.fossasia.phimpme.utilities.SnackBarHandler; |
131 | 125 | import org.fossasia.phimpme.utilities.Utils; |
132 | 126 | import org.json.JSONException; |
133 | 127 | import org.json.JSONObject; |
| 128 | +import retrofit2.Call; |
| 129 | +import retrofit2.Callback; |
| 130 | +import retrofit2.Response; |
134 | 131 |
|
135 | 132 | /** |
136 | 133 | * Class which deals with Sharing images to multiple Account logged in by the user in the app. If |
@@ -202,6 +199,8 @@ public class SharingActivity extends ThemedActivity |
202 | 199 | public String uploadName; |
203 | 200 | private int positionShareOption; |
204 | 201 | private boolean triedUploading = false; |
| 202 | + private ImgurApiInterface imgurApiInterface = |
| 203 | + ImgurRetrofitClient.getImgurRetrofitClient().create(ImgurApiInterface.class); |
205 | 204 |
|
206 | 205 | public static String getClientAuth() { |
207 | 206 | return Constants.IMGUR_HEADER_CLIENt + " " + Constants.MY_IMGUR_CLIENT_ID; |
@@ -1081,107 +1080,93 @@ void uploadImgur() { |
1081 | 1080 | Bitmap bitmap = getBitmapFromPath(saveFilePath); |
1082 | 1081 | final String imageString = getStringImage(bitmap); |
1083 | 1082 | // 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>() { |
| 1083 | + ArrayMap<String, String> body = new ArrayMap<>(); |
| 1084 | + body.put("image", imageString); |
| 1085 | + if (caption != null && !caption.isEmpty()) { |
| 1086 | + body.put("title", caption); |
| 1087 | + } |
| 1088 | + String authorization; |
| 1089 | + if (isPersonal && imgurAuth != null) { |
| 1090 | + authorization = imgurAuth; |
| 1091 | + } else { |
| 1092 | + authorization = getClientAuth(); |
| 1093 | + } |
| 1094 | + imgurApiInterface |
| 1095 | + .uploadImageToImgur(authorization, body) |
| 1096 | + .enqueue( |
| 1097 | + new Callback<JsonElement>() { |
1089 | 1098 | @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"); |
| 1099 | + public void onResponse(Call<JsonElement> call, Response<JsonElement> response) { |
| 1100 | + if (response.body() != null && response.isSuccessful()) { |
| 1101 | + dialog.dismiss(); |
| 1102 | + JSONObject jsonObject = null; |
| 1103 | + try { |
| 1104 | + jsonObject = new JSONObject(response.body().toString()); |
| 1105 | + boolean success = jsonObject.getBoolean("success"); |
| 1106 | + if (success) { |
| 1107 | + final String url = jsonObject.getJSONObject("data").getString("link"); |
| 1108 | + |
| 1109 | + if (isPersonal) { |
| 1110 | + imgurString = getString(R.string.upload_personal) + "\n" + url; |
| 1111 | + } else { |
| 1112 | + imgurString = getString(R.string.upload_anonymous) + "\n" + url; |
| 1113 | + } |
1099 | 1114 |
|
1100 | | - if (isPersonal) { |
1101 | | - imgurString = getString(R.string.upload_personal) + "\n" + url; |
| 1115 | + AlertDialogsHelper.getTextDialog( |
| 1116 | + SharingActivity.this, |
| 1117 | + dialogBuilder, |
| 1118 | + R.string.imgur_uplaoded_dialog_title, |
| 1119 | + 0, |
| 1120 | + imgurString); |
| 1121 | + dialogBuilder.setPositiveButton( |
| 1122 | + getString(R.string.share).toUpperCase(), |
| 1123 | + new DialogInterface.OnClickListener() { |
| 1124 | + @Override |
| 1125 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 1126 | + shareMsgOnIntent(SharingActivity.this, url); |
| 1127 | + sendResult(Constants.SUCCESS); |
| 1128 | + } |
| 1129 | + }); |
| 1130 | + |
| 1131 | + dialogBuilder.setNeutralButton( |
| 1132 | + getString(R.string.copy_action).toUpperCase(), |
| 1133 | + new DialogInterface.OnClickListener() { |
| 1134 | + @Override |
| 1135 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 1136 | + copyToClipBoard(SharingActivity.this, url); |
| 1137 | + sendResult(Constants.SUCCESS); |
| 1138 | + } |
| 1139 | + }); |
| 1140 | + dialogBuilder.setNegativeButton(getString(R.string.exit).toUpperCase(), null); |
| 1141 | + AlertDialog alertDialog = dialogBuilder.create(); |
| 1142 | + alertDialog.show(); |
| 1143 | + AlertDialogsHelper.setButtonTextColor( |
| 1144 | + new int[] { |
| 1145 | + DialogInterface.BUTTON_POSITIVE, |
| 1146 | + DialogInterface.BUTTON_NEGATIVE, |
| 1147 | + DialogInterface.BUTTON_NEUTRAL |
| 1148 | + }, |
| 1149 | + getAccentColor(), |
| 1150 | + alertDialog); |
1102 | 1151 | } else { |
1103 | | - imgurString = getString(R.string.upload_anonymous) + "\n" + url; |
| 1152 | + SnackBarHandler.create(parent, getString(R.string.error_on_imgur)).show(); |
| 1153 | + sendResult(FAIL); |
1104 | 1154 | } |
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); |
| 1155 | + } catch (JSONException e) { |
| 1156 | + e.printStackTrace(); |
1145 | 1157 | } |
1146 | | - } catch (JSONException e) { |
1147 | | - e.printStackTrace(); |
| 1158 | + } else { |
| 1159 | + dialog.dismiss(); |
| 1160 | + SnackBarHandler.create(parent, getString(R.string.error_volly)).show(); |
1148 | 1161 | } |
1149 | 1162 | } |
1150 | | - }, |
1151 | | - new Response.ErrorListener() { |
| 1163 | + |
1152 | 1164 | @Override |
1153 | | - public void onErrorResponse(VolleyError volleyError) { |
| 1165 | + public void onFailure(Call<JsonElement> call, Throwable t) { |
1154 | 1166 | dialog.dismiss(); |
1155 | | - SnackBarHandler.create(parent, getString(R.string.error_volly)) |
1156 | | - .show(); // add volleyError to check error |
| 1167 | + SnackBarHandler.create(parent, getString(R.string.error_volly)).show(); |
1157 | 1168 | } |
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); |
| 1169 | + }); |
1185 | 1170 | } |
1186 | 1171 |
|
1187 | 1172 | /** |
|
0 commit comments