1 /*
2  * Copyright (C) 2023 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 android.health.connect;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.health.connect.datatypes.Record;
22 
23 import java.util.Objects;
24 
25 /** A class to represent filtering based on record ID or client ID. */
26 public class RecordIdFilter {
27     private final Class<? extends Record> mRecordType;
28     private final String mId;
29     private final String mClientRecordId;
30 
31     /**
32      * Creates an instance of Record id filter based on client record id.
33      *
34      * <p>Note: this only works when reading own records, using client record ids to read
35      * records inserted by another app will return no result.
36      *
37      * @param recordType Record class for which the client record id must be set.
38      * @param clientRecordId Client identifier that was set while inserting the record.
39      * @return Object of {@link RecordIdFilter}
40      */
41     @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
42     @NonNull
fromClientRecordId( @onNull Class<? extends Record> recordType, @NonNull String clientRecordId)43     public static RecordIdFilter fromClientRecordId(
44             @NonNull Class<? extends Record> recordType, @NonNull String clientRecordId) {
45         Objects.requireNonNull(recordType);
46         Objects.requireNonNull(clientRecordId);
47         return new RecordIdFilter(recordType, null, clientRecordId);
48     }
49 
50     /**
51      * Creates an instance of Record id filter based on record id.
52      *
53      * @param recordType Record class for which the id must be set.
54      * @param id Identifier generated by the platform and returned by {@link
55      *     HealthConnectManager#insertRecords}
56      * @return Object of {@link RecordIdFilter}
57      */
58     @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
59     @NonNull
fromId( @onNull Class<? extends Record> recordType, @NonNull String id)60     public static RecordIdFilter fromId(
61             @NonNull Class<? extends Record> recordType, @NonNull String id) {
62         Objects.requireNonNull(recordType);
63         Objects.requireNonNull(id);
64         return new RecordIdFilter(recordType, id, null);
65     }
66 
RecordIdFilter(Class<? extends Record> recordType, String id, String clientRecordId)67     private RecordIdFilter(Class<? extends Record> recordType, String id, String clientRecordId) {
68         mRecordType = recordType;
69         mId = id;
70         mClientRecordId = clientRecordId;
71     }
72 
73     /**
74      * @return Record class for this identifier
75      */
76     @NonNull
getRecordType()77     public Class<? extends Record> getRecordType() {
78         return mRecordType;
79     }
80 
81     /**
82      * @return Identifier given by the platform
83      */
84     @Nullable
getId()85     public String getId() {
86         return mId;
87     }
88 
89     /**
90      * @return Client record identifier
91      */
92     @Nullable
getClientRecordId()93     public String getClientRecordId() {
94         return mClientRecordId;
95     }
96 }
97