• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.healthconnect.storage.utils;
18 
19 import android.annotation.Nullable;
20 import android.health.connect.Constants;
21 import android.util.Log;
22 
23 import com.android.server.healthconnect.storage.datatypehelpers.PreferenceHelper;
24 
25 import java.time.Instant;
26 
27 /**
28  * Manages entries in {@link PreferenceHelper} which do not belong to a specific class.
29  *
30  * @hide
31  */
32 public class PreferencesManager {
33     private static final String TAG = "HCPreferencesManager";
34 
35     private static final String AUTO_DELETE_DURATION_RECORDS_KEY =
36             "auto_delete_duration_records_key";
37 
38     /**
39      * Key to store timestamp of the last time any PHR <b>read medical resources</b> API is called.
40      */
41     private static final String PREFS_KEY_PHR_LAST_READ_MEDICAL_RESOURCES_API =
42             "phr_last_read_medical_resources_api";
43 
44     private final PreferenceHelper mPreferenceHelper;
45 
PreferencesManager(PreferenceHelper preferenceHelper)46     public PreferencesManager(PreferenceHelper preferenceHelper) {
47         mPreferenceHelper = preferenceHelper;
48     }
49 
50     /** Gets auto delete period for automatically deleting record entries */
getRecordRetentionPeriodInDays()51     public int getRecordRetentionPeriodInDays() {
52         String result = mPreferenceHelper.getPreference(AUTO_DELETE_DURATION_RECORDS_KEY);
53 
54         if (result == null) return 0;
55         return Integer.parseInt(result);
56     }
57 
58     /** Sets auto delete period for automatically deleting record entries */
setRecordRetentionPeriodInDays(int days)59     public void setRecordRetentionPeriodInDays(int days) {
60         mPreferenceHelper.insertOrReplacePreference(
61                 AUTO_DELETE_DURATION_RECORDS_KEY, String.valueOf(days));
62     }
63 
64     /** Sets timestamp of the last time any PHR <b>read medical resources</b> API is called. */
setLastPhrReadMedicalResourcesApiTimeStamp(Instant instant)65     public void setLastPhrReadMedicalResourcesApiTimeStamp(Instant instant) {
66         mPreferenceHelper.insertOrReplacePreference(
67                 PREFS_KEY_PHR_LAST_READ_MEDICAL_RESOURCES_API,
68                 String.valueOf(instant.toEpochMilli()));
69     }
70 
71     /**
72      * Returns timestamp of the last time any PHR <b>read medical resources</b> API is called,
73      * <code>null</code> if it's not stored or the stored value is unfetchable for some reason.
74      */
75     @Nullable
getPhrLastReadMedicalResourcesApiTimeStamp()76     public Instant getPhrLastReadMedicalResourcesApiTimeStamp() {
77         String epochMilliString =
78                 mPreferenceHelper.getPreference(PREFS_KEY_PHR_LAST_READ_MEDICAL_RESOURCES_API);
79         if (epochMilliString == null) {
80             return null;
81         }
82         try {
83             long epochMilli = Long.parseLong(epochMilliString);
84             return Instant.ofEpochMilli(epochMilli);
85         } catch (Exception exception) {
86             if (Constants.DEBUG) {
87                 Log.e(
88                         TAG,
89                         "Stored epoch milli for \""
90                                 + PREFS_KEY_PHR_LAST_READ_MEDICAL_RESOURCES_API
91                                 + "\" is corrupted, it cannot be converted to an Instant. Its"
92                                 + "string value is: "
93                                 + epochMilliString,
94                         exception);
95             }
96             return null;
97         }
98     }
99 }
100