Files
arcanechat-android/src/main/java/org/thoughtcrime/securesms/preferences/AdvancedPreferenceFragment.java
T
2026-05-25 15:35:22 +02:00

229 lines
8.3 KiB
Java

package org.thoughtcrime.securesms.preferences;
import static android.app.Activity.RESULT_OK;
import static android.text.InputType.TYPE_TEXT_VARIATION_URI;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_BCC_SELF;
import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_STATS_SENDING;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.CheckBoxPreference;
import androidx.preference.Preference;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.LogViewActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.StatsSending;
import org.thoughtcrime.securesms.connect.DcEventCenter;
import org.thoughtcrime.securesms.proxy.ProxySettingsActivity;
import org.thoughtcrime.securesms.relay.RelayListActivity;
import org.thoughtcrime.securesms.util.Prefs;
import org.thoughtcrime.securesms.util.ScreenLockUtil;
import org.thoughtcrime.securesms.util.StreamUtil;
public class AdvancedPreferenceFragment extends ListSummaryPreferenceFragment
implements DcEventCenter.DcEventDelegate {
private static final String TAG = "AdvancedPreferenceFragment";
CheckBoxPreference selfReportingCheckbox;
CheckBoxPreference multiDeviceCheckbox;
private ActivityResultLauncher<Intent> screenLockLauncher;
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
screenLockLauncher =
registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK) {
openRelayListActivity();
}
});
multiDeviceCheckbox = (CheckBoxPreference) this.findPreference("pref_bcc_self");
if (multiDeviceCheckbox != null) {
multiDeviceCheckbox.setOnPreferenceChangeListener(
(preference, newValue) -> {
boolean enabled = (Boolean) newValue;
if (enabled) {
dcContext.setConfigInt(CONFIG_BCC_SELF, 1);
return true;
} else {
new AlertDialog.Builder(requireContext())
.setMessage(R.string.pref_multidevice_change_warn)
.setPositiveButton(
R.string.ok,
(dialogInterface, i) -> {
dcContext.setConfigInt(CONFIG_BCC_SELF, 0);
((CheckBoxPreference) preference).setChecked(false);
})
.setNegativeButton(R.string.cancel, null)
.show();
return false;
}
});
}
Preference submitDebugLog = this.findPreference("pref_view_log");
if (submitDebugLog != null) {
submitDebugLog.setOnPreferenceClickListener(new ViewLogListener());
}
Preference webxdcStore = this.findPreference(Prefs.WEBXDC_STORE_URL_PREF);
if (webxdcStore != null) {
webxdcStore.setOnPreferenceClickListener(new WebxdcStoreUrlListener());
}
updateWebxdcStoreSummary();
selfReportingCheckbox = this.findPreference("pref_stats_sending");
if (selfReportingCheckbox != null) {
selfReportingCheckbox.setOnPreferenceChangeListener(
(preference, newValue) -> {
boolean enabled = (Boolean) newValue;
if (enabled) {
StatsSending.showStatsConfirmationDialog(
requireActivity(),
() -> {
((CheckBoxPreference) preference).setChecked(true);
});
return false;
} else {
dcContext.setConfigInt(CONFIG_STATS_SENDING, 0);
return true;
}
});
}
Preference proxySettings = this.findPreference("proxy_settings_button");
if (proxySettings != null) {
proxySettings.setOnPreferenceClickListener(
(preference) -> {
startActivity(new Intent(requireActivity(), ProxySettingsActivity.class));
return true;
});
}
Preference relayListBtn = this.findPreference("pref_relay_list_button");
if (relayListBtn != null) {
relayListBtn.setOnPreferenceClickListener(
((preference) -> {
boolean result =
ScreenLockUtil.applyScreenLock(
requireActivity(),
getString(R.string.transports),
getString(R.string.enter_system_secret_to_continue),
screenLockLauncher);
if (!result) {
openRelayListActivity();
}
return true;
}));
}
}
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences_advanced);
}
@Override
public void onResume() {
super.onResume();
Objects.requireNonNull(
((ApplicationPreferencesActivity) requireActivity()).getSupportActionBar())
.setTitle(R.string.menu_advanced);
selfReportingCheckbox.setChecked(0 != dcContext.getConfigInt(CONFIG_STATS_SENDING));
multiDeviceCheckbox.setChecked(0 != dcContext.getConfigInt(CONFIG_BCC_SELF));
}
protected File copyToCacheDir(Uri uri) throws IOException {
try (InputStream inputStream = requireActivity().getContentResolver().openInputStream(uri)) {
File file = File.createTempFile("tmp-keys-file", ".tmp", requireActivity().getCacheDir());
try (OutputStream outputStream = new FileOutputStream(file)) {
StreamUtil.copy(inputStream, outputStream);
}
return file;
}
}
public static @NonNull String getVersion(@Nullable Context context) {
try {
if (context == null) return "";
String app = context.getString(R.string.app_name);
String version =
context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
return String.format("%s %s", app, version);
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, e);
return context.getString(R.string.app_name);
}
}
private class ViewLogListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
final Intent intent = new Intent(requireActivity(), LogViewActivity.class);
startActivity(intent);
return true;
}
}
private class WebxdcStoreUrlListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
View gl = View.inflate(requireActivity(), R.layout.single_line_input, null);
EditText inputField = gl.findViewById(R.id.input_field);
inputField.setHint(Prefs.DEFAULT_WEBXDC_STORE_URL);
inputField.setText(Prefs.getWebxdcStoreUrl(requireActivity()));
inputField.setSelection(inputField.getText().length());
inputField.setInputType(TYPE_TEXT_VARIATION_URI);
new AlertDialog.Builder(requireActivity())
.setTitle(R.string.webxdc_store_url)
.setMessage(R.string.webxdc_store_url_explain)
.setView(gl)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(
android.R.string.ok,
(dlg, btn) -> {
Prefs.setWebxdcStoreUrl(requireActivity(), inputField.getText().toString());
updateWebxdcStoreSummary();
})
.show();
return true;
}
}
private void updateWebxdcStoreSummary() {
Preference preference = this.findPreference(Prefs.WEBXDC_STORE_URL_PREF);
if (preference != null) {
preference.setSummary(Prefs.getWebxdcStoreUrl(requireActivity()));
}
}
private void openRelayListActivity() {
Intent intent = new Intent(requireActivity(), RelayListActivity.class);
startActivity(intent);
}
}