From d7ebd7d4538b3118cfe5b6413c22f2d437823b70 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 18 Jun 2018 14:15:19 +0200 Subject: [PATCH] WIP: set room avatar and slight redesign of group details --- build.gradle | 2 +- src/main/AndroidManifest.xml | 7 +- .../conversations/entities/MucOptions.java | 16 +- .../persistance/FileBackend.java | 22 + .../services/XmppConnectionService.java | 77 ++- .../ui/ConferenceDetailsActivity.java | 41 +- ...ublishGroupChatProfilePictureActivity.java | 157 ++++++ .../ui/PublishProfilePictureActivity.java | 455 +++++++----------- .../ui/interfaces/OnAvatarPublication.java | 39 ++ src/main/res/layout/activity_muc_details.xml | 186 ++++--- .../activity_publish_profile_picture.xml | 170 +++---- src/main/res/menu/muc_details.xml | 7 - src/main/res/menu/publish_avatar.xml | 9 - src/main/res/values/strings.xml | 4 +- 14 files changed, 725 insertions(+), 467 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/PublishGroupChatProfilePictureActivity.java create mode 100644 src/main/java/eu/siacs/conversations/ui/interfaces/OnAvatarPublication.java delete mode 100644 src/main/res/menu/publish_avatar.xml diff --git a/build.gradle b/build.gradle index 11dcd19b6..cb1679851 100644 --- a/build.gradle +++ b/build.gradle @@ -35,7 +35,7 @@ dependencies { exclude group: 'com.google.firebase', module: 'firebase-core' } implementation 'org.sufficientlysecure:openpgp-api:10.0' - implementation 'com.soundcloud.android:android-crop:1.0.1@aar' + implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+' implementation "com.android.support:support-v13:$supportLibVersion" implementation "com.android.support:appcompat-v7:$supportLibVersion" implementation "com.android.support:cardview-v7:$supportLibVersion" diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index aa85c0faa..8f4d4a72f 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -180,6 +180,7 @@ android:windowSoftInputMode="stateHidden|adjustResize"/> + - + diff --git a/src/main/java/eu/siacs/conversations/entities/MucOptions.java b/src/main/java/eu/siacs/conversations/entities/MucOptions.java index 8a9ed9e41..836f33b64 100644 --- a/src/main/java/eu/siacs/conversations/entities/MucOptions.java +++ b/src/main/java/eu/siacs/conversations/entities/MucOptions.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.entities; import android.annotation.SuppressLint; +import android.util.Log; import java.util.ArrayList; import java.util.HashSet; @@ -373,7 +374,7 @@ public class MucOptions { this.self = new User(this, createJoinJid(getProposedNick())); } - public boolean updateConfiguration(List features, Data data) { + public boolean updateConfiguration(List features, String name, Data data) { updateFeatures(features); updateFormData(data == null ? new Data() : data); Field allowPmField = this.form.getFieldByName("muc#roomconfig_allowpm"); @@ -382,6 +383,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 +404,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 +694,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 getFallbackUsersFromCryptoTargets() { List users = new ArrayList<>(); for (Jid jid : conversation.getAcceptedCryptoTargets()) { diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 9a01a9dbd..8adfc2c9e 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -736,6 +736,15 @@ 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; @@ -749,6 +758,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/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 84c705f87..1fb43f189 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -60,6 +60,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; +import javax.security.auth.callback.Callback; + import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.crypto.OmemoSetting; @@ -97,6 +99,7 @@ import eu.siacs.conversations.persistance.DatabaseBackend; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.ui.SettingsActivity; import eu.siacs.conversations.ui.UiCallback; +import eu.siacs.conversations.ui.interfaces.OnAvatarPublication; import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable; import eu.siacs.conversations.utils.ConversationsFileObserver; import eu.siacs.conversations.utils.CryptoHelper; @@ -2446,18 +2449,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 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); } @@ -2681,25 +2687,78 @@ public class XmppConnectionService extends Service { } } - public void publishAvatar(final Account account, final Uri image, final UiCallback 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 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() { @@ -2717,11 +2776,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); } } } @@ -2730,7 +2789,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/eu/siacs/conversations/ui/ConferenceDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java index 8c7fc18af..35ef2327c 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java @@ -2,6 +2,7 @@ package eu.siacs.conversations.ui; import android.app.PendingIntent; import android.content.Context; +import android.content.Intent; import android.content.IntentSender.SendIntentException; import android.content.res.Resources; import android.databinding.DataBindingUtil; @@ -12,6 +13,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; @@ -247,6 +249,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.yourPhoto.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 @@ -267,14 +283,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; @@ -318,7 +326,6 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers MenuItem menuItemSaveBookmark = menu.findItem(R.id.action_save_as_bookmark); MenuItem menuItemDeleteBookmark = menu.findItem(R.id.action_delete_bookmark); 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; @@ -330,7 +337,6 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers menuItemDeleteBookmark.setVisible(false); menuItemSaveBookmark.setVisible(true); } - menuItemChangeSubject.setVisible(mConversation.getMucOptions().canChangeSubject()); return true; } @@ -525,21 +531,16 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers account = mConversation.getAccount().getJid().asBareJid().toString(); } this.binding.detailsAccount.setText(getString(R.string.using_account, account)); - this.binding.yourPhoto.setImageBitmap(avatarService().get(mConversation.getAccount(), getPixel(48))); - setTitle(mConversation.getName()); - this.binding.mucJabberid.setText(mConversation.getJid().asBareJid().toString()); + this.binding.yourPhoto.setImageBitmap(avatarService().get(mConversation, getPixel(72))); + 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.mucRole.setVisibility(View.VISIBLE); + this.binding.mucRole.setText(getStatus(self)); if (mucOptions.membersOnly()) { this.binding.mucConferenceType.setText(R.string.private_conference); } else { diff --git a/src/main/java/eu/siacs/conversations/ui/PublishGroupChatProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/PublishGroupChatProfilePictureActivity.java new file mode 100644 index 000000000..ffb99c00f --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/PublishGroupChatProfilePictureActivity.java @@ -0,0 +1,157 @@ +/* + * 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 eu.siacs.conversations.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.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Toast; + +import com.theartofdev.edmodo.cropper.CropImage; + +import eu.siacs.conversations.Config; +import eu.siacs.conversations.R; +import eu.siacs.conversations.databinding.ActivityPublishProfilePictureBinding; +import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.ui.interfaces.OnAvatarPublication; +import eu.siacs.conversations.ui.util.PendingItem; + +public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication { + + private static final int REQUEST_CHOOSE_FILE = 0xac24; + + private ActivityPublishProfilePictureBinding binding; + + private final PendingItem 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 = (int) getResources().getDimension(R.dimen.publish_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); + }); + } +} diff --git a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java index 26c305fb2..50b76c3b2 100644 --- a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java @@ -1,16 +1,11 @@ package eu.siacs.conversations.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,293 +13,217 @@ 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 eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; -import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.XmppConnectionService; -import eu.siacs.conversations.utils.FileUtils; +import eu.siacs.conversations.ui.interfaces.OnAvatarPublication; import eu.siacs.conversations.utils.PhoneHelper; -import eu.siacs.conversations.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 hintOrWarning; - private TextView secondaryHint; - private Button cancelButton; - private Button publishButton; - private Uri avatarUri; - private Uri defaultUri; - private Account account; - private boolean support = false; - private boolean publishing = false; - private OnLongClickListener backToDefaultListener = new OnLongClickListener() { + private ImageView avatar; + 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 boolean publishing = false; + private OnLongClickListener backToDefaultListener = new OnLongClickListener() { - @Override - public boolean onLongClick(View v) { - avatarUri = defaultUri; - loadImageIntoPreview(defaultUri); - return true; - } - }; - private boolean mInitialAccountSetup; - private UiCallback avatarPublication = new UiCallback() { + @Override + public boolean onLongClick(View v) { + avatarUri = defaultUri; + loadImageIntoPreview(defaultUri); + return true; + } + }; + private boolean mInitialAccountSetup; - @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); - } - Toast.makeText(PublishProfilePictureActivity.this, - R.string.avatar_has_been_published, - Toast.LENGTH_SHORT).show(); - finish(); - }); - } + @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 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 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) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_publish_profile_picture); + setSupportActionBar(findViewById(R.id.toolbar)); + configureActionBar(getSupportActionBar()); - @Override - public void userInputRequried(PendingIntent pi, Avatar object) { - } - }; + 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) { + publishing = true; + togglePublishButton(false, R.string.publishing); + xmppConnectionService.publishAvatar(account, avatarUri, this); + } + }); + this.cancelButton.setOnClickListener(v -> { + if (mInitialAccountSetup) { + Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class); + if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) { + WelcomeActivity.addInviteUri(intent, getIntent()); + intent.putExtra("init", true); + } + startActivity(intent); + } + finish(); + }); + this.avatar.setOnClickListener(v -> chooseAvatar()); + this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext()); + } - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - 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); - this.hintOrWarning = findViewById(R.id.hint_or_warning); - this.secondaryHint = findViewById(R.id.secondary_hint); - this.publishButton.setOnClickListener(v -> { - if (avatarUri != null) { - publishing = true; - togglePublishButton(false,R.string.publishing); - xmppConnectionService.publishAvatar(account, avatarUri, avatarPublication); - } - }); - this.cancelButton.setOnClickListener(v -> { - if (mInitialAccountSetup) { - Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class); - if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) { - WelcomeActivity.addInviteUri(intent, getIntent()); - intent.putExtra("init", true); - } - startActivity(intent); - } - finish(); - }); - this.avatar.setOnClickListener(v -> { - if (hasStoragePermission(REQUEST_CHOOSE_FILE)) { - chooseAvatar(false); - } + @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.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(); + } + } + } + } - }); - this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext()); - } + private void chooseAvatar() { + CropImage.activity() + .setOutputCompressFormat(Bitmap.CompressFormat.PNG) + .setAspectRatio(1, 1) + .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE) + .start(this); + } - 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); - } + @Override + protected void onBackendConnected() { + this.account = extractAccount(getIntent()); + if (this.account != null) { + reloadAvatar(); + } + } - @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); - } - } else { - Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); - } - } + 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); + } else { + this.avatarUri = this.defaultUri; + loadImageIntoPreview(this.defaultUri); + } + } else { + loadImageIntoPreview(avatarUri); + } + } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.publish_avatar, menu); - return super.onCreateOptionsMenu(menu); - } + @Override + protected void onStart() { + super.onStart(); + if (getIntent() != null) { + this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", false); + } + if (this.mInitialAccountSetup) { + this.cancelButton.setText(R.string.skip); + } + } - @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); - } - } + protected void loadImageIntoPreview(Uri uri) { - @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(192); - 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(); - } - } - } - } + Bitmap bm = null; + 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); + } + } - @Override - protected void onBackendConnected() { - this.account = extractAccount(getIntent()); - if (this.account != null) { - reloadAvatar(); - } - } + if (bm == null) { + togglePublishButton(false, R.string.publish); + this.hintOrWarning.setVisibility(View.VISIBLE); + this.hintOrWarning.setTextColor(getWarningTextColor()); + this.hintOrWarning.setText(R.string.error_publish_avatar_converting); + return; + } + this.avatar.setImageBitmap(bm); + if (support) { + togglePublishButton(uri != null, R.string.publish); + this.hintOrWarning.setVisibility(View.INVISIBLE); + } else { + togglePublishButton(false, R.string.publish); + this.hintOrWarning.setVisibility(View.VISIBLE); + this.hintOrWarning.setTextColor(getWarningTextColor()); + 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); + } + } + if (this.defaultUri == null || this.defaultUri.equals(uri)) { + this.secondaryHint.setVisibility(View.INVISIBLE); + this.avatar.setOnLongClickListener(null); + } else if (this.defaultUri != null) { + this.secondaryHint.setVisibility(View.VISIBLE); + this.avatar.setOnLongClickListener(this.backToDefaultListener); + } + } - 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); - } else { - this.avatarUri = this.defaultUri; - loadImageIntoPreview(this.defaultUri); - } - } else { - loadImageIntoPreview(avatarUri); - } - } + 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); + } - @Override - protected void onStart() { - super.onStart(); - if (getIntent() != null) { - this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", false); - } - if (this.mInitialAccountSetup) { - this.cancelButton.setText(R.string.skip); - } - } + public void refreshUiReal() { + if (this.account != null) { + reloadAvatar(); + } + } - protected void loadImageIntoPreview(Uri uri) { + @Override + public void onAccountUpdate() { + refreshUi(); + } - Bitmap bm = null; - 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); - } - } - - if (bm == null) { - togglePublishButton(false,R.string.publish); - this.hintOrWarning.setVisibility(View.VISIBLE); - this.hintOrWarning.setTextColor(getWarningTextColor()); - this.hintOrWarning.setText(R.string.error_publish_avatar_converting); - return; - } - this.avatar.setImageBitmap(bm); - if (support) { - togglePublishButton(uri != null,R.string.publish); - this.hintOrWarning.setVisibility(View.INVISIBLE); - } else { - togglePublishButton(false,R.string.publish); - this.hintOrWarning.setVisibility(View.VISIBLE); - this.hintOrWarning.setTextColor(getWarningTextColor()); - 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); - } - } - if (this.defaultUri == null || this.defaultUri.equals(uri)) { - this.secondaryHint.setVisibility(View.INVISIBLE); - this.avatar.setOnLongClickListener(null); - } else if (this.defaultUri != null) { - this.secondaryHint.setVisibility(View.VISIBLE); - this.avatar.setOnLongClickListener(this.backToDefaultListener); - } - } - - 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); - } - - public void refreshUiReal() { - if (this.account != null) { - reloadAvatar(); - } - } - - @Override - public void onAccountUpdate() { - refreshUi(); - } } diff --git a/src/main/java/eu/siacs/conversations/ui/interfaces/OnAvatarPublication.java b/src/main/java/eu/siacs/conversations/ui/interfaces/OnAvatarPublication.java new file mode 100644 index 000000000..5d84c5853 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/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 eu.siacs.conversations.ui.interfaces; + +import android.support.annotation.StringRes; + +public interface OnAvatarPublication { + + void onAvatarPublicationSucceeded(); + void onAvatarPublicationFailed(@StringRes int res); + +} diff --git a/src/main/res/layout/activity_muc_details.xml b/src/main/res/layout/activity_muc_details.xml index 70f4bb742..34e42fe89 100644 --- a/src/main/res/layout/activity_muc_details.xml +++ b/src/main/res/layout/activity_muc_details.xml @@ -8,8 +8,9 @@ android:background="?attr/color_background_secondary" android:orientation="vertical"> - + - - - + - + + + + + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + + + + + + + + + + + + + + + + + + + + android:layout_alignParentBottom="true" + android:layout_alignParentLeft="true" + android:layout_alignParentRight="true"> - + android:layout_weight="1" + android:text="@string/cancel"/> - - + - - - - - - + android:layout_weight="1" + android:enabled="false" + android:text="@string/publish"/> - - - -