Adds file name
This commit is contained in:
parent
789afba048
commit
ec2dd65e4b
|
@ -103,6 +103,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
||||||
private ImageView pp_actionBar;
|
private ImageView pp_actionBar;
|
||||||
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_HEADER = 754;
|
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_HEADER = 754;
|
||||||
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_PICTURE = 755;
|
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_PICTURE = 755;
|
||||||
|
private String avatarName, headerName;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -400,7 +401,8 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
GlideApp.get(getApplicationContext()).clearMemory();
|
GlideApp.get(getApplicationContext()).clearMemory();
|
||||||
new UpdateCredentialAsyncTask(getApplicationContext(), profile_username, profile_note, profile_picture, header_picture, profile_privacy, EditProfileActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
||||||
|
new UpdateCredentialAsyncTask(getApplicationContext(), profile_username, profile_note, profile_picture, avatarName, header_picture, headerName, profile_privacy, EditProfileActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
||||||
|
@ -452,6 +454,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Uri fileUri = data.getData();
|
Uri fileUri = data.getData();
|
||||||
|
headerName = Helper.getFileName(EditProfileActivity.this, fileUri);
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
|
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
|
||||||
assert inputStream != null;
|
assert inputStream != null;
|
||||||
|
@ -469,6 +472,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Uri fileUri = data.getData();
|
Uri fileUri = data.getData();
|
||||||
|
avatarName = Helper.getFileName(EditProfileActivity.this, fileUri);
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
|
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(fileUri);
|
||||||
assert inputStream != null;
|
assert inputStream != null;
|
||||||
|
|
|
@ -32,14 +32,14 @@ import fr.gouv.etalab.mastodon.interfaces.OnUpdateCredentialInterface;
|
||||||
|
|
||||||
public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
|
public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
|
|
||||||
private String display_name, note;
|
private String display_name, note, avatarName, headerName;
|
||||||
private ByteArrayInputStream avatar, header;
|
private ByteArrayInputStream avatar, header;
|
||||||
private API.accountPrivacy privacy;
|
private API.accountPrivacy privacy;
|
||||||
private APIResponse apiResponse;
|
private APIResponse apiResponse;
|
||||||
private OnUpdateCredentialInterface listener;
|
private OnUpdateCredentialInterface listener;
|
||||||
private WeakReference<Context> contextReference;
|
private WeakReference<Context> contextReference;
|
||||||
|
|
||||||
public UpdateCredentialAsyncTask(Context context, String display_name, String note, ByteArrayInputStream avatar, ByteArrayInputStream header, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
|
public UpdateCredentialAsyncTask(Context context, String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
|
||||||
this.contextReference = new WeakReference<>(context);
|
this.contextReference = new WeakReference<>(context);
|
||||||
this.display_name = display_name;
|
this.display_name = display_name;
|
||||||
this.note = note;
|
this.note = note;
|
||||||
|
@ -47,11 +47,13 @@ public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
this.header = header;
|
this.header = header;
|
||||||
this.listener = onUpdateCredentialInterface;
|
this.listener = onUpdateCredentialInterface;
|
||||||
this.privacy = privacy;
|
this.privacy = privacy;
|
||||||
|
this.avatarName = avatarName;
|
||||||
|
this.headerName = headerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
apiResponse = new API(this.contextReference.get()).updateCredential(display_name, note, avatar, header, privacy);
|
apiResponse = new API(this.contextReference.get()).updateCredential(display_name, note, avatar, avatarName, header, headerName, privacy);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -143,7 +143,7 @@ public class API {
|
||||||
* Update credential of the authenticated user *synchronously*
|
* Update credential of the authenticated user *synchronously*
|
||||||
* @return APIResponse
|
* @return APIResponse
|
||||||
*/
|
*/
|
||||||
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, ByteArrayInputStream header, accountPrivacy privacy) {
|
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy) {
|
||||||
|
|
||||||
HashMap<String, String> requestParams = new HashMap<>();
|
HashMap<String, String> requestParams = new HashMap<>();
|
||||||
if( display_name != null)
|
if( display_name != null)
|
||||||
|
@ -162,7 +162,7 @@ public class API {
|
||||||
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
|
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, header, prefKeyOauthTokenT);
|
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, avatarName, header, headerName, prefKeyOauthTokenT);
|
||||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
setError(e.getStatusCode(), e);
|
setError(e.getStatusCode(), e);
|
||||||
|
|
|
@ -628,7 +628,7 @@ public class HttpsConnection {
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("SameParameterValue")
|
@SuppressWarnings("SameParameterValue")
|
||||||
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, InputStream avatar, InputStream header, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
void patch(String urlConnection, int timeout, HashMap<String, String> paramaters, InputStream avatar, String avatarName, InputStream header, String headerName, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
||||||
String twoHyphens = "--";
|
String twoHyphens = "--";
|
||||||
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
|
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
|
||||||
String lineEnd = "\r\n";
|
String lineEnd = "\r\n";
|
||||||
|
@ -676,7 +676,7 @@ public class HttpsConnection {
|
||||||
|
|
||||||
lengthSentAvatar = pixelsAvatar.length;
|
lengthSentAvatar = pixelsAvatar.length;
|
||||||
lengthSentAvatar += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
|
lengthSentAvatar += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
|
||||||
lengthSentAvatar += ("Content-Disposition: form-data; name=\"avatar\";filename=\"avatar.png\"" + lineEnd).getBytes().length;
|
lengthSentAvatar += ("Content-Disposition: form-data; name=\"avatar\";filename=\""+avatarName+"\"" + lineEnd).getBytes().length;
|
||||||
lengthSentAvatar += 2 * (lineEnd).getBytes().length;
|
lengthSentAvatar += 2 * (lineEnd).getBytes().length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -704,7 +704,7 @@ public class HttpsConnection {
|
||||||
|
|
||||||
lengthSentHeader = pixelsHeader.length;
|
lengthSentHeader = pixelsHeader.length;
|
||||||
lengthSentHeader += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
|
lengthSentHeader += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
|
||||||
lengthSentHeader += ("Content-Disposition: form-data; name=\"header\";filename=\"header.png\"" + lineEnd).getBytes().length;
|
lengthSentHeader += ("Content-Disposition: form-data; name=\"header\";filename=\""+headerName+"\"" + lineEnd).getBytes().length;
|
||||||
lengthSentHeader += 2 * (lineEnd).getBytes().length;
|
lengthSentHeader += 2 * (lineEnd).getBytes().length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -725,11 +725,14 @@ public class HttpsConnection {
|
||||||
httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
|
httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
|
||||||
httpsURLConnection.setRequestMethod("POST");
|
httpsURLConnection.setRequestMethod("POST");
|
||||||
}
|
}
|
||||||
|
if( lengthSent > 0) {
|
||||||
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
|
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||||
httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
|
httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
|
||||||
|
}
|
||||||
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
|
|
||||||
|
if( lengthSent > 0)
|
||||||
|
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
|
||||||
|
|
||||||
if (token != null)
|
if (token != null)
|
||||||
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
||||||
|
@ -737,16 +740,15 @@ public class HttpsConnection {
|
||||||
if( lengthSent > 0)
|
if( lengthSent > 0)
|
||||||
httpsURLConnection.setFixedLengthStreamingMode(lengthSent+postDataBytes.length);
|
httpsURLConnection.setFixedLengthStreamingMode(lengthSent+postDataBytes.length);
|
||||||
|
|
||||||
|
OutputStream outputStream = httpsURLConnection.getOutputStream();
|
||||||
httpsURLConnection.getOutputStream().write(postDataBytes);
|
outputStream.write(postDataBytes);
|
||||||
|
|
||||||
if(lengthSentAvatar > 0){
|
if(lengthSentAvatar > 0){
|
||||||
OutputStream outPutStream = httpsURLConnection.getOutputStream();
|
DataOutputStream request = new DataOutputStream(outputStream);
|
||||||
DataOutputStream request = new DataOutputStream(outPutStream);
|
|
||||||
int totalSize = pixelsAvatar.length;
|
int totalSize = pixelsAvatar.length;
|
||||||
int bytesTransferred = 0;
|
int bytesTransferred = 0;
|
||||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||||
request.writeBytes("Content-Disposition: form-data; name=\"avatar\";filename=\"avatar.png\"" + lineEnd);
|
request.writeBytes("Content-Disposition: form-data; name=\"avatar\";filename=\""+avatarName+"\"" + lineEnd);
|
||||||
request.writeBytes(lineEnd);
|
request.writeBytes(lineEnd);
|
||||||
while (bytesTransferred < totalSize) {
|
while (bytesTransferred < totalSize) {
|
||||||
int nextChunkSize = totalSize - bytesTransferred;
|
int nextChunkSize = totalSize - bytesTransferred;
|
||||||
|
@ -755,6 +757,7 @@ public class HttpsConnection {
|
||||||
}
|
}
|
||||||
request.write(pixelsAvatar, bytesTransferred, nextChunkSize);
|
request.write(pixelsAvatar, bytesTransferred, nextChunkSize);
|
||||||
bytesTransferred += nextChunkSize;
|
bytesTransferred += nextChunkSize;
|
||||||
|
request.flush();
|
||||||
}
|
}
|
||||||
request.writeBytes(lineEnd);
|
request.writeBytes(lineEnd);
|
||||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||||
|
@ -768,7 +771,7 @@ public class HttpsConnection {
|
||||||
OutputStream outPutStream = httpsURLConnection.getOutputStream();
|
OutputStream outPutStream = httpsURLConnection.getOutputStream();
|
||||||
DataOutputStream request = new DataOutputStream(outPutStream);
|
DataOutputStream request = new DataOutputStream(outPutStream);
|
||||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||||
request.writeBytes("Content-Disposition: form-data; name=\"header\";filename=\"header.png\"" + lineEnd);
|
request.writeBytes("Content-Disposition: form-data; name=\"header\";filename=\""+headerName+"\"" + lineEnd);
|
||||||
request.writeBytes(lineEnd);
|
request.writeBytes(lineEnd);
|
||||||
while (bytesTransferred < totalSize) {
|
while (bytesTransferred < totalSize) {
|
||||||
int nextChunkSize = totalSize - bytesTransferred;
|
int nextChunkSize = totalSize - bytesTransferred;
|
||||||
|
@ -777,6 +780,7 @@ public class HttpsConnection {
|
||||||
}
|
}
|
||||||
request.write(pixelsHeader, bytesTransferred, nextChunkSize);
|
request.write(pixelsHeader, bytesTransferred, nextChunkSize);
|
||||||
bytesTransferred += nextChunkSize;
|
bytesTransferred += nextChunkSize;
|
||||||
|
request.flush();
|
||||||
}
|
}
|
||||||
request.writeBytes(lineEnd);
|
request.writeBytes(lineEnd);
|
||||||
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
||||||
|
|
|
@ -21,6 +21,7 @@ import android.annotation.SuppressLint;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.NotificationChannel;
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Matrix;
|
import android.graphics.Matrix;
|
||||||
|
@ -29,6 +30,7 @@ import android.graphics.Rect;
|
||||||
import android.graphics.RectF;
|
import android.graphics.RectF;
|
||||||
import android.os.CountDownTimer;
|
import android.os.CountDownTimer;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
import android.provider.OpenableColumns;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.customtabs.CustomTabsIntent;
|
import android.support.customtabs.CustomTabsIntent;
|
||||||
import android.support.media.ExifInterface;
|
import android.support.media.ExifInterface;
|
||||||
|
@ -2104,6 +2106,20 @@ public class Helper {
|
||||||
return bs;
|
return bs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getFileName(Context context, Uri uri) {
|
||||||
|
ContentResolver resolver = context.getContentResolver();
|
||||||
|
Cursor returnCursor =
|
||||||
|
resolver.query(uri, null, null, null, null);
|
||||||
|
assert returnCursor != null;
|
||||||
|
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||||
|
returnCursor.moveToFirst();
|
||||||
|
String name = returnCursor.getString(nameIndex);
|
||||||
|
returnCursor.close();
|
||||||
|
Random r = new Random();
|
||||||
|
int suf = r.nextInt(9999 - 1000) + 1000;
|
||||||
|
return String.valueOf(suf)+name;
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
@SuppressWarnings("WeakerAccess")
|
||||||
public static void largeLog(String content) {
|
public static void largeLog(String content) {
|
||||||
if (content.length() > 4000) {
|
if (content.length() > 4000) {
|
||||||
|
|
Loading…
Reference in New Issue