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 java.time.Instant; 24 import java.util.Map; 25 26 /** 27 * State of user health permissions first grant times. Used by {@link FirstGrantTimeDatastore}. 28 * 29 * @hide 30 */ 31 public class UserGrantTimeState { 32 /** Special value for {@link #mVersion} to indicate that no version was read. */ 33 public static final int NO_VERSION = -1; 34 35 /** The first grant times by packages. */ 36 @NonNull private final Map<String, Instant> mPackagePermissions; 37 38 /** The first grant time of shared users. */ 39 @NonNull private final Map<String, Instant> mSharedUserPermissions; 40 41 /** The version of the grant times state. */ 42 private final int mVersion; 43 UserGrantTimeState(@onNull int version)44 UserGrantTimeState(@NonNull int version) { 45 this(new ArrayMap<>(), new ArrayMap<>(), version); 46 } 47 UserGrantTimeState( @onNull Map<String, Instant> packagePermissions, @NonNull Map<String, Instant> sharedUserPermissions, @NonNull int version)48 UserGrantTimeState( 49 @NonNull Map<String, Instant> packagePermissions, 50 @NonNull Map<String, Instant> sharedUserPermissions, 51 @NonNull int version) { 52 mPackagePermissions = packagePermissions; 53 mSharedUserPermissions = sharedUserPermissions; 54 mVersion = version; 55 } 56 57 @NonNull getPackageGrantTimes()58 Map<String, Instant> getPackageGrantTimes() { 59 return mPackagePermissions; 60 } 61 62 @NonNull getSharedUserGrantTimes()63 Map<String, Instant> getSharedUserGrantTimes() { 64 return mSharedUserPermissions; 65 } 66 setPackageGrantTime(@onNull String packageName, @Nullable Instant time)67 void setPackageGrantTime(@NonNull String packageName, @Nullable Instant time) { 68 mPackagePermissions.put(packageName, time); 69 } 70 setSharedUserGrantTime(@onNull String sharedUserId, @Nullable Instant time)71 void setSharedUserGrantTime(@NonNull String sharedUserId, @Nullable Instant time) { 72 mSharedUserPermissions.put(sharedUserId, time); 73 } 74 containsPackageGrantTime(@onNull String packageName)75 boolean containsPackageGrantTime(@NonNull String packageName) { 76 return mPackagePermissions.containsKey(packageName); 77 } 78 containsSharedUserGrantTime(@onNull String sharedUserId)79 boolean containsSharedUserGrantTime(@NonNull 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