1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.ui.settings.fragments; 18 19 import android.os.Build; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.RequiresApi; 27 import androidx.fragment.app.Fragment; 28 import androidx.lifecycle.ViewModelProvider; 29 import androidx.recyclerview.widget.LinearLayoutManager; 30 import androidx.recyclerview.widget.RecyclerView; 31 32 import com.android.adservices.api.R; 33 import com.android.adservices.data.topics.Topic; 34 import com.android.adservices.ui.settings.activities.BlockedTopicsActivity; 35 import com.android.adservices.ui.settings.delegates.BlockedTopicsActionDelegate; 36 import com.android.adservices.ui.settings.viewadatpors.TopicsListViewAdapter; 37 import com.android.adservices.ui.settings.viewmodels.BlockedTopicsViewModel; 38 39 import java.util.function.Function; 40 41 /** Fragment for the blocked topics view of the AdServices Settings App. */ 42 @RequiresApi(Build.VERSION_CODES.S) 43 public class AdServicesSettingsBlockedTopicsFragment extends Fragment { 44 45 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)46 public View onCreateView( 47 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 48 return inflater.inflate(R.layout.blocked_topics_fragment, container, false); 49 } 50 51 @Override onViewCreated(@onNull View view, Bundle savedInstanceState)52 public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { 53 setupViewModel(view); 54 initActionListeners(); 55 } 56 57 // initialize all action listeners except for actions in blocked topics list initActionListeners()58 private void initActionListeners() { 59 BlockedTopicsActionDelegate actionDelegate = 60 ((BlockedTopicsActivity) requireActivity()).getActionDelegate(); 61 actionDelegate.initBlockedTopicsFragment(); 62 } 63 64 // initializes view model connection with blocked topics list. 65 // (Action listeners for each item in the list will be handled by the adapter) setupViewModel(View rootView)66 private void setupViewModel(View rootView) { 67 // create adapter 68 BlockedTopicsViewModel viewModel = 69 new ViewModelProvider(requireActivity()).get(BlockedTopicsViewModel.class); 70 Function<Topic, View.OnClickListener> getOnclickListener = 71 topic -> view -> viewModel.restoreTopicConsentButtonClickHandler(topic); 72 TopicsListViewAdapter adapter = 73 new TopicsListViewAdapter( 74 requireContext(), viewModel.getBlockedTopics(), getOnclickListener, true); 75 76 // set adapter for recyclerView 77 RecyclerView recyclerView = rootView.findViewById(R.id.blocked_topics_list); 78 recyclerView.setLayoutManager(new LinearLayoutManager(requireContext())); 79 recyclerView.setAdapter(adapter); 80 81 View noBlockedTopicsGaMessage = rootView.findViewById(R.id.no_blocked_topics_ga_message); 82 viewModel 83 .getBlockedTopics() 84 .observe( 85 getViewLifecycleOwner(), 86 blockedTopicsList -> { 87 noBlockedTopicsGaMessage.setVisibility( 88 blockedTopicsList.isEmpty() ? View.VISIBLE : View.GONE); 89 adapter.notifyDataSetChanged(); 90 }); 91 } 92 } 93