Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,32 @@

import android.content.Context;
import android.text.TextUtils;

import com.android.mms.service_alt.exception.MmsHttpException;
import com.squareup.okhttp.ConnectionPool;
import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.Dns;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.huc.HttpURLConnectionImpl;
import com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl;

import timber.log.Timber;

import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
Expand All @@ -48,6 +52,7 @@
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -86,13 +91,13 @@ public class MmsHttpClient {
/**
* Constructor
*
* @param context The Context object
* @param socketFactory The socket factory for creating an OKHttp client
* @param hostResolver The host name resolver for creating an OKHttp client
* @param context The Context object
* @param socketFactory The socket factory for creating an OKHttp client
* @param hostResolver The host name resolver for creating an OKHttp client
* @param connectionPool The connection pool for creating an OKHttp client
*/
public MmsHttpClient(Context context, SocketFactory socketFactory, MmsNetworkManager hostResolver,
ConnectionPool connectionPool) {
ConnectionPool connectionPool) {
mContext = context;
mSocketFactory = socketFactory;
mHostResolver = hostResolver;
Expand All @@ -102,19 +107,19 @@ public MmsHttpClient(Context context, SocketFactory socketFactory, MmsNetworkMan
/**
* Execute an MMS HTTP request, either a POST (sending) or a GET (downloading)
*
* @param urlString The request URL, for sending it is usually the MMSC, and for downloading
* it is the message URL
* @param pdu For POST (sending) only, the PDU to send
* @param method HTTP method, POST for sending and GET for downloading
* @param urlString The request URL, for sending it is usually the MMSC, and for downloading
* it is the message URL
* @param pdu For POST (sending) only, the PDU to send
* @param method HTTP method, POST for sending and GET for downloading
* @param isProxySet Is there a proxy for the MMSC
* @param proxyHost The proxy host
* @param proxyPort The proxy port
* @param mmsConfig The MMS config to use
* @param proxyHost The proxy host
* @param proxyPort The proxy port
* @param mmsConfig The MMS config to use
* @return The HTTP response body
* @throws MmsHttpException For any failures
*/
public byte[] execute(String urlString, byte[] pdu, String method, boolean isProxySet,
String proxyHost, int proxyPort, MmsConfig.Overridden mmsConfig)
String proxyHost, int proxyPort, MmsConfig.Overridden mmsConfig)
throws MmsHttpException {
Timber.d("HTTP: " + method + " " + urlString
+ (isProxySet ? (", proxy=" + proxyHost + ":" + proxyPort) : "")
Expand Down Expand Up @@ -214,20 +219,40 @@ public byte[] execute(String urlString, byte[] pdu, String method, boolean isPro

/**
* Open an HTTP connection
*
* <p>
* TODO: The following code is borrowed from android.net.Network.openConnection
* Once that method supports proxy, we should use that instead
* Also we should remove the associated HostResolver and ConnectionPool from
* MmsNetworkManager
*
* @param url The URL to connect to
* @param url The URL to connect to
* @param proxy The proxy to use
* @return The opened HttpURLConnection
* @throws MalformedURLException If URL is malformed
*/
private HttpURLConnection openConnection(URL url, final Proxy proxy) throws MalformedURLException {
final String protocol = url.getProtocol();
OkHttpClient okHttpClient;

Dns resolverDns = new Dns() {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
try {
InetAddress[] addrs = mHostResolver.resolveInetAddresses(hostname);
if (addrs == null || addrs.length == 0) {
throw new UnknownHostException("No addresses for " + hostname);
}
return Arrays.asList(addrs);
} catch (UnknownHostException e) {
throw e;
} catch (Exception e) {
UnknownHostException uhe = new UnknownHostException("Failed to resolve " + hostname);
uhe.initCause(e);
throw uhe;
}
}
};

if (protocol.equals("http")) {
okHttpClient = new OkHttpClient();
okHttpClient.setFollowRedirects(false);
Expand Down Expand Up @@ -260,8 +285,14 @@ public Request authenticateProxy(Proxy proxy, Response response) throws IOExcept
});
okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT));
okHttpClient.setConnectionPool(new ConnectionPool(3, 60000));
okHttpClient.setSocketFactory(SocketFactory.getDefault());
Internal.instance.setNetwork(okHttpClient, mHostResolver);

okHttpClient.setDns(resolverDns);

if (mSocketFactory != null) {
okHttpClient.setSocketFactory(mSocketFactory);
} else {
okHttpClient.setSocketFactory(SocketFactory.getDefault());
}

if (proxy != null) {
okHttpClient.setProxy(proxy);
Expand Down Expand Up @@ -298,7 +329,8 @@ public Request authenticateProxy(Proxy proxy, Response response) throws IOExcept
});
okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT));
okHttpClient.setConnectionPool(new ConnectionPool(3, 60000));
Internal.instance.setNetwork(okHttpClient, mHostResolver);

