~singpolyma/cheogram-android

9d9514a091dd76ac6a925e68feaa9b3ae754c6e5 — Daniel Gultsch 3 years ago bc58fb0
Add User-Agent to all HTTP calls
M build.gradle => build.gradle +7 -4
@@ 98,8 98,9 @@ android {
        archivesBaseName += "-$versionName"
        applicationId "eu.siacs.conversations"
        resValue "string", "applicationId", applicationId
        resValue "string", "app_name", "Conversations"
        buildConfigField "String", "LOGTAG", "\"conversations\""
        def appName = "Conversations"
        resValue "string", "app_name", appName
        buildConfigField "String", "APP_NAME", "\"$appName\"";
    }

    splits {


@@ 135,9 136,11 @@ android {
        quicksy {
            dimension "mode"
            applicationId = "im.quicksy.client"
            resValue "string", "app_name", "Quicksy"
            resValue "string", "applicationId", applicationId
            buildConfigField "String", "LOGTAG", "\"quicksy\""

            def appName = "Quicksy"
            resValue "string", "app_name", appName
            buildConfigField "String", "APP_NAME", "\"$appName\"";
        }

        conversations {

M src/main/java/eu/siacs/conversations/Config.java => src/main/java/eu/siacs/conversations/Config.java +2 -1
@@ 6,6 6,7 @@ import android.net.Uri;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import eu.siacs.conversations.crypto.XmppDomainVerifier;
import eu.siacs.conversations.xmpp.Jid;


@@ 35,7 36,7 @@ public final class Config {
        return (ENCRYPTION_MASK & (ENCRYPTION_MASK - 1)) != 0;
    }

    public static final String LOGTAG = BuildConfig.LOGTAG;
    public static final String LOGTAG = BuildConfig.APP_NAME.toLowerCase(Locale.US);

    public static final Jid BUG_REPORTS = Jid.of("bugs@conversations.im");
    public static final Uri HELP = Uri.parse("https://help.conversations.im");

M src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java => src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java +3 -10
@@ 12,6 12,7 @@ import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import eu.siacs.conversations.BuildConfig;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.axolotl.AxolotlService;


@@ 65,7 66,6 @@ public abstract class AbstractGenerator {
            Namespace.JINGLE_MESSAGE
    };
    protected XmppConnectionService mXmppConnectionService;
    private String mVersion = null;

    AbstractGenerator(XmppConnectionService service) {
        this.mXmppConnectionService = service;


@@ 77,18 77,11 @@ public abstract class AbstractGenerator {
    }

    String getIdentityVersion() {
        if (mVersion == null) {
            this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService);
        }
        return this.mVersion;
        return BuildConfig.VERSION_NAME;
    }

    String getIdentityName() {
        return mXmppConnectionService.getString(R.string.app_name);
    }

    public String getUserAgent() {
        return mXmppConnectionService.getString(R.string.app_name) + '/' + getIdentityVersion();
        return BuildConfig.APP_NAME;
    }

    String getIdentityType() {

M src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java => src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java +19 -2
@@ 19,10 19,10 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import eu.siacs.conversations.BuildConfig;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Message;


@@ 41,7 41,24 @@ public class HttpConnectionManager extends AbstractConnectionManager {

    public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);

    private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient();
    public static final OkHttpClient OK_HTTP_CLIENT;

    static {
        OK_HTTP_CLIENT = new OkHttpClient.Builder()
                .addInterceptor(chain -> {
                    final Request original = chain.request();
                    final Request modified = original.newBuilder()
                            .header("User-Agent", getUserAgent())
                            .build();
                    return chain.proceed(modified);
                })
                .build();
    }


    public static String getUserAgent() {
        return String.format("%s/%s", BuildConfig.APP_NAME, BuildConfig.VERSION_NAME);
    }

    public HttpConnectionManager(XmppConnectionService service) {
        super(service);

M src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java => src/main/java/eu/siacs/conversations/services/ChannelDiscoveryService.java +1 -1
@@ 50,7 50,7 @@ public class ChannelDiscoveryService {
    }

    void initializeMuclumbusService() {
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder();
        if (service.useTorToConnect()) {
            builder.proxy(HttpConnectionManager.getProxy());
        }

M src/main/java/eu/siacs/conversations/ui/AboutPreference.java => src/main/java/eu/siacs/conversations/ui/AboutPreference.java +8 -6
@@ 5,24 5,26 @@ import android.content.Intent;
import android.preference.Preference;
import android.util.AttributeSet;

import eu.siacs.conversations.BuildConfig;
import eu.siacs.conversations.R;
import eu.siacs.conversations.utils.PhoneHelper;

public class AboutPreference extends Preference {
	public AboutPreference(final Context context, final AttributeSet attrs, final int defStyle) {
		super(context, attrs, defStyle);
        final String appName = context.getString(R.string.app_name);
        setSummary(appName +' '+ PhoneHelper.getVersionName(context));
        setTitle(context.getString(R.string.title_activity_about_x, appName));
        setSummaryAndTitle(context);
	}

	public AboutPreference(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		final String appName = context.getString(R.string.app_name);
		setSummary(appName +' '+ PhoneHelper.getVersionName(context));
		setTitle(context.getString(R.string.title_activity_about_x, appName));
		setSummaryAndTitle(context);
	}

	private void setSummaryAndTitle(final Context context) {
	    setSummary(String.format("%s %s", BuildConfig.APP_NAME, BuildConfig.VERSION_NAME));
        setTitle(context.getString(R.string.title_activity_about_x, BuildConfig.APP_NAME));
    }

    @Override
    protected void onClick() {
        super.onClick();

M src/main/java/eu/siacs/conversations/utils/PhoneHelper.java => src/main/java/eu/siacs/conversations/utils/PhoneHelper.java +0 -13
@@ 35,17 35,4 @@ public class PhoneHelper {
		cursor.close();
		return uri == null ? null : Uri.parse(uri);
	}

	public static String getVersionName(Context context) {
		final String packageName = context == null ? null : context.getPackageName();
		if (packageName != null) {
			try {
				return context.getPackageManager().getPackageInfo(packageName, 0).versionName;
			} catch (final PackageManager.NameNotFoundException | RuntimeException e) {
				return "unknown";
			}
		} else {
			return "unknown";
		}
	}
}

M src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java => src/quicksy/java/eu/siacs/conversations/services/QuickConversationsService.java +2 -1
@@ 48,6 48,7 @@ import eu.siacs.conversations.crypto.sasl.Plain;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Entry;
import eu.siacs.conversations.http.HttpConnectionManager;
import eu.siacs.conversations.utils.AccountUtils;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;


@@ 260,7 261,7 @@ public class QuickConversationsService extends AbstractQuickConversationsService
    }

    private void setHeader(HttpURLConnection connection) {
        connection.setRequestProperty("User-Agent", service.getIqGenerator().getUserAgent());
        connection.setRequestProperty("User-Agent", HttpConnectionManager.getUserAgent());
        connection.setRequestProperty("Installation-Id", getInstallationId());
        connection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage());
    }