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.net.Uri;
 37import android.os.Bundle;
 38import android.util.Log;
 39import android.view.View;
 40import android.widget.Toast;
 41import androidx.annotation.StringRes;
 42import androidx.databinding.DataBindingUtil;
 43import com.canhub.cropper.CropImage;
 44import eu.siacs.conversations.Config;
 45import eu.siacs.conversations.R;
 46import eu.siacs.conversations.databinding.ActivityPublishProfilePictureBinding;
 47import eu.siacs.conversations.entities.Conversation;
 48import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
 49import eu.siacs.conversations.ui.util.PendingItem;
 50
 51public class PublishGroupChatProfilePictureActivity extends XmppActivity
 52        implements OnAvatarPublication {
 53    private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
 54    private ActivityPublishProfilePictureBinding binding;
 55    private Conversation conversation;
 56    private Uri uri;
 57
 58    @Override
 59    protected void refreshUiReal() {}
 60
 61    @Override
 62    protected void onBackendConnected() {
 63        String uuid = pendingConversationUuid.pop();
 64        if (uuid != null) {
 65            this.conversation = xmppConnectionService.findConversationByUuid(uuid);
 66        }
 67        if (this.conversation == null) {
 68            return;
 69        }
 70        reloadAvatar();
 71    }
 72
 73    private void reloadAvatar() {
 74        final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
 75        final Bitmap bitmap;
 76        if (uri == null) {
 77            bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
 78        } else {
 79            Log.d(Config.LOGTAG, "loading " + uri + " into preview");
 80            bitmap = xmppConnectionService.getFileBackend().cropCenterSquare(uri, size);
 81        }
 82        this.binding.accountImage.setImageBitmap(bitmap);
 83        this.binding.publishButton.setEnabled(uri != null);
 84    }
 85
 86    @Override
 87    public void onCreate(final Bundle savedInstanceState) {
 88        super.onCreate(savedInstanceState);
 89        this.binding =
 90                DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
 91        this.binding.contactOnly.setVisibility(View.GONE);
 92        Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
 93        setSupportActionBar(this.binding.toolbar);
 94        configureActionBar(getSupportActionBar());
 95        this.binding.cancelButton.setOnClickListener((v) -> this.finish());
 96        this.binding.secondaryHint.setVisibility(View.GONE);
 97        this.binding.accountImage.setOnClickListener(
 98                (v) -> PublishProfilePictureActivity.chooseAvatar(this));
 99        final var intent = getIntent();
100        final var uuid = intent == null ? null : intent.getStringExtra("uuid");
101        if (uuid != null) {
102            pendingConversationUuid.push(uuid);
103        }
104        this.binding.publishButton.setEnabled(uri != null);
105        this.binding.publishButton.setOnClickListener(this::publish);
106    }
107
108    private void publish(final View view) {
109        binding.publishButton.setText(R.string.publishing);
110        binding.publishButton.setEnabled(false);
111        xmppConnectionService.publishMucAvatar(conversation, uri, this);
112    }
113
114    @Override
115    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
116        super.onActivityResult(requestCode, resultCode, data);
117        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
118            final CropImage.ActivityResult result = CropImage.getActivityResult(data);
119            if (resultCode == RESULT_OK) {
120                this.uri = result == null ? null : result.getUri();
121                if (xmppConnectionServiceBound) {
122                    reloadAvatar();
123                }
124            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
125                final var error = result == null ? null : result.getError();
126                if (error != null) {
127                    Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
128                }
129            }
130        } else if (requestCode == REQUEST_CHOOSE_PICTURE) {
131            if (resultCode == RESULT_OK) {
132                PublishProfilePictureActivity.cropUri(this, data.getData());
133            }
134        }
135    }
136
137    @Override
138    public void onAvatarPublicationSucceeded() {
139        runOnUiThread(
140                () -> {
141                    Toast.makeText(this, R.string.avatar_has_been_published, Toast.LENGTH_SHORT)
142                            .show();
143                    finish();
144                });
145    }
146
147    @Override
148    public void onAvatarPublicationFailed(@StringRes int res) {
149        runOnUiThread(
150                () -> {
151                    Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
152                    this.binding.publishButton.setText(R.string.publish);
153                    this.binding.publishButton.setEnabled(true);
154                });
155    }
156}