summaryrefslogtreecommitdiff
path: root/adservices/apk/java/com/android/adservices/ui/settings/viewmodels/TopicsViewModel.java
blob: fb6f50a85ed4f31d5716191a1f5c9685a8bbfb22 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * 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 android.util.Pair;

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

import com.android.adservices.data.topics.Topic;
import com.android.adservices.service.consent.ConsentManager;
import com.android.adservices.ui.settings.fragments.AdServicesSettingsBlockedTopicsFragment;
import com.android.adservices.ui.settings.fragments.AdServicesSettingsTopicsFragment;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;

/**
 * View model for the topics view and blocked topics view of the AdServices Settings App. This view
 * model is responsible for serving topics to the topics view and blocked topics view, and
 * interacting with the {@link ConsentManager} that persists and changes the topics data in a
 * storage.
 */
public class TopicsViewModel extends AndroidViewModel {

    private final MutableLiveData<Pair<TopicsViewModelUiEvent, Topic>> mEventTrigger =
            new MutableLiveData<>();
    private final MutableLiveData<ImmutableList<Topic>> mTopics;
    private final MutableLiveData<ImmutableList<Topic>> mBlockedTopics;
    private final ConsentManager mConsentManager;

    /** UI event triggered by view model */
    public enum TopicsViewModelUiEvent {
        BLOCK_TOPIC,
        RESTORE_TOPIC,
        RESET_TOPICS,
        DISPLAY_BLOCKED_TOPICS_FRAGMENT,
    }

    public TopicsViewModel(@NonNull Application application) {
        super(application);

        mConsentManager = ConsentManager.getInstance(application);
        mTopics = new MutableLiveData<>(getTopicsFromConsentManager());
        mBlockedTopics = new MutableLiveData<>(getBlockedTopicsFromConsentManager());
    }

    @VisibleForTesting
    public TopicsViewModel(@NonNull Application application, ConsentManager consentManager) {
        super(application);

        mConsentManager = consentManager;
        mTopics = new MutableLiveData<>(getTopicsFromConsentManager());
        mBlockedTopics = new MutableLiveData<>(getBlockedTopicsFromConsentManager());
    }

    // ---------------------------------------------------------------------------------------------
    // Topics View
    // ---------------------------------------------------------------------------------------------

    /**
     * Provides the topics displayed in {@link AdServicesSettingsTopicsFragment}.
     *
     * @return {@link mTopics} a list of topics that represents the user's interests.
     */
    public LiveData<ImmutableList<Topic>> getTopics() {
        return mTopics;
    }

    /**
     * Revoke the consent for the specified topic (i.e. block the topic).
     *
     * @param topic the topic to be blocked.
     */
    public void revokeTopicConsent(Topic topic) {
        mConsentManager.revokeConsentForTopic(topic);
        refresh();
    }

    /**
     * Reads all the data from {@link ConsentManager}.
     *
     * <p>TODO(b/238387560): To be moved to private when is fixed.
     */
    public void refresh() {
        mTopics.postValue(getTopicsFromConsentManager());
        mBlockedTopics.postValue(getBlockedTopicsFromConsentManager());
    }

    /** Reset all information related to topics but blocked topics. */
    public void resetTopics() {
        mConsentManager.resetTopics();
        mTopics.postValue(getTopicsFromConsentManager());
    }

    // ---------------------------------------------------------------------------------------------
    // Blocked Topics View
    // ---------------------------------------------------------------------------------------------

    /**
     * Provides the blocked topics displayed in {@link AdServicesSettingsBlockedTopicsFragment}.
     *
     * @return {@link mBlockedTopics} a list of topics that represents the user's blocked interests.
     */
    public LiveData<ImmutableList<Topic>> getBlockedTopics() {
        return mBlockedTopics;
    }

    /**
     * Restore the consent for the specified topic (i.e. unblock the topic).
     *
     * @param topic the topic to be restored.
     */
    public void restoreTopicConsent(Topic topic) {
        mConsentManager.restoreConsentForTopic(topic);
        refresh();
    }

    // ---------------------------------------------------------------------------------------------
    // Action Handlers
    // ---------------------------------------------------------------------------------------------

    /** Returns an observable but immutable event enum representing an view action on UI. */
    public LiveData<Pair<TopicsViewModelUiEvent, Topic>> 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(new Pair<>(null, null));
    }

    /**
     * Triggers the block of the specified topic in the list of topics in {@link
     * AdServicesSettingsTopicsFragment}.
     *
     * @param topic the topic to be blocked.
     */
    public void revokeTopicConsentButtonClickHandler(Topic topic) {
        mEventTrigger.postValue(new Pair<>(TopicsViewModelUiEvent.BLOCK_TOPIC, topic));
    }

    /**
     * Triggers the block of the specified topic in the list of topics in {@link
     * AdServicesSettingsTopicsFragment}.
     *
     * @param topic the topic to be blocked.
     */
    public void restoreTopicConsentButtonClickHandler(Topic topic) {
        mEventTrigger.postValue(new Pair<>(TopicsViewModelUiEvent.RESTORE_TOPIC, topic));
    }

    /** Triggers a reset of all topics related data. */
    public void resetTopicsButtonClickHandler() {
        mEventTrigger.postValue(new Pair<>(TopicsViewModelUiEvent.RESET_TOPICS, null));
    }

    /** Triggers {@link AdServicesSettingsTopicsFragment}. */
    public void blockedTopicsFragmentButtonClickHandler() {
        mEventTrigger.postValue(
                new Pair<>(TopicsViewModelUiEvent.DISPLAY_BLOCKED_TOPICS_FRAGMENT, null));
    }

    // ---------------------------------------------------------------------------------------------
    // Private Methods
    // ---------------------------------------------------------------------------------------------

    private ImmutableList<Topic> getTopicsFromConsentManager() {
        return mConsentManager.getKnownTopicsWithConsent();
    }

    private ImmutableList<Topic> getBlockedTopicsFromConsentManager() {
        return mConsentManager.getTopicsWithRevokedConsent();
    }
}