1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
package eu.siacs.conversations.ui;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.soundcloud.android.crop.Crop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.pep.Avatar;
public class PublishProfilePictureActivity extends XmppActivity {
private static final int REQUEST_CHOOSE_FILE = 0xac23;
private ImageView avatar;
private TextView accountTextView;
private TextView hintOrWarning;
private TextView secondaryHint;
private Button cancelButton;
private Button publishButton;
final static int REQUEST_CROP_PICTURE = 92374;
private Uri avatarUri;
private Uri defaultUri;
private OnLongClickListener backToDefaultListener = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
avatarUri = defaultUri;
loadImageIntoPreview(defaultUri);
return true;
}
};
private Account account;
private boolean support = false;
private boolean mInitialAccountSetup;
private UiCallback<Avatar> avatarPublication = new UiCallback<Avatar>() {
@Override
public void success(Avatar object) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mInitialAccountSetup) {
Intent intent = new Intent(getApplicationContext(),
StartConversationActivity.class);
intent.putExtra("init",true);
startActivity(intent);
}
Toast.makeText(PublishProfilePictureActivity.this,
R.string.avatar_has_been_published,
Toast.LENGTH_SHORT).show();
finish();
}
});
}
@Override
public void error(final int errorCode, Avatar object) {
runOnUiThread(new Runnable() {
@Override
public void run() {
hintOrWarning.setText(errorCode);
hintOrWarning.setTextColor(getWarningTextColor());
publishButton.setText(R.string.publish);
enablePublishButton();
}
});
}
@Override
public void userInputRequried(PendingIntent pi, Avatar object) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_publish_profile_picture);
this.avatar = (ImageView) findViewById(R.id.account_image);
this.cancelButton = (Button) findViewById(R.id.cancel_button);
this.publishButton = (Button) findViewById(R.id.publish_button);
this.accountTextView = (TextView) findViewById(R.id.account);
this.hintOrWarning = (TextView) findViewById(R.id.hint_or_warning);
this.secondaryHint = (TextView) findViewById(R.id.secondary_hint);
this.publishButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (avatarUri != null) {
publishButton.setText(R.string.publishing);
disablePublishButton();
xmppConnectionService.publishAvatar(account, avatarUri,
avatarPublication);
}
}
});
this.cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mInitialAccountSetup) {
Intent intent = new Intent(getApplicationContext(),
StartConversationActivity.class);
if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
intent.putExtra("init", true);
}
startActivity(intent);
}
finish();
}
});
this.avatar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent attachFileIntent = new Intent();
attachFileIntent.setType("image/*");
attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent chooser = Intent.createChooser(attachFileIntent,
getString(R.string.attach_file));
startActivityForResult(chooser, REQUEST_CHOOSE_FILE);
}
});
this.defaultUri = PhoneHelper.getSefliUri(getApplicationContext());
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CHOOSE_FILE) {
this.avatarUri = data.getData();
Uri destination = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
Crop.of(this.avatarUri, destination).asSquare().start(PublishProfilePictureActivity.this);
}
}
if (requestCode == Crop.REQUEST_CROP) {
this.avatarUri = Uri.fromFile(new File(getCacheDir(), "croppedAvatar"));
loadImageIntoPreview(this.avatarUri);
}
}
@Override
protected void onBackendConnected() {
if (getIntent() != null) {
Jid jid;
try {
jid = Jid.fromString(getIntent().getStringExtra("account"));
} catch (InvalidJidException e) {
jid = null;
}
if (jid != null) {
this.account = xmppConnectionService.findAccountByJid(jid);
if (this.account.getXmppConnection() != null) {
this.support = this.account.getXmppConnection().getFeatures().pep();
}
if (this.avatarUri == null) {
if (this.account.getAvatar() != null
|| this.defaultUri == null) {
this.avatar.setImageBitmap(avatarService().get(account,
getPixel(194)));
if (this.defaultUri != null) {
this.avatar
.setOnLongClickListener(this.backToDefaultListener);
} else {
this.secondaryHint.setVisibility(View.INVISIBLE);
}
if (!support) {
this.hintOrWarning
.setTextColor(getWarningTextColor());
this.hintOrWarning
.setText(R.string.error_publish_avatar_no_server_support);
}
} else {
this.avatarUri = this.defaultUri;
loadImageIntoPreview(this.defaultUri);
this.secondaryHint.setVisibility(View.INVISIBLE);
}
} else {
loadImageIntoPreview(avatarUri);
}
String account;
if (Config.DOMAIN_LOCK != null) {
account = this.account.getJid().getLocalpart();
} else {
account = this.account.getJid().toBareJid().toString();
}
this.accountTextView.setText(account);
}
}
}
@Override
protected void onStart() {
super.onStart();
if (getIntent() != null) {
this.mInitialAccountSetup = getIntent().getBooleanExtra("setup",
false);
}
if (this.mInitialAccountSetup) {
this.cancelButton.setText(R.string.skip);
}
}
private int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
private Bitmap loadScaledBitmap(String filePath, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath,options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath,options);
}
protected void loadImageIntoPreview(Uri uri) {
Bitmap bm = null;
try{
bm = loadScaledBitmap(uri.getPath(), Config.AVATAR_SIZE, Config.AVATAR_SIZE);
} catch (Exception e) {
e.printStackTrace();
}
if (bm == null) {
disablePublishButton();
this.hintOrWarning.setTextColor(getWarningTextColor());
this.hintOrWarning
.setText(R.string.error_publish_avatar_converting);
return;
}
this.avatar.setImageBitmap(bm);
if (support) {
enablePublishButton();
this.publishButton.setText(R.string.publish);
this.hintOrWarning.setText(R.string.publish_avatar_explanation);
this.hintOrWarning.setTextColor(getPrimaryTextColor());
} else {
disablePublishButton();
this.hintOrWarning.setTextColor(getWarningTextColor());
this.hintOrWarning
.setText(R.string.error_publish_avatar_no_server_support);
}
if (this.defaultUri != null && uri.equals(this.defaultUri)) {
this.secondaryHint.setVisibility(View.INVISIBLE);
this.avatar.setOnLongClickListener(null);
} else if (this.defaultUri != null) {
this.secondaryHint.setVisibility(View.VISIBLE);
this.avatar.setOnLongClickListener(this.backToDefaultListener);
}
}
protected void enablePublishButton() {
this.publishButton.setEnabled(true);
this.publishButton.setTextColor(getPrimaryTextColor());
}
protected void disablePublishButton() {
this.publishButton.setEnabled(false);
this.publishButton.setTextColor(getSecondaryTextColor());
}
public void refreshUiReal() {
//nothing to do. This Activity doesn't implement any listeners
}
}
|