Change patch header
This commit is contained in:
parent
37086e1c9b
commit
789afba048
|
@ -20,7 +20,6 @@ import android.annotation.SuppressLint;
|
|||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
|
@ -30,6 +29,7 @@ import android.database.sqlite.SQLiteDatabase;
|
|||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
@ -41,7 +41,6 @@ import android.support.v7.app.AlertDialog;
|
|||
import android.text.Editable;
|
||||
import android.text.Html;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Base64;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
@ -60,11 +59,8 @@ import com.bumptech.glide.request.transition.Transition;
|
|||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import fr.gouv.etalab.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountInfoAsyncTask;
|
||||
|
@ -100,7 +96,8 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
|||
private CheckBox set_lock_account;
|
||||
private static final int PICK_IMAGE_HEADER = 4565;
|
||||
private static final int PICK_IMAGE_PROFILE = 6545;
|
||||
private String profile_picture, header_picture, profile_username, profile_note;
|
||||
private String profile_username, profile_note;
|
||||
private ByteArrayInputStream profile_picture, header_picture;
|
||||
private API.accountPrivacy profile_privacy;
|
||||
private Bitmap profile_picture_bmp, profile_header_bmp;
|
||||
private ImageView pp_actionBar;
|
||||
|
@ -454,8 +451,9 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
|||
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
Uri fileUri = data.getData();
|
||||
try {
|
||||
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(data.getData());
|
||||
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
|
||||
assert inputStream != null;
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
||||
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
|
||||
|
@ -464,31 +462,15 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
|||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ByteArrayInputStream bs = Helper.compressImage(EditProfileActivity.this, data.getData(), Helper.MediaType.MEDIA);
|
||||
assert bs != null;
|
||||
int n = bs.available();
|
||||
byte[] bytes = new byte[n];
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
bs.read(bytes, 0, n);
|
||||
String s;
|
||||
ContentResolver cr = getContentResolver();
|
||||
String mime = cr.getType(data.getData());
|
||||
set_header_picture.setImageBitmap(profile_header_bmp);
|
||||
try {
|
||||
s = new String(bytes, "UTF-8");
|
||||
header_picture = "data:"+mime+";base64, " + s;
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
|
||||
e.printStackTrace();
|
||||
}
|
||||
header_picture = Helper.compressImage(EditProfileActivity.this, fileUri, Helper.MediaType.MEDIA);
|
||||
}else if(requestCode == PICK_IMAGE_PROFILE && resultCode == Activity.RESULT_OK) {
|
||||
if (data == null || data.getData() == null) {
|
||||
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
Uri fileUri = data.getData();
|
||||
try {
|
||||
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(data.getData());
|
||||
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
|
||||
assert inputStream != null;
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
||||
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
|
||||
|
@ -497,34 +479,19 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
|||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ByteArrayInputStream bs = Helper.compressImage(EditProfileActivity.this, data.getData(), Helper.MediaType.MEDIA);
|
||||
assert bs != null;
|
||||
int n = bs.available();
|
||||
byte[] bytes = new byte[n];
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
bs.read(bytes, 0, n);
|
||||
String s;
|
||||
ContentResolver cr = getContentResolver();
|
||||
String mime = cr.getType(data.getData());
|
||||
|
||||
try {
|
||||
s = new String(bytes, "UTF-8");
|
||||
profile_picture = "data:"+mime+";base64, " + s;
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Toast.makeText(getApplicationContext(),R.string.toot_select_image_error,Toast.LENGTH_LONG).show();
|
||||
e.printStackTrace();
|
||||
}
|
||||
profile_picture = Helper.compressImage(EditProfileActivity.this, fileUri, Helper.MediaType.PROFILE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateCredential(APIResponse apiResponse) {
|
||||
set_profile_save.setEnabled(true);
|
||||
if( apiResponse.getError() != null){
|
||||
Toast.makeText(getApplicationContext(), R.string.toast_error, Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
Toast.makeText(getApplicationContext(), R.string.toast_update_credential_ok, Toast.LENGTH_LONG).show();
|
||||
set_profile_save.setEnabled(true);
|
||||
|
||||
Intent mStartActivity = new Intent(EditProfileActivity.this, BaseMainActivity.class);
|
||||
int mPendingIntentId = 45641;
|
||||
PendingIntent mPendingIntent = PendingIntent.getActivity(EditProfileActivity.this, mPendingIntentId, mStartActivity,
|
||||
|
|
|
@ -18,6 +18,7 @@ package fr.gouv.etalab.mastodon.asynctasks;
|
|||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
|
@ -31,13 +32,14 @@ import fr.gouv.etalab.mastodon.interfaces.OnUpdateCredentialInterface;
|
|||
|
||||
public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private String display_name, note, avatar, header;
|
||||
private String display_name, note;
|
||||
private ByteArrayInputStream avatar, header;
|
||||
private API.accountPrivacy privacy;
|
||||
private APIResponse apiResponse;
|
||||
private OnUpdateCredentialInterface listener;
|
||||
private WeakReference<Context> contextReference;
|
||||
|
||||
public UpdateCredentialAsyncTask(Context context, String display_name, String note, String avatar, String header, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
|
||||
public UpdateCredentialAsyncTask(Context context, String display_name, String note, ByteArrayInputStream avatar, ByteArrayInputStream header, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
|
||||
this.contextReference = new WeakReference<>(context);
|
||||
this.display_name = display_name;
|
||||
this.note = note;
|
||||
|
|
|
@ -16,12 +16,12 @@ package fr.gouv.etalab.mastodon.client;
|
|||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.*;
|
||||
import java.net.URLEncoder;
|
||||
|
@ -137,11 +137,13 @@ public class API {
|
|||
return apiResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* Update credential of the authenticated user *synchronously*
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse updateCredential(String display_name, String note, String avatar, String header, accountPrivacy privacy) {
|
||||
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, ByteArrayInputStream header, accountPrivacy privacy) {
|
||||
|
||||
HashMap<String, String> requestParams = new HashMap<>();
|
||||
if( display_name != null)
|
||||
|
@ -156,24 +158,11 @@ public class API {
|
|||
} catch (UnsupportedEncodingException e) {
|
||||
requestParams.put("note",note);
|
||||
}
|
||||
if( avatar != null)
|
||||
Log.v(Helper.TAG,"avatar" + avatar);
|
||||
try {
|
||||
requestParams.put("avatar",URLEncoder.encode(avatar, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
requestParams.put("avatar",avatar);
|
||||
}
|
||||
if( header != null)
|
||||
try {
|
||||
requestParams.put("header",URLEncoder.encode(header, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
requestParams.put("header",header);
|
||||
}
|
||||
if( privacy != null)
|
||||
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
|
||||
|
||||
try {
|
||||
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, prefKeyOauthTokenT);
|
||||
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, header, prefKeyOauthTokenT);
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
e.printStackTrace();
|
||||
setError(e.getStatusCode(), e);
|
||||
|
@ -184,7 +173,6 @@ public class API {
|
|||
return apiResponse;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* Verifiy credential of the authenticated user *synchronously*
|
||||
* @return Account
|
||||
|
@ -1686,7 +1674,7 @@ public class API {
|
|||
}
|
||||
status.setEmojis(emojiList);
|
||||
}catch (Exception e){
|
||||
status.setEmojis(new ArrayList<Emojis>());
|
||||
status.setEmojis(new ArrayList<>());
|
||||
}
|
||||
|
||||
//Retrieve Application
|
||||
|
|
|
@ -18,7 +18,6 @@ import android.content.SharedPreferences;
|
|||
import android.os.Build;
|
||||
import android.text.Html;
|
||||
import android.text.SpannableString;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
|
@ -33,6 +32,7 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Authenticator;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
|
@ -625,6 +625,233 @@ public class HttpsConnection {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, InputStream avatar, InputStream header, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
||||
String twoHyphens = "--";
|
||||
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
|
||||
String lineEnd = "\r\n";
|
||||
if( urlConnection.startsWith("https://")) {
|
||||
URL url = new URL(urlConnection);
|
||||
Map<String, Object> params = new LinkedHashMap<>();
|
||||
if (paramaters != null) {
|
||||
Iterator it = paramaters.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
params.put(pair.getKey().toString(), pair.getValue());
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
StringBuilder postData = new StringBuilder();
|
||||
for (Map.Entry<String, Object> param : params.entrySet()) {
|
||||
if (postData.length() != 0) postData.append('&');
|
||||
postData.append(param.getKey());
|
||||
postData.append('=');
|
||||
postData.append(String.valueOf(param.getValue()));
|
||||
}
|
||||
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
|
||||
|
||||
int lengthSentAvatar = 0;
|
||||
byte[] pixelsAvatar = new byte[0];
|
||||
if( avatar != null) {
|
||||
ByteArrayOutputStream ous = null;
|
||||
try {
|
||||
try {
|
||||
byte[] buffer = new byte[CHUNK_SIZE];
|
||||
ous = new ByteArrayOutputStream();
|
||||
int read;
|
||||
while ((read = avatar.read(buffer)) != -1) {
|
||||
ous.write(buffer, 0, read);
|
||||
}
|
||||
ous.flush();
|
||||
} finally {
|
||||
if (ous != null)
|
||||
ous.close();
|
||||
}
|
||||
} catch (FileNotFoundException ignored) {
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
pixelsAvatar = ous.toByteArray();
|
||||
|
||||
lengthSentAvatar = pixelsAvatar.length;
|
||||
lengthSentAvatar += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
|
||||
lengthSentAvatar += ("Content-Disposition: form-data; name=\"avatar\";filename=\"avatar.png\"" + lineEnd).getBytes().length;
|
||||
lengthSentAvatar += 2 * (lineEnd).getBytes().length;
|
||||
}
|
||||
|
||||
int lengthSentHeader = 0;
|
||||
byte[] pixelsHeader = new byte[0];
|
||||
if( header != null) {
|
||||
ByteArrayOutputStream ous = null;
|
||||
try {
|
||||
try {
|
||||
byte[] buffer = new byte[CHUNK_SIZE];
|
||||
ous = new ByteArrayOutputStream();
|
||||
int read;
|
||||
while ((read = header.read(buffer)) != -1) {
|
||||
ous.write(buffer, 0, read);
|
||||
}
|
||||
ous.flush();
|
||||
} finally {
|
||||
if (ous != null)
|
||||
ous.close();
|
||||
}
|
||||
} catch (FileNotFoundException ignored) {
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
pixelsHeader = ous.toByteArray();
|
||||
|
||||
lengthSentHeader = pixelsHeader.length;
|
||||
lengthSentHeader += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
|
||||
lengthSentHeader += ("Content-Disposition: form-data; name=\"header\";filename=\"header.png\"" + lineEnd).getBytes().length;
|
||||
lengthSentHeader += 2 * (lineEnd).getBytes().length;
|
||||
}
|
||||
|
||||
int lengthSent = lengthSentHeader + lengthSentAvatar;
|
||||
if (proxy != null)
|
||||
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
||||
else
|
||||
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
||||
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
|
||||
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
||||
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
|
||||
httpsURLConnection.setDoInput(true);
|
||||
httpsURLConnection.setDoOutput(true);
|
||||
httpsURLConnection.setUseCaches(false);
|
||||
if( Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ){
|
||||
httpsURLConnection.setRequestMethod("PATCH");
|
||||
}else {
|
||||
httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
|
||||
httpsURLConnection.setRequestMethod("POST");
|
||||
}
|
||||
|
||||
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||
httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
|
||||
|
||||
if (token != null)
|
||||
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
||||
|
||||
if( lengthSent > 0)
|
||||
httpsURLConnection.setFixedLengthStreamingMode(lengthSent+postDataBytes.length);
|
||||
|
||||
|
||||
httpsURLConnection.getOutputStream().write(postDataBytes);
|
||||
|
||||
if(lengthSentAvatar > 0){
|
||||
OutputStream outPutStream = httpsURLConnection.getOutputStream();
|
||||
DataOutputStream request = new DataOutputStream(outPutStream);
|
||||
int totalSize = pixelsAvatar.length;
|
||||
int bytesTransferred = 0;
|
||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||
request.writeBytes("Content-Disposition: form-data; name=\"avatar\";filename=\"avatar.png\"" + lineEnd);
|
||||
request.writeBytes(lineEnd);
|
||||
while (bytesTransferred < totalSize) {
|
||||
int nextChunkSize = totalSize - bytesTransferred;
|
||||
if (nextChunkSize > CHUNK_SIZE) {
|
||||
nextChunkSize = CHUNK_SIZE;
|
||||
}
|
||||
request.write(pixelsAvatar, bytesTransferred, nextChunkSize);
|
||||
bytesTransferred += nextChunkSize;
|
||||
}
|
||||
request.writeBytes(lineEnd);
|
||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||
request.flush();
|
||||
request.close();
|
||||
}
|
||||
|
||||
if(lengthSentHeader > 0){
|
||||
int totalSize = pixelsHeader.length;
|
||||
int bytesTransferred = 0;
|
||||
OutputStream outPutStream = httpsURLConnection.getOutputStream();
|
||||
DataOutputStream request = new DataOutputStream(outPutStream);
|
||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||
request.writeBytes("Content-Disposition: form-data; name=\"header\";filename=\"header.png\"" + lineEnd);
|
||||
request.writeBytes(lineEnd);
|
||||
while (bytesTransferred < totalSize) {
|
||||
int nextChunkSize = totalSize - bytesTransferred;
|
||||
if (nextChunkSize > CHUNK_SIZE) {
|
||||
nextChunkSize = CHUNK_SIZE;
|
||||
}
|
||||
request.write(pixelsHeader, bytesTransferred, nextChunkSize);
|
||||
bytesTransferred += nextChunkSize;
|
||||
}
|
||||
request.writeBytes(lineEnd);
|
||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||
request.flush();
|
||||
request.close();
|
||||
}
|
||||
|
||||
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
||||
new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
|
||||
} else {
|
||||
String error = null;
|
||||
if( httpsURLConnection.getErrorStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
|
||||
else if( httpsURLConnection.getInputStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
|
||||
int responseCode = httpsURLConnection.getResponseCode();
|
||||
try {
|
||||
httpsURLConnection.getInputStream().close();
|
||||
}catch (Exception ignored){}
|
||||
|
||||
throw new HttpsConnectionException(responseCode, error);
|
||||
}
|
||||
httpsURLConnection.getInputStream().close();
|
||||
}else {
|
||||
URL url = new URL(urlConnection);
|
||||
Map<String, Object> params = new LinkedHashMap<>();
|
||||
if (paramaters != null) {
|
||||
Iterator it = paramaters.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
params.put(pair.getKey().toString(), pair.getValue());
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
StringBuilder postData = new StringBuilder();
|
||||
for (Map.Entry<String, Object> param : params.entrySet()) {
|
||||
if (postData.length() != 0) postData.append('&');
|
||||
postData.append(param.getKey());
|
||||
postData.append('=');
|
||||
postData.append(String.valueOf(param.getValue()));
|
||||
}
|
||||
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
|
||||
|
||||
if (proxy != null)
|
||||
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
||||
else
|
||||
httpURLConnection = (HttpURLConnection) url.openConnection();
|
||||
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
|
||||
httpURLConnection.setConnectTimeout(timeout * 1000);
|
||||
httpURLConnection.setRequestMethod("PATCH");
|
||||
if (token != null)
|
||||
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
||||
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
||||
httpURLConnection.setDoOutput(true);
|
||||
|
||||
httpURLConnection.getOutputStream().write(postDataBytes);
|
||||
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
||||
new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
|
||||
} else {
|
||||
String error = null;
|
||||
if( httpsURLConnection.getErrorStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
|
||||
else if( httpsURLConnection.getInputStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
|
||||
int responseCode = httpURLConnection.getResponseCode();
|
||||
httpURLConnection.getInputStream().close();
|
||||
throw new HttpsConnectionException(responseCode, error);
|
||||
}
|
||||
httpURLConnection.getInputStream().close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Upload method - https only
|
||||
* @param inputStream InputStream of the file to upload
|
||||
|
@ -1036,116 +1263,7 @@ public class HttpsConnection {
|
|||
|
||||
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
||||
if( urlConnection.startsWith("https://")) {
|
||||
URL url = new URL(urlConnection);
|
||||
Map<String, Object> params = new LinkedHashMap<>();
|
||||
if (paramaters != null) {
|
||||
Iterator it = paramaters.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
params.put(pair.getKey().toString(), pair.getValue());
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
StringBuilder postData = new StringBuilder();
|
||||
for (Map.Entry<String, Object> param : params.entrySet()) {
|
||||
if (postData.length() != 0) postData.append('&');
|
||||
postData.append(param.getKey());
|
||||
postData.append('=');
|
||||
postData.append(String.valueOf(param.getValue()));
|
||||
}
|
||||
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
|
||||
|
||||
if (proxy != null)
|
||||
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
||||
else
|
||||
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
||||
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
|
||||
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
||||
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
|
||||
if( Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ){
|
||||
httpsURLConnection.setRequestMethod("PATCH");
|
||||
}else {
|
||||
httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
|
||||
httpsURLConnection.setRequestMethod("POST");
|
||||
}
|
||||
|
||||
if (token != null)
|
||||
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
||||
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
||||
httpsURLConnection.setDoOutput(true);
|
||||
|
||||
httpsURLConnection.getOutputStream().write(postDataBytes);
|
||||
Log.v(Helper.TAG,"postDataBytes: " + postData.toString());
|
||||
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
||||
new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
|
||||
} else {
|
||||
String error = null;
|
||||
if( httpsURLConnection.getErrorStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
|
||||
else if( httpsURLConnection.getInputStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
|
||||
int responseCode = httpsURLConnection.getResponseCode();
|
||||
try {
|
||||
httpsURLConnection.getInputStream().close();
|
||||
}catch (Exception ignored){}
|
||||
|
||||
throw new HttpsConnectionException(responseCode, error);
|
||||
}
|
||||
httpsURLConnection.getInputStream().close();
|
||||
}else {
|
||||
URL url = new URL(urlConnection);
|
||||
Map<String, Object> params = new LinkedHashMap<>();
|
||||
if (paramaters != null) {
|
||||
Iterator it = paramaters.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry) it.next();
|
||||
params.put(pair.getKey().toString(), pair.getValue());
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
StringBuilder postData = new StringBuilder();
|
||||
for (Map.Entry<String, Object> param : params.entrySet()) {
|
||||
if (postData.length() != 0) postData.append('&');
|
||||
postData.append(param.getKey());
|
||||
postData.append('=');
|
||||
postData.append(String.valueOf(param.getValue()));
|
||||
}
|
||||
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
|
||||
|
||||
if (proxy != null)
|
||||
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
||||
else
|
||||
httpURLConnection = (HttpURLConnection) url.openConnection();
|
||||
httpURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
|
||||
httpURLConnection.setConnectTimeout(timeout * 1000);
|
||||
httpURLConnection.setRequestMethod("PATCH");
|
||||
if (token != null)
|
||||
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
||||
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
||||
httpURLConnection.setDoOutput(true);
|
||||
|
||||
httpURLConnection.getOutputStream().write(postDataBytes);
|
||||
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
||||
new String(ByteStreams.toByteArray(httpURLConnection.getInputStream()));
|
||||
} else {
|
||||
String error = null;
|
||||
if( httpsURLConnection.getErrorStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getErrorStream()));
|
||||
else if( httpsURLConnection.getInputStream() != null)
|
||||
error = new String(ByteStreams.toByteArray(httpsURLConnection.getInputStream()));
|
||||
int responseCode = httpURLConnection.getResponseCode();
|
||||
httpURLConnection.getInputStream().close();
|
||||
throw new HttpsConnectionException(responseCode, error);
|
||||
}
|
||||
httpURLConnection.getInputStream().close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int delete(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
||||
|
|
|
@ -138,7 +138,6 @@ import fr.gouv.etalab.mastodon.R;
|
|||
import fr.gouv.etalab.mastodon.activities.HashTagActivity;
|
||||
import fr.gouv.etalab.mastodon.activities.LoginActivity;
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.activities.MediaActivity;
|
||||
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
|
||||
import fr.gouv.etalab.mastodon.activities.WebviewActivity;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RemoveAccountAsyncTask;
|
||||
|
@ -164,10 +163,11 @@ import static android.content.Context.DOWNLOAD_SERVICE;
|
|||
* - Reusable methods are implemented in this section
|
||||
*/
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class Helper {
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||
public static final String TAG = "mastodon_etalab";
|
||||
public static final String CLIENT_NAME_VALUE = "Mastalab";
|
||||
public static final String OAUTH_SCOPES = "read write follow";
|
||||
|
@ -2038,7 +2038,7 @@ public class Helper {
|
|||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
int resizeSet = sharedpreferences.getInt(Helper.SET_PICTURE_RESIZE, Helper.S_1MO);
|
||||
if( mediaType == MediaType.PROFILE)
|
||||
resizeSet = Helper.S_2MO;
|
||||
resizeSet = Helper.S_1MO;
|
||||
double resizeby = size;
|
||||
if( resizeSet == Helper.S_512KO){
|
||||
resizeby = 4194304;
|
||||
|
@ -2047,10 +2047,7 @@ public class Helper {
|
|||
}else if(resizeSet == Helper.S_2MO){
|
||||
resizeby = 16777216;
|
||||
}
|
||||
Log.v(Helper.TAG,"resizeby: " + resizeby);
|
||||
|
||||
double resize = ((double)size)/resizeby;
|
||||
Log.v(Helper.TAG,"resize: " + resize);
|
||||
if( resize > 1 ){
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
String mime = cr.getType(uriFile);
|
||||
|
@ -2062,7 +2059,6 @@ public class Helper {
|
|||
else
|
||||
adjustedBitmap = newBitmap;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
Log.v(Helper.TAG,"mime: " + mime);
|
||||
if( mime !=null && (mime.contains("png") || mime.contains(".PNG")))
|
||||
adjustedBitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
|
||||
else
|
||||
|
@ -2107,4 +2103,14 @@ public class Helper {
|
|||
}
|
||||
return bs;
|
||||
}
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public static void largeLog(String content) {
|
||||
if (content.length() > 4000) {
|
||||
Log.v(Helper.TAG, content.substring(0, 4000));
|
||||
largeLog(content.substring(4000));
|
||||
} else {
|
||||
Log.v(Helper.TAG, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue