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