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 QUERY_TIME_MILLIS = "QUERY_TIME_MILLIS"; 30 31 private final SharedPreferences mPreferences; 32 33 private static final SparseArray<EntitlementConfigurationsDataStore> sInstances = 34 new SparseArray<>(); 35 getInstance(Context context, int subId)36 public static EntitlementConfigurationsDataStore getInstance(Context context, int subId) { 37 if (sInstances.get(subId) == null) { 38 sInstances.put(subId, new EntitlementConfigurationsDataStore(context, subId)); 39 } 40 return sInstances.get(subId); 41 } 42 EntitlementConfigurationsDataStore(Context context, int subId)43 private EntitlementConfigurationsDataStore(Context context, int subId) { 44 this.mPreferences = context.getSharedPreferences( 45 PREFERENCE_ENTITLEMENT_CHARACTERISTICS + "_" + subId, 46 Context.MODE_PRIVATE); 47 } 48 set(String characteristics)49 public void set(String characteristics) { 50 mPreferences 51 .edit() 52 .putString(XML_DOCUMENT, characteristics) 53 .putLong(QUERY_TIME_MILLIS, System.currentTimeMillis()) 54 .apply(); 55 } 56 get()57 public Optional<String> get() { 58 return Optional.ofNullable(mPreferences.getString(XML_DOCUMENT, null)); 59 } 60 getQueryTimeMillis()61 public long getQueryTimeMillis() { 62 return mPreferences.getLong(QUERY_TIME_MILLIS, 0); 63 } 64 } 65