summaryrefslogtreecommitdiff
path: root/adservices/apk/java/com/android/adservices/ui/settings/viewmodels/MainViewModel.java
blob: cc1cbccaac2dededca60c3636eb51d55fc8292d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.adservices.ui.settings.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.adservices.service.consent.ConsentManager;
import com.android.adservices.ui.settings.fragments.AdServicesSettingsAppsFragment;
import com.android.adservices.ui.settings.fragments.AdServicesSettingsMainFragment;
import com.android.adservices.ui.settings.fragments.AdServicesSettingsTopicsFragment;
import com.android.settingslib.widget.MainSwitchBar;

/**
 * View model for the main view of the AdServices Settings App. This view model is responsible
 * for serving consent to the main view, and interacting with the {@link ConsentManager} that
 * persists the user consent data in a storage.
 */
public class MainViewModel extends AndroidViewModel {
    private final MutableLiveData<MainViewModelUiEvent> mEventTrigger = new MutableLiveData<>();
    private final MutableLiveData<Boolean> mAdServicesConsent;
    private final ConsentManager mConsentManager;

    /** UI event triggered by view model */
    public enum MainViewModelUiEvent {
        SWITCH_ON_PRIVACY_SANDBOX_BETA,
        SWITCH_OFF_PRIVACY_SANDBOX_BETA,
        DISPLAY_TOPICS_FRAGMENT,
        DISPLAY_APPS_FRAGMENT,
    }

    public MainViewModel(@NonNull Application application) {
        this(application, ConsentManager.getInstance(application));
    }

    @VisibleForTesting
    public MainViewModel(@NonNull Application application, ConsentManager consentManager) {
        super(application);
        mConsentManager = consentManager;
        mAdServicesConsent = new MutableLiveData<>(getConsentFromConsentManager());
    }

    /**
     * Provides {@link AdServicesApiConsent} displayed in {@link AdServicesSettingsMainFragment}
     * as a Switch value.
     *
     * @return {@link mAdServicesConsent} indicates if user has consented to PP API usage.
     */
    public MutableLiveData<Boolean> getConsent() {
        return mAdServicesConsent;
    }

    /**
     * Sets the user consent for PP APIs.
     *
     * @param newConsentValue the new value that user consent should be set to for PP APIs.
     */
    public void setConsent(Boolean newConsentValue) {
        if (newConsentValue) {
            mConsentManager.enable(getApplication());
        } else {
            mConsentManager.disable(getApplication());
        }
        mAdServicesConsent.postValue(getConsentFromConsentManager());
    }

    /** Returns an observable but immutable event enum representing an view action on UI. */
    public LiveData<MainViewModelUiEvent> getUiEvents() {
        return mEventTrigger;
    }

    /**
     * Sets the UI Event as handled so the action will not be handled again if activity is
     * recreated.
     */
    public void uiEventHandled() {
        mEventTrigger.postValue(null);
    }

    /** Triggers {@link AdServicesSettingsTopicsFragment}. */
    public void topicsButtonClickHandler() {
        mEventTrigger.postValue(MainViewModelUiEvent.DISPLAY_TOPICS_FRAGMENT);
    }

    /** Triggers {@link AdServicesSettingsAppsFragment}. */
    public void appsButtonClickHandler() {
        mEventTrigger.postValue(MainViewModelUiEvent.DISPLAY_APPS_FRAGMENT);
    }

    /**
     * Triggers opt out process for Privacy Sandbox. Also reverts the switch state, since
     * confirmation dialog will handle switch change.
     */
    public void consentSwitchClickHandler(MainSwitchBar mainSwitchBar) {
        if (mainSwitchBar.isChecked()) {
            mainSwitchBar.setChecked(false);
            mEventTrigger.postValue(MainViewModelUiEvent.SWITCH_ON_PRIVACY_SANDBOX_BETA);
        } else {
            mainSwitchBar.setChecked(true);
            mEventTrigger.postValue(MainViewModelUiEvent.SWITCH_OFF_PRIVACY_SANDBOX_BETA);
        }
    }

    private boolean getConsentFromConsentManager() {
        return mConsentManager.getConsent().isGiven();
    }
}