2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Intent;
|
2016-01-13 11:05:59 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2017-12-15 19:49:48 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-12-16 10:00:49 +00:00
|
|
|
import android.support.annotation.StringRes;
|
|
|
|
import android.util.Log;
|
2016-03-04 13:32:38 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnLongClickListener;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2015-01-15 15:26:46 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2015-11-06 10:14:05 +00:00
|
|
|
import com.soundcloud.android.crop.Crop;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
2017-12-16 10:00:49 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
2016-04-07 18:29:40 +00:00
|
|
|
import eu.siacs.conversations.persistance.FileBackend;
|
2017-12-16 10:00:49 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2016-01-04 14:17:02 +00:00
|
|
|
import eu.siacs.conversations.utils.FileUtils;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.utils.PhoneHelper;
|
|
|
|
import eu.siacs.conversations.xmpp.pep.Avatar;
|
|
|
|
|
2017-12-16 10:00:49 +00:00
|
|
|
public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate {
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2016-03-04 13:32:38 +00:00
|
|
|
private static final int REQUEST_CHOOSE_FILE_AND_CROP = 0xac23;
|
|
|
|
private static final int REQUEST_CHOOSE_FILE = 0xac24;
|
2014-10-22 16:38:44 +00:00
|
|
|
private ImageView avatar;
|
|
|
|
private TextView hintOrWarning;
|
|
|
|
private TextView secondaryHint;
|
|
|
|
private Button cancelButton;
|
|
|
|
private Button publishButton;
|
|
|
|
private Uri avatarUri;
|
|
|
|
private Uri defaultUri;
|
2016-01-04 14:17:02 +00:00
|
|
|
private Account account;
|
|
|
|
private boolean support = false;
|
2017-12-16 10:00:49 +00:00
|
|
|
private boolean publishing = false;
|
2015-01-15 15:26:46 +00:00
|
|
|
private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-01-15 15:26:46 +00:00
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
avatarUri = defaultUri;
|
|
|
|
loadImageIntoPreview(defaultUri);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-10-22 16:38:44 +00:00
|
|
|
private boolean mInitialAccountSetup;
|
|
|
|
private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void success(Avatar object) {
|
2017-12-15 19:49:48 +00:00
|
|
|
runOnUiThread(() -> {
|
|
|
|
if (mInitialAccountSetup) {
|
2017-12-16 19:38:14 +00:00
|
|
|
Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
|
2018-02-14 15:55:45 +00:00
|
|
|
WelcomeActivity.addInviteUri(intent, getIntent());
|
2017-12-15 19:49:48 +00:00
|
|
|
intent.putExtra("init", true);
|
|
|
|
startActivity(intent);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2017-12-15 19:49:48 +00:00
|
|
|
Toast.makeText(PublishProfilePictureActivity.this,
|
|
|
|
R.string.avatar_has_been_published,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
2014-10-22 16:38:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void error(final int errorCode, Avatar object) {
|
2017-12-15 19:49:48 +00:00
|
|
|
runOnUiThread(() -> {
|
|
|
|
hintOrWarning.setText(errorCode);
|
|
|
|
hintOrWarning.setTextColor(getWarningTextColor());
|
2017-12-15 20:47:16 +00:00
|
|
|
hintOrWarning.setVisibility(View.VISIBLE);
|
2017-12-16 10:00:49 +00:00
|
|
|
publishing = false;
|
|
|
|
togglePublishButton(true,R.string.publish);
|
2014-10-22 16:38:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userInputRequried(PendingIntent pi, Avatar object) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_publish_profile_picture);
|
2017-12-15 19:49:48 +00:00
|
|
|
this.avatar = findViewById(R.id.account_image);
|
|
|
|
this.cancelButton = findViewById(R.id.cancel_button);
|
|
|
|
this.publishButton = findViewById(R.id.publish_button);
|
|
|
|
this.hintOrWarning = findViewById(R.id.hint_or_warning);
|
|
|
|
this.secondaryHint = findViewById(R.id.secondary_hint);
|
|
|
|
this.publishButton.setOnClickListener(v -> {
|
|
|
|
if (avatarUri != null) {
|
2017-12-16 10:00:49 +00:00
|
|
|
publishing = true;
|
|
|
|
togglePublishButton(false,R.string.publishing);
|
|
|
|
xmppConnectionService.publishAvatar(account, avatarUri, avatarPublication);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
});
|
2017-12-15 19:49:48 +00:00
|
|
|
this.cancelButton.setOnClickListener(v -> {
|
|
|
|
if (mInitialAccountSetup) {
|
2017-12-16 19:38:14 +00:00
|
|
|
Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
|
2017-12-15 19:49:48 +00:00
|
|
|
if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
|
2018-02-14 15:55:45 +00:00
|
|
|
WelcomeActivity.addInviteUri(intent, getIntent());
|
2017-12-15 19:49:48 +00:00
|
|
|
intent.putExtra("init", true);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2017-12-15 19:49:48 +00:00
|
|
|
startActivity(intent);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2017-12-15 19:49:48 +00:00
|
|
|
finish();
|
2014-10-22 16:38:44 +00:00
|
|
|
});
|
2017-12-15 19:49:48 +00:00
|
|
|
this.avatar.setOnClickListener(v -> {
|
|
|
|
if (hasStoragePermission(REQUEST_CHOOSE_FILE)) {
|
|
|
|
chooseAvatar(false);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2017-12-15 19:49:48 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
});
|
2017-11-17 09:28:51 +00:00
|
|
|
this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 13:32:38 +00:00
|
|
|
private void chooseAvatar(boolean crop) {
|
2016-01-13 11:05:59 +00:00
|
|
|
Intent attachFileIntent = new Intent();
|
|
|
|
attachFileIntent.setType("image/*");
|
|
|
|
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
|
|
|
|
Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file));
|
2016-03-04 13:32:38 +00:00
|
|
|
startActivityForResult(chooser, crop ? REQUEST_CHOOSE_FILE_AND_CROP : REQUEST_CHOOSE_FILE);
|
2016-01-13 11:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-15 19:49:48 +00:00
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
|
2016-01-13 11:05:59 +00:00
|
|
|
if (grantResults.length > 0)
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
2016-03-04 13:32:38 +00:00
|
|
|
if (requestCode == REQUEST_CHOOSE_FILE_AND_CROP) {
|
|
|
|
chooseAvatar(true);
|
|
|
|
} else if (requestCode == REQUEST_CHOOSE_FILE) {
|
|
|
|
chooseAvatar(false);
|
2016-01-13 11:05:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 13:32:38 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.publish_avatar, menu);
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
|
|
|
if (item.getItemId() == R.id.action_crop_image) {
|
|
|
|
if (hasStoragePermission(REQUEST_CHOOSE_FILE_AND_CROP)) {
|
|
|
|
chooseAvatar(true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
2016-03-13 16:38:59 +00:00
|
|
|
return super.onOptionsItemSelected(item);
|
2016-03-04 13:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
@Override
|
2016-01-04 14:17:02 +00:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
|
2014-10-22 16:38:44 +00:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
if (resultCode == RESULT_OK) {
|
2016-04-07 18:29:40 +00:00
|
|
|
Uri source = data.getData();
|
2016-01-04 14:17:02 +00:00
|
|
|
switch (requestCode) {
|
2016-03-04 13:32:38 +00:00
|
|
|
case REQUEST_CHOOSE_FILE_AND_CROP:
|
2016-04-13 09:14:36 +00:00
|
|
|
if (FileBackend.weOwnFile(this, source)) {
|
2016-04-07 18:29:40 +00:00
|
|
|
Toast.makeText(this,R.string.security_error_invalid_file_access,Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
2016-01-04 14:17:02 +00:00
|
|
|
String original = FileUtils.getPath(this, source);
|
|
|
|
if (original != null) {
|
|
|
|
source = Uri.parse("file://"+original);
|
|
|
|
}
|
|
|
|
Uri destination = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
|
|
|
|
final int size = getPixel(192);
|
|
|
|
Crop.of(source, destination).asSquare().withMaxSize(size, size).start(this);
|
|
|
|
break;
|
2016-03-04 13:32:38 +00:00
|
|
|
case REQUEST_CHOOSE_FILE:
|
2016-04-13 09:14:36 +00:00
|
|
|
if (FileBackend.weOwnFile(this, source)) {
|
2016-04-07 18:29:40 +00:00
|
|
|
Toast.makeText(this,R.string.security_error_invalid_file_access,Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.avatarUri = source;
|
2016-03-04 13:32:38 +00:00
|
|
|
if (xmppConnectionServiceBound) {
|
|
|
|
loadImageIntoPreview(this.avatarUri);
|
|
|
|
}
|
|
|
|
break;
|
2016-01-04 14:17:02 +00:00
|
|
|
case Crop.REQUEST_CROP:
|
|
|
|
this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
|
2016-03-04 13:32:38 +00:00
|
|
|
if (xmppConnectionServiceBound) {
|
|
|
|
loadImageIntoPreview(this.avatarUri);
|
|
|
|
}
|
2016-01-04 14:17:02 +00:00
|
|
|
break;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-01-15 22:47:16 +00:00
|
|
|
} else {
|
2016-01-20 15:11:17 +00:00
|
|
|
if (requestCode == Crop.REQUEST_CROP && data != null) {
|
2016-01-15 22:47:16 +00:00
|
|
|
Throwable throwable = Crop.getError(data);
|
|
|
|
if (throwable != null && throwable instanceof OutOfMemoryError) {
|
|
|
|
Toast.makeText(this,R.string.selection_too_large, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onBackendConnected() {
|
2016-01-22 19:46:24 +00:00
|
|
|
this.account = extractAccount(getIntent());
|
|
|
|
if (this.account != null) {
|
2017-12-16 10:00:49 +00:00
|
|
|
reloadAvatar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reloadAvatar() {
|
|
|
|
this.support = this.account.getXmppConnection() != null && this.account.getXmppConnection().getFeatures().pep();
|
|
|
|
if (this.avatarUri == null) {
|
|
|
|
if (this.account.getAvatar() != null || this.defaultUri == null) {
|
|
|
|
loadImageIntoPreview(null);
|
2016-01-22 19:46:24 +00:00
|
|
|
} else {
|
2017-12-16 10:00:49 +00:00
|
|
|
this.avatarUri = this.defaultUri;
|
|
|
|
loadImageIntoPreview(this.defaultUri);
|
2016-01-22 19:46:24 +00:00
|
|
|
}
|
2017-12-16 10:00:49 +00:00
|
|
|
} else {
|
|
|
|
loadImageIntoPreview(avatarUri);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
if (getIntent() != null) {
|
2016-01-04 14:17:02 +00:00
|
|
|
this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", false);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
if (this.mInitialAccountSetup) {
|
|
|
|
this.cancelButton.setText(R.string.skip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void loadImageIntoPreview(Uri uri) {
|
2017-12-16 10:00:49 +00:00
|
|
|
|
2015-11-06 10:14:05 +00:00
|
|
|
Bitmap bm = null;
|
2017-12-16 10:00:49 +00:00
|
|
|
if (uri == null) {
|
|
|
|
bm = avatarService().get(account, getPixel(192));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
bm = xmppConnectionService.getFileBackend().cropCenterSquare(uri, getPixel(192));
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(Config.LOGTAG,"unable to load bitmap into image view",e);
|
|
|
|
}
|
2015-11-06 10:14:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
if (bm == null) {
|
2017-12-16 10:00:49 +00:00
|
|
|
togglePublishButton(false,R.string.publish);
|
2017-12-15 20:47:16 +00:00
|
|
|
this.hintOrWarning.setVisibility(View.VISIBLE);
|
2014-10-22 16:38:44 +00:00
|
|
|
this.hintOrWarning.setTextColor(getWarningTextColor());
|
2017-12-15 19:49:48 +00:00
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_converting);
|
2014-10-22 16:38:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.avatar.setImageBitmap(bm);
|
|
|
|
if (support) {
|
2017-12-16 10:00:49 +00:00
|
|
|
togglePublishButton(uri != null,R.string.publish);
|
2017-12-15 20:47:16 +00:00
|
|
|
this.hintOrWarning.setVisibility(View.INVISIBLE);
|
2014-10-22 16:38:44 +00:00
|
|
|
} else {
|
2017-12-16 10:00:49 +00:00
|
|
|
togglePublishButton(false,R.string.publish);
|
2017-12-15 20:47:16 +00:00
|
|
|
this.hintOrWarning.setVisibility(View.VISIBLE);
|
2014-10-22 16:38:44 +00:00
|
|
|
this.hintOrWarning.setTextColor(getWarningTextColor());
|
2016-10-06 20:06:09 +00:00
|
|
|
if (account.getStatus() == Account.State.ONLINE) {
|
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_no_server_support);
|
|
|
|
} else {
|
|
|
|
this.hintOrWarning.setText(R.string.error_publish_avatar_offline);
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2017-12-16 10:00:49 +00:00
|
|
|
if (this.defaultUri == null || this.defaultUri.equals(uri)) {
|
2014-10-22 16:38:44 +00:00
|
|
|
this.secondaryHint.setVisibility(View.INVISIBLE);
|
|
|
|
this.avatar.setOnLongClickListener(null);
|
|
|
|
} else if (this.defaultUri != null) {
|
|
|
|
this.secondaryHint.setVisibility(View.VISIBLE);
|
|
|
|
this.avatar.setOnLongClickListener(this.backToDefaultListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 10:00:49 +00:00
|
|
|
protected void togglePublishButton(boolean enabled, @StringRes int res) {
|
|
|
|
final boolean status = enabled && !publishing;
|
|
|
|
this.publishButton.setText(publishing ? R.string.publishing : res);
|
|
|
|
this.publishButton.setEnabled(status);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 10:00:49 +00:00
|
|
|
public void refreshUiReal() {
|
|
|
|
if (this.account != null) {
|
|
|
|
reloadAvatar();
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 10:00:49 +00:00
|
|
|
@Override
|
|
|
|
public void onAccountUpdate() {
|
|
|
|
refreshUi();
|
2015-07-20 13:48:58 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|