• 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.VisibleForTesting;
22 
23 /**
24  * Default implementation of {@link com.android.managedprovisioning.common.SharedPreferences}.
25  */
26 public class ManagedProvisioningSharedPreferences implements
27         com.android.managedprovisioning.common.SharedPreferences {
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     private static final String KEY_IS_PROVISIONING_FLOW_DELEGATED_TO_ROLE_HOLDER =
54             "is_provisioning_flow_delegated_to_role_holder";
55     private static final String KEY_IS_ESTABLISH_NETWORK_CONNECTION_RUN =
56             "is_establish_network_connection_run";
57 
58     private final SharedPreferences mSharedPreferences;
59 
ManagedProvisioningSharedPreferences(Context context)60     public ManagedProvisioningSharedPreferences(Context context) {
61         mSharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE);
62     }
63 
64     @VisibleForTesting
getProvisioningId()65     public long getProvisioningId() {
66         return mSharedPreferences.getLong(KEY_PROVISIONING_ID, DEFAULT_PROVISIONING_ID);
67     }
68 
69     /**
70      * Can assume the id is unique across all provisioning sessions
71      * @return a new provisioning id by incrementing the current id
72      */
incrementAndGetProvisioningId()73     public long incrementAndGetProvisioningId() {
74         synchronized (sWriteLock) {
75             long provisioningId = getProvisioningId();
76             provisioningId++;
77             // commit synchronously
78             mSharedPreferences.edit().putLong(KEY_PROVISIONING_ID, provisioningId).commit();
79             return provisioningId;
80         }
81     }
82 
83     /**
84      * @param time the provisioning started timestamp, in milliseconds
85      */
writeProvisioningStartedTimestamp(long time)86     public void writeProvisioningStartedTimestamp(long time) {
87         mSharedPreferences.edit()
88                 .putLong(KEY_PROVISIONING_START_TIMESTAMP, time)
89                 .apply();
90     }
91 
92     /**
93      * @return the provisioning started timestamp, in milliseconds
94      */
getProvisioningStartedTimestamp()95     public long getProvisioningStartedTimestamp() {
96         return mSharedPreferences.getLong(KEY_PROVISIONING_START_TIMESTAMP, 0L);
97     }
98 
99     /**
100      * Writes the navigation bar color
101      */
writeNavigationBarColor(int color)102     public void writeNavigationBarColor(int color) {
103         mSharedPreferences.edit()
104                 .putInt(KEY_NAVIGATION_BAR_BACKGROUND_COLOR, color)
105                 .apply();
106     }
107 
108     /**
109      * Returns the navigation bar color
110      */
getNavigationBarColor()111     public int getNavigationBarColor() {
112         return mSharedPreferences.getInt(KEY_NAVIGATION_BAR_BACKGROUND_COLOR, 0);
113     }
114 
115     /**
116      * Writes the navigation bar divider color
117      */
writeNavigationBarDividerColor(int color)118     public void writeNavigationBarDividerColor(int color) {
119         mSharedPreferences.edit()
120                 .putInt(KEY_NAVIGATION_BAR_DIVIDER_COLOR, color)
121                 .apply();
122     }
123 
124     /**
125      * Returns the navigation bar divider color
126      */
getNavigationBarDividerColor()127     public int getNavigationBarDividerColor() {
128         return mSharedPreferences.getInt(KEY_NAVIGATION_BAR_DIVIDER_COLOR, 0);
129     }
130 
131     /**
132      * Writes the text primary color
133      */
writeTextPrimaryColor(int color)134     public void writeTextPrimaryColor(int color) {
135         mSharedPreferences.edit()
136                 .putInt(KEY_TEXT_PRIMARY_COLOR, color)
137                 .apply();
138     }
139 
140     /**
141      * Returns the text primary color
142      */
getTextPrimaryColor()143     public int getTextPrimaryColor() {
144         return mSharedPreferences.getInt(KEY_TEXT_PRIMARY_COLOR, 0);
145     }
146 
147     /**
148      * Writes the text secondary color
149      */
writeTextSecondaryColor(int color)150     public void writeTextSecondaryColor(int color) {
151         mSharedPreferences.edit()
152                 .putInt(KEY_TEXT_SECONDARY_COLOR, color)
153                 .apply();
154     }
155 
156     /**
157      * Returns the text secondary color
158      */
getTextSecondaryColor()159     public int getTextSecondaryColor() {
160         return mSharedPreferences.getInt(KEY_TEXT_SECONDARY_COLOR, 0);
161     }
162 
163     /**
164      * Writes the theme background color
165      */
writeBackgroundColor(int color)166     public void writeBackgroundColor(int color) {
167         mSharedPreferences.edit()
168                 .putInt(KEY_BACKGROUND_COLOR, color)
169                 .apply();
170     }
171 
172     /**
173      * Returns the theme background color
174      */
getBackgroundColor()175     public int getBackgroundColor() {
176         return mSharedPreferences.getInt(KEY_BACKGROUND_COLOR, 0);
177     }
178 
179     /**
180      * Writes the theme accent color
181      */
writeAccentColor(int color)182     public void writeAccentColor(int color) {
183         mSharedPreferences.edit()
184                 .putInt(KEY_ACCENT_COLOR, color)
185                 .apply();
186     }
187 
188     /**
189      * Returns the theme accent color
190      */
getAccentColor()191     public int getAccentColor() {
192         return mSharedPreferences.getInt(KEY_ACCENT_COLOR, 0);
193     }
194 
195     /**
196      * Writes the notification background color
197      */
writeNotificationBackgroundColor(int color)198     public void writeNotificationBackgroundColor(int color) {
199         mSharedPreferences.edit()
200                 .putInt(KEY_NOTIFICATION_BACKGROUND_COLOR, color)
201                 .apply();
202     }
203 
204     /**
205      * Returns the notification background color
206      */
getNotificationBackgroundColor()207     public int getNotificationBackgroundColor() {
208         return mSharedPreferences.getInt(KEY_NOTIFICATION_BACKGROUND_COLOR, 0);
209     }
210 
211     @Override
setIsProvisioningFlowDelegatedToRoleHolder(boolean value)212     public void setIsProvisioningFlowDelegatedToRoleHolder(boolean value) {
213         mSharedPreferences.edit()
214                 .putBoolean(KEY_IS_PROVISIONING_FLOW_DELEGATED_TO_ROLE_HOLDER, value)
215                 .apply();
216     }
217 
218     @Override
isProvisioningFlowDelegatedToRoleHolder()219     public boolean isProvisioningFlowDelegatedToRoleHolder() {
220         return mSharedPreferences.getBoolean(
221                 KEY_IS_PROVISIONING_FLOW_DELEGATED_TO_ROLE_HOLDER,
222                 /* defaultValue= */ false);
223     }
224 
225     @Override
setIsEstablishNetworkConnectionRun(boolean value)226     public void setIsEstablishNetworkConnectionRun(boolean value) {
227         mSharedPreferences.edit()
228                 .putBoolean(KEY_IS_ESTABLISH_NETWORK_CONNECTION_RUN, value)
229                 .apply();
230     }
231 
232     @Override
isEstablishNetworkConnectionRun()233     public boolean isEstablishNetworkConnectionRun() {
234         return mSharedPreferences.getBoolean(
235                 KEY_IS_ESTABLISH_NETWORK_CONNECTION_RUN,
236                 /* defaultValue= */ false);
237     }
238 }
239