PublishGroupChatProfilePictureActivity.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.ui;
 31
 32import static eu.siacs.conversations.ui.PublishProfilePictureActivity.REQUEST_CHOOSE_PICTURE;
 33
 34import android.content.Intent;
 35import android.graphics.Bitmap;
 36import android.graphics.drawable.AnimatedImageDrawable;
 37import android.graphics.drawable.BitmapDrawable;
 38import android.graphics.drawable.Drawable;
 39import android.net.Uri;
 40import android.os.Build;
 41import android.os.Bundle;
 42import android.util.Log;
 43import android.view.View;
 44import android.widget.Toast;
 45import androidx.annotation.StringRes;
 46import androidx.databinding.DataBindingUtil;
 47import com.canhub.cropper.CropImage;
 48import eu.siacs.conversations.Config;
 49import eu.siacs.conversations.R;
 50import eu.siacs.conversations.databinding.ActivityPublishProfilePictureBinding;
 51import eu.siacs.conversations.entities.Conversation;
 52import eu.siacs.conversations.persistance.FileBackend;
 53import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
 54import eu.siacs.conversations.ui.util.PendingItem;
 55
 56public class PublishGroupChatProfilePictureActivity extends XmppActivity
 57        implements OnAvatarPublication {
 58    private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
 59    private ActivityPublishProfilePictureBinding binding;
 60    private Conversation conversation;
 61    private Uri uri;
 62
 63    @Override
 64    protected void refreshUiReal() {}
 65
 66    @Override
 67    protected void onBackendConnected() {
 68        String uuid = pendingConversationUuid.pop();
 69        if (uuid != null) {
 70            this.conversation = xmppConnectionService.findConversationByUuid(uuid);
 71        }
 72        if (this.conversation == null) {
 73            return;
 74        }
 75        reloadAvatar();
 76    }
 77
 78    private void reloadAvatar() {
 79        if (xmppConnectionService == null) return; // We will get called again in onBackendConnected
 80        final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
 81        Drawable bitmap;
 82        if (uri == null) {
 83            bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
 84        } else {
 85            Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
 86            bitmap = xmppConnectionService.getFileBackend().cropCenterSquareDrawable(uri, size);
 87        }
 88        this.binding.accountImage.setImageDrawable(bitmap);
 89        this.binding.publishButton.setEnabled(uri != null);
 90        if (Build.VERSION.SDK_INT >= 28 && bitmap instanceof AnimatedImageDrawable) {
 91            ((AnimatedImageDrawable) bitmap).start();
 92        }
 93    }
 94
 95    @Override
 96    public void onCreate(final Bundle savedInstanceState) {
 97        super.onCreate(savedInstanceState);
 98        this.binding =
 99                DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
100        this.binding.contactOnly.setVisibility(View.GONE);
101        Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
102        setSupportActionBar(this.binding.toolbar);
103        configureActionBar(getSupportActionBar());
104        this.binding.cancelButton.setOnClickListener((v) -> this.finish());
105        this.binding.secondaryHint.setVisibility(View.GONE);
106        this.binding.accountImage.setOnClickListener(
107                (v) -> PublishProfilePictureActivity.chooseAvatar(this));
108        final var intent = getIntent();
109        final var uuid = intent == null ? null : intent.getStringExtra("uuid");
110        if (uuid != null) {
111            pendingConversationUuid.push(uuid);
112        }
113        this.binding.publishButton.setEnabled(uri != null);
114        this.binding.publishButton.setOnClickListener(this::publish);
115    }
116
117    private void publish(final View view) {
118        binding.publishButton.setText(R.string.publishing);
119        binding.publishButton.setEnabled(false);
120        xmppConnectionService.publishMucAvatar(conversation, uri, this);
121    }
122
123    @Override
124    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
125        super.onActivityResult(requestCode, resultCode, data);
126        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
127            final CropImage.ActivityResult result = CropImage.getActivityResult(data);
128            if (resultCode == RESULT_OK) {
129                this.uri = result == null ? null : result.getUri();
130                if (xmppConnectionServiceBound) {
131                    reloadAvatar();
132                }
133            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
134                final var error = result == null ? null : result.getError();
135                if (error != null) {
136                    Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
137                }
138            }
139        } else if (requestCode == REQUEST_CHOOSE_PICTURE) {
140            if (resultCode == RESULT_OK) {
141                cropUri(data.getData());
142            }
143        }
144    }
145
146    public void cropUri(final Uri uri) {
147        if (Build.VERSION.SDK_INT >= 28) {
148            this.uri = uri;
149            reloadAvatar();
150            if (this.binding.accountImage.getDrawable() instanceof AnimatedImageDrawable || this.binding.accountImage.getDrawable() instanceof FileBackend.SVGDrawable) {
151                return;
152            }
153        }
154
155        CropImage.activity(uri).setOutputCompressFormat(Bitmap.CompressFormat.PNG)
156                .setAspectRatio(1, 1)
157                .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
158                .start(this);
159    }
160
161    @Override
162    public void onAvatarPublicationSucceeded() {
163        runOnUiThread(
164                () -> {
165                    Toast.makeText(this, R.string.avatar_has_been_published, Toast.LENGTH_SHORT)
166                            .show();
167                    finish();
168                });
169    }
170
171    @Override
172    public void onAvatarPublicationFailed(@StringRes int res) {
173        runOnUiThread(
174                () -> {
175                    Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
176                    this.binding.publishButton.setText(R.string.publish);
177                    this.binding.publishButton.setEnabled(true);
178                });
179    }
180}