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 17 package com.android.server.healthconnect.storage.request; 18 19 import static android.health.connect.Constants.DEFAULT_INT; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.health.connect.PageTokenWrapper; 24 import android.health.connect.aidl.ReadRecordsRequestParcel; 25 26 import com.android.server.healthconnect.storage.datatypehelpers.RecordHelper; 27 import com.android.server.healthconnect.storage.utils.RecordHelperProvider; 28 29 import java.util.ArrayList; 30 import java.util.Collections; 31 import java.util.List; 32 import java.util.Map; 33 import java.util.Optional; 34 import java.util.Set; 35 import java.util.UUID; 36 37 /** 38 * Refines a request from what the user sent to a format that makes the most sense for the 39 * TransactionManager. 40 * 41 * <p>Notes, This class refines the queries the records stored in the DB 42 * 43 * @hide 44 */ 45 // TODO(b/308158714): Separate two types of requests: read by id and read by filter. 46 public class ReadTransactionRequest { 47 public static final String TYPE_NOT_PRESENT_PACKAGE_NAME = "package_name"; 48 private final List<ReadTableRequest> mReadTableRequests; 49 @Nullable // page token is null for read by id requests 50 private final PageTokenWrapper mPageToken; 51 private final int mPageSize; 52 ReadTransactionRequest( String callingPackageName, ReadRecordsRequestParcel request, long startDateAccessMillis, boolean enforceSelfRead, Set<String> grantedExtraReadPermissions, boolean isInForeground)53 public ReadTransactionRequest( 54 String callingPackageName, 55 ReadRecordsRequestParcel request, 56 long startDateAccessMillis, 57 boolean enforceSelfRead, 58 Set<String> grantedExtraReadPermissions, 59 boolean isInForeground) { 60 RecordHelper<?> recordHelper = 61 RecordHelperProvider.getRecordHelper(request.getRecordType()); 62 mReadTableRequests = 63 Collections.singletonList( 64 recordHelper.getReadTableRequest( 65 request, 66 callingPackageName, 67 enforceSelfRead, 68 startDateAccessMillis, 69 grantedExtraReadPermissions, 70 isInForeground)); 71 if (request.getRecordIdFiltersParcel() == null) { 72 mPageToken = PageTokenWrapper.from(request.getPageToken(), request.isAscending()); 73 mPageSize = request.getPageSize(); 74 } else { 75 mPageSize = DEFAULT_INT; 76 mPageToken = null; 77 } 78 } 79 ReadTransactionRequest( String packageName, Map<Integer, List<UUID>> recordTypeToUuids, long startDateAccessMillis, Set<String> grantedExtraReadPermissions, boolean isInForeground)80 public ReadTransactionRequest( 81 String packageName, 82 Map<Integer, List<UUID>> recordTypeToUuids, 83 long startDateAccessMillis, 84 Set<String> grantedExtraReadPermissions, 85 boolean isInForeground) { 86 mReadTableRequests = new ArrayList<>(); 87 recordTypeToUuids.forEach( 88 (recordType, uuids) -> 89 mReadTableRequests.add( 90 RecordHelperProvider.getRecordHelper(recordType) 91 .getReadTableRequest( 92 packageName, 93 uuids, 94 startDateAccessMillis, 95 grantedExtraReadPermissions, 96 isInForeground))); 97 mPageSize = DEFAULT_INT; 98 mPageToken = null; 99 } 100 101 @NonNull getReadRequests()102 public List<ReadTableRequest> getReadRequests() { 103 return mReadTableRequests; 104 } 105 106 @Nullable getPageToken()107 public PageTokenWrapper getPageToken() { 108 return mPageToken; 109 } 110 111 /** 112 * Returns optional of page size in the {@link android.health.connect.ReadRecordsRequest} 113 * refined by this {@link ReadTransactionRequest}. 114 * 115 * <p>For {@link android.health.connect.ReadRecordsRequestUsingIds} requests, page size is 116 * {@code Optional.empty}. 117 */ getPageSize()118 public Optional<Integer> getPageSize() { 119 return mPageSize == DEFAULT_INT ? Optional.empty() : Optional.of(mPageSize); 120 } 121 } 122