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 package com.android.adservices.ui.notifications;
17 
18 import static com.android.adservices.ui.notifications.ConsentNotificationActivity.NotificationFragmentEnum.CONFIRMATION_PAGE_DISMISSED;
19 import static com.android.adservices.ui.notifications.ConsentNotificationActivity.NotificationFragmentEnum.CONFIRMATION_PAGE_DISPLAYED;
20 import static com.android.adservices.ui.notifications.ConsentNotificationActivity.NotificationFragmentEnum.CONFIRMATION_PAGE_OPT_IN_GOT_IT_BUTTON_CLICKED;
21 import static com.android.adservices.ui.notifications.ConsentNotificationActivity.NotificationFragmentEnum.CONFIRMATION_PAGE_OPT_IN_SETTINGS_CLICKED;
22 import static com.android.adservices.ui.notifications.ConsentNotificationActivity.NotificationFragmentEnum.CONFIRMATION_PAGE_OPT_OUT_GOT_IT_BUTTON_CLICKED;
23 import static com.android.adservices.ui.notifications.ConsentNotificationActivity.NotificationFragmentEnum.CONFIRMATION_PAGE_OPT_OUT_SETTINGS_CLICKED;
24 import static com.android.adservices.ui.settings.activities.AdServicesSettingsMainActivity.FROM_NOTIFICATION_KEY;
25 
26 import android.content.Intent;
27 import android.os.Build;
28 import android.os.Bundle;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.Button;
33 
34 import androidx.annotation.NonNull;
35 import androidx.annotation.RequiresApi;
36 import androidx.fragment.app.Fragment;
37 
38 import com.android.adservices.api.R;
39 import com.android.adservices.ui.settings.activities.AdServicesSettingsMainActivity;
40 
41 /**
42  * Fragment for the confirmation view after accepting or rejecting to be part of Privacy Sandbox
43  * Beta.
44  */
45 // TODO(b/269798827): Enable for R.
46 @RequiresApi(Build.VERSION_CODES.S)
47 public class ConsentNotificationConfirmationFragment extends Fragment {
48     public static final String IS_CONSENT_GIVEN_ARGUMENT_KEY = "isConsentGiven";
49 
50     private boolean mOptedIn;
51 
52     @Override
onCreateView( @onNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)53     public View onCreateView(
54             @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
55         mOptedIn = getArguments().getBoolean(IS_CONSENT_GIVEN_ARGUMENT_KEY, false);
56 
57         if (mOptedIn) {
58             return inflater.inflate(
59                     R.layout.consent_notification_accept_confirmation_fragment, container, false);
60         }
61         return inflater.inflate(
62                 R.layout.consent_notification_decline_confirmation_fragment, container, false);
63     }
64 
65     @Override
onViewCreated(@onNull View view, Bundle savedInstanceState)66     public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
67         setupListeners();
68 
69         ConsentNotificationActivity.handleAction(CONFIRMATION_PAGE_DISPLAYED, getContext());
70     }
71 
72     @Override
onSaveInstanceState(@onNull Bundle savedInstanceState)73     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
74         super.onSaveInstanceState(savedInstanceState);
75 
76         ConsentNotificationActivity.handleAction(CONFIRMATION_PAGE_DISMISSED, getContext());
77     }
78 
setupListeners()79     private void setupListeners() {
80         Button leftControlButton =
81                 requireActivity().findViewById(R.id.leftControlButtonConfirmation);
82         leftControlButton.setOnClickListener(
83                 view -> {
84                     if (mOptedIn) {
85                         ConsentNotificationActivity.handleAction(
86                                 CONFIRMATION_PAGE_OPT_IN_SETTINGS_CLICKED, getContext());
87                     } else {
88                         ConsentNotificationActivity.handleAction(
89                                 CONFIRMATION_PAGE_OPT_OUT_SETTINGS_CLICKED, getContext());
90                     }
91 
92                     Intent intent =
93                             new Intent(requireActivity(), AdServicesSettingsMainActivity.class);
94                     intent.putExtra(FROM_NOTIFICATION_KEY, true);
95                     startActivity(intent);
96                     requireActivity().finish();
97                 });
98 
99         Button rightControlButton =
100                 requireActivity().findViewById(R.id.rightControlButtonConfirmation);
101         rightControlButton.setOnClickListener(
102                 view -> {
103                     if (mOptedIn) {
104                         ConsentNotificationActivity.handleAction(
105                                 CONFIRMATION_PAGE_OPT_IN_GOT_IT_BUTTON_CLICKED, getContext());
106                     } else {
107                         ConsentNotificationActivity.handleAction(
108                                 CONFIRMATION_PAGE_OPT_OUT_GOT_IT_BUTTON_CLICKED, getContext());
109                     }
110 
111                     // acknowledge and dismiss
112                     requireActivity().finish();
113                 });
114     }
115 }
116