1 /*
2  * Copyright 2016, 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 package com.android.managedprovisioning.common;
17 
18 import android.content.Context;
19 import android.content.SharedPreferences;
20 import android.util.TimeUtils;
21 
22 import androidx.annotation.VisibleForTesting;
23 
24 public class ManagedProvisioningSharedPreferences {
25     public static final long DEFAULT_PROVISIONING_ID = 0L;
26 
27     @VisibleForTesting
28     static final String KEY_PROVISIONING_ID = "provisioning_id";
29 
30     @VisibleForTesting
31     static final String KEY_PROVISIONING_START_TIMESTAMP = "provisioning_start_timestamp";
32 
33     @VisibleForTesting
34     static final String SHARED_PREFERENCE = "managed_profile_shared_preferences";
35 
36     /**
37      * It's a process-wise in-memory write lock. No other processes will write the same file.
38      */
39     private static final Object sWriteLock = new Object();
40 
41     private final SharedPreferences mSharedPreferences;
42 
ManagedProvisioningSharedPreferences(Context context)43     public ManagedProvisioningSharedPreferences(Context context) {
44         mSharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE);
45     }
46 
47     @VisibleForTesting
getProvisioningId()48     public long getProvisioningId() {
49         return mSharedPreferences.getLong(KEY_PROVISIONING_ID, DEFAULT_PROVISIONING_ID);
50     }
51 
52     /**
53      * Can assume the id is unique across all provisioning sessions
54      * @return a new provisioning id by incrementing the current id
55      */
incrementAndGetProvisioningId()56     public long incrementAndGetProvisioningId() {
57         synchronized (sWriteLock) {
58             long provisioningId = getProvisioningId();
59             provisioningId++;
60             // commit synchronously
61             mSharedPreferences.edit().putLong(KEY_PROVISIONING_ID, provisioningId).commit();
62             return provisioningId;
63         }
64     }
65 
66     /**
67      * @param time the provisioning started timestamp, in milliseconds
68      */
writeProvisioningStartedTimestamp(long time)69     public void writeProvisioningStartedTimestamp(long time) {
70         mSharedPreferences.edit()
71                 .putLong(KEY_PROVISIONING_START_TIMESTAMP, time)
72                 .apply();
73     }
74 
75     /**
76      * @return the provisioning started timestamp, in milliseconds
77      */
getProvisioningStartedTimestamp()78     public long getProvisioningStartedTimestamp() {
79         return mSharedPreferences.getLong(KEY_PROVISIONING_START_TIMESTAMP, 0L);
80     }
81 }
82