Skip to content

Commit 029c90c

Browse files
Merge pull request #252 from MihaiCristianCondrea/codex/remove-canshowpersonalizedads-function
Remove manual personalized ads gating
2 parents 3b18768 + 0d9c164 commit 029c90c

File tree

3 files changed

+7
-54
lines changed

3 files changed

+7
-54
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.d4rk.androidtutorials.java.databinding.FragmentAndroidStudioBinding;
3333
import com.d4rk.androidtutorials.java.databinding.ItemAndroidStudioCategoryBinding;
3434
import com.d4rk.androidtutorials.java.databinding.ItemAndroidStudioLessonBinding;
35-
import com.d4rk.androidtutorials.java.utils.ConsentUtils;
3635
import com.google.android.gms.ads.AdListener;
3736
import com.google.android.gms.ads.LoadAdError;
3837
import com.google.android.material.card.MaterialCardView;
@@ -57,7 +56,6 @@ public class AndroidStudioFragment extends Fragment {
5756
private static boolean mobileAdsInitialized = false;
5857
private final List<Object> allItems = new ArrayList<>();
5958
private LessonsAdapter adapter;
60-
private boolean showAds;
6159
private FragmentAndroidStudioBinding binding;
6260

6361
@Nullable
@@ -71,20 +69,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
7169
@Override
7270
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
7371
super.onViewCreated(view, savedInstanceState);
74-
showAds = ConsentUtils.canShowPersonalizedAds(requireContext());
75-
if (showAds) {
76-
ensureMobileAdsInitialized();
77-
}
72+
ensureMobileAdsInitialized();
7873
RecyclerView list = binding.lessonsList;
7974
list.setLayoutManager(new LinearLayoutManager(requireContext()));
8075
adapter = new LessonsAdapter();
8176
list.setAdapter(adapter);
82-
if (showAds) {
83-
list.addItemDecoration(new LessonAdSpacingDecoration(requireContext()));
84-
}
77+
list.addItemDecoration(new LessonAdSpacingDecoration(requireContext()));
8578
allItems.clear();
8679
allItems.addAll(loadItems());
87-
populateAdapter(allItems, showAds);
80+
populateAdapter(allItems);
8881

8982
MenuHost menuHost = requireActivity();
9083
menuHost.addMenuProvider(new MenuProvider() {
@@ -208,7 +201,7 @@ private boolean isBrowserIntent(Intent intent) {
208201
&& ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme));
209202
}
210203

211-
private void populateAdapter(List<Object> source, boolean showAds) {
204+
private void populateAdapter(List<Object> source) {
212205
List<Object> items = new ArrayList<>();
213206
List<Integer> eligible = new ArrayList<>();
214207
int lessonCount = 0;
@@ -224,7 +217,7 @@ private void populateAdapter(List<Object> source, boolean showAds) {
224217
firstInCategory = false;
225218
}
226219
}
227-
int adCount = showAds ? lessonCount / 3 : 0;
220+
int adCount = lessonCount / 3;
228221
Collections.shuffle(eligible, new Random());
229222
if (adCount > eligible.size()) {
230223
adCount = eligible.size();
@@ -248,7 +241,7 @@ private void populateAdapter(List<Object> source, boolean showAds) {
248241
private void filterLessons(String query) {
249242
String lower = query == null ? "" : query.toLowerCase();
250243
if (lower.isEmpty()) {
251-
populateAdapter(allItems, showAds);
244+
populateAdapter(allItems);
252245
return;
253246
}
254247
List<Object> filtered = new ArrayList<>();
@@ -268,7 +261,7 @@ private void filterLessons(String query) {
268261
}
269262
}
270263
}
271-
populateAdapter(filtered, showAds);
264+
populateAdapter(filtered);
272265
}
273266

274267
private static class AdItem {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,4 @@ public static void updateFirebaseConsent(Context context,
3939
adPersonalization ? FirebaseAnalytics.ConsentStatus.GRANTED : FirebaseAnalytics.ConsentStatus.DENIED);
4040
FirebaseAnalytics.getInstance(context).setConsent(consentMap);
4141
}
42-
43-
public static boolean canShowPersonalizedAds(Context context) {
44-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
45-
return prefs.getBoolean(context.getString(R.string.key_consent_ad_storage), true);
46-
}
4742
}

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.d4rk.androidtutorials.java.utils;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
5-
import static org.junit.Assert.assertTrue;
64
import static org.mockito.Mockito.mock;
75
import static org.mockito.Mockito.verify;
86
import static org.mockito.Mockito.when;
@@ -103,37 +101,4 @@ public void updateFirebaseConsent_setsExpectedStatuses() {
103101
}
104102
}
105103

106-
@Test
107-
public void canShowAds_readsStoredPreference() {
108-
Context context = mock(Context.class);
109-
SharedPreferences prefs = mock(SharedPreferences.class);
110-
when(context.getString(R.string.key_consent_ad_storage)).thenReturn("consent_ad_storage");
111-
112-
try (MockedStatic<PreferenceManager> prefsStatic = Mockito.mockStatic(PreferenceManager.class)) {
113-
prefsStatic.when(() -> PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(prefs);
114-
115-
when(prefs.getBoolean("consent_ad_storage", true)).thenReturn(false);
116-
assertFalse(ConsentUtils.canShowPersonalizedAds(context));
117-
118-
when(prefs.getBoolean("consent_ad_storage", true)).thenReturn(true);
119-
assertTrue(ConsentUtils.canShowPersonalizedAds(context));
120-
}
121-
}
122-
123-
@Test
124-
public void canShowAds_returnsDefaultWhenPreferenceMissing() {
125-
Context context = mock(Context.class);
126-
SharedPreferences prefs = mock(SharedPreferences.class);
127-
when(context.getString(R.string.key_consent_ad_storage)).thenReturn("consent_ad_storage");
128-
129-
try (MockedStatic<PreferenceManager> prefsStatic = Mockito.mockStatic(PreferenceManager.class)) {
130-
prefsStatic.when(() -> PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(prefs);
131-
132-
when(prefs.getBoolean("consent_ad_storage", true))
133-
.thenAnswer(invocation -> invocation.getArgument(1));
134-
135-
assertTrue(ConsentUtils.canShowPersonalizedAds(context));
136-
verify(prefs).getBoolean("consent_ad_storage", true);
137-
}
138-
}
139104
}

0 commit comments

Comments
 (0)