Add preference for custom client resource name
Some checks failed
Android CI / build (pull_request) Has been cancelled

This commit is contained in:
Arne 2025-08-31 23:06:18 +02:00
commit 16a70dd838
5 changed files with 50 additions and 0 deletions

View file

@ -38,6 +38,7 @@ public class AppSettings {
public static final String TRUST_SYSTEM_CA_STORE = "trust_system_ca_store";
public static final String DANE_ENFORCED = "enforce_dane";
public static final String CUSTOM_RESOURCE_NAME = "custom_resource_name";
public static final String REQUIRE_CHANNEL_BINDING = "channel_binding_required";
public static final String NOTIFICATION_RINGTONE = "notification_ringtone";
public static final String NOTIFICATION_HEADS_UP = "notification_headsup";

View file

@ -79,6 +79,9 @@ public class PrivacySettingsFragment extends XmppPreferenceFragment {
requireService().toggleScreenEventReceiver();
requireService().refreshAllPresences();
}
case AppSettings.CUSTOM_RESOURCE_NAME -> {
reconnectAccounts();
}
case AppSettings.CONFIRM_MESSAGES,
AppSettings.BROADCAST_LAST_ACTIVITY,
AppSettings.ALLOW_MESSAGE_CORRECTION,

View file

@ -3,6 +3,7 @@ package eu.siacs.conversations.xmpp;
import static eu.siacs.conversations.utils.Random.SECURE_RANDOM;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
@ -14,6 +15,7 @@ import android.util.Pair;
import android.util.SparseArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
@ -2113,11 +2115,18 @@ public class XmppConnection implements Runnable {
return;
}
clearIqCallbacks();
/*
if (account.getJid().isBareJid()) {
account.setResource(createNewResource());
} else {
fixResource(mXmppConnectionService, account);
}
*/
// New resource setting
Context context = mXmppConnectionService.getApplicationContext();
String clientResource = getEffectiveClientResource(context);
account.setResource(clientResource);
final Iq iq = new Iq(Iq.Type.SET);
final String resource =
Config.USE_RANDOM_RESOURCE_ON_EVERY_BIND
@ -3340,4 +3349,31 @@ public class XmppConnection implements Runnable {
return hasDiscoFeature(account.getJid().asBareJid(), Namespace.MDS_DISPLAYED);
}
}
/**
* Retrieves the custom resource from SharedPreferences or use a default one
*/
private String getEffectiveClientResource(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String customResource = sharedPreferences.getString("custom_resource_name", null);
if (customResource != null && !customResource.trim().isEmpty()) {
Log.d(Config.LOGTAG, "Using custom resource: " + customResource);
return customResource;
} else {
// Fallback to original default resource generation logic
String appName = "monocles chat"; // A sensible default
try {
// appName = context.getString(R.string.app_name);
appName = BuildConfig.APP_NAME + " " + BuildConfig.VERSION_NAME;
} catch (Exception e) {
Log.w(Config.LOGTAG, "Could not get appName from BuildConfig, using default.",e);
}
// String defaultResource = String.format("%s.%s", appName, CryptoHelper.random(3));
String defaultResource = String.format("%s", appName);
Log.d(Config.LOGTAG, "Using default generated resource: " + defaultResource);
return defaultResource;
}
}
}

View file

@ -1512,4 +1512,7 @@
<string name="you_retracted_this_message">You retracted this message</string>
<string name="cannot_retract_message_no_id">Cannot retract message, no ID.</string>
<string name="message_was_retracted_by">Message was retracted by</string>
<string name="pref_custom_resource_name">Custom client resource name</string>
<string name="pref_custom_resource_name_summary">Set a custom name for your client\'s resource</string>
<string name="pref_custom_resource_name_dialog_title">Enter custom resource name</string>
</resources>

View file

@ -20,6 +20,13 @@
android:icon="@drawable/ic_visibility_24dp"
android:summary="@string/pref_broadcast_last_activity_summary"
android:title="@string/pref_broadcast_last_activity" />
<EditTextPreference
app:key="custom_resource_name"
android:icon="@drawable/monocleslogo_chat_signet"
app:title="@string/pref_custom_resource_name"
app:summary="@string/pref_custom_resource_name_summary"
app:dialogTitle="@string/pref_custom_resource_name_dialog_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_interaction">
<SwitchPreferenceCompat