1 /*
2  * Copyright (C) 2021 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.imsserviceentitlement.entitlement;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.util.SparseArray;
22 
23 import java.util.Optional;
24 
25 class EntitlementConfigurationsDataStore {
26     private static final String PREFERENCE_ENTITLEMENT_CHARACTERISTICS =
27             "ENTITLEMENT_CHARACTERISTICS";
28     private static final String XML_DOCUMENT = "XML_DOCUMENT";
29     private static final String ENTITLEMENT_VERSION = "ENTITLEMENT_VERSION";
30     private static final String QUERY_TIME_MILLIS = "QUERY_TIME_MILLIS";
31 
32     private final SharedPreferences mPreferences;
33 
34     private static final SparseArray<EntitlementConfigurationsDataStore> sInstances =
35             new SparseArray<>();
36 
getInstance(Context context, int subId)37     public static EntitlementConfigurationsDataStore getInstance(Context context, int subId) {
38         if (sInstances.get(subId) == null) {
39             sInstances.put(subId, new EntitlementConfigurationsDataStore(context, subId));
40         }
41         return sInstances.get(subId);
42     }
43 
EntitlementConfigurationsDataStore(Context context, int subId)44     private EntitlementConfigurationsDataStore(Context context, int subId) {
45         this.mPreferences = context.getSharedPreferences(
46                 PREFERENCE_ENTITLEMENT_CHARACTERISTICS + "_" + subId,
47                 Context.MODE_PRIVATE);
48     }
49 
set(int entitlementVersion, String characteristics)50     public void set(int entitlementVersion, String characteristics) {
51         mPreferences
52                 .edit()
53                 .putString(XML_DOCUMENT, characteristics)
54                 .putString(ENTITLEMENT_VERSION, String.valueOf(entitlementVersion))
55                 .putLong(QUERY_TIME_MILLIS, System.currentTimeMillis())
56                 .apply();
57     }
58 
set(String characteristics)59     public void set(String characteristics) {
60         mPreferences
61                 .edit()
62                 .putString(XML_DOCUMENT, characteristics)
63                 .putLong(QUERY_TIME_MILLIS, System.currentTimeMillis())
64                 .apply();
65     }
66 
getRawXml()67     public Optional<String> getRawXml() {
68         return Optional.ofNullable(mPreferences.getString(XML_DOCUMENT, null));
69     }
70 
getQueryTimeMillis()71     public long getQueryTimeMillis() {
72         return mPreferences.getLong(QUERY_TIME_MILLIS, 0);
73     }
74 
getEntitlementVersion()75     public Optional<String> getEntitlementVersion() {
76         return Optional.ofNullable(mPreferences.getString(ENTITLEMENT_VERSION, null));
77     }
78 }
79