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.Menu;
41import android.view.MenuItem;
42import android.view.View;
43import android.widget.Toast;
44
45import com.theartofdev.edmodo.cropper.CropImage;
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.ui.interfaces.OnAvatarPublication;
52import eu.siacs.conversations.ui.util.PendingItem;
53
54public class PublishGroupChatProfilePictureActivity extends XmppActivity implements OnAvatarPublication {
55
56 private static final int REQUEST_CHOOSE_FILE = 0xac24;
57
58 private ActivityPublishProfilePictureBinding binding;
59
60 private final PendingItem<String> pendingConversationUuid = new PendingItem<>();
61
62 private Conversation conversation;
63 private Uri uri;
64
65 @Override
66 protected void refreshUiReal() {
67
68 }
69
70 @Override
71 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 final int size = (int) getResources().getDimension(R.dimen.publish_avatar_size);
84 Bitmap bitmap;
85 if (uri == null) {
86 bitmap = xmppConnectionService.getAvatarService().get(conversation, size);
87 } else {
88 Log.d(Config.LOGTAG, "loading " + uri.toString() + " into preview");
89 bitmap = xmppConnectionService.getFileBackend().cropCenterSquare(uri, size);
90 }
91 this.binding.accountImage.setImageBitmap(bitmap);
92 this.binding.publishButton.setEnabled(uri != null);
93 }
94
95 @Override
96 public void onCreate(Bundle savedInstanceState) {
97 super.onCreate(savedInstanceState);
98 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_publish_profile_picture);
99 setSupportActionBar((Toolbar) this.binding.toolbar);
100 configureActionBar(getSupportActionBar());
101 this.binding.cancelButton.setOnClickListener((v) -> this.finish());
102 this.binding.secondaryHint.setVisibility(View.GONE);
103 this.binding.accountImage.setOnClickListener((v) -> this.chooseAvatar());
104 Intent intent = getIntent();
105 String uuid = intent == null ? null : intent.getStringExtra("uuid");
106 if (uuid != null) {
107 pendingConversationUuid.push(uuid);
108 }
109 this.binding.publishButton.setEnabled(uri != null);
110 this.binding.publishButton.setOnClickListener(this::publish);
111 }
112
113
114 private void publish(View view) {
115 xmppConnectionService.publishMucAvatar(conversation, uri, this);
116 }
117
118 @Override
119 public void onActivityResult(int requestCode, int resultCode, Intent data) {
120 if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
121 CropImage.ActivityResult result = CropImage.getActivityResult(data);
122 if (resultCode == RESULT_OK) {
123 this.uri = result.getUri();
124 if (xmppConnectionServiceBound) {
125 reloadAvatar();
126 }
127 } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
128 Exception error = result.getError();
129 if (error != null) {
130 Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
131 }
132 }
133 }
134 }
135
136 private void chooseAvatar() {
137 CropImage.activity()
138 .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
139 .setAspectRatio(1, 1)
140 .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE)
141 .start(this);
142 }
143
144 @Override
145 public void onAvatarPublicationSucceeded() {
146 finish();
147 }
148
149 @Override
150 public void onAvatarPublicationFailed(@StringRes int res) {
151 runOnUiThread(() -> {
152 Toast.makeText(this,res,Toast.LENGTH_SHORT).show();
153 this.binding.publishButton.setText(R.string.publish);
154 this.binding.publishButton.setEnabled(true);
155 });
156 }
157}