• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // TODO(b/311183933): Remove passed in Context from static method.
32 @SuppressLint({"NewAdServicesFile", "AvoidStaticContext"})
33 public final class FileCompatUtils {
34     private static final String ADSERVICES_PREFIX = "adservices";
35 
FileCompatUtils()36     private FileCompatUtils() {
37         // prevent instantiation
38     }
39 
40     /**
41      * returns the appropriate filename to use based on Android version, prepending "adservices_"
42      * for S-. The underscore is for human readability. The code for deleting files after OTA will
43      * check only for "adservices" so files that begin with this already do not need to be updated
44      */
getAdservicesFilename(String basename)45     public static String getAdservicesFilename(String basename) {
46         if (SdkLevel.isAtLeastT()
47                 || ADSERVICES_PREFIX.regionMatches(
48                         /* ignoreCase= */ true,
49                         /* toffset= */ 0,
50                         basename,
51                         /* ooffset= */ 0,
52                         /* len= */ ADSERVICES_PREFIX.length())) {
53             return basename;
54         }
55 
56         return ADSERVICES_PREFIX + "_" + basename;
57     }
58 
59     /**
60      * returns a RoomDatabase.Builder instance for the given context, class, and name, while
61      * ensuring the filename is prepended with "adservices" on S-.
62      */
63     @SuppressLint("NewAdServicesFile")
roomDatabaseBuilderHelper( Context context, Class<T> klass, String name)64     public static <T extends RoomDatabase> RoomDatabase.Builder<T> roomDatabaseBuilderHelper(
65             Context context, Class<T> klass, String name) {
66         return Room.databaseBuilder(
67                 context,
68                 klass,
69                 getAdservicesFilename(name) /* make sure filename is valid for S- */);
70     }
71 
72     /**
73      * calls Context.getDataPath to return a File for the given context and name, while ensuring the
74      * filename is prepended with "adservices" on S-.
75      */
getDatabasePathHelper(Context context, String name)76     public static File getDatabasePathHelper(Context context, String name) {
77         return context.getDatabasePath(getAdservicesFilename(name));
78     }
79 
80     /**
81      * creates a new File from the given parent and child, while ensuring the child filename is
82      * prepended with "adservices" on S-.
83      */
newFileHelper(File parent, String child)84     public static File newFileHelper(File parent, String child) {
85         return new File(parent, getAdservicesFilename(child));
86     }
87 
88     /**
89      * returns a Sharedpreferences for the given context, name, and mode, while ensuring the
90      * filename is prepended with "adservices" on S-.
91      */
92     @SuppressWarnings("AvoidSharedPreferences") // Legacy usage
getSharedPreferencesHelper( Context context, String name, int mode)93     public static SharedPreferences getSharedPreferencesHelper(
94             Context context, String name, int mode) {
95         return context.getSharedPreferences(getAdservicesFilename(name), mode);
96     }
97 }
98