1 /* 2 * Copyright (C) 2024 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.ondevicepersonalization.services.data; 18 19 import android.content.Context; 20 21 import com.android.odp.module.common.data.OdpSQLiteOpenHelper; 22 import com.android.odp.module.common.encryption.OdpEncryptionKeyManager; 23 import com.android.ondevicepersonalization.services.Flags; 24 import com.android.ondevicepersonalization.services.FlagsFactory; 25 import com.android.ondevicepersonalization.services.OnDevicePersonalizationExecutors; 26 27 import com.google.common.util.concurrent.ListeningExecutorService; 28 29 /** 30 * Utility class that configures and provides appropriate {@link OdpEncryptionKeyManager} instance 31 * for use by ODP code. 32 */ 33 public class EncryptionUtils { 34 /** 35 * Flag based implementation of {@link 36 * com.android.odp.module.common.encryption.OdpEncryptionKeyManager.KeyManagerConfig}. 37 */ 38 public static class FlagKeyManagerConfig implements OdpEncryptionKeyManager.KeyManagerConfig { 39 40 private final Flags mFlags; 41 private final OnDevicePersonalizationDbHelper mDbHelper; 42 FlagKeyManagerConfig(Flags flags, OnDevicePersonalizationDbHelper dbHelper)43 FlagKeyManagerConfig(Flags flags, OnDevicePersonalizationDbHelper dbHelper) { 44 mFlags = flags; 45 this.mDbHelper = dbHelper; 46 } 47 48 @Override getEncryptionKeyFetchUrl()49 public String getEncryptionKeyFetchUrl() { 50 return mFlags.getEncryptionKeyFetchUrl(); 51 } 52 53 @Override getHttpRequestRetryLimit()54 public int getHttpRequestRetryLimit() { 55 return mFlags.getAggregatedErrorReportingHttpRetryLimit(); 56 } 57 58 @Override getEncryptionKeyMaxAgeSeconds()59 public long getEncryptionKeyMaxAgeSeconds() { 60 return mFlags.getEncryptionKeyMaxAgeSeconds(); 61 } 62 63 @Override getSQLiteOpenHelper()64 public OdpSQLiteOpenHelper getSQLiteOpenHelper() { 65 return mDbHelper; 66 } 67 68 @Override getBackgroundExecutor()69 public ListeningExecutorService getBackgroundExecutor() { 70 return OnDevicePersonalizationExecutors.getBackgroundExecutor(); 71 } 72 73 @Override getBlockingExecutor()74 public ListeningExecutorService getBlockingExecutor() { 75 return OnDevicePersonalizationExecutors.getBlockingExecutor(); 76 } 77 } 78 EncryptionUtils()79 private EncryptionUtils() {} 80 81 /** 82 * Returns an instance of the {@link OdpEncryptionKeyManager}. 83 * 84 * <p>Creates a {@link FlagKeyManagerConfig} for use by the {@link OdpEncryptionKeyManager} that 85 * reflects current relevant flag values. 86 * 87 * @param context calling context. 88 */ getEncryptionKeyManager(Context context)89 public static OdpEncryptionKeyManager getEncryptionKeyManager(Context context) { 90 return OdpEncryptionKeyManager.getInstance( 91 context, 92 new FlagKeyManagerConfig( 93 FlagsFactory.getFlags(), 94 OnDevicePersonalizationDbHelper.getInstance(context))); 95 } 96 } 97