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.graphics.Bitmap;
 33import android.graphics.drawable.AnimatedImageDrawable;
 34import android.graphics.drawable.BitmapDrawable;
 35import android.graphics.drawable.Drawable;
 36import android.net.Uri;
 37import android.os.Build;
 38import android.os.Bundle;
 39import android.util.Log;
 40import android.view.View;
 41import android.widget.Toast;
 42import androidx.activity.result.ActivityResultLauncher;
 43import androidx.annotation.StringRes;
 44import androidx.databinding.DataBindingUtil;
 45import com.canhub.cropper.CropImageContract;
 46import com.canhub.cropper.CropImageContractOptions;
 47import eu.siacs.conversations.Config;
 48import eu.siacs.conversations.R;
 49import eu.siacs.conversations.databinding.ActivityPublishProfilePictureBinding;
 50import eu.siacs.conversations.entities.Conversation;
 51import eu.siacs.conversations.persistance.FileBackend;
 52import eu.siacs.conversations.ui.interfaces.OnAvatarPublication;
 53import eu.siacs.conversations.ui.util.PendingItem;
 54
 55public class PublishGroupChatProfilePictureActivity extends XmppActivity
 56        implements OnAvatarPublication {
 57    private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
 58    private ActivityPublishProfilePictureBinding binding;
 59    private Conversation conversation;
 60    private Uri uri;
 61
 62    final ActivityResultLauncher<CropImageContractOptions> cropImage =
 63            registerForActivityResult(
 64                    new CropImageContract(),
 65                    cropResult -> {
 66                        if (cropResult.isSuccessful()) {
 67                            onAvatarPicked(cropResult.getUriContent());
 68                        }
 69                    });
 70
 71    @Override
 72    protected void refreshUiReal() {}
 73
 74    @Override
 75    protected void onBackendConnected() {
 76        String uuid = pendingConversationUuid.pop();
 77        if (uuid != null) {
 78            this.conversation = xmppConnectionService.findConversationByUuid(uuid);
 79        }
 80        if (this.conversation == null) {
 81            return;
 82        }
 83        reloadAvatar();
 84    }
 85
 86    private void reloadAvatar() {
 87        if (xmppConnectionService == null) return; // We will get called again in onBackendConnected
 88        final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
 89        Drawable bitmap;
 90        if (uri == null) {
 91            bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
 92        } else {
 93            Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
 94            bitmap = xmppConnectionService.getFileBackend().cropCenterSquareDrawable(uri, size);
 95        }
 96        this.binding.accountImage.setImageDrawable(bitmap);
 97        this.binding.publishButton.setEnabled(uri != null);
 98        if (Build.VERSION.SDK_INT >= 28 && bitmap instanceof AnimatedImageDrawable) {
 99            ((AnimatedImageDrawable) bitmap).start();
100        }
101    }
102
103    @Override
104    public void onCreate(final Bundle savedInstanceState) {
105        super.onCreate(savedInstanceState);
106        this.binding =
107                DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
108        this.binding.contactOnly.setVisibility(View.GONE);
109        Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
110        setSupportActionBar(this.binding.toolbar);
111        configureActionBar(getSupportActionBar());
112        this.binding.cancelButton.setOnClickListener((v) -> this.finish());
113        this.binding.secondaryHint.setVisibility(View.GONE);
114        this.binding.accountImage.setOnClickListener((v) -> pickAvatar());
115
116        final var intent = getIntent();
117        final var uuid = intent == null ? null : intent.getStringExtra("uuid");
118        if (uuid != null) {
119            pendingConversationUuid.push(uuid);
120        }
121        this.binding.publishButton.setEnabled(uri != null);
122        this.binding.publishButton.setOnClickListener(this::publish);
123    }
124
125    private void publish(final View view) {
126        binding.publishButton.setText(R.string.publishing);
127        binding.publishButton.setEnabled(false);
128        xmppConnectionService.publishMucAvatar(conversation, uri, this);
129    }
130
131    public void pickAvatar() {
132        this.cropImage.launch(
133                new CropImageContractOptions(
134                        null, PublishProfilePictureActivity.getCropImageOptions()));
135    }
136
137    private void onAvatarPicked(final Uri uri) {
138        this.uri = uri;
139        if (xmppConnectionServiceBound) {
140            reloadAvatar();
141        }
142    }
143
144    @Override
145    public void onAvatarPublicationSucceeded() {
146        runOnUiThread(
147                () -> {
148                    Toast.makeText(this, R.string.avatar_has_been_published, Toast.LENGTH_SHORT)
149                            .show();
150                    finish();
151                });
152    }
153
154    @Override
155    public void onAvatarPublicationFailed(@StringRes int res) {
156        runOnUiThread(
157                () -> {
158                    Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
159                    this.binding.publishButton.setText(R.string.publish);
160                    this.binding.publishButton.setEnabled(true);
161                });
162    }
163}