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.graphics.drawable.AnimatedImageDrawable;
35import android.graphics.drawable.BitmapDrawable;
36import android.graphics.drawable.Drawable;
37import android.net.Uri;
38import android.os.Build;
39import android.os.Bundle;
40import android.util.Log;
41import android.view.View;
42import android.widget.Toast;
43
44import androidx.annotation.StringRes;
45import androidx.databinding.DataBindingUtil;
46
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
55import static eu.siacs.conversations.ui.PublishProfilePictureActivity.REQUEST_CHOOSE_PICTURE;
56
57import com.canhub.cropper.CropImage;
58
59public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication {
60 private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
61 private ActivityPublishProfilePictureBinding binding;
62 private Conversation conversation;
63 private Uri uri;
64
65 @Override
66 protected void refreshUiReal() {
67
68 }
69
70 @Override
71 protected void onBackendConnected() {
72 String uuid = pendingConversationUuid.pop();
73 if (uuid != null) {
74 this.conversation = xmppConnectionService.findConversationByUuid(uuid);
75 }
76 if (this.conversation == null) {
77 return;
78 }
79 reloadAvatar();
80 }
81
82 private void reloadAvatar() {
83 if (xmppConnectionService == null) return; // We will get called again in onBackendConnected
84 final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
85 Drawable bitmap;
86 if (uri == null) {
87 bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
88 } else {
89 Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
90 bitmap = xmppConnectionService.getFileBackend().cropCenterSquareDrawable(uri, size);
91 }
92 this.binding.accountImage.setImageDrawable(bitmap);
93 this.binding.publishButton.setEnabled(uri != null);
94 if (Build.VERSION.SDK_INT >= 28 && bitmap instanceof AnimatedImageDrawable) {
95 ((AnimatedImageDrawable) bitmap).start();
96 }
97 }
98
99 @Override
100 public void onCreate(Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
102 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
103 Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
104 setSupportActionBar(this.binding.toolbar);
105 configureActionBar(getSupportActionBar());
106 this.binding.cancelButton.setOnClickListener((v) -> this.finish());
107 this.binding.secondaryHint.setVisibility(View.GONE);
108 this.binding.accountImage.setOnClickListener((v) -> PublishProfilePictureActivity.chooseAvatar(this));
109 Intent intent = getIntent();
110 String uuid = intent == null ? null : intent.getStringExtra("uuid");
111 if (uuid != null) {
112 pendingConversationUuid.push(uuid);
113 }
114 this.binding.publishButton.setEnabled(uri != null);
115 this.binding.publishButton.setOnClickListener(this::publish);
116 }
117
118
119 private void publish(View view) {
120 binding.publishButton.setText(R.string.publishing);
121 binding.publishButton.setEnabled(false);
122 xmppConnectionService.publishMucAvatar(conversation, uri, this);
123 }
124
125 @Override
126 public void onActivityResult(int requestCode, int resultCode, Intent data) {
127 super.onActivityResult(requestCode, resultCode, data);
128 if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
129 final CropImage.ActivityResult result = CropImage.getActivityResult(data);
130 if (resultCode == RESULT_OK) {
131 this.uri = result.getUri();
132 if (xmppConnectionServiceBound) {
133 reloadAvatar();
134 }
135 } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
136 Exception error = result.getError();
137 if (error != null) {
138 Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
139 }
140 }
141 } else if (requestCode == REQUEST_CHOOSE_PICTURE) {
142 if (resultCode == RESULT_OK) {
143 cropUri(data.getData());
144 }
145 }
146 }
147
148 public void cropUri(final Uri uri) {
149 if (Build.VERSION.SDK_INT >= 28) {
150 this.uri = uri;
151 reloadAvatar();
152 if (this.binding.accountImage.getDrawable() instanceof AnimatedImageDrawable || this.binding.accountImage.getDrawable() instanceof FileBackend.SVGDrawable) {
153 return;
154 }
155 }
156
157 CropImage.activity(uri).setOutputCompressFormat(Bitmap.CompressFormat.PNG)
158 .setAspectRatio(1, 1)
159 .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
160 .start(this);
161 }
162
163 @Override
164 public void onAvatarPublicationSucceeded() {
165 runOnUiThread(() -> {
166 Toast.makeText(this, R.string.avatar_has_been_published, Toast.LENGTH_SHORT).show();
167 finish();
168 });
169 }
170
171 @Override
172 public void onAvatarPublicationFailed(@StringRes int res) {
173 runOnUiThread(() -> {
174 Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
175 this.binding.publishButton.setText(R.string.publish);
176 this.binding.publishButton.setEnabled(true);
177 });
178 }
179}