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 import android.util.Pair; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.RequiresApi; 24 import androidx.lifecycle.AndroidViewModel; 25 import androidx.lifecycle.LiveData; 26 import androidx.lifecycle.MutableLiveData; 27 28 import com.android.adservices.data.topics.Topic; 29 import com.android.adservices.service.consent.ConsentManager; 30 import com.android.adservices.ui.settings.fragments.AdServicesSettingsBlockedTopicsFragment; 31 import com.android.adservices.ui.settings.fragments.AdServicesSettingsTopicsFragment; 32 33 import com.google.common.annotations.VisibleForTesting; 34 import com.google.common.collect.ImmutableList; 35 36 /** 37 * View model for the topics view and blocked topics view of the AdServices Settings App. This view 38 * model is responsible for serving topics to the topics view and blocked topics view, and 39 * interacting with the {@link ConsentManager} that persists and changes the topics data in a 40 * storage. 41 */ 42 // TODO(b/269798827): Enable for R. 43 @RequiresApi(Build.VERSION_CODES.S) 44 public class BlockedTopicsViewModel extends AndroidViewModel { 45 46 private final MutableLiveData<Pair<BlockedTopicsViewModelUiEvent, Topic>> mEventTrigger = 47 new MutableLiveData<>(); 48 private final MutableLiveData<ImmutableList<Topic>> mBlockedTopics; 49 private final ConsentManager mConsentManager; 50 51 /** UI event triggered by view model */ 52 public enum BlockedTopicsViewModelUiEvent { 53 RESTORE_TOPIC, 54 } 55 BlockedTopicsViewModel(@onNull Application application)56 public BlockedTopicsViewModel(@NonNull Application application) { 57 super(application); 58 59 mConsentManager = ConsentManager.getInstance(application); 60 mBlockedTopics = new MutableLiveData<>(getBlockedTopicsFromConsentManager()); 61 } 62 63 @VisibleForTesting BlockedTopicsViewModel(@onNull Application application, ConsentManager consentManager)64 public BlockedTopicsViewModel(@NonNull Application application, ConsentManager consentManager) { 65 super(application); 66 67 mConsentManager = consentManager; 68 mBlockedTopics = new MutableLiveData<>(getBlockedTopicsFromConsentManager()); 69 } 70 71 /** 72 * Provides the blocked topics displayed in {@link AdServicesSettingsBlockedTopicsFragment}. 73 * 74 * @return a list of topics that represents the user's blocked interests. 75 */ getBlockedTopics()76 public LiveData<ImmutableList<Topic>> getBlockedTopics() { 77 return mBlockedTopics; 78 } 79 80 /** 81 * Restore the consent for the specified topic (i.e. unblock the topic). 82 * 83 * @param topic the topic to be restored. 84 */ restoreTopicConsent(Topic topic)85 public void restoreTopicConsent(Topic topic) { 86 mConsentManager.restoreConsentForTopic(topic); 87 refresh(); 88 } 89 90 /** 91 * Reads all the data from {@link ConsentManager}. 92 * 93 * <p>TODO(b/238387560): To be moved to private when is fixed. 94 */ refresh()95 public void refresh() { 96 mBlockedTopics.postValue(getBlockedTopicsFromConsentManager()); 97 } 98 99 /** Returns an observable but immutable event enum representing a view action on UI. */ getUiEvents()100 public LiveData<Pair<BlockedTopicsViewModelUiEvent, Topic>> getUiEvents() { 101 return mEventTrigger; 102 } 103 104 /** 105 * Sets the UI Event as handled so the action will not be handled again if activity is 106 * recreated. 107 */ uiEventHandled()108 public void uiEventHandled() { 109 mEventTrigger.postValue(new Pair<>(null, null)); 110 } 111 112 /** 113 * Triggers the block of the specified topic in the list of topics in {@link 114 * AdServicesSettingsTopicsFragment}. 115 * 116 * @param topic the topic to be blocked. 117 */ restoreTopicConsentButtonClickHandler(Topic topic)118 public void restoreTopicConsentButtonClickHandler(Topic topic) { 119 mEventTrigger.postValue(new Pair<>(BlockedTopicsViewModelUiEvent.RESTORE_TOPIC, topic)); 120 } 121 getBlockedTopicsFromConsentManager()122 private ImmutableList<Topic> getBlockedTopicsFromConsentManager() { 123 return mConsentManager.getTopicsWithRevokedConsent(); 124 } 125 } 126