From 6ec71659749da711ef2069d053cf2f3076437a2e Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Mon, 13 May 2024 21:07:46 -0400 Subject: [PATCH] Added Tiramasu support with version check for older versions of getApplicationInfo() API (#1092) * added Tiramasu support with API check for older versions * formatted file * format correcetly * format comment * more formatting * Added test * removed unused imiports * tiramisu check * Update FlutterRequestAgentProviderTest.java * Format Fix --------- Co-authored-by: Justin Malandruccolo Co-authored-by: Aldo Becerril --- .../FlutterRequestAgentProvider.java | 21 ++++++++++++++----- .../FlutterRequestAgentProviderTest.java | 18 +++++++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProvider.java b/packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProvider.java index 2366c40bf..aa8fc75d9 100644 --- a/packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProvider.java +++ b/packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProvider.java @@ -24,11 +24,22 @@ class FlutterRequestAgentProvider { private void processGameAndNewsTemplateVersions(Context context) { try { - ApplicationInfo info = - context - .getApplicationContext() - .getPackageManager() - .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); + ApplicationInfo info; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { + info = + context + .getApplicationContext() + .getPackageManager() + .getApplicationInfo( + context.getPackageName(), + PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA)); + } else { + info = + context + .getApplicationContext() + .getPackageManager() + .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); + } Bundle metaData = info.metaData; if (metaData != null) { gameTemplateVersion = info.metaData.getString(GAME_VERSION_KEY); diff --git a/packages/google_mobile_ads/android/src/test/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProviderTest.java b/packages/google_mobile_ads/android/src/test/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProviderTest.java index 980e2ff5a..c457f87a8 100644 --- a/packages/google_mobile_ads/android/src/test/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProviderTest.java +++ b/packages/google_mobile_ads/android/src/test/java/io/flutter/plugins/googlemobileads/FlutterRequestAgentProviderTest.java @@ -14,7 +14,10 @@ package io.flutter.plugins.googlemobileads; +import static android.os.Build.VERSION_CODES.S; +import static android.os.Build.VERSION_CODES.TIRAMISU; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -23,14 +26,17 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.os.Build; import android.os.Bundle; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; /** Tests {@link FlutterRequestAgentProvider}. */ @RunWith(RobolectricTestRunner.class) +@Config(sdk = {S, TIRAMISU}) public class FlutterRequestAgentProviderTest { private Context mockContext; @@ -49,9 +55,15 @@ public void setUp() throws NameNotFoundException { doReturn(mockContext).when(mockContext).getApplicationContext(); doReturn(mockPackageManager).when(mockContext).getPackageManager(); doReturn(PACKAGE_NAME).when(mockContext).getPackageName(); - doReturn(mockApplicationInfo) - .when(mockPackageManager) - .getApplicationInfo(eq(PACKAGE_NAME), eq(PackageManager.GET_META_DATA)); + if (Build.VERSION.SDK_INT >= TIRAMISU) { + doReturn(mockApplicationInfo) + .when(mockPackageManager) + .getApplicationInfo(eq(PACKAGE_NAME), any()); + } else { + doReturn(mockApplicationInfo) + .when(mockPackageManager) + .getApplicationInfo(eq(PACKAGE_NAME), eq(PackageManager.GET_META_DATA)); + } } @Test