1 /*
2  * Copyright (C) 2023 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.adservices.service.common.compat;
18 
19 import android.annotation.SuppressLint;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 
23 import androidx.room.Room;
24 import androidx.room.RoomDatabase;
25 
26 import com.android.modules.utils.build.SdkLevel;
27 
28 import java.io.File;
29 
30 /** Utility class for handling file names in a backward-compatible manner */
31 @SuppressLint("NewAdServicesFile")
32 public final class FileCompatUtils {
33     private static final String ADSERVICES_PREFIX = "adservices";
34 
FileCompatUtils()35     private FileCompatUtils() {
36         // prevent instantiation
37     }
38 
39     /**
40      * returns the appropriate filename to use based on Android version, prepending "adservices_"
41      * for S-. The underscore is for human readability. The code for deleting files after OTA will
42      * check only for "adservices" so files that begin with this already do not need to be updated
43      */
getAdservicesFilename(String basename)44     public static String getAdservicesFilename(String basename) {
45         if (SdkLevel.isAtLeastT()
46                 || ADSERVICES_PREFIX.regionMatches(
47                         /* ignoreCase= */ true,
48                         /* toffset= */ 0,
49                         basename,
50                         /* ooffset= */ 0,
51                         /* len= */ ADSERVICES_PREFIX.length())) {
52             return basename;
53         }
54 
55         return ADSERVICES_PREFIX + "_" + basename;
56     }
57 
58     /**
59      * returns a RoomDatabase.Builder instance for the given context, class, and name, while
60      * ensuring the filename is prepended with "adservices" on S-.
61      */
62     @SuppressLint("NewAdServicesFile")
roomDatabaseBuilderHelper( Context context, Class<T> klass, String name)63     public static <T extends RoomDatabase> RoomDatabase.Builder<T> roomDatabaseBuilderHelper(
64             Context context, Class<T> klass, String name) {
65         return Room.databaseBuilder(
66                 context,
67                 klass,
68                 getAdservicesFilename(name) /* make sure filename is valid for S- */);
69     }
70 
71     /**
72      * calls Context.getDataPath to return a File for the given context and name, while ensuring the
73      * filename is prepended with "adservices" on S-.
74      */
getDatabasePathHelper(Context context, String name)75     public static File getDatabasePathHelper(Context context, String name) {
76         return context.getDatabasePath(getAdservicesFilename(name));
77     }
78 
79     /**
80      * creates a new File from the given parent and child, while ensuring the child filename is
81      * prepended with "adservices" on S-.
82      */
newFileHelper(File parent, String child)83     public static File newFileHelper(File parent, String child) {
84         return new File(parent, getAdservicesFilename(child));
85     }
86 
87     /**
88      * returns a Sharedpreferences for the given context, name, and mode, while ensuring the
89      * filename is prepended with "adservices" on S-.
90      */
getSharedPreferencesHelper( Context context, String name, int mode)91     public static SharedPreferences getSharedPreferencesHelper(
92             Context context, String name, int mode) {
93         return context.getSharedPreferences(getAdservicesFilename(name), mode);
94     }
95 }
96