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