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.service.consent.App;
34 import com.android.adservices.ui.settings.activities.BlockedAppsActivity;
35 import com.android.adservices.ui.settings.delegates.BlockedAppsActionDelegate;
36 import com.android.adservices.ui.settings.viewadatpors.AppsListViewAdapter;
37 import com.android.adservices.ui.settings.viewmodels.BlockedAppsViewModel;
38 
39 import java.util.function.Function;
40 
41 /** Fragment for the blocked apps view of the AdServices Settings App. */
42 @RequiresApi(Build.VERSION_CODES.S)
43 public class AdServicesSettingsBlockedAppsFragment 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_apps_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 apps list
initActionListeners()58     private void initActionListeners() {
59         BlockedAppsActionDelegate actionDelegate =
60                 ((BlockedAppsActivity) requireActivity()).getActionDelegate();
61         actionDelegate.initBlockedAppsFragment();
62     }
63 
64     // initializes view model connection with blocked apps 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         BlockedAppsViewModel viewModel =
69                 new ViewModelProvider(requireActivity()).get(BlockedAppsViewModel.class);
70         Function<App, View.OnClickListener> getOnclickListener =
71                 app -> view -> viewModel.restoreAppConsentButtonClickHandler(app);
72         AppsListViewAdapter adapter =
73                 new AppsListViewAdapter(
74                         requireContext(), viewModel.getBlockedApps(), getOnclickListener, true);
75 
76         // set adapter for recyclerView
77         RecyclerView recyclerView = rootView.findViewById(R.id.blocked_apps_list);
78         recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
79         recyclerView.setAdapter(adapter);
80 
81         View noBlockedAppsGaMessage = rootView.findViewById(R.id.no_blocked_apps_ga_message);
82         viewModel
83                 .getBlockedApps()
84                 .observe(
85                         getViewLifecycleOwner(),
86                         blockedAppsList -> {
87                             noBlockedAppsGaMessage.setVisibility(
88                                     blockedAppsList.isEmpty() ? View.VISIBLE : View.GONE);
89                             adapter.notifyDataSetChanged();
90                         });
91     }
92 }
93