okHttpClient.setDns(resolverDns);

return new HttpsURLConnectionImpl(url, okHttpClient);
} else {
Expand Down Expand Up @@ -385,6 +417,7 @@ private static void addLocaleToHttpAcceptLanguage(StringBuilder builder, Locale
}

private static final Pattern MACRO_P = Pattern.compile("##(\\S+)##");

/**
* Resolve the macro in HTTP param value text
* For example, "something##LINE1##something" is resolved to "something9139531419something"
Expand All @@ -393,7 +426,7 @@ private static void addLocaleToHttpAcceptLanguage(StringBuilder builder, Locale
* @return The HTTP param with macro resolved to real value
*/
private static String resolveMacro(Context context, String value,
MmsConfig.Overridden mmsConfig) {
MmsConfig.Overridden mmsConfig) {
if (TextUtils.isEmpty(value)) {
return value;
}
Expand Down Expand Up @@ -429,7 +462,7 @@ private static String resolveMacro(Context context, String value,
* macros like "##LINE1##" or "##NAI##" which is resolved with methods in this class
*
* @param connection The HttpURLConnection that we add headers to
* @param mmsConfig The MmsConfig object
* @param mmsConfig The MmsConfig object
*/
private void addExtraHeaders(HttpURLConnection connection, MmsConfig.Overridden mmsConfig) {
final String extraHttpParams = mmsConfig.getHttpParams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.SSLCertificateSocketFactory;
import android.os.Build;
import android.os.SystemClock;
import com.android.mms.service_alt.exception.MmsNetworkException;
import com.squareup.okhttp.ConnectionPool;
Expand All @@ -32,7 +31,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MmsNetworkManager implements com.squareup.okhttp.internal.Network {
public class MmsNetworkManager {

// Timeout used to call ConnectivityManager.requestNetwork
private static final int NETWORK_REQUEST_TIMEOUT_MILLIS = 60 * 1000;
Expand Down Expand Up @@ -236,7 +235,6 @@ private void resetLocked() {

private static final InetAddress[] EMPTY_ADDRESS_ARRAY = new InetAddress[0];

@Override
public InetAddress[] resolveInetAddresses(String host) throws UnknownHostException {
Network network = null;
synchronized (this) {
Expand Down
35 changes: 24 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import org.jetbrains.kotlin.gradle.plugin.KaptExtension
import com.android.build.gradle.BaseExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

// Needed until we upstream
buildscript {
dependencies {
classpath("io.realm:realm-gradle-plugin:10.15.0")
classpath("io.realm:realm-gradle-plugin:10.19.0")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.5.2")
}
}

plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) version "1.7.21" apply false
alias(libs.plugins.google.services) version "4.3.14" apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.google.services) apply false
}

tasks.register<Delete>("clean") {
delete(rootProject.buildDir)
delete(rootProject.layout.buildDirectory)
}

subprojects {
afterEvaluate {
extensions.findByType(KaptExtension::class.java)?.apply {
javacOptions {
option("-source", "8")
option("-target", "8")
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "17"
}

plugins.withId("com.android.application") {
extensions.configure(BaseExtension::class.java) {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
}
}
plugins.withId("com.android.library") {
extensions.configure(BaseExtension::class.java) {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
}
}
5 changes: 0 additions & 5 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ android {
version = release(36)
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

defaultConfig {
minSdk = 23
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
5 changes: 0 additions & 5 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ android {
version = release(36)
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

defaultConfig {
minSdk = 23
}
Expand Down
14 changes: 14 additions & 0 deletions domain/src/main/java/com/moez/QKSMS/util/FlowPreference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moez.QKSMS.util

import com.f2prateek.rx.preferences2.Preference
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.rx2.asFlow

class FlowPreference<T : Any>(
private val pref: Preference<T>
) {
val flow: Flow<T> = pref.asObservable().asFlow()

fun get(): T = pref.get()
fun set(value: T) = pref.set(value)
}
Loading