blob: a5ff6052743ece36e315e894f9e6015b510dccf8 (
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
|
/*
* 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 BlockedTopicsViewModel extends AndroidViewModel {
private final MutableLiveData<Pair<BlockedTopicsViewModelUiEvent, Topic>> mEventTrigger =
new MutableLiveData<>();
private final MutableLiveData<ImmutableList<Topic>> mBlockedTopics;
private final ConsentManager mConsentManager;
/** UI event triggered by view model */
public enum BlockedTopicsViewModelUiEvent {
RESTORE_TOPIC,
}
public BlockedTopicsViewModel(@NonNull Application application) {
super(application);
mConsentManager = ConsentManager.getInstance(application);
mBlockedTopics = new MutableLiveData<>(getBlockedTopicsFromConsentManager());
}
@VisibleForTesting
public BlockedTopicsViewModel(@NonNull Application application, ConsentManager consentManager) {
super(application);
mConsentManager = consentManager;
mBlockedTopics = new MutableLiveData<>(getBlockedTopicsFromConsentManager());
}
/**
* Provides the blocked topics displayed in {@link AdServicesSettingsBlockedTopicsFragment}.
*
* @return 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();
}
/**
* Reads all the data from {@link ConsentManager}.
*
* <p>TODO(b/238387560): To be moved to private when is fixed.
*/
public void refresh() {
mBlockedTopics.postValue(getBlockedTopicsFromConsentManager());
}
/** Returns an observable but immutable event enum representing a view action on UI. */
public LiveData<Pair<BlockedTopicsViewModelUiEvent, 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 restoreTopicConsentButtonClickHandler(Topic topic) {
mEventTrigger.postValue(new Pair<>(BlockedTopicsViewModelUiEvent.RESTORE_TOPIC, topic));
}
private ImmutableList<Topic> getBlockedTopicsFromConsentManager() {
return mConsentManager.getTopicsWithRevokedConsent();
}
}
|