aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2018-06-24 21:34:47 +0200
committerChristian Schneppe <christian@pix-art.de>2018-06-29 21:55:43 +0200
commitd9d40ecb4297a7ade4205d6dca411c01aa728663 (patch)
tree2853511ab2432799cbb9d2ab1a7d736e424c8e63 /src/main/java/de
parent6633eedbb75d5fa85186c053aa7797b5b5458fc6 (diff)
WIP: set room avatar and slight redesign of group details
*show jid in group details (in advanced mode)
Diffstat (limited to 'src/main/java/de')
-rw-r--r--src/main/java/de/pixart/messenger/entities/MucOptions.java15
-rw-r--r--src/main/java/de/pixart/messenger/persistance/FileBackend.java21
-rw-r--r--src/main/java/de/pixart/messenger/services/XmppConnectionService.java75
-rw-r--r--src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java44
-rw-r--r--src/main/java/de/pixart/messenger/ui/PublishGroupChatProfilePictureActivity.java155
-rw-r--r--src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java188
-rw-r--r--src/main/java/de/pixart/messenger/ui/interfaces/OnAvatarPublication.java39
7 files changed, 370 insertions, 167 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/MucOptions.java b/src/main/java/de/pixart/messenger/entities/MucOptions.java
index a6968b66f..eb5629b2e 100644
--- a/src/main/java/de/pixart/messenger/entities/MucOptions.java
+++ b/src/main/java/de/pixart/messenger/entities/MucOptions.java
@@ -373,7 +373,7 @@ public class MucOptions {
this.self = new User(this, createJoinJid(getProposedNick()));
}
- public boolean updateConfiguration(List<String> features, Data data) {
+ public boolean updateConfiguration(List<String> features, String name, Data data) {
updateFeatures(features);
updateFormData(data == null ? new Data() : data);
Field allowPmField = this.form.getFieldByName("muc#roomconfig_allowpm");
@@ -382,6 +382,7 @@ public class MucOptions {
changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MEMBERS_ONLY, this.hasFeature("muc_membersonly"));
changed |= conversation.setAttribute(Conversation.ATTRIBUTE_MODERATED, this.hasFeature("muc_moderated"));
changed |= conversation.setAttribute(Conversation.ATTRIBUTE_NON_ANONYMOUS, this.hasFeature("muc_nonanonymous"));
+ changed |= setName(name);
return changed;
}
@@ -402,6 +403,10 @@ public class MucOptions {
return this.features.contains(feature);
}
+ public boolean hasVCards() {
+ return hasFeature("vcard-temp");
+ }
+
public boolean canInvite() {
Field field = this.form.getFieldByName("muc#roomconfig_allowinvites");
return !membersOnly() || self.getRole().ranks(Role.MODERATOR) || (field != null && "1".equals(field.getValue()));
@@ -688,6 +693,14 @@ public class MucOptions {
return this.conversation.getAttribute("subject");
}
+ private boolean setName(String name) {
+ return this.conversation.setAttribute("muc_name", name);
+ }
+
+ public String getName() {
+ return this.conversation.getAttribute("muc_name");
+ }
+
public List<User> getFallbackUsersFromCryptoTargets() {
List<User> users = new ArrayList<>();
for (Jid jid : conversation.getAcceptedCryptoTargets()) {
diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java
index e893ea129..3bb1e5c4e 100644
--- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java
+++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java
@@ -614,6 +614,14 @@ public class FileBackend {
}
public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
+ final Avatar uncompressAvatar = getUncompressedAvatar(image);
+ if (uncompressAvatar != null && uncompressAvatar.image.length() <= Config.AVATAR_CHAR_LIMIT) {
+ return uncompressAvatar;
+ }
+ if (uncompressAvatar != null) {
+ Log.d(Config.LOGTAG, "uncompressed avatar exceeded char limit by " + (uncompressAvatar.image.length() - Config.AVATAR_CHAR_LIMIT));
+ }
+
Bitmap bm = cropCenterSquare(image, size);
if (bm == null) {
return null;
@@ -627,6 +635,19 @@ public class FileBackend {
return getPepAvatar(bm, format, 100);
}
+ private Avatar getUncompressedAvatar(Uri uri) {
+ Bitmap bitmap = null;
+ try {
+ bitmap = BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri));
+ return getPepAvatar(bitmap, Bitmap.CompressFormat.PNG, 100);
+ } catch (Exception e) {
+ if (bitmap != null) {
+ bitmap.recycle();
+ }
+ }
+ return null;
+ }
+
private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
try {
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
index 2556138e8..487308c52 100644
--- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
+++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
@@ -109,6 +109,7 @@ import de.pixart.messenger.persistance.DatabaseBackend;
import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.ui.SettingsActivity;
import de.pixart.messenger.ui.UiCallback;
+import de.pixart.messenger.ui.interfaces.OnAvatarPublication;
import de.pixart.messenger.ui.interfaces.OnSearchResultsAvailable;
import de.pixart.messenger.utils.ConversationsFileObserver;
import de.pixart.messenger.utils.CryptoHelper;
@@ -2698,18 +2699,21 @@ public class XmppConnectionService extends Service {
public void onIqPacketReceived(Account account, IqPacket packet) {
Element query = packet.findChild("query", "http://jabber.org/protocol/disco#info");
if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
+ String name = null;
ArrayList<String> features = new ArrayList<>();
for (Element child : query.getChildren()) {
- if (child != null && child.getName().equals("feature")) {
+ if (child.getName().equals("feature")) {
String var = child.getAttribute("var");
if (var != null) {
features.add(var);
}
+ } else if (child.getName().equals("identity")) {
+ name = child.getAttribute("name");
}
}
Element form = query.findChild("x", Namespace.DATA);
Data data = form == null ? null : Data.parse(form);
- if (conversation.getMucOptions().updateConfiguration(features, data)) {
+ if (conversation.getMucOptions().updateConfiguration(features, name, data)) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": muc configuration changed for " + conversation.getJid().asBareJid());
updateConversation(conversation);
}
@@ -3025,24 +3029,77 @@ public class XmppConnectionService extends Service {
}
}
- public void publishAvatar(final Account account, final Uri image, final UiCallback<Avatar> callback) {
+ public void publishMucAvatar(final Conversation conversation, final Uri image, final OnAvatarPublication callback) {
new Thread(() -> {
final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
final int size = Config.AVATAR_SIZE;
final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
if (avatar != null) {
if (!getFileBackend().save(avatar)) {
- callback.error(R.string.error_saving_avatar, avatar);
+ callback.onAvatarPublicationFailed(R.string.error_saving_avatar);
+ return;
+ }
+ avatar.owner = conversation.getJid().asBareJid();
+ publishMucAvatar(conversation, avatar, callback);
+ } else {
+ callback.onAvatarPublicationFailed(R.string.error_publish_avatar_converting);
+ }
+ }).start();
+ }
+
+ public void publishAvatar(final Account account, final Uri image, final OnAvatarPublication callback) {
+ new Thread(() -> {
+ final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
+ final int size = Config.AVATAR_SIZE;
+ final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
+ if (avatar != null) {
+ if (!getFileBackend().save(avatar)) {
+ Log.d(Config.LOGTAG, "unable to save vcard");
+ callback.onAvatarPublicationFailed(R.string.error_saving_avatar);
return;
}
publishAvatar(account, avatar, callback);
} else {
- callback.error(R.string.error_publish_avatar_converting, null);
+ callback.onAvatarPublicationFailed(R.string.error_publish_avatar_converting);
}
}).start();
}
- public void publishAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
+ private void publishMucAvatar(Conversation conversation, Avatar avatar, OnAvatarPublication callback) {
+ final IqPacket retrieve = mIqGenerator.retrieveVcardAvatar(avatar);
+ sendIqPacket(conversation.getAccount(), retrieve, (account, response) -> {
+ boolean itemNotFound = response.getType() == IqPacket.TYPE.ERROR && response.hasChild("error") && response.findChild("error").hasChild("item-not-found");
+ if (response.getType() == IqPacket.TYPE.RESULT || itemNotFound) {
+ Element vcard = response.findChild("vCard", "vcard-temp");
+ if (vcard == null) {
+ vcard = new Element("vCard", "vcard-temp");
+ }
+ Element photo = vcard.findChild("PHOTO");
+ if (photo == null) {
+ photo = vcard.addChild("PHOTO");
+ }
+ photo.clearChildren();
+ photo.addChild("TYPE").setContent(avatar.type);
+ photo.addChild("BINVAL").setContent(avatar.image);
+ IqPacket publication = new IqPacket(IqPacket.TYPE.SET);
+ publication.setTo(conversation.getJid().asBareJid());
+ publication.addChild(vcard);
+ sendIqPacket(account, publication, (a1, publicationResponse) -> {
+ if (publicationResponse.getType() == IqPacket.TYPE.RESULT) {
+ callback.onAvatarPublicationSucceeded();
+ } else {
+ Log.d(Config.LOGTAG, "failed to publish vcard " + publicationResponse.getError());
+ callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
+ }
+ });
+ } else {
+ Log.d(Config.LOGTAG, "failed to request vcard " + response.toString());
+ callback.onAvatarPublicationFailed(R.string.error_publish_avatar_no_server_support);
+ }
+ });
+ }
+
+ public void publishAvatar(Account account, final Avatar avatar, final OnAvatarPublication callback) {
IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
this.sendIqPacket(account, packet, new OnIqPacketReceived() {
@@ -3060,11 +3117,11 @@ public class XmppConnectionService extends Service {
}
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": published avatar "+(avatar.size/1024)+"KiB");
if (callback != null) {
- callback.success(avatar);
+ callback.onAvatarPublicationSucceeded();
}
} else {
if (callback != null) {
- callback.error(R.string.error_publish_avatar_server_reject,avatar);
+ callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
}
}
}
@@ -3073,7 +3130,7 @@ public class XmppConnectionService extends Service {
Element error = result.findChild("error");
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": server rejected avatar "+(avatar.size/1024)+"KiB "+(error!=null?error.toString():""));
if (callback != null) {
- callback.error(R.string.error_publish_avatar_server_reject, avatar);
+ callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
}
}
}
diff --git a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java
index c55a936cd..ed2dfee04 100644
--- a/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java
+++ b/src/main/java/de/pixart/messenger/ui/ConferenceDetailsActivity.java
@@ -299,7 +299,20 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
this.mAdvancedMode = getPreferences().getBoolean("advanced_muc_mode", false);
this.binding.mucInfoMore.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
this.binding.notificationStatusButton.setOnClickListener(this.mNotifyStatusClickListener);
- this.binding.notificationStatusButton.setOnClickListener(this.mNotifyStatusClickListener);
+ this.binding.detailsMucAvatar.setOnClickListener(v -> {
+ final MucOptions mucOptions = mConversation.getMucOptions();
+ if (!mucOptions.hasVCards()) {
+ Toast.makeText(this, R.string.host_does_not_support_group_chat_avatars, Toast.LENGTH_SHORT).show();
+ return;
+ }
+ if (!mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
+ Toast.makeText(this, R.string.only_the_owner_can_change_group_chat_avatar, Toast.LENGTH_SHORT).show();
+ return;
+ }
+ final Intent intent = new Intent(this, PublishGroupChatProfilePictureActivity.class);
+ intent.putExtra("uuid", mConversation.getUuid());
+ startActivity(intent);
+ });
}
@Override
@@ -320,14 +333,6 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
case android.R.id.home:
finish();
break;
- case R.id.action_edit_subject:
- if (mConversation != null) {
- quickEdit(mConversation.getMucOptions().getSubject(),
- R.string.edit_subject_hint,
- this.onSubjectEdited,
- true);
- }
- break;
case R.id.action_share_http:
shareLink(true);
break;
@@ -363,12 +368,10 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItemAdvancedMode = menu.findItem(R.id.action_advanced_mode);
- MenuItem menuItemChangeSubject = menu.findItem(R.id.action_edit_subject);
menuItemAdvancedMode.setChecked(mAdvancedMode);
if (mConversation == null) {
return true;
}
- menuItemChangeSubject.setVisible(mConversation.getMucOptions().canChangeSubject());
return true;
}
@@ -587,28 +590,25 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
absubtitle.setClickable(false);
}
}
- this.binding.conferenceName.setText(mConversation.getName());
this.binding.detailsAccount.setText(getString(R.string.using_account, account));
+ this.binding.jid.setText(mConversation.getJid().asBareJid().toEscapedString());
if (xmppConnectionService.multipleAccounts()) {
this.binding.detailsAccount.setVisibility(View.VISIBLE);
} else {
this.binding.detailsAccount.setVisibility(View.GONE);
}
this.binding.yourPhoto.setImageBitmap(avatarService().get(mConversation.getAccount(), getPixel(48)));
- setTitle(mConversation.getName());
- this.binding.mucJabberid.setText(mConversation.getJid().asBareJid().toString());
+
+ this.binding.mucTitle.setText(mucOptions.getName());
+ this.binding.mucSubject.setText(mucOptions.getSubject());
this.binding.mucYourNick.setText(mucOptions.getActualNick());
if (mucOptions.online()) {
this.binding.mucMoreDetails.setVisibility(View.VISIBLE);
this.binding.mucSettings.setVisibility(View.VISIBLE);
this.binding.mucInfoMore.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
- final String status = getStatus(self);
- if (status != null) {
- this.binding.mucRole.setVisibility(View.VISIBLE);
- this.binding.mucRole.setText(status);
- } else {
- this.binding.mucRole.setVisibility(View.GONE);
- }
+ this.binding.jid.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
+ this.binding.mucRole.setVisibility(View.VISIBLE);
+ this.binding.mucRole.setText(getStatus(self));
if (mucOptions.membersOnly()) {
this.binding.mucConferenceType.setText(R.string.private_conference);
} else {
@@ -629,7 +629,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
this.binding.changeConferenceButton.setVisibility(View.VISIBLE);
} else {
this.binding.destroy.setVisibility(View.GONE);
- this.binding.changeConferenceButton.setVisibility(View.GONE);
+ this.binding.changeConferenceButton.setVisibility(View.INVISIBLE);
}
this.binding.leaveMuc.setVisibility(View.VISIBLE);
this.binding.leaveMuc.setOnClickListener(v1 -> {
diff --git a/src/main/java/de/pixart/messenger/ui/PublishGroupChatProfilePictureActivity.java b/src/main/java/de/pixart/messenger/ui/PublishGroupChatProfilePictureActivity.java
new file mode 100644
index 000000000..a4bf6b440
--- /dev/null
+++ b/src/main/java/de/pixart/messenger/ui/PublishGroupChatProfilePictureActivity.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2018, Daniel Gultsch All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package de.pixart.messenger.ui;
+
+import android.content.Intent;
+import android.databinding.DataBindingUtil;
+import android.graphics.Bitmap;
+import android.net.Uri;
+import android.os.Bundle;
+import android.support.annotation.StringRes;
+import android.support.v7.widget.Toolbar;
+import android.util.Log;
+import android.view.View;
+import android.widget.Toast;
+
+import com.theartofdev.edmodo.cropper.CropImage;
+
+import de.pixart.messenger.Config;
+import de.pixart.messenger.R;
+import de.pixart.messenger.databinding.ActivityPublishProfilePictureBinding;
+import de.pixart.messenger.entities.Conversation;
+import de.pixart.messenger.ui.interfaces.OnAvatarPublication;
+import de.pixart.messenger.ui.util.PendingItem;
+
+public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication {
+
+ private static final int REQUEST_CHOOSE_FILE = 0xac24;
+
+ private ActivityPublishProfilePictureBinding binding;
+
+ private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
+
+ private Conversation conversation;
+ private Uri uri;
+
+ @Override
+ protected void refreshUiReal() {
+
+ }
+
+ @Override
+ void onBackendConnected() {
+ String uuid = pendingConversationUuid.pop();
+ if (uuid != null) {
+ this.conversation = xmppConnectionService.findConversationByUuid(uuid);
+ }
+ if (this.conversation == null) {
+ return;
+ }
+ reloadAvatar();
+ }
+
+ private void reloadAvatar() {
+ final int size = getPixel(Config.AVATAR_SIZE);
+ Bitmap bitmap;
+ if (uri == null) {
+ bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
+ } else {
+ Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
+ bitmap = xmppConnectionService.getFileBackend().cropCenterSquare(uri, size);
+ }
+ this.binding.accountImage.setImageBitmap(bitmap);
+ this.binding.publishButton.setEnabled(uri != null);
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ this.binding = DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
+ setSupportActionBar((Toolbar) this.binding.toolbar);
+ configureActionBar(getSupportActionBar());
+ this.binding.cancelButton.setOnClickListener((v) -> this.finish());
+ this.binding.secondaryHint.setVisibility(View.GONE);
+ this.binding.accountImage.setOnClickListener((v) -> this.chooseAvatar());
+ Intent intent = getIntent();
+ String uuid = intent == null ? null : intent.getStringExtra("uuid");
+ if (uuid != null) {
+ pendingConversationUuid.push(uuid);
+ }
+ this.binding.publishButton.setEnabled(uri != null);
+ this.binding.publishButton.setOnClickListener(this::publish);
+ }
+
+
+ private void publish(View view) {
+ xmppConnectionService.publishMucAvatar(conversation, uri, this);
+ }
+
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
+ CropImage.ActivityResult result = CropImage.getActivityResult(data);
+ if (resultCode == RESULT_OK) {
+ this.uri = result.getUri();
+ if (xmppConnectionServiceBound) {
+ reloadAvatar();
+ }
+ } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
+ Exception error = result.getError();
+ if (error != null) {
+ Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
+ }
+ }
+ }
+ }
+
+ private void chooseAvatar() {
+ CropImage.activity()
+ .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
+ .setAspectRatio(1, 1)
+ .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
+ .start(this);
+ }
+
+ @Override
+ public void onAvatarPublicationSucceeded() {
+ finish();
+ }
+
+ @Override
+ public void onAvatarPublicationFailed(@StringRes int res) {
+ runOnUiThread(() -> {
+ Toast.makeText(this,res,Toast.LENGTH_SHORT).show();
+ this.binding.publishButton.setText(R.string.publish);
+ this.binding.publishButton.setEnabled(true);
+ });
+ }
+} \ No newline at end of file
diff --git a/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java b/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java
index 8dfcd11da..a6f158646 100644
--- a/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java
+++ b/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java
@@ -1,16 +1,11 @@
package de.pixart.messenger.ui;
-import android.app.PendingIntent;
import android.content.Intent;
-import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
-import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.util.Log;
-import android.view.Menu;
-import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
@@ -18,25 +13,18 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
-import com.soundcloud.android.crop.Crop;
-
-import java.io.File;
+import com.theartofdev.edmodo.cropper.CropImage;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
-import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.services.XmppConnectionService;
-import de.pixart.messenger.utils.FileUtils;
+import de.pixart.messenger.ui.interfaces.OnAvatarPublication;
import de.pixart.messenger.utils.PhoneHelper;
-import de.pixart.messenger.xmpp.pep.Avatar;
-public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate {
+public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate, OnAvatarPublication {
- private static final int REQUEST_CHOOSE_FILE_AND_CROP = 0xac23;
- private static final int REQUEST_CHOOSE_FILE = 0xac24;
private ImageView avatar;
- private TextView accountTextView;
private TextView hintOrWarning;
private TextView secondaryHint;
private Button cancelButton;
@@ -56,41 +44,33 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
}
};
private boolean mInitialAccountSetup;
- private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
-
- @Override
- public void success(Avatar object) {
- runOnUiThread(() -> {
- if (mInitialAccountSetup) {
- Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
- WelcomeActivity.addInviteUri(intent, getIntent());
- intent.putExtra("init", true);
- startActivity(intent);
- overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
- }
- Toast.makeText(PublishProfilePictureActivity.this,
- R.string.avatar_has_been_published,
- Toast.LENGTH_SHORT).show();
- finish();
- });
- }
-
- @Override
- public void error(final int errorCode, Avatar object) {
- runOnUiThread(() -> {
- hintOrWarning.setText(errorCode);
- hintOrWarning.setTextColor(getWarningTextColor());
- hintOrWarning.setVisibility(View.VISIBLE);
- publishing = false;
- togglePublishButton(true,R.string.publish);
- });
- }
+ @Override
+ public void onAvatarPublicationSucceeded() {
+ runOnUiThread(() -> {
+ if (mInitialAccountSetup) {
+ Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
+ WelcomeActivity.addInviteUri(intent, getIntent());
+ intent.putExtra("init", true);
+ startActivity(intent);
+ }
+ Toast.makeText(PublishProfilePictureActivity.this,
+ R.string.avatar_has_been_published,
+ Toast.LENGTH_SHORT).show();
+ finish();
+ });
+ }
- @Override
- public void userInputRequried(PendingIntent pi, Avatar object) {
- }
- };
+ @Override
+ public void onAvatarPublicationFailed(int res) {
+ runOnUiThread(() -> {
+ hintOrWarning.setText(res);
+ hintOrWarning.setTextColor(getWarningTextColor());
+ hintOrWarning.setVisibility(View.VISIBLE);
+ publishing = false;
+ togglePublishButton(true, R.string.publish);
+ });
+ }
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -98,6 +78,7 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
setContentView(R.layout.activity_publish_profile_picture);
setSupportActionBar(findViewById(R.id.toolbar));
configureActionBar(getSupportActionBar());
+
this.avatar = findViewById(R.id.account_image);
this.cancelButton = findViewById(R.id.cancel_button);
this.publishButton = findViewById(R.id.publish_button);
@@ -106,8 +87,8 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
this.publishButton.setOnClickListener(v -> {
if (avatarUri != null) {
publishing = true;
- togglePublishButton(false,R.string.publishing);
- xmppConnectionService.publishAvatar(account, avatarUri, avatarPublication);
+ togglePublishButton(false, R.string.publishing);
+ xmppConnectionService.publishAvatar(account, avatarUri, this);
}
});
this.cancelButton.setOnClickListener(v -> {
@@ -118,103 +99,38 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
intent.putExtra("init", true);
}
startActivity(intent);
- overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
}
finish();
});
- this.avatar.setOnClickListener(v -> {
- if (hasStoragePermission(REQUEST_CHOOSE_FILE)) {
- chooseAvatar(false);
- }
- });
+ this.avatar.setOnClickListener(v -> chooseAvatar());
this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext());
}
- private void chooseAvatar(boolean crop) {
- 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, crop ? REQUEST_CHOOSE_FILE_AND_CROP : REQUEST_CHOOSE_FILE);
- overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
- }
@Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
- if (grantResults.length > 0)
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- if (requestCode == REQUEST_CHOOSE_FILE_AND_CROP) {
- chooseAvatar(true);
- } else if (requestCode == REQUEST_CHOOSE_FILE) {
- chooseAvatar(false);
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
+ CropImage.ActivityResult result = CropImage.getActivityResult(data);
+ if (resultCode == RESULT_OK) {
+ this.avatarUri = result.getUri();
+ if (xmppConnectionServiceBound) {
+ loadImageIntoPreview(this.avatarUri);
+ }
+ } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
+ Exception error = result.getError();
+ if (error != null) {
+ Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
- } else {
- Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
- }
- }
-
- @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 {
- return super.onOptionsItemSelected(item);
}
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- Uri source = data.getData();
- switch (requestCode) {
- case REQUEST_CHOOSE_FILE_AND_CROP:
- if (FileBackend.weOwnFile(this, source)) {
- Toast.makeText(this, R.string.security_error_invalid_file_access, Toast.LENGTH_SHORT).show();
- return;
- }
- 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(Config.AVATAR_SIZE);
- Crop.of(source, destination).asSquare().withMaxSize(size, size).start(this);
- break;
- case REQUEST_CHOOSE_FILE:
- if (FileBackend.weOwnFile(this, source)) {
- Toast.makeText(this, R.string.security_error_invalid_file_access, Toast.LENGTH_SHORT).show();
- return;
- }
- this.avatarUri = source;
- if (xmppConnectionServiceBound) {
- loadImageIntoPreview(this.avatarUri);
- }
- break;
- case Crop.REQUEST_CROP:
- this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
- if (xmppConnectionServiceBound) {
- 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();
- }
- }
- }
+ private void chooseAvatar() {
+ CropImage.activity()
+ .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
+ .setAspectRatio(1, 1)
+ .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
+ .start(this);
}
@Override
@@ -251,6 +167,7 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
}
protected void loadImageIntoPreview(Uri uri) {
+
Bitmap bm = null;
if (uri == null) {
bm = avatarService().get(account, getPixel(Config.AVATAR_SIZE));
@@ -308,4 +225,5 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
public void onAccountUpdate() {
refreshUi();
}
-}
+
+} \ No newline at end of file
diff --git a/src/main/java/de/pixart/messenger/ui/interfaces/OnAvatarPublication.java b/src/main/java/de/pixart/messenger/ui/interfaces/OnAvatarPublication.java
new file mode 100644
index 000000000..86df8d420
--- /dev/null
+++ b/src/main/java/de/pixart/messenger/ui/interfaces/OnAvatarPublication.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2018, Daniel Gultsch All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package de.pixart.messenger.ui.interfaces;
+
+import android.support.annotation.StringRes;
+
+public interface OnAvatarPublication {
+
+ void onAvatarPublicationSucceeded();
+
+ void onAvatarPublicationFailed(@StringRes int res);
+
+} \ No newline at end of file