Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b18768

Browse files
Refactor ad loading logic and update ic_open_in_new icon
- Removed consent check before loading ads in `HomeFragment`, `AboutFragment`, and `MainActivity`. - Renamed `ConsentUtils.canShowAds` to `canShowPersonalizedAds`. - Updated `ic_open_in_new.xml` with a new path data. - Updated tests in `ConsentUtilsTest` to reflect the method rename. - `AndroidStudioFragment` now uses `canShowPersonalizedAds` to determine if ads should be shown.
1 parent 9f7181f commit 3b18768

File tree

7 files changed

+20
-38
lines changed

7 files changed

+20
-38
lines changed

‎app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/about/AboutFragment.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ public android.view.View onCreateView(@NonNull android.view.LayoutInflater infla
4040

4141
new FastScrollerBuilder(binding.scrollView).useMd2Style().build();
4242

43-
if (ConsentUtils.canShowAds(requireContext())) {
44-
binding.adView.setVisibility(android.view.View.VISIBLE);
45-
AdUtils.loadBanner(binding.adView);
46-
} else {
47-
binding.adView.setVisibility(android.view.View.GONE);
48-
}
43+
binding.adView.setVisibility(android.view.View.VISIBLE);
44+
AdUtils.loadBanner(binding.adView);
4945

5046
String version = aboutViewModel.getVersionString();
5147
binding.textViewAppVersion.setText(version);

‎app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/android/AndroidStudioFragment.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
7171
@Override
7272
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
7373
super.onViewCreated(view, savedInstanceState);
74-
showAds = ConsentUtils.canShowAds(requireContext());
74+
showAds = ConsentUtils.canShowPersonalizedAds(requireContext());
7575
if (showAds) {
7676
ensureMobileAdsInitialized();
7777
}

‎app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/home/HomeFragment.java‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,10 @@ public void onDestroyView() {
8383
}
8484

8585
private void initializeAds() {
86-
if (ConsentUtils.canShowAds(requireContext())) {
87-
binding.smallBannerAd.setVisibility(View.VISIBLE);
88-
binding.largeBannerAd.setVisibility(View.VISIBLE);
89-
AdUtils.loadBanner(binding.smallBannerAd);
90-
AdUtils.loadBanner(binding.largeBannerAd);
91-
} else {
92-
binding.smallBannerAd.setVisibility(View.GONE);
93-
binding.largeBannerAd.setVisibility(View.GONE);
94-
}
86+
binding.smallBannerAd.setVisibility(View.VISIBLE);
87+
binding.largeBannerAd.setVisibility(View.VISIBLE);
88+
AdUtils.loadBanner(binding.smallBannerAd);
89+
AdUtils.loadBanner(binding.largeBannerAd);
9590
}
9691

9792
private void shareTip(String tip) {

‎app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/main/MainActivity.java‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,9 @@ public class MainActivity extends AppCompatActivity {
7272
public void onResume(@NonNull LifecycleOwner owner) {
7373
ConsentUtils.applyStoredConsent(MainActivity.this);
7474
if (mBinding != null && mBinding.adView != null) {
75-
if (ConsentUtils.canShowAds(MainActivity.this)) {
76-
mBinding.adPlaceholder.setVisibility(View.GONE);
77-
mBinding.adView.setVisibility(View.VISIBLE);
78-
AdUtils.loadBanner(mBinding.adView);
79-
} else {
80-
mBinding.adView.setVisibility(View.GONE);
81-
mBinding.adPlaceholder.setVisibility(View.VISIBLE);
82-
}
75+
mBinding.adPlaceholder.setVisibility(View.GONE);
76+
mBinding.adView.setVisibility(View.VISIBLE);
77+
AdUtils.loadBanner(mBinding.adView);
8378
}
8479
}
8580
};
@@ -216,14 +211,9 @@ private void observeViewModel() {
216211

217212
navBarView.setLabelVisibilityMode(uiState.bottomNavVisibility());
218213
if (binding.adView != null) {
219-
if (ConsentUtils.canShowAds(this)) {
220-
binding.adPlaceholder.setVisibility(View.GONE);
221-
binding.adView.setVisibility(View.VISIBLE);
222-
AdUtils.loadBanner(binding.adView);
223-
} else {
224-
binding.adView.setVisibility(View.GONE);
225-
binding.adPlaceholder.setVisibility(View.VISIBLE);
226-
}
214+
binding.adPlaceholder.setVisibility(View.GONE);
215+
binding.adView.setVisibility(View.VISIBLE);
216+
AdUtils.loadBanner(binding.adView);
227217
}
228218
}
229219

‎app/src/main/java/com/d4rk/androidtutorials/java/utils/ConsentUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void updateFirebaseConsent(Context context,
4040
FirebaseAnalytics.getInstance(context).setConsent(consentMap);
4141
}
4242

43-
public static boolean canShowAds(Context context) {
43+
public static boolean canShowPersonalizedAds(Context context) {
4444
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
4545
return prefs.getBoolean(context.getString(R.string.key_consent_ad_storage), true);
4646
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<vector xmlns:android="http://schemas.android.com/apk/res/android"
23
android:width="24dp"
34
android:height="24dp"
45
android:viewportWidth="24"
56
android:viewportHeight="24">
67
<path
78
android:fillColor="?attr/colorOnSurfaceVariant"
8-
android:pathData="M14,3v2h3.59l-4.83,4.83 1.41,1.41L19,6.41V10h2V3h-7zM5,5h6V3H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2v-6h-2v6H5V5z" />
9-
</vector>
9+
android:pathData="M19 4H5c-1.11 0-2 0.9-2 2v12c0 1.1 0.89 2 2 2h4v-2H5V8h14v10h-4v2h4c1.1 0 2-0.9 2-2V6c0-1.1-0.89-2-2-2zm-7 6l-4 4h3v6h2v-6h3l-4-4z" />
10+
</vector>

‎app/src/test/java/com/d4rk/androidtutorials/java/utils/ConsentUtilsTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ public void canShowAds_readsStoredPreference() {
113113
prefsStatic.when(() -> PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(prefs);
114114

115115
when(prefs.getBoolean("consent_ad_storage", true)).thenReturn(false);
116-
assertFalse(ConsentUtils.canShowAds(context));
116+
assertFalse(ConsentUtils.canShowPersonalizedAds(context));
117117

118118
when(prefs.getBoolean("consent_ad_storage", true)).thenReturn(true);
119-
assertTrue(ConsentUtils.canShowAds(context));
119+
assertTrue(ConsentUtils.canShowPersonalizedAds(context));
120120
}
121121
}
122122

@@ -132,7 +132,7 @@ public void canShowAds_returnsDefaultWhenPreferenceMissing() {
132132
when(prefs.getBoolean("consent_ad_storage", true))
133133
.thenAnswer(invocation -> invocation.getArgument(1));
134134

135-
assertTrue(ConsentUtils.canShowAds(context));
135+
assertTrue(ConsentUtils.canShowPersonalizedAds(context));
136136
verify(prefs).getBoolean("consent_ad_storage", true);
137137
}
138138
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /