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 android.content.Intent;
 33import android.graphics.Bitmap;
 34import android.net.Uri;
 35import android.os.Bundle;
 36import android.util.Log;
 37import android.view.View;
 38import android.widget.Toast;
 39
 40import androidx.annotation.StringRes;
 41import androidx.databinding.DataBindingUtil;
 42
 43import com.theartofdev.edmodo.cropper.CropImage;
 44
 45import eu.siacs.conversations.Config;
 46import eu.siacs.conversations.R;
 47import eu.siacs.conversations.databinding.ActivityPublishProfilePictureBinding;
 48import eu.siacs.conversations.entities.Conversation;
 49import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
 50import eu.siacs.conversations.ui.util.PendingItem;
 51
 52import static eu.siacs.conversations.ui.PublishProfilePictureActivity.REQUEST_CHOOSE_PICTURE;
 53
 54public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication {
 55    private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
 56    private ActivityPublishProfilePictureBinding binding;
 57    private Conversation conversation;
 58    private Uri uri;
 59
 60    @Override
 61    protected void refreshUiReal() {
 62
 63    }
 64
 65    @Override
 66    void onBackendConnected() {
 67        String uuid = pendingConversationUuid.pop();
 68        if (uuid != null) {
 69            this.conversation = xmppConnectionService.findConversationByUuid(uuid);
 70        }
 71        if (this.conversation == null) {
 72            return;
 73        }
 74        reloadAvatar();
 75    }
 76
 77    private void reloadAvatar() {
 78        final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
 79        Bitmap bitmap;
 80        if (uri == null) {
 81            bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
 82        } else {
 83            Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
 84            bitmap = xmppConnectionService.getFileBackend().cropCenterSquare(uri, size);
 85        }
 86        this.binding.accountImage.setImageBitmap(bitmap);
 87        this.binding.publishButton.setEnabled(uri != null);
 88    }
 89
 90    @Override
 91    public void onCreate(Bundle savedInstanceState) {
 92        super.onCreate(savedInstanceState);
 93        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
 94        setSupportActionBar(this.binding.toolbar);
 95        configureActionBar(getSupportActionBar());
 96        this.binding.cancelButton.setOnClickListener((v) -> this.finish());
 97        this.binding.secondaryHint.setVisibility(View.GONE);
 98        this.binding.accountImage.setOnClickListener((v) -> PublishProfilePictureActivity.chooseAvatar(this));
 99        Intent intent = getIntent();
100        String 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
109    private void publish(View view) {
110        binding.publishButton.setText(R.string.publishing);
111        binding.publishButton.setEnabled(false);
112        xmppConnectionService.publishMucAvatar(conversation, uri, this);
113    }
114
115    @Override
116    public void onActivityResult(int requestCode, int resultCode, Intent 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.getUri();
121                if (xmppConnectionServiceBound) {
122                    reloadAvatar();
123                }
124            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
125                Exception error = 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            Toast.makeText(this, R.string.avatar_has_been_published, Toast.LENGTH_SHORT).show();
141            finish();
142        });
143    }
144
145    @Override
146    public void onAvatarPublicationFailed(@StringRes int res) {
147        runOnUiThread(() -> {
148            Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
149            this.binding.publishButton.setText(R.string.publish);
150            this.binding.publishButton.setEnabled(true);
151        });
152    }
153}