1package eu.siacs.conversations.ui;
2
3import android.app.PendingIntent;
4import android.content.Intent;
5import android.content.pm.PackageManager;
6import android.graphics.Bitmap;
7import android.net.Uri;
8import android.os.Bundle;
9import android.support.annotation.NonNull;
10import android.support.annotation.StringRes;
11import android.util.Log;
12import android.view.Menu;
13import android.view.MenuItem;
14import android.view.View;
15import android.view.View.OnLongClickListener;
16import android.widget.Button;
17import android.widget.ImageView;
18import android.widget.TextView;
19import android.widget.Toast;
20
21import com.soundcloud.android.crop.Crop;
22
23import java.io.File;
24
25import eu.siacs.conversations.Config;
26import eu.siacs.conversations.R;
27import eu.siacs.conversations.entities.Account;
28import eu.siacs.conversations.persistance.FileBackend;
29import eu.siacs.conversations.services.XmppConnectionService;
30import eu.siacs.conversations.utils.FileUtils;
31import eu.siacs.conversations.utils.PhoneHelper;
32import eu.siacs.conversations.xmpp.pep.Avatar;
33
34public class PublishProfilePictureActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate {
35
36 private static final int REQUEST_CHOOSE_FILE_AND_CROP = 0xac23;
37 private static final int REQUEST_CHOOSE_FILE = 0xac24;
38 private ImageView avatar;
39 private TextView hintOrWarning;
40 private TextView secondaryHint;
41 private Button cancelButton;
42 private Button publishButton;
43 private Uri avatarUri;
44 private Uri defaultUri;
45 private Account account;
46 private boolean support = false;
47 private boolean publishing = false;
48 private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
49
50 @Override
51 public boolean onLongClick(View v) {
52 avatarUri = defaultUri;
53 loadImageIntoPreview(defaultUri);
54 return true;
55 }
56 };
57 private boolean mInitialAccountSetup;
58 private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
59
60 @Override
61 public void success(Avatar object) {
62 runOnUiThread(() -> {
63 if (mInitialAccountSetup) {
64 Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
65 WelcomeActivity.addInviteUri(intent, getIntent());
66 intent.putExtra("init", true);
67 startActivity(intent);
68 }
69 Toast.makeText(PublishProfilePictureActivity.this,
70 R.string.avatar_has_been_published,
71 Toast.LENGTH_SHORT).show();
72 finish();
73 });
74 }
75
76 @Override
77 public void error(final int errorCode, Avatar object) {
78 runOnUiThread(() -> {
79 hintOrWarning.setText(errorCode);
80 hintOrWarning.setTextColor(getWarningTextColor());
81 hintOrWarning.setVisibility(View.VISIBLE);
82 publishing = false;
83 togglePublishButton(true,R.string.publish);
84 });
85
86 }
87
88 @Override
89 public void userInputRequried(PendingIntent pi, Avatar object) {
90 }
91 };
92
93 @Override
94 public void onCreate(Bundle savedInstanceState) {
95 super.onCreate(savedInstanceState);
96 setContentView(R.layout.activity_publish_profile_picture);
97 setSupportActionBar(findViewById(R.id.toolbar));
98 configureActionBar(getSupportActionBar());
99
100 this.avatar = findViewById(R.id.account_image);
101 this.cancelButton = findViewById(R.id.cancel_button);
102 this.publishButton = findViewById(R.id.publish_button);
103 this.hintOrWarning = findViewById(R.id.hint_or_warning);
104 this.secondaryHint = findViewById(R.id.secondary_hint);
105 this.publishButton.setOnClickListener(v -> {
106 if (avatarUri != null) {
107 publishing = true;
108 togglePublishButton(false,R.string.publishing);
109 xmppConnectionService.publishAvatar(account, avatarUri, avatarPublication);
110 }
111 });
112 this.cancelButton.setOnClickListener(v -> {
113 if (mInitialAccountSetup) {
114 Intent intent = new Intent(getApplicationContext(), StartConversationActivity.class);
115 if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
116 WelcomeActivity.addInviteUri(intent, getIntent());
117 intent.putExtra("init", true);
118 }
119 startActivity(intent);
120 }
121 finish();
122 });
123 this.avatar.setOnClickListener(v -> {
124 if (hasStoragePermission(REQUEST_CHOOSE_FILE)) {
125 chooseAvatar(false);
126 }
127
128 });
129 this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext());
130 }
131
132 private void chooseAvatar(boolean crop) {
133 Intent attachFileIntent = new Intent();
134 attachFileIntent.setType("image/*");
135 attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
136 Intent chooser = Intent.createChooser(attachFileIntent, getString(R.string.attach_file));
137 startActivityForResult(chooser, crop ? REQUEST_CHOOSE_FILE_AND_CROP : REQUEST_CHOOSE_FILE);
138 }
139
140 @Override
141 public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
142 if (grantResults.length > 0)
143 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
144 if (requestCode == REQUEST_CHOOSE_FILE_AND_CROP) {
145 chooseAvatar(true);
146 } else if (requestCode == REQUEST_CHOOSE_FILE) {
147 chooseAvatar(false);
148 }
149 } else {
150 Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
151 }
152 }
153
154 @Override
155 public boolean onCreateOptionsMenu(Menu menu) {
156 getMenuInflater().inflate(R.menu.publish_avatar, menu);
157 return super.onCreateOptionsMenu(menu);
158 }
159
160 @Override
161 public boolean onOptionsItemSelected(final MenuItem item) {
162 if (item.getItemId() == R.id.action_crop_image) {
163 if (hasStoragePermission(REQUEST_CHOOSE_FILE_AND_CROP)) {
164 chooseAvatar(true);
165 }
166 return true;
167 } else {
168 return super.onOptionsItemSelected(item);
169 }
170 }
171
172 @Override
173 protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
174 super.onActivityResult(requestCode, resultCode, data);
175 if (resultCode == RESULT_OK) {
176 Uri source = data.getData();
177 switch (requestCode) {
178 case REQUEST_CHOOSE_FILE_AND_CROP:
179 if (FileBackend.weOwnFile(this, source)) {
180 Toast.makeText(this,R.string.security_error_invalid_file_access,Toast.LENGTH_SHORT).show();
181 return;
182 }
183 String original = FileUtils.getPath(this, source);
184 if (original != null) {
185 source = Uri.parse("file://"+original);
186 }
187 Uri destination = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
188 final int size = getPixel(192);
189 Crop.of(source, destination).asSquare().withMaxSize(size, size).start(this);
190 break;
191 case REQUEST_CHOOSE_FILE:
192 if (FileBackend.weOwnFile(this, source)) {
193 Toast.makeText(this,R.string.security_error_invalid_file_access,Toast.LENGTH_SHORT).show();
194 return;
195 }
196 this.avatarUri = source;
197 if (xmppConnectionServiceBound) {
198 loadImageIntoPreview(this.avatarUri);
199 }
200 break;
201 case Crop.REQUEST_CROP:
202 this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
203 if (xmppConnectionServiceBound) {
204 loadImageIntoPreview(this.avatarUri);
205 }
206 break;
207 }
208 } else {
209 if (requestCode == Crop.REQUEST_CROP && data != null) {
210 Throwable throwable = Crop.getError(data);
211 if (throwable != null && throwable instanceof OutOfMemoryError) {
212 Toast.makeText(this,R.string.selection_too_large, Toast.LENGTH_SHORT).show();
213 }
214 }
215 }
216 }
217
218 @Override
219 protected void onBackendConnected() {
220 this.account = extractAccount(getIntent());
221 if (this.account != null) {
222 reloadAvatar();
223 }
224 }
225
226 private void reloadAvatar() {
227 this.support = this.account.getXmppConnection() != null && this.account.getXmppConnection().getFeatures().pep();
228 if (this.avatarUri == null) {
229 if (this.account.getAvatar() != null || this.defaultUri == null) {
230 loadImageIntoPreview(null);
231 } else {
232 this.avatarUri = this.defaultUri;
233 loadImageIntoPreview(this.defaultUri);
234 }
235 } else {
236 loadImageIntoPreview(avatarUri);
237 }
238 }
239
240 @Override
241 protected void onStart() {
242 super.onStart();
243 if (getIntent() != null) {
244 this.mInitialAccountSetup = getIntent().getBooleanExtra("setup", false);
245 }
246 if (this.mInitialAccountSetup) {
247 this.cancelButton.setText(R.string.skip);
248 }
249 }
250
251 protected void loadImageIntoPreview(Uri uri) {
252
253 Bitmap bm = null;
254 if (uri == null) {
255 bm = avatarService().get(account, getPixel(192));
256 } else {
257 try {
258 bm = xmppConnectionService.getFileBackend().cropCenterSquare(uri, getPixel(192));
259 } catch (Exception e) {
260 Log.d(Config.LOGTAG,"unable to load bitmap into image view",e);
261 }
262 }
263
264 if (bm == null) {
265 togglePublishButton(false,R.string.publish);
266 this.hintOrWarning.setVisibility(View.VISIBLE);
267 this.hintOrWarning.setTextColor(getWarningTextColor());
268 this.hintOrWarning.setText(R.string.error_publish_avatar_converting);
269 return;
270 }
271 this.avatar.setImageBitmap(bm);
272 if (support) {
273 togglePublishButton(uri != null,R.string.publish);
274 this.hintOrWarning.setVisibility(View.INVISIBLE);
275 } else {
276 togglePublishButton(false,R.string.publish);
277 this.hintOrWarning.setVisibility(View.VISIBLE);
278 this.hintOrWarning.setTextColor(getWarningTextColor());
279 if (account.getStatus() == Account.State.ONLINE) {
280 this.hintOrWarning.setText(R.string.error_publish_avatar_no_server_support);
281 } else {
282 this.hintOrWarning.setText(R.string.error_publish_avatar_offline);
283 }
284 }
285 if (this.defaultUri == null || this.defaultUri.equals(uri)) {
286 this.secondaryHint.setVisibility(View.INVISIBLE);
287 this.avatar.setOnLongClickListener(null);
288 } else if (this.defaultUri != null) {
289 this.secondaryHint.setVisibility(View.VISIBLE);
290 this.avatar.setOnLongClickListener(this.backToDefaultListener);
291 }
292 }
293
294 protected void togglePublishButton(boolean enabled, @StringRes int res) {
295 final boolean status = enabled && !publishing;
296 this.publishButton.setText(publishing ? R.string.publishing : res);
297 this.publishButton.setEnabled(status);
298 }
299
300 public void refreshUiReal() {
301 if (this.account != null) {
302 reloadAvatar();
303 }
304 }
305
306 @Override
307 public void onAccountUpdate() {
308 refreshUi();
309 }
310}