aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
committerlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
commit3c400703e082a1b180b35d891b8fb3460c7d5b87 (patch)
tree28738dd90fc41b4ab71897f38d324828778ad2e3 /src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java
parent72114d732427266024cdd6e27cd8d1aa60afae2f (diff)
parentf28d77dc42f6bac5a026e0b1c78562dee8de45ac (diff)
Merge branch 'trz/rebase' into trz/rename
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java167
1 files changed, 111 insertions, 56 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java
index 2045f001..9b827861 100644
--- a/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java
+++ b/src/main/java/de/thedevstack/conversationsplus/ui/PublishProfilePictureActivity.java
@@ -2,7 +2,9 @@ package de.thedevstack.conversationsplus.ui;
import android.app.PendingIntent;
import android.content.Intent;
+import android.content.pm.PackageManager;
import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
@@ -13,28 +15,34 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
+import com.soundcloud.android.crop.Crop;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+
+import de.thedevstack.conversationsplus.utils.ImageUtil;
+import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.services.AvatarService;
-import de.thedevstack.conversationsplus.utils.ImageUtil;
+import de.thedevstack.conversationsplus.utils.ExifHelper;
+import de.thedevstack.conversationsplus.utils.FileUtils;
import de.thedevstack.conversationsplus.utils.PhoneHelper;
-import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException;
-import de.thedevstack.conversationsplus.xmpp.jid.Jid;
import de.thedevstack.conversationsplus.xmpp.pep.Avatar;
public class PublishProfilePictureActivity extends XmppActivity {
private static final int REQUEST_CHOOSE_FILE = 0xac23;
-
private ImageView avatar;
private TextView accountTextView;
private TextView hintOrWarning;
private TextView secondaryHint;
private Button cancelButton;
private Button publishButton;
-
private Uri avatarUri;
private Uri defaultUri;
+ private Account account;
+ private boolean support = false;
private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
@Override
@@ -44,8 +52,6 @@ public class PublishProfilePictureActivity extends XmppActivity {
return true;
}
};
- private Account account;
- private boolean support = false;
private boolean mInitialAccountSetup;
private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
@@ -58,7 +64,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
if (mInitialAccountSetup) {
Intent intent = new Intent(getApplicationContext(),
StartConversationActivity.class);
- intent.putExtra("init",true);
+ intent.putExtra("init", true);
startActivity(intent);
}
Toast.makeText(PublishProfilePictureActivity.this,
@@ -130,26 +136,60 @@ public class PublishProfilePictureActivity extends XmppActivity {
@Override
public void onClick(View v) {
- Intent attachFileIntent = new Intent();
- attachFileIntent.setType("image/*");
- attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
- Intent chooser = Intent.createChooser(attachFileIntent,
- getString(R.string.attach_file));
- startActivityForResult(chooser, REQUEST_CHOOSE_FILE);
+ if (hasStoragePermission(REQUEST_CHOOSE_FILE)) {
+ chooseAvatar();
+ }
+
}
});
this.defaultUri = PhoneHelper.getSefliUri(getApplicationContext());
}
+ private void chooseAvatar() {
+ Intent attachFileIntent = new Intent();
+ attachFileIntent.setType("image/*");
+ attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
+ Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file));
+ startActivityForResult(chooser, REQUEST_CHOOSE_FILE);
+ }
+
+ @Override
+ public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
+ if (grantResults.length > 0)
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+ if (requestCode == REQUEST_CHOOSE_FILE) {
+ chooseAvatar();
+ }
+ } else {
+ Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
+ }
+ }
+
@Override
- protected void onActivityResult(int requestCode, int resultCode,
- final Intent data) {
+ protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
- if (requestCode == REQUEST_CHOOSE_FILE) {
- this.avatarUri = data.getData();
- if (xmppConnectionServiceBound) {
+ switch (requestCode) {
+ case REQUEST_CHOOSE_FILE:
+ Uri source = data.getData();
+ 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;
+ case Crop.REQUEST_CROP:
+ this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
loadImageIntoPreview(this.avatarUri);
+ break;
+ }
+ } else {
+ if (requestCode == Crop.REQUEST_CROP && data != null) {
+ Throwable throwable = Crop.getError(data);
+ if (throwable != null && throwable instanceof OutOfMemoryError) {
+ Toast.makeText(this,R.string.selection_too_large, Toast.LENGTH_SHORT).show();
}
}
}
@@ -157,63 +197,75 @@ public class PublishProfilePictureActivity extends XmppActivity {
@Override
protected void onBackendConnected() {
- if (getIntent() != null) {
- Jid jid;
- try {
- jid = Jid.fromString(getIntent().getStringExtra("account"));
- } catch (InvalidJidException e) {
- jid = null;
- }
- if (jid != null) {
- this.account = xmppConnectionService.findAccountByJid(jid);
- if (this.account.getXmppConnection() != null) {
- this.support = this.account.getXmppConnection().getFeatures().pep();
- }
- if (this.avatarUri == null) {
- if (this.account.getAvatar() != null
- || this.defaultUri == null) {
- this.avatar.setImageBitmap(AvatarService.getInstance().get(account,
- getPixel(194)));
- if (this.defaultUri != null) {
- this.avatar
- .setOnLongClickListener(this.backToDefaultListener);
- } else {
- this.secondaryHint.setVisibility(View.INVISIBLE);
- }
- if (!support) {
- this.hintOrWarning
- .setTextColor(getWarningTextColor());
- this.hintOrWarning
- .setText(R.string.error_publish_avatar_no_server_support);
- }
+ this.account = extractAccount(getIntent());
+ if (this.account != null) {
+ if (this.account.getXmppConnection() != null) {
+ this.support = this.account.getXmppConnection().getFeatures().pep();
+ }
+ if (this.avatarUri == null) {
+ if (this.account.getAvatar() != null
+ || this.defaultUri == null) {
+ this.avatar.setImageBitmap(AvatarService.getInstance().get(account, getPixel(192)));
+ if (this.defaultUri != null) {
+ this.avatar
+ .setOnLongClickListener(this.backToDefaultListener);
} else {
- this.avatarUri = this.defaultUri;
- loadImageIntoPreview(this.defaultUri);
this.secondaryHint.setVisibility(View.INVISIBLE);
}
+ if (!support) {
+ this.hintOrWarning
+ .setTextColor(getWarningTextColor());
+ this.hintOrWarning
+ .setText(R.string.error_publish_avatar_no_server_support);
+ }
} else {
- loadImageIntoPreview(avatarUri);
+ this.avatarUri = this.defaultUri;
+ loadImageIntoPreview(this.defaultUri);
+ this.secondaryHint.setVisibility(View.INVISIBLE);
}
- this.accountTextView.setText(this.account.getJid().toBareJid().toString());
+ } else {
+ loadImageIntoPreview(avatarUri);
+ }
+ String account;
+ if (Config.DOMAIN_LOCK != null) {
+ account = this.account.getJid().getLocalpart();
+ } else {
+ account = this.account.getJid().toBareJid().toString();
}
+ this.accountTextView.setText(account);
}
-
}
@Override
protected void onStart() {
super.onStart();
if (getIntent() != null) {
- this.mInitialAccountSetup = getIntent().getBooleanExtra("setup",
- false);
+ this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", false);
}
if (this.mInitialAccountSetup) {
this.cancelButton.setText(R.string.skip);
}
}
+ private Bitmap loadScaledBitmap(Uri uri, int reqSize) throws FileNotFoundException {
+ final BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
+ int rotation = ExifHelper.getOrientation(getContentResolver().openInputStream(uri));
+ options.inSampleSize = ImageUtil.calcSampleSize(options, reqSize);
+ options.inJustDecodeBounds = false;
+ Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
+ return ImageUtil.rotate(bm,rotation);
+ }
+
protected void loadImageIntoPreview(Uri uri) {
- Bitmap bm = ImageUtil.cropCenterSquare(uri, 384);
+ Bitmap bm = null;
+ try {
+ bm = loadScaledBitmap(uri, getPixel(192));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
if (bm == null) {
disablePublishButton();
this.hintOrWarning.setTextColor(getWarningTextColor());
@@ -252,4 +304,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
this.publishButton.setTextColor(getSecondaryTextColor());
}
+ public void refreshUiReal() {
+ //nothing to do. This Activity doesn't implement any listeners
+ }
}