• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import androidx.annotation.Keep;
22 import androidx.annotation.VisibleForTesting;
23 
24 import java.util.Collections;
25 import java.util.Set;
26 
27 public class ManagedProvisioningSharedPreferences {
28     public static final long DEFAULT_PROVISIONING_ID = 0L;
29 
30     @VisibleForTesting
31     static final String KEY_PROVISIONING_ID = "provisioning_id";
32 
33     @VisibleForTesting
34     static final String KEY_PROVISIONING_START_TIMESTAMP = "provisioning_start_timestamp";
35 
36     private static final String KEY_NAVIGATION_BAR_BACKGROUND_COLOR =
37             "navigation_bar_background_color";
38     private static final String KEY_NAVIGATION_BAR_DIVIDER_COLOR =
39             "navigation_bar_divider_color";
40     private static final String KEY_TEXT_PRIMARY_COLOR = "text_primary_color";
41     private static final String KEY_TEXT_SECONDARY_COLOR = "text_secondary_color";
42     private static final String KEY_BACKGROUND_COLOR = "background_color";
43     private static final String KEY_NOTIFICATION_BACKGROUND_COLOR = "notification_background_color";
44 
45     @VisibleForTesting
46     static final String SHARED_PREFERENCE = "managed_profile_shared_preferences";
47 
48     /**
49      * It's a process-wise in-memory write lock. No other processes will write the same file.
50      */
51     private static final Object sWriteLock = new Object();
52     private static final String KEY_ACCENT_COLOR = "accent_color";
53 
54     private final SharedPreferences mSharedPreferences;
55 
ManagedProvisioningSharedPreferences(Context context)56     public ManagedProvisioningSharedPreferences(Context context) {
57         mSharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE);
58     }
59 
60     @VisibleForTesting
getProvisioningId()61     public long getProvisioningId() {
62         return mSharedPreferences.getLong(KEY_PROVISIONING_ID, DEFAULT_PROVISIONING_ID);
63     }
64 
65     /**
66      * Can assume the id is unique across all provisioning sessions
67      * @return a new provisioning id by incrementing the current id
68      */
incrementAndGetProvisioningId()69     public long incrementAndGetProvisioningId() {
70         synchronized (sWriteLock) {
71             long provisioningId = getProvisioningId();
72             provisioningId++;
73             // commit synchronously
74             mSharedPreferences.edit().putLong(KEY_PROVISIONING_ID, provisioningId).commit();
75             return provisioningId;
76         }
77     }
78 
79     /**
80      * @param time the provisioning started timestamp, in milliseconds
81      */
writeProvisioningStartedTimestamp(long time)82     public void writeProvisioningStartedTimestamp(long time) {
83         mSharedPreferences.edit()
84                 .putLong(KEY_PROVISIONING_START_TIMESTAMP, time)
85                 .apply();
86     }
87 
88     /**
89      * @return the provisioning started timestamp, in milliseconds
90      */
getProvisioningStartedTimestamp()91     public long getProvisioningStartedTimestamp() {
92         return mSharedPreferences.getLong(KEY_PROVISIONING_START_TIMESTAMP, 0L);
93     }
94 
95     /**
96      * Writes the navigation bar color
97      */
writeNavigationBarColor(int color)98     public void writeNavigationBarColor(int color) {
99         mSharedPreferences.edit()
100                 .putInt(KEY_NAVIGATION_BAR_BACKGROUND_COLOR, color)
101                 .apply();
102     }
103 
104     /**
105      * Returns the navigation bar color
106      */
getNavigationBarColor()107     public int getNavigationBarColor() {
108         return mSharedPreferences.getInt(KEY_NAVIGATION_BAR_BACKGROUND_COLOR, 0);
109     }
110 
111     /**
112      * Writes the navigation bar divider color
113      */
writeNavigationBarDividerColor(int color)114     public void writeNavigationBarDividerColor(int color) {
115         mSharedPreferences.edit()
116                 .putInt(KEY_NAVIGATION_BAR_DIVIDER_COLOR, color)
117                 .apply();
118     }
119 
120     /**
121      * Returns the navigation bar divider color
122      */
getNavigationBarDividerColor()123     public int getNavigationBarDividerColor() {
124         return mSharedPreferences.getInt(KEY_NAVIGATION_BAR_DIVIDER_COLOR, 0);
125     }
126 
127     /**
128      * Writes the text primary color
129      */
writeTextPrimaryColor(int color)130     public void writeTextPrimaryColor(int color) {
131         mSharedPreferences.edit()
132                 .putInt(KEY_TEXT_PRIMARY_COLOR, color)
133                 .apply();
134     }
135 
136     /**
137      * Returns the text primary color
138      */
getTextPrimaryColor()139     public int getTextPrimaryColor() {
140         return mSharedPreferences.getInt(KEY_TEXT_PRIMARY_COLOR, 0);
141     }
142 
143     /**
144      * Writes the text secondary color
145      */
writeTextSecondaryColor(int color)146     public void writeTextSecondaryColor(int color) {
147         mSharedPreferences.edit()
148                 .putInt(KEY_TEXT_SECONDARY_COLOR, color)
149                 .apply();
150     }
151 
152     /**
153      * Returns the text secondary color
154      */
getTextSecondaryColor()155     public int getTextSecondaryColor() {
156         return mSharedPreferences.getInt(KEY_TEXT_SECONDARY_COLOR, 0);
157     }
158 
159     /**
160      * Writes the theme background color
161      */
writeBackgroundColor(int color)162     public void writeBackgroundColor(int color) {
163         mSharedPreferences.edit()
164                 .putInt(KEY_BACKGROUND_COLOR, color)
165                 .apply();
166     }
167 
168     /**
169      * Returns the theme background color
170      */
getBackgroundColor()171     public int getBackgroundColor() {
172         return mSharedPreferences.getInt(KEY_BACKGROUND_COLOR, 0);
173     }
174 
175     /**
176      * Writes the theme accent color
177      */
writeAccentColor(int color)178     public void writeAccentColor(int color) {
179         mSharedPreferences.edit()
180                 .putInt(KEY_ACCENT_COLOR, color)
181                 .apply();
182     }
183 
184     /**
185      * Returns the theme accent color
186      */
getAccentColor()187     public int getAccentColor() {
188         return mSharedPreferences.getInt(KEY_ACCENT_COLOR, 0);
189     }
190 
191     /**
192      * Writes the notification background color
193      */
writeNotificationBackgroundColor(int color)194     public void writeNotificationBackgroundColor(int color) {
195         mSharedPreferences.edit()
196                 .putInt(KEY_NOTIFICATION_BACKGROUND_COLOR, color)
197                 .apply();
198     }
199 
200     /**
201      * Returns the notification background color
202      */
getNotificationBackgroundColor()203     public int getNotificationBackgroundColor() {
204         return mSharedPreferences.getInt(KEY_NOTIFICATION_BACKGROUND_COLOR, 0);
205     }
206 }
207