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