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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
package de.pixart.messenger.ui;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.security.KeyStoreException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.services.ExportLogsService;
import de.pixart.messenger.services.MemorizingTrustManager;
import de.pixart.messenger.ui.util.Color;
import de.pixart.messenger.xmpp.XmppConnection;
import de.pixart.messenger.xmpp.jid.InvalidJidException;
import de.pixart.messenger.xmpp.jid.Jid;
public class SettingsActivity extends XmppActivity implements
OnSharedPreferenceChangeListener {
public static final String AWAY_WHEN_SCREEN_IS_OFF = "away_when_screen_off";
public static final String TREAT_VIBRATE_AS_SILENT = "treat_vibrate_as_silent";
public static final String DND_ON_SILENT_MODE = "dnd_on_silent_mode";
public static final String MANUALLY_CHANGE_PRESENCE = "manually_change_presence";
public static final String BLIND_TRUST_BEFORE_VERIFICATION = "btbv";
public static final String AUTOMATIC_MESSAGE_DELETION = "automatic_message_deletion";
public static final String BROADCAST_LAST_ACTIVITY = "last_activity";
public static final String WARN_UNENCRYPTED_CHAT = "warn_unencrypted_chat";
public static final String THEME = "theme";
public static final String SHOW_DYNAMIC_TAGS = "show_dynamic_tags";
public static final String SHOW_FOREGROUND_SERVICE = "show_foreground_service";
public static final String USE_BUNDLED_EMOJIS = "use_bundled_emoji";
public static final String USE_MULTI_ACCOUNTS = "use_multi_accounts";
public static final int REQUEST_WRITE_LOGS = 0xbf8701;
private SettingsFragment mSettingsFragment;
Preference multiAccountPreference;
boolean isMultiAccountChecked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
mSettingsFragment = (SettingsFragment) fm.findFragmentById(android.R.id.content);
if (mSettingsFragment == null || !mSettingsFragment.getClass().equals(SettingsFragment.class)) {
mSettingsFragment = new SettingsFragment();
fm.beginTransaction().replace(android.R.id.content, mSettingsFragment).commit();
}
mSettingsFragment.setActivityIntent(getIntent());
this.mTheme = findTheme();
setTheme(this.mTheme);
getWindow().getDecorView().setBackgroundColor(Color.get(this, R.attr.color_background_primary));
}
@Override
void onBackendConnected() {
}
@Override
public void onStart() {
super.onStart();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
multiAccountPreference = mSettingsFragment.findPreference("enable_multi_accounts");
if (multiAccountPreference != null) {
isMultiAccountChecked = ((CheckBoxPreference) multiAccountPreference).isChecked();
}
ListPreference resources = (ListPreference) mSettingsFragment.findPreference("resource");
if (resources != null) {
ArrayList<CharSequence> entries = new ArrayList<>(Arrays.asList(resources.getEntries()));
if (!entries.contains(Build.MODEL)) {
entries.add(0, Build.MODEL);
resources.setEntries(entries.toArray(new CharSequence[entries.size()]));
resources.setEntryValues(entries.toArray(new CharSequence[entries.size()]));
}
}
if (Config.FORCE_ORBOT) {
PreferenceCategory connectionOptions = (PreferenceCategory) mSettingsFragment.findPreference("connection_options");
PreferenceScreen expert = (PreferenceScreen) mSettingsFragment.findPreference("expert");
if (connectionOptions != null) {
expert.removePreference(connectionOptions);
}
}
//this feature is only available on Huawei Android 6.
PreferenceScreen huaweiPreferenceScreen = (PreferenceScreen) mSettingsFragment.findPreference("huawei");
if (huaweiPreferenceScreen != null) {
Intent intent = huaweiPreferenceScreen.getIntent();
//remove when Api version is above M (Version 6.0) or if the intent is not callable
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M || !isCallable(intent)) {
PreferenceCategory generalCategory = (PreferenceCategory) mSettingsFragment.findPreference("general");
generalCategory.removePreference(huaweiPreferenceScreen);
}
}
final Preference removeCertsPreference = mSettingsFragment.findPreference("remove_trusted_certificates");
if (removeCertsPreference != null) {
removeCertsPreference.setOnPreferenceClickListener(preference -> {
final MemorizingTrustManager mtm = xmppConnectionService.getMemorizingTrustManager();
final ArrayList<String> aliases = Collections.list(mtm.getCertificates());
if (aliases.size() == 0) {
displayToast(getString(R.string.toast_no_trusted_certs));
return true;
}
final ArrayList selectedItems = new ArrayList<>();
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this);
dialogBuilder.setTitle(getResources().getString(R.string.dialog_manage_certs_title));
dialogBuilder.setMultiChoiceItems(aliases.toArray(new CharSequence[aliases.size()]), null,
(dialog, indexSelected, isChecked) -> {
if (isChecked) {
selectedItems.add(indexSelected);
} else if (selectedItems.contains(indexSelected)) {
selectedItems.remove(Integer.valueOf(indexSelected));
}
if (selectedItems.size() > 0)
((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
else {
((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
});
dialogBuilder.setPositiveButton(
getResources().getString(R.string.dialog_manage_certs_positivebutton), (dialog, which) -> {
int count = selectedItems.size();
if (count > 0) {
for (int i = 0; i < count; i++) {
try {
Integer item = Integer.valueOf(selectedItems.get(i).toString());
String alias = aliases.get(item);
mtm.deleteCertificate(alias);
} catch (KeyStoreException e) {
e.printStackTrace();
displayToast("Error: " + e.getLocalizedMessage());
}
}
if (xmppConnectionServiceBound) {
reconnectAccounts();
}
displayToast(getResources().getQuantityString(R.plurals.toast_delete_certificates, count, count));
}
});
dialogBuilder.setNegativeButton(getResources().getString(R.string.dialog_manage_certs_negativebutton), null);
AlertDialog removeCertsDialog = dialogBuilder.create();
removeCertsDialog.show();
removeCertsDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return true;
});
}
final Preference exportLogsPreference = mSettingsFragment.findPreference("export_logs");
if (exportLogsPreference != null) {
exportLogsPreference.setOnPreferenceClickListener(preference -> {
if (hasStoragePermission(REQUEST_WRITE_LOGS)) {
startExport();
}
return true;
});
}
if (Config.ONLY_INTERNAL_STORAGE) {
final Preference cleanCachePreference = mSettingsFragment.findPreference("clean_cache");
if (cleanCachePreference != null) {
cleanCachePreference.setOnPreferenceClickListener(preference -> cleanCache());
}
final Preference cleanPrivateStoragePreference = mSettingsFragment.findPreference("clean_private_storage");
if (cleanPrivateStoragePreference != null) {
cleanPrivateStoragePreference.setOnPreferenceClickListener(preference -> cleanPrivateStorage());
}
}
final Preference deleteOmemoPreference = mSettingsFragment.findPreference("delete_omemo_identities");
if (deleteOmemoPreference != null) {
deleteOmemoPreference.setOnPreferenceClickListener(preference -> deleteOmemoIdentities());
}
final Preference enableMultiAccountsPreference = mSettingsFragment.findPreference("enable_multi_accounts");
if (enableMultiAccountsPreference != null) {
Log.d(Config.LOGTAG, "Multi account checkbox checked: " + isMultiAccountChecked);
if (isMultiAccountChecked) {
enableMultiAccountsPreference.setEnabled(false);
/*
if (xmppConnectionServiceBound) { // todo doesn't work --> it seems the service is never bound
final List<Account> accounts = xmppConnectionService.getAccounts();
Log.d(Config.LOGTAG, "Disabled multi account: Number of accounts " + accounts.size());
if (accounts.size() > 1) {
Log.d(Config.LOGTAG, "Disabled multi account not possible because you have more than one account");
enableMultiAccountsPreference.setEnabled(false);
} else {
Log.d(Config.LOGTAG, "Disabled multi account possible because you have one account");
enableMultiAccountsPreference.setEnabled(true);
}
} else {
enableMultiAccountsPreference.setEnabled(false);
}
*/
} else {
enableMultiAccountsPreference.setEnabled(true);
enableMultiAccountsPreference.setOnPreferenceClickListener(preference -> {
enableMultiAccounts();
return true;
});
}
}
}
private boolean isCallable(final Intent i) {
return i != null && getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}
private boolean cleanCache() {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
return true;
}
private boolean cleanPrivateStorage() {
cleanPrivatePictures();
cleanPrivateFiles();
return true;
}
private void cleanPrivatePictures() {
try {
File dir = new File(getFilesDir().getAbsolutePath(), "/Pictures/");
File[] array = dir.listFiles();
if (array != null) {
for (int b = 0; b < array.length; b++) {
String name = array[b].getName().toLowerCase();
if (name.equals(".nomedia")) {
continue;
}
if (array[b].isFile()) {
array[b].delete();
}
}
}
} catch (Throwable e) {
Log.e("CleanCache", e.toString());
}
}
private void cleanPrivateFiles() {
try {
File dir = new File(getFilesDir().getAbsolutePath(), "/Files/");
File[] array = dir.listFiles();
if (array != null) {
for (int b = 0; b < array.length; b++) {
String name = array[b].getName().toLowerCase();
if (name.equals(".nomedia")) {
continue;
}
if (array[b].isFile()) {
array[b].delete();
}
}
}
} catch (Throwable e) {
Log.e("CleanCache", e.toString());
}
}
private boolean deleteOmemoIdentities() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.pref_delete_omemo_identities);
final List<CharSequence> accounts = new ArrayList<>();
for (Account account : xmppConnectionService.getAccounts()) {
if (account.isEnabled()) {
accounts.add(account.getJid().toBareJid().toString());
}
}
final boolean[] checkedItems = new boolean[accounts.size()];
builder.setMultiChoiceItems(accounts.toArray(new CharSequence[accounts.size()]), checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
final AlertDialog alertDialog = (AlertDialog) dialog;
for (boolean item : checkedItems) {
if (item) {
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
return;
}
}
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
});
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.delete_selected_keys, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; ++i) {
if (checkedItems[i]) {
try {
Jid jid = Jid.fromString(accounts.get(i).toString());
Account account = xmppConnectionService.findAccountByJid(jid);
if (account != null) {
account.getAxolotlService().regenerateKeys(true);
}
} catch (InvalidJidException e) {
//
}
}
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return true;
}
private void enableMultiAccounts() {
if (!isMultiAccountChecked) {
multiAccountPreference.setEnabled(true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.pref_enable_multi_accounts_title);
builder.setMessage(R.string.pref_enable_multi_accounts_summary);
builder.setNegativeButton(R.string.cancel, (dialog, which) -> {
((CheckBoxPreference) multiAccountPreference).setChecked(false);
});
builder.setPositiveButton(R.string.enter_password, (dialog, which) -> {
((CheckBoxPreference) multiAccountPreference).setChecked(false);
enterPasswordDialog();
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
public void enterPasswordDialog() {
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.password, null);
final Preference preference = mSettingsFragment.findPreference("enable_multi_accounts");
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText password = promptsView.findViewById(R.id.password);
final EditText confirm_password = promptsView.findViewById(R.id.confirm_password);
confirm_password.setVisibility(View.VISIBLE);
alertDialogBuilder.setTitle(R.string.enter_password);
alertDialogBuilder.setMessage(R.string.enter_password);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton(R.string.ok,
(dialog, id) -> {
final String pw1 = password.getText().toString();
final String pw2 = confirm_password.getText().toString();
if (!pw1.equals(pw2)) {
((CheckBoxPreference) preference).setChecked(false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.error);
builder.setMessage(R.string.passwords_do_not_match);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.try_again, (dialog12, id12) -> enterPasswordDialog());
builder.create().show();
} else if (pw1.trim().isEmpty()) {
((CheckBoxPreference) preference).setChecked(false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.error);
builder.setMessage(R.string.password_should_not_be_empty);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.try_again, (dialog1, id1) -> enterPasswordDialog());
builder.create().show();
} else {
((CheckBoxPreference) preference).setChecked(true);
SharedPreferences multiaccount_prefs = getApplicationContext().getSharedPreferences(USE_MULTI_ACCOUNTS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = multiaccount_prefs.edit();
editor.putString("BackupPW", pw1);
editor.commit();
}
})
.setNegativeButton(R.string.cancel, null);
alertDialogBuilder.create().show();
}
@Override
public void onStop() {
super.onStop();
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String name) {
final List<String> resendPresence = Arrays.asList(
"confirm_messages",
DND_ON_SILENT_MODE,
AWAY_WHEN_SCREEN_IS_OFF,
"allow_message_correction",
TREAT_VIBRATE_AS_SILENT,
MANUALLY_CHANGE_PRESENCE,
BROADCAST_LAST_ACTIVITY);
if (name.equals("resource")) {
String resource = preferences.getString("resource", "mobile")
.toLowerCase(Locale.US);
if (xmppConnectionServiceBound) {
for (Account account : xmppConnectionService.getAccounts()) {
if (account.setResource(resource)) {
if (account.isEnabled()) {
XmppConnection connection = account.getXmppConnection();
if (connection != null) {
connection.resetStreamId();
}
xmppConnectionService.reconnectAccountInBackground(account);
}
}
}
}
} else if (name.equals(SHOW_FOREGROUND_SERVICE)) {
xmppConnectionService.toggleForegroundService();
} else if (resendPresence.contains(name)) {
if (xmppConnectionServiceBound) {
if (name.equals(AWAY_WHEN_SCREEN_IS_OFF)
|| name.equals(MANUALLY_CHANGE_PRESENCE)) {
xmppConnectionService.toggleScreenEventReceiver();
}
if (name.equals(MANUALLY_CHANGE_PRESENCE) && !noAccountUsesPgp()) {
Toast.makeText(this, R.string.republish_pgp_keys, Toast.LENGTH_LONG).show();
}
xmppConnectionService.refreshAllPresences();
}
} else if (name.equals("dont_trust_system_cas")) {
xmppConnectionService.updateMemorizingTrustmanager();
reconnectAccounts();
} else if (name.equals("use_tor")) {
reconnectAccounts();
} else if (name.equals(AUTOMATIC_MESSAGE_DELETION)) {
xmppConnectionService.expireOldMessages(true);
} else if (name.equals(THEME)) {
final int theme = findTheme();
if (this.mTheme != theme) {
recreate();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults.length > 0)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (requestCode == REQUEST_WRITE_LOGS) {
startExport();
}
} else {
Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
}
}
private void startExport() {
startService(new Intent(getApplicationContext(), ExportLogsService.class));
}
private void displayToast(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(SettingsActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
}
private void reconnectAccounts() {
for (Account account : xmppConnectionService.getAccounts()) {
if (account.isEnabled()) {
xmppConnectionService.reconnectAccountInBackground(account);
}
}
}
public void refreshUiReal() {
//nothing to do. This Activity doesn't implement any listeners
}
}
|