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.permission;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.util.ArrayMap;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 import java.time.Instant;
26 import java.util.Map;
27 
28 /**
29  * State of user health permissions first grant times. Used by {@link FirstGrantTimeDatastore}.
30  *
31  * @hide
32  */
33 public class UserGrantTimeState {
34     /** Special value for {@link #mVersion} to indicate that no version was read. */
35     public static final int NO_VERSION = -1;
36 
37     /** The first grant times by packages. */
38     @NonNull private final Map<String, Instant> mPackagePermissions;
39 
40     /** The first grant time of shared users. */
41     @NonNull private final Map<String, Instant> mSharedUserPermissions;
42 
43     /** The version of the grant times state. */
44     private final int mVersion;
45 
UserGrantTimeState(int version)46     UserGrantTimeState(int version) {
47         this(new ArrayMap<>(), new ArrayMap<>(), version);
48     }
49 
50     @VisibleForTesting
UserGrantTimeState( @onNull Map<String, Instant> packagePermissions, @NonNull Map<String, Instant> sharedUserPermissions, int version)51     public UserGrantTimeState(
52             @NonNull Map<String, Instant> packagePermissions,
53             @NonNull Map<String, Instant> sharedUserPermissions,
54             int version) {
55         mPackagePermissions = packagePermissions;
56         mSharedUserPermissions = sharedUserPermissions;
57         mVersion = version;
58     }
59 
60     @NonNull
getPackageGrantTimes()61     Map<String, Instant> getPackageGrantTimes() {
62         return mPackagePermissions;
63     }
64 
65     @NonNull
getSharedUserGrantTimes()66     Map<String, Instant> getSharedUserGrantTimes() {
67         return mSharedUserPermissions;
68     }
69 
setPackageGrantTime(@onNull String packageName, @Nullable Instant time)70     void setPackageGrantTime(@NonNull String packageName, @Nullable Instant time) {
71         mPackagePermissions.put(packageName, time);
72     }
73 
setSharedUserGrantTime(@onNull String sharedUserId, @Nullable Instant time)74     void setSharedUserGrantTime(@NonNull String sharedUserId, @Nullable Instant time) {
75         mSharedUserPermissions.put(sharedUserId, time);
76     }
77 
containsPackageGrantTime(@onNull String packageName)78     boolean containsPackageGrantTime(@NonNull String packageName) {
79         return mPackagePermissions.containsKey(packageName);
80     }
81 
containsSharedUserGrantTime(@onNull String sharedUserId)82     boolean containsSharedUserGrantTime(@NonNull String sharedUserId) {
83         return mSharedUserPermissions.containsKey(sharedUserId);
84     }
85 
86     /**
87      * Get the version of the grant time.
88      *
89      * @return the version of the grant time
90      */
getVersion()91     int getVersion() {
92         return mVersion;
93     }
94 
95     @Override
toString()96     public String toString() {
97         return "GrantTimeState{version="
98                 + mVersion
99                 + ",packagePermissions="
100                 + mPackagePermissions.toString()
101                 + ",sharedUserPermissions="
102                 + mSharedUserPermissions.toString()
103                 + "}";
104     }
105 }
106