diff --git a/app/src/main/java/com/tomclaw/appsend/Appteka.java b/app/src/main/java/com/tomclaw/appsend/Appteka.java index 50b2e572..89179157 100644 --- a/app/src/main/java/com/tomclaw/appsend/Appteka.java +++ b/app/src/main/java/com/tomclaw/appsend/Appteka.java @@ -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; @@ -105,8 +104,4 @@ public static int getLastRunBuildNumber() { return lastRunBuildNumber; } - public String getInstallationID() { - return Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); - } - } diff --git a/app/src/main/java/com/tomclaw/appsend/core/DeviceIdInterceptor.kt b/app/src/main/java/com/tomclaw/appsend/core/DeviceIdInterceptor.kt new file mode 100644 index 00000000..2454a328 --- /dev/null +++ b/app/src/main/java/com/tomclaw/appsend/core/DeviceIdInterceptor.kt @@ -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() + ) + } + +} diff --git a/app/src/main/java/com/tomclaw/appsend/core/DeviceIdProvider.kt b/app/src/main/java/com/tomclaw/appsend/core/DeviceIdProvider.kt new file mode 100644 index 00000000..9eebf781 --- /dev/null +++ b/app/src/main/java/com/tomclaw/appsend/core/DeviceIdProvider.kt @@ -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) + } + +} diff --git a/app/src/main/java/com/tomclaw/appsend/di/AppModule.kt b/app/src/main/java/com/tomclaw/appsend/di/AppModule.kt index 3f4a1f9a..9412d43f 100644 --- a/app/src/main/java/com/tomclaw/appsend/di/AppModule.kt +++ b/app/src/main/java/com/tomclaw/appsend/di/AppModule.kt @@ -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 @@ -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 = @@ -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() diff --git a/app/src/main/java/com/tomclaw/appsend/main/controller/UpdateController.java b/app/src/main/java/com/tomclaw/appsend/main/controller/UpdateController.java index 4c76b1be..6434594f 100644 --- a/app/src/main/java/com/tomclaw/appsend/main/controller/UpdateController.java +++ b/app/src/main/java/com/tomclaw/appsend/main/controller/UpdateController.java @@ -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; @@ -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); @@ -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(); diff --git a/app/src/main/java/com/tomclaw/appsend/util/HttpUtil.java b/app/src/main/java/com/tomclaw/appsend/util/HttpUtil.java index 986facaa..213ff726 100644 --- a/app/src/main/java/com/tomclaw/appsend/util/HttpUtil.java +++ b/app/src/main/java/com/tomclaw/appsend/util/HttpUtil.java @@ -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; /** @@ -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; /** @@ -46,12 +47,18 @@ public static String prepareParameters(List> 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 {