1 /* 2 * Copyright (C) 2021 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 17 package com.android.managedprovisioning.preprovisioning.terms; 18 19 import static android.content.pm.PackageManager.GET_META_DATA; 20 import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; 21 22 import static java.util.Objects.requireNonNull; 23 24 import android.app.Application; 25 import android.util.ArraySet; 26 27 import androidx.lifecycle.ViewModel; 28 import androidx.lifecycle.ViewModelProvider; 29 30 import com.android.managedprovisioning.common.StoreUtils; 31 import com.android.managedprovisioning.common.Utils; 32 import com.android.managedprovisioning.model.ProvisioningParams; 33 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.Set; 38 39 /** 40 * A {@link ViewModel} which maintains data related to terms. 41 */ 42 final class TermsViewModel extends ViewModel { 43 private final TermsProvider mTermsProvider; 44 private List<TermsDocument> mTerms; 45 private final Set<Integer> mExpandedGroupsPosition = new ArraySet<>(); 46 private final Map<Integer, Boolean> mTermsListExpandedStates = new HashMap<>(); 47 TermsViewModel(TermsProvider termsProvider)48 TermsViewModel(TermsProvider termsProvider) { 49 mTermsProvider = requireNonNull(termsProvider); 50 } 51 getTerms()52 List<TermsDocument> getTerms() { 53 if (mTerms == null) { 54 mTerms = mTermsProvider.getTerms(); 55 } 56 return mTerms; 57 } 58 getGeneralDisclaimer()59 TermsDocument getGeneralDisclaimer() { 60 return mTermsProvider.getGeneralDisclaimer(); 61 } 62 setTermExpanded(int position, boolean expanded)63 void setTermExpanded(int position, boolean expanded) { 64 validateIndex(position); 65 mTermsListExpandedStates.put(position, expanded); 66 if (expanded) { 67 markTermRead(position); 68 } 69 } 70 isTermExpanded(int position)71 boolean isTermExpanded(int position) { 72 validateIndex(position); 73 return mTermsListExpandedStates.getOrDefault(position, /* defaultValue= */ false); 74 } 75 getNumberOfReadTerms()76 int getNumberOfReadTerms() { 77 return mExpandedGroupsPosition.size(); 78 } 79 markTermRead(int position)80 private void markTermRead(int position) { 81 mExpandedGroupsPosition.add(position); 82 } 83 validateIndex(int position)84 private void validateIndex(int position) { 85 final int termsCount = getTerms().size(); 86 if (position < 0 || position >= termsCount) { 87 throw new IllegalArgumentException("Index " + position + " does not belong to the " 88 + "terms array with count " + termsCount); 89 } 90 } 91 92 static class TermsViewModelFactory implements ViewModelProvider.Factory { 93 private final Application mApplication; 94 private final ProvisioningParams mParams; 95 TermsViewModelFactory(Application application, ProvisioningParams params)96 TermsViewModelFactory(Application application, ProvisioningParams params) { 97 mApplication = requireNonNull(application); 98 mParams = requireNonNull(params); 99 } 100 101 @Override create(Class<T> modelClass)102 public <T extends ViewModel> T create(Class<T> modelClass) { 103 if (!TermsViewModel.class.isAssignableFrom(modelClass)) { 104 throw new IllegalArgumentException("Invalid class for creating a " 105 + "TermsViewModel: " + modelClass); 106 } 107 return (T) new TermsViewModel( 108 new TermsProvider( 109 mApplication.getApplicationContext(), 110 StoreUtils::readString, 111 mParams, 112 new Utils(), 113 () -> mApplication.getApplicationContext().getPackageManager() 114 .getInstalledApplications( 115 MATCH_SYSTEM_ONLY | GET_META_DATA))); 116 } 117 } 118 } 119