2017-04-28 13:29:42 +10:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2017-04-17 08:51:09 +10:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.Manifest;
|
2017-04-19 14:01:04 +10:00
|
|
|
import android.content.ContentResolver;
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
2017-04-19 14:01:04 +10:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.net.Uri;
|
2017-04-19 14:01:04 +10:00
|
|
|
import android.os.AsyncTask;
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.os.Build;
|
2017-04-17 08:51:09 +10:00
|
|
|
import android.os.Bundle;
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.support.annotation.NonNull;
|
2017-04-17 08:51:09 +10:00
|
|
|
import android.support.annotation.Nullable;
|
2017-04-21 08:56:36 +10:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-04-17 08:51:09 +10:00
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-04-19 14:01:04 +10:00
|
|
|
import android.util.Base64;
|
2017-04-17 08:51:09 +10:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2017-04-17 16:49:56 +10:00
|
|
|
import android.view.View;
|
2017-04-17 08:51:09 +10:00
|
|
|
import android.widget.EditText;
|
2017-05-01 05:57:15 +10:00
|
|
|
import android.widget.ImageButton;
|
2017-04-19 14:01:04 +10:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ProgressBar;
|
2017-04-17 08:51:09 +10:00
|
|
|
|
|
|
|
import com.keylesspalace.tusky.entity.Account;
|
2017-04-17 16:49:56 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Profile;
|
2017-05-05 08:55:34 +10:00
|
|
|
import com.keylesspalace.tusky.util.IOUtils;
|
|
|
|
import com.keylesspalace.tusky.util.Log;
|
2017-05-01 23:23:34 +10:00
|
|
|
import com.pkmmte.view.CircularImageView;
|
|
|
|
import com.squareup.picasso.Picasso;
|
2017-04-19 14:01:04 +10:00
|
|
|
import com.theartofdev.edmodo.cropper.CropImage;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-04-17 08:51:09 +10:00
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
public class EditProfileActivity extends BaseActivity {
|
|
|
|
private static final String TAG = "EditProfileActivity";
|
2017-04-19 14:01:04 +10:00
|
|
|
private static final int AVATAR_PICK_RESULT = 1;
|
|
|
|
private static final int HEADER_PICK_RESULT = 2;
|
2017-04-17 16:49:56 +10:00
|
|
|
private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
|
2017-04-19 14:01:04 +10:00
|
|
|
private static final int AVATAR_WIDTH = 120;
|
|
|
|
private static final int AVATAR_HEIGHT = 120;
|
|
|
|
private static final int HEADER_WIDTH = 700;
|
|
|
|
private static final int HEADER_HEIGHT = 335;
|
|
|
|
|
|
|
|
private enum PickType {
|
|
|
|
NOTHING,
|
|
|
|
AVATAR,
|
|
|
|
HEADER
|
|
|
|
}
|
2017-04-17 08:51:09 +10:00
|
|
|
|
2017-05-01 05:57:15 +10:00
|
|
|
@BindView(R.id.edit_profile_header) ImageButton headerButton;
|
2017-04-19 14:01:04 +10:00
|
|
|
@BindView(R.id.edit_profile_header_preview) ImageView headerPreview;
|
|
|
|
@BindView(R.id.edit_profile_header_progress) ProgressBar headerProgress;
|
2017-05-01 05:57:15 +10:00
|
|
|
@BindView(R.id.edit_profile_avatar) ImageButton avatarButton;
|
|
|
|
@BindView(R.id.edit_profile_avatar_preview) ImageView avatarPreview;
|
|
|
|
@BindView(R.id.edit_profile_avatar_progress) ProgressBar avatarProgress;
|
|
|
|
@BindView(R.id.edit_profile_display_name) EditText displayNameEditText;
|
|
|
|
@BindView(R.id.edit_profile_note) EditText noteEditText;
|
2017-04-21 08:56:36 +10:00
|
|
|
@BindView(R.id.edit_profile_save_progress) ProgressBar saveProgress;
|
2017-04-17 08:51:09 +10:00
|
|
|
|
|
|
|
private String priorDisplayName;
|
|
|
|
private String priorNote;
|
2017-04-17 16:49:56 +10:00
|
|
|
private boolean isAlreadySaving;
|
2017-04-19 14:01:04 +10:00
|
|
|
private PickType currentlyPicking;
|
|
|
|
private String avatarBase64;
|
|
|
|
private String headerBase64;
|
2017-04-17 08:51:09 +10:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_edit_profile);
|
|
|
|
ButterKnife.bind(this);
|
|
|
|
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
2017-05-01 05:57:15 +10:00
|
|
|
actionBar.setTitle(getString(R.string.title_edit_profile));
|
2017-04-17 08:51:09 +10:00
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
priorDisplayName = savedInstanceState.getString("priorDisplayName");
|
|
|
|
priorNote = savedInstanceState.getString("priorNote");
|
2017-04-17 16:49:56 +10:00
|
|
|
isAlreadySaving = savedInstanceState.getBoolean("isAlreadySaving");
|
2017-04-19 14:01:04 +10:00
|
|
|
currentlyPicking = (PickType) savedInstanceState.getSerializable("currentlyPicking");
|
|
|
|
avatarBase64 = savedInstanceState.getString("avatarBase64");
|
|
|
|
headerBase64 = savedInstanceState.getString("headerBase64");
|
2017-04-17 08:51:09 +10:00
|
|
|
} else {
|
|
|
|
priorDisplayName = null;
|
|
|
|
priorNote = null;
|
2017-04-17 16:49:56 +10:00
|
|
|
isAlreadySaving = false;
|
2017-04-19 14:01:04 +10:00
|
|
|
currentlyPicking = PickType.NOTHING;
|
|
|
|
avatarBase64 = null;
|
|
|
|
headerBase64 = null;
|
2017-04-17 08:51:09 +10:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:57:15 +10:00
|
|
|
|
|
|
|
|
2017-04-17 16:49:56 +10:00
|
|
|
avatarButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-04-19 14:01:04 +10:00
|
|
|
onMediaPick(PickType.AVATAR);
|
2017-04-17 16:49:56 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
headerButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-04-19 14:01:04 +10:00
|
|
|
onMediaPick(PickType.HEADER);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
avatarPreview.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
avatarPreview.setImageBitmap(null);
|
2017-05-01 23:23:34 +10:00
|
|
|
avatarPreview.setVisibility(View.INVISIBLE);
|
2017-04-19 14:01:04 +10:00
|
|
|
avatarBase64 = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
headerPreview.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
headerPreview.setImageBitmap(null);
|
2017-05-01 23:23:34 +10:00
|
|
|
headerPreview.setVisibility(View.INVISIBLE);
|
2017-04-19 14:01:04 +10:00
|
|
|
headerBase64 = null;
|
2017-04-17 16:49:56 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-17 08:51:09 +10:00
|
|
|
mastodonAPI.accountVerifyCredentials().enqueue(new Callback<Account>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<Account> call, Response<Account> response) {
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
onAccountVerifyCredentialsFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Account me = response.body();
|
|
|
|
priorDisplayName = me.getDisplayName();
|
|
|
|
priorNote = me.note.toString();
|
2017-05-01 23:23:34 +10:00
|
|
|
CircularImageView avatar = (CircularImageView) findViewById(R.id.edit_profile_avatar_preview);
|
|
|
|
ImageView header = (ImageView) findViewById(R.id.edit_profile_header_preview);
|
|
|
|
|
2017-04-17 08:51:09 +10:00
|
|
|
displayNameEditText.setText(priorDisplayName);
|
2017-04-17 16:49:56 +10:00
|
|
|
noteEditText.setText(priorNote);
|
2017-05-01 23:23:34 +10:00
|
|
|
Picasso.with(avatar.getContext())
|
|
|
|
.load(me.avatar)
|
|
|
|
.placeholder(R.drawable.avatar_default)
|
|
|
|
.error(R.drawable.avatar_error)
|
|
|
|
.into(avatar);
|
|
|
|
Picasso.with(header.getContext())
|
|
|
|
.load(me.header)
|
2017-05-05 02:07:59 +10:00
|
|
|
.placeholder(R.drawable.account_header_default)
|
2017-05-01 23:23:34 +10:00
|
|
|
.into(header);
|
2017-04-17 08:51:09 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Account> call, Throwable t) {
|
|
|
|
onAccountVerifyCredentialsFailed();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
outState.putString("priorDisplayName", priorDisplayName);
|
|
|
|
outState.putString("priorNote", priorNote);
|
2017-04-17 16:49:56 +10:00
|
|
|
outState.putBoolean("isAlreadySaving", isAlreadySaving);
|
2017-04-19 14:01:04 +10:00
|
|
|
outState.putSerializable("currentlyPicking", currentlyPicking);
|
|
|
|
outState.putString("avatarBase64", avatarBase64);
|
|
|
|
outState.putString("headerBase64", headerBase64);
|
2017-04-17 08:51:09 +10:00
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onAccountVerifyCredentialsFailed() {
|
|
|
|
Log.e(TAG, "The account failed to load.");
|
|
|
|
}
|
|
|
|
|
2017-04-19 14:01:04 +10:00
|
|
|
private void onMediaPick(PickType pickType) {
|
2017-04-21 08:56:36 +10:00
|
|
|
if (currentlyPicking != PickType.NOTHING) {
|
|
|
|
// Ignore inputs if another pick operation is still occurring.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentlyPicking = pickType;
|
2017-04-17 16:49:56 +10:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
|
|
|
|
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
|
ActivityCompat.requestPermissions(this,
|
|
|
|
new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
|
|
|
|
PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
|
|
|
|
} else {
|
|
|
|
initiateMediaPicking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
|
|
|
|
@NonNull int[] grantResults) {
|
|
|
|
switch (requestCode) {
|
|
|
|
case PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
|
|
|
|
if (grantResults.length > 0
|
|
|
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
initiateMediaPicking();
|
|
|
|
} else {
|
2017-04-19 14:01:04 +10:00
|
|
|
endMediaPicking();
|
2017-04-21 08:56:36 +10:00
|
|
|
Snackbar.make(avatarButton, R.string.error_media_upload_permission,
|
|
|
|
Snackbar.LENGTH_LONG).show();
|
2017-04-17 16:49:56 +10:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initiateMediaPicking() {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
2017-04-21 08:56:36 +10:00
|
|
|
intent.setType("image/*");
|
2017-04-19 14:01:04 +10:00
|
|
|
switch (currentlyPicking) {
|
2017-04-21 08:56:36 +10:00
|
|
|
case AVATAR: { startActivityForResult(intent, AVATAR_PICK_RESULT); break; }
|
|
|
|
case HEADER: { startActivityForResult(intent, HEADER_PICK_RESULT); break; }
|
2017-04-19 14:01:04 +10:00
|
|
|
}
|
2017-04-17 16:49:56 +10:00
|
|
|
}
|
|
|
|
|
2017-04-17 08:51:09 +10:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.edit_profile_toolbar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.action_save: {
|
|
|
|
save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void save() {
|
2017-04-19 14:01:04 +10:00
|
|
|
if (isAlreadySaving || currentlyPicking != PickType.NOTHING) {
|
2017-04-17 16:49:56 +10:00
|
|
|
return;
|
|
|
|
}
|
2017-04-17 08:51:09 +10:00
|
|
|
String newDisplayName = displayNameEditText.getText().toString();
|
|
|
|
if (newDisplayName.isEmpty()) {
|
|
|
|
displayNameEditText.setError(getString(R.string.error_empty));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (priorDisplayName != null && priorDisplayName.equals(newDisplayName)) {
|
|
|
|
// If it's not any different, don't patch it.
|
|
|
|
newDisplayName = null;
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:49:56 +10:00
|
|
|
String newNote = noteEditText.getText().toString();
|
2017-04-17 08:51:09 +10:00
|
|
|
if (newNote.isEmpty()) {
|
|
|
|
noteEditText.setError(getString(R.string.error_empty));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (priorNote != null && priorNote.equals(newNote)) {
|
|
|
|
// If it's not any different, don't patch it.
|
|
|
|
newNote = null;
|
|
|
|
}
|
2017-04-21 08:56:36 +10:00
|
|
|
if (newDisplayName == null && newNote == null && avatarBase64 == null
|
|
|
|
&& headerBase64 == null) {
|
|
|
|
// If nothing is changed, then there's nothing to save.
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 08:51:09 +10:00
|
|
|
|
2017-04-21 08:56:36 +10:00
|
|
|
saveProgress.setVisibility(View.VISIBLE);
|
2017-04-17 16:49:56 +10:00
|
|
|
|
2017-04-21 08:56:36 +10:00
|
|
|
isAlreadySaving = true;
|
2017-04-19 14:01:04 +10:00
|
|
|
|
2017-04-17 16:49:56 +10:00
|
|
|
Profile profile = new Profile();
|
|
|
|
profile.displayName = newDisplayName;
|
|
|
|
profile.note = newNote;
|
2017-04-19 14:01:04 +10:00
|
|
|
profile.avatar = avatarBase64;
|
|
|
|
profile.header = headerBase64;
|
2017-04-17 16:49:56 +10:00
|
|
|
mastodonAPI.accountUpdateCredentials(profile).enqueue(new Callback<Account>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(Call<Account> call, Response<Account> response) {
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
onSaveFailure();
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 08:56:36 +10:00
|
|
|
getPrivatePreferences().edit()
|
|
|
|
.putBoolean("refreshProfileHeader", true)
|
|
|
|
.apply();
|
2017-04-17 16:49:56 +10:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(Call<Account> call, Throwable t) {
|
|
|
|
onSaveFailure();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onSaveFailure() {
|
|
|
|
isAlreadySaving = false;
|
2017-04-21 08:56:36 +10:00
|
|
|
Snackbar.make(avatarButton, R.string.error_media_upload_sending, Snackbar.LENGTH_LONG)
|
|
|
|
.show();
|
|
|
|
saveProgress.setVisibility(View.GONE);
|
2017-04-17 16:49:56 +10:00
|
|
|
}
|
|
|
|
|
2017-04-21 08:56:36 +10:00
|
|
|
private void beginMediaPicking() {
|
2017-04-19 14:01:04 +10:00
|
|
|
switch (currentlyPicking) {
|
2017-04-21 08:56:36 +10:00
|
|
|
case AVATAR: {
|
|
|
|
avatarProgress.setVisibility(View.VISIBLE);
|
|
|
|
avatarPreview.setVisibility(View.INVISIBLE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HEADER: {
|
|
|
|
headerProgress.setVisibility(View.VISIBLE);
|
|
|
|
headerPreview.setVisibility(View.INVISIBLE);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-19 14:01:04 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void endMediaPicking() {
|
|
|
|
switch (currentlyPicking) {
|
2017-04-21 08:56:36 +10:00
|
|
|
case AVATAR: {
|
|
|
|
avatarProgress.setVisibility(View.GONE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HEADER: {
|
|
|
|
headerProgress.setVisibility(View.GONE);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-19 14:01:04 +10:00
|
|
|
}
|
|
|
|
currentlyPicking = PickType.NOTHING;
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:49:56 +10:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2017-04-19 14:01:04 +10:00
|
|
|
switch (requestCode) {
|
|
|
|
case AVATAR_PICK_RESULT: {
|
|
|
|
if (resultCode == RESULT_OK && data != null) {
|
|
|
|
CropImage.activity(data.getData())
|
|
|
|
.setInitialCropWindowPaddingRatio(0)
|
|
|
|
.setAspectRatio(AVATAR_WIDTH, AVATAR_HEIGHT)
|
|
|
|
.start(this);
|
2017-04-21 08:56:36 +10:00
|
|
|
} else {
|
|
|
|
endMediaPicking();
|
2017-04-19 14:01:04 +10:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HEADER_PICK_RESULT: {
|
|
|
|
if (resultCode == RESULT_OK && data != null) {
|
|
|
|
CropImage.activity(data.getData())
|
|
|
|
.setInitialCropWindowPaddingRatio(0)
|
|
|
|
.setAspectRatio(HEADER_WIDTH, HEADER_HEIGHT)
|
|
|
|
.start(this);
|
2017-04-21 08:56:36 +10:00
|
|
|
} else {
|
|
|
|
endMediaPicking();
|
2017-04-19 14:01:04 +10:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: {
|
|
|
|
CropImage.ActivityResult result = CropImage.getActivityResult(data);
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
beginResize(result.getUri());
|
|
|
|
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
|
|
|
|
onResizeFailure();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void beginResize(Uri uri) {
|
2017-04-21 08:56:36 +10:00
|
|
|
beginMediaPicking();
|
2017-04-19 14:01:04 +10:00
|
|
|
int width, height;
|
|
|
|
switch (currentlyPicking) {
|
|
|
|
default: {
|
|
|
|
throw new AssertionError("PickType not set.");
|
|
|
|
}
|
|
|
|
case AVATAR: {
|
|
|
|
width = AVATAR_WIDTH;
|
|
|
|
height = AVATAR_HEIGHT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HEADER: {
|
|
|
|
width = HEADER_WIDTH;
|
|
|
|
height = HEADER_HEIGHT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new ResizeImageTask(getContentResolver(), width, height, new ResizeImageTask.Listener() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(List<Bitmap> contentList) {
|
|
|
|
Bitmap bitmap = contentList.get(0);
|
2017-04-21 08:56:36 +10:00
|
|
|
PickType pickType = currentlyPicking;
|
|
|
|
endMediaPicking();
|
|
|
|
switch (pickType) {
|
2017-04-19 14:01:04 +10:00
|
|
|
case AVATAR: {
|
|
|
|
avatarPreview.setImageBitmap(bitmap);
|
|
|
|
avatarPreview.setVisibility(View.VISIBLE);
|
|
|
|
avatarBase64 = bitmapToBase64(bitmap);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HEADER: {
|
|
|
|
headerPreview.setImageBitmap(bitmap);
|
|
|
|
headerPreview.setVisibility(View.VISIBLE);
|
|
|
|
headerBase64 = bitmapToBase64(bitmap);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure() {
|
|
|
|
onResizeFailure();
|
|
|
|
}
|
|
|
|
}).execute(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onResizeFailure() {
|
2017-04-21 08:56:36 +10:00
|
|
|
Snackbar.make(avatarButton, R.string.error_media_upload_sending, Snackbar.LENGTH_LONG)
|
|
|
|
.show();
|
2017-04-19 14:01:04 +10:00
|
|
|
endMediaPicking();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String bitmapToBase64(Bitmap bitmap) {
|
|
|
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
|
|
|
byte[] byteArray = stream.toByteArray();
|
|
|
|
IOUtils.closeQuietly(stream);
|
|
|
|
return "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ResizeImageTask extends AsyncTask<Uri, Void, Boolean> {
|
|
|
|
private ContentResolver contentResolver;
|
|
|
|
private int resizeWidth;
|
|
|
|
private int resizeHeight;
|
|
|
|
private Listener listener;
|
|
|
|
private List<Bitmap> resultList;
|
|
|
|
|
|
|
|
ResizeImageTask(ContentResolver contentResolver, int width, int height, Listener listener) {
|
|
|
|
this.contentResolver = contentResolver;
|
|
|
|
this.resizeWidth = width;
|
|
|
|
this.resizeHeight = height;
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Uri... uris) {
|
|
|
|
resultList = new ArrayList<>();
|
|
|
|
for (Uri uri : uris) {
|
|
|
|
InputStream inputStream;
|
|
|
|
try {
|
|
|
|
inputStream = contentResolver.openInputStream(uri);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Bitmap sourceBitmap;
|
|
|
|
try {
|
|
|
|
sourceBitmap = BitmapFactory.decodeStream(inputStream, null, null);
|
|
|
|
} catch (OutOfMemoryError error) {
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
IOUtils.closeQuietly(inputStream);
|
|
|
|
}
|
|
|
|
if (sourceBitmap == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Bitmap bitmap = Bitmap.createScaledBitmap(sourceBitmap, resizeWidth, resizeHeight,
|
|
|
|
false);
|
|
|
|
sourceBitmap.recycle();
|
|
|
|
if (bitmap == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
resultList.add(bitmap);
|
|
|
|
if (isCancelled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean successful) {
|
|
|
|
if (successful) {
|
|
|
|
listener.onSuccess(resultList);
|
|
|
|
} else {
|
|
|
|
listener.onFailure();
|
|
|
|
}
|
|
|
|
super.onPostExecute(successful);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Listener {
|
|
|
|
void onSuccess(List<Bitmap> contentList);
|
|
|
|
void onFailure();
|
2017-04-17 16:49:56 +10:00
|
|
|
}
|
2017-04-17 08:51:09 +10:00
|
|
|
}
|
|
|
|
}
|