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.databinding.DataBindingUtil;
 34import android.graphics.Bitmap;
 35import android.net.Uri;
 36import android.os.Bundle;
 37import android.support.annotation.StringRes;
 38import android.support.v7.widget.Toolbar;
 39import android.util.Log;
 40import android.view.View;
 41import android.widget.Toast;
 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
 52public class PublishGroupChatProfilePictureActivity extends XmppActivity 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    }
 62
 63    @Override
 64    void onBackendConnected() {
 65        String uuid = pendingConversationUuid.pop();
 66        if (uuid != null) {
 67            this.conversation = xmppConnectionService.findConversationByUuid(uuid);
 68        }
 69        if (this.conversation == null) {
 70            return;
 71        }
 72        reloadAvatar();
 73    }
 74
 75    private void reloadAvatar() {
 76        final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
 77        Bitmap bitmap;
 78        if (uri == null) {
 79            bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
 80        } else {
 81            Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
 82            bitmap = xmppConnectionService.getFileBackend().cropCenterSquare(uri, size);
 83        }
 84        this.binding.accountImage.setImageBitmap(bitmap);
 85        this.binding.publishButton.setEnabled(uri != null);
 86    }
 87
 88    @Override
 89    public void onCreate(Bundle savedInstanceState) {
 90        super.onCreate(savedInstanceState);
 91        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
 92        setSupportActionBar((Toolbar) this.binding.toolbar);
 93        configureActionBar(getSupportActionBar());
 94        this.binding.cancelButton.setOnClickListener((v) -> this.finish());
 95        this.binding.secondaryHint.setVisibility(View.GONE);
 96        this.binding.accountImage.setOnClickListener((v) -> this.chooseAvatar());
 97        Intent intent = getIntent();
 98        String uuid = intent == null ? null : intent.getStringExtra("uuid");
 99        if (uuid != null) {
100            pendingConversationUuid.push(uuid);
101        }
102        this.binding.publishButton.setEnabled(uri != null);
103        this.binding.publishButton.setOnClickListener(this::publish);
104    }
105
106
107    private void publish(View view) {
108        binding.publishButton.setText(R.string.publishing);
109        binding.publishButton.setEnabled(false);
110        xmppConnectionService.publishMucAvatar(conversation, uri, this);
111    }
112
113    @Override
114    public void onActivityResult(int requestCode, int resultCode, Intent data) {
115        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
116            CropImage.ActivityResult result = CropImage.getActivityResult(data);
117            if (resultCode == RESULT_OK) {
118                this.uri = result.getUri();
119                if (xmppConnectionServiceBound) {
120                    reloadAvatar();
121                }
122            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
123                Exception error = result.getError();
124                if (error != null) {
125                    Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
126                }
127            }
128        }
129    }
130
131    private void chooseAvatar() {
132        CropImage.activity()
133                .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
134                .setAspectRatio(1, 1)
135                .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
136                .start(this);
137    }
138
139    @Override
140    public void onAvatarPublicationSucceeded() {
141        runOnUiThread(() -> {
142            Toast.makeText(this, R.string.avatar_has_been_published, Toast.LENGTH_SHORT).show();
143            finish();
144        });
145    }
146
147    @Override
148    public void onAvatarPublicationFailed(@StringRes int res) {
149        runOnUiThread(() -> {
150            Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
151            this.binding.publishButton.setText(R.string.publish);
152            this.binding.publishButton.setEnabled(true);
153        });
154    }
155}