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.settings.viewmodels;
17 
18 import android.app.Application;
19 import android.os.Build;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.RequiresApi;
23 import androidx.annotation.VisibleForTesting;
24 import androidx.lifecycle.AndroidViewModel;
25 import androidx.lifecycle.LiveData;
26 import androidx.lifecycle.MutableLiveData;
27 
28 import com.android.adservices.service.FlagsFactory;
29 import com.android.adservices.service.consent.AdServicesApiConsent;
30 import com.android.adservices.service.consent.AdServicesApiType;
31 import com.android.adservices.service.consent.ConsentManager;
32 import com.android.adservices.ui.settings.activities.AdServicesSettingsMainActivity;
33 import com.android.adservices.ui.settings.fragments.AdServicesSettingsAppsFragment;
34 import com.android.adservices.ui.settings.fragments.AdServicesSettingsMeasurementFragment;
35 import com.android.adservices.ui.settings.fragments.AdServicesSettingsTopicsFragment;
36 
37 /**
38  * View model for the main view of the AdServices Settings App. This view model is responsible for
39  * serving consent to the main view, and interacting with the {@link ConsentManager} that persists
40  * the user consent data in a storage.
41  */
42 // TODO(b/269798827): Enable for R.
43 @RequiresApi(Build.VERSION_CODES.S)
44 public class MainViewModel extends AndroidViewModel {
45     private final MutableLiveData<MainViewModelUiEvent> mEventTrigger = new MutableLiveData<>();
46     private final MutableLiveData<Boolean> mAdServicesConsent;
47     private final ConsentManager mConsentManager;
48 
49     /** UI event triggered by view model */
50     public enum MainViewModelUiEvent {
51         DISPLAY_TOPICS_FRAGMENT,
52         DISPLAY_APPS_FRAGMENT,
53         DISPLAY_MEASUREMENT_FRAGMENT,
54     }
55 
MainViewModel(@onNull Application application)56     public MainViewModel(@NonNull Application application) {
57         this(application, ConsentManager.getInstance());
58     }
59 
60     @VisibleForTesting
MainViewModel(@onNull Application application, ConsentManager consentManager)61     public MainViewModel(@NonNull Application application, ConsentManager consentManager) {
62         super(application);
63         mConsentManager = consentManager;
64         mAdServicesConsent = new MutableLiveData<>(getConsentFromConsentManager());
65     }
66 
67     /**
68      * Provides {@link AdServicesApiConsent} displayed in {@link AdServicesSettingsMainActivity} as
69      * a Switch value.
70      *
71      * @return {@link #mAdServicesConsent} indicates if user has consented to PP API usage.
72      */
getConsent()73     public MutableLiveData<Boolean> getConsent() {
74         return mAdServicesConsent;
75     }
76 
77     /**
78      * Sets the user consent for PP APIs.
79      *
80      * @param newConsentValue the new value that user consent should be set to for PP APIs.
81      */
setConsent(Boolean newConsentValue)82     public void setConsent(Boolean newConsentValue) {
83         if (newConsentValue) {
84             mConsentManager.enable(getApplication());
85         } else {
86             mConsentManager.disable(getApplication());
87         }
88         mAdServicesConsent.postValue(getConsentFromConsentManager());
89         if (FlagsFactory.getFlags().getRecordManualInteractionEnabled()) {
90             ConsentManager.getInstance()
91                     .recordUserManualInteractionWithConsent(
92                             ConsentManager.MANUAL_INTERACTIONS_RECORDED);
93         }
94     }
95 
96     /** Returns an observable but immutable event enum representing an view action on UI. */
getUiEvents()97     public LiveData<MainViewModelUiEvent> getUiEvents() {
98         return mEventTrigger;
99     }
100 
101     /**
102      * Sets the UI Event as handled so the action will not be handled again if activity is
103      * recreated.
104      */
uiEventHandled()105     public void uiEventHandled() {
106         mEventTrigger.postValue(null);
107     }
108 
109     /** Triggers {@link AdServicesSettingsTopicsFragment}. */
topicsButtonClickHandler()110     public void topicsButtonClickHandler() {
111         mEventTrigger.postValue(MainViewModelUiEvent.DISPLAY_TOPICS_FRAGMENT);
112     }
113 
114     /** Triggers {@link AdServicesSettingsAppsFragment}. */
appsButtonClickHandler()115     public void appsButtonClickHandler() {
116         mEventTrigger.postValue(MainViewModelUiEvent.DISPLAY_APPS_FRAGMENT);
117     }
118 
119     /** Triggers {@link AdServicesSettingsMeasurementFragment} */
measurementClickHandler()120     public void measurementClickHandler() {
121         mEventTrigger.postValue(MainViewModelUiEvent.DISPLAY_MEASUREMENT_FRAGMENT);
122     }
123 
getConsentFromConsentManager()124     private boolean getConsentFromConsentManager() {
125         return mConsentManager.getConsent().isGiven();
126     }
127 
getMeasurementConsentFromConsentManager()128     public boolean getMeasurementConsentFromConsentManager() {
129         return mConsentManager.getConsent(AdServicesApiType.MEASUREMENTS).isGiven();
130     }
131 
getTopicsConsentFromConsentManager()132     public boolean getTopicsConsentFromConsentManager() {
133         return mConsentManager.getConsent(AdServicesApiType.TOPICS).isGiven();
134     }
135 
getAppsConsentFromConsentManager()136     public boolean getAppsConsentFromConsentManager() {
137         return mConsentManager.getConsent(AdServicesApiType.FLEDGE).isGiven();
138     }
139 
getCountOfTopics()140     public int getCountOfTopics() {
141         return mConsentManager.getKnownTopicsWithConsent().size();
142     }
143 
getCountOfApps()144     public int getCountOfApps() {
145         return mConsentManager.getKnownAppsWithConsent().size();
146     }
147 }
148