Skip to content

Commit

Permalink
Provide X-Device-Id in headers; fix update controller
Browse files Browse the repository at this point in the history
  • Loading branch information
solkin committed Jan 29, 2024
1 parent 1aaf049 commit dd58ad3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
5 changes: 0 additions & 5 deletions app/src/main/java/com/tomclaw/appsend/Appteka.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Collections.singletonList;

import android.app.Application;
import android.provider.Settings;

import com.tomclaw.appsend.di.AppComponent;
import com.tomclaw.appsend.di.AppModule;
Expand Down Expand Up @@ -105,8 +104,4 @@ public static int getLastRunBuildNumber() {
return lastRunBuildNumber;
}

public String getInstallationID() {
return Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
}

}
18 changes: 18 additions & 0 deletions app/src/main/java/com/tomclaw/appsend/core/DeviceIdInterceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tomclaw.appsend.core

import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

class DeviceIdInterceptor(private val deviceId: String) : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
return chain.proceed(
chain.request().newBuilder()
.header("X-Device-ID", deviceId)
.build()
)
}

}
23 changes: 23 additions & 0 deletions app/src/main/java/com/tomclaw/appsend/core/DeviceIdProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tomclaw.appsend.core

import android.annotation.SuppressLint
import android.content.Context
import android.provider.Settings
import android.provider.Settings.Secure.ANDROID_ID

interface DeviceIdProvider {

fun getDeviceId(): String

}

class DeviceIdProviderImpl(
private val context: Context
) : DeviceIdProvider {

@SuppressLint("HardwareIds")
override fun getDeviceId(): String {
return Settings.Secure.getString(context.contentResolver, ANDROID_ID)
}

}
9 changes: 9 additions & 0 deletions app/src/main/java/com/tomclaw/appsend/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import com.google.gson.GsonBuilder
import com.tomclaw.appsend.categories.CategoriesInteractor
import com.tomclaw.appsend.categories.CategoriesInteractorImpl
import com.tomclaw.appsend.core.Config
import com.tomclaw.appsend.core.DeviceIdInterceptor
import com.tomclaw.appsend.core.DeviceIdProvider
import com.tomclaw.appsend.core.DeviceIdProviderImpl
import com.tomclaw.appsend.core.PersistentCookieJar
import com.tomclaw.appsend.core.StoreApi
import com.tomclaw.appsend.core.UserAgentInterceptor
Expand Down Expand Up @@ -92,6 +95,10 @@ class AppModule(private val app: Application) {
locale: Locale
): UserAgentProvider = UserAgentProviderImpl(app, packageManager, locale)

@Provides
@Singleton
internal fun provideDeviceIdProvider(): DeviceIdProvider = DeviceIdProviderImpl(app)

@Provides
@Singleton
internal fun provideDownloadNotifications(): DownloadNotifications =
Expand Down Expand Up @@ -165,10 +172,12 @@ class AppModule(private val app: Application) {
internal fun provideHttClient(
cookieJar: CookieJar,
userAgentProvider: UserAgentProvider,
deviceIdProvider: DeviceIdProvider,
): OkHttpClient = OkHttpClient.Builder()
.readTimeout(2, TimeUnit.MINUTES)
.connectTimeout(20, TimeUnit.SECONDS)
.addInterceptor(UserAgentInterceptor(userAgentProvider.getUserAgent()))
.addInterceptor(DeviceIdInterceptor(deviceIdProvider.getDeviceId()))
.addInterceptor(ChuckerInterceptor.Builder(app).build())
.cookieJar(cookieJar)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import android.content.Context;

import com.tomclaw.appsend.core.DeviceIdProvider;
import com.tomclaw.appsend.core.DeviceIdProviderImpl;
import com.tomclaw.appsend.core.MainExecutor;
import com.tomclaw.appsend.main.item.StoreItem;
import com.tomclaw.appsend.util.HttpParamsBuilder;
Expand Down Expand Up @@ -120,9 +122,9 @@ private void loadInternal(int build) {
HttpURLConnection connection = null;
InputStream in = null;
try {
DeviceIdProvider devIdProvider = new DeviceIdProviderImpl(app());
HttpParamsBuilder builder = new HttpParamsBuilder()
.appendParam("build", String.valueOf(build))
.appendParam("inst_id", app().getInstallationID());
.appendParam("build", String.valueOf(build));
String storeUrl = HOST_UPDATE_URL + "?" + builder.build();
LegacyLogger.log(String.format("Store url: %s", storeUrl));
URL url = new URL(storeUrl);
Expand All @@ -135,6 +137,7 @@ private void loadInternal(int build) {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("X-Device-ID", devIdProvider.getDeviceId());
connection.connect();
// Open connection to response.
int responseCode = connection.getResponseCode();
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/tomclaw/appsend/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
Expand All @@ -21,7 +20,9 @@ public class HttpUtil {

public static final String GET = "GET";
public static final String POST = "POST";


public static final String UTF8_ENCODING = "UTF-8";

public static final int SC_BAD_REQUEST = 400;

/**
Expand All @@ -46,12 +47,18 @@ public static String prepareParameters(List<Pair<String, String>> pairs)
return builder.toString();
}

/**
* @noinspection CharsetObjectCanBeUsed
*/
public static String urlEncode(String string) throws UnsupportedEncodingException {
return URLEncoder.encode(string, StandardCharsets.UTF_8).replace("+", "%20");
return URLEncoder.encode(string, UTF8_ENCODING).replace("+", "%20");
}

/**
* @noinspection CharsetObjectCanBeUsed
*/
public static String streamToString(InputStream inputStream) throws IOException {
return new String(streamToArray(inputStream), StandardCharsets.UTF_8);
return new String(streamToArray(inputStream), UTF8_ENCODING);
}

public static byte[] streamToArray(InputStream inputStream) throws IOException {
Expand Down

0 comments on commit dd58ad3

Please sign in to comment.