3636import com .google .android .material .card .MaterialCardView ;
3737import com .google .android .material .shape .CornerFamily ;
3838import com .google .android .material .shape .ShapeAppearanceModel ;
39+ import com .d4rk .androidtutorials .java .utils .ConsentUtils ;
3940
4041import org .xmlpull .v1 .XmlPullParser ;
4142import org .xmlpull .v1 .XmlPullParserException ;
@@ -54,6 +55,7 @@ public class AndroidStudioFragment extends Fragment {
5455 private static boolean mobileAdsInitialized = false ;
5556 private final List <Object > allItems = new ArrayList <>();
5657 private LessonsAdapter adapter ;
58+ private boolean showAds ;
5759
5860 @ Nullable
5961 @ Override
@@ -65,15 +67,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
6567 @ Override
6668 public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
6769 super .onViewCreated (view , savedInstanceState );
68- ensureMobileAdsInitialized ();
70+ showAds = ConsentUtils .canShowAds (requireContext ());
71+ if (showAds ) {
72+ ensureMobileAdsInitialized ();
73+ }
6974 RecyclerView list = view .findViewById (R .id .lessons_list );
7075 list .setLayoutManager (new LinearLayoutManager (requireContext ()));
7176 adapter = new LessonsAdapter ();
7277 list .setAdapter (adapter );
73- list .addItemDecoration (new LessonAdSpacingDecoration (requireContext ()));
78+ if (showAds ) {
79+ list .addItemDecoration (new LessonAdSpacingDecoration (requireContext ()));
80+ }
7481 allItems .clear ();
7582 allItems .addAll (loadItems ());
76- populateAdapter (allItems );
83+ populateAdapter (allItems , showAds );
7784
7885 MenuHost menuHost = requireActivity ();
7986 menuHost .addMenuProvider (new MenuProvider () {
@@ -87,14 +94,14 @@ public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater)
8794 searchView .setOnQueryTextListener (new SearchView .OnQueryTextListener () {
8895 @ Override
8996 public boolean onQueryTextSubmit (String query ) {
90- filterLessons (query );
91- return true ;
97+ filterLessons (query );
98+ return true ;
9299 }
93100
94101 @ Override
95102 public boolean onQueryTextChange (String newText ) {
96- filterLessons (newText );
97- return true ;
103+ filterLessons (newText );
104+ return true ;
98105 }
99106 });
100107 }
@@ -174,7 +181,7 @@ private List<Object> loadItems() {
174181 return items ;
175182 }
176183
177- private void populateAdapter (List <Object > source ) {
184+ private void populateAdapter (List <Object > source , boolean showAds ) {
178185 List <Object > items = new ArrayList <>();
179186 List <Integer > eligible = new ArrayList <>();
180187 int lessonCount = 0 ;
@@ -190,7 +197,7 @@ private void populateAdapter(List<Object> source) {
190197 firstInCategory = false ;
191198 }
192199 }
193- int adCount = lessonCount / 3 ;
200+ int adCount = showAds ? lessonCount / 3 : 0 ;
194201 Collections .shuffle (eligible , new Random ());
195202 if (adCount > eligible .size ()) {
196203 adCount = eligible .size ();
@@ -214,7 +221,7 @@ private void populateAdapter(List<Object> source) {
214221 private void filterLessons (String query ) {
215222 String lower = query == null ? "" : query .toLowerCase ();
216223 if (lower .isEmpty ()) {
217- populateAdapter (allItems );
224+ populateAdapter (allItems , showAds );
218225 return ;
219226 }
220227 List <Object > filtered = new ArrayList <>();
@@ -234,7 +241,7 @@ private void filterLessons(String query) {
234241 }
235242 }
236243 }
237- populateAdapter (filtered );
244+ populateAdapter (filtered , showAds );
238245 }
239246
240247 private static class AdItem {}
0 commit comments