1 /* 2 * Copyright (C) 2020 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 android.content; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.annotation.TestApi; 22 import android.os.Environment; 23 import android.os.UserHandle; 24 25 import java.io.File; 26 import java.util.Objects; 27 28 /** 29 * Provides information about the environment for a particular APEX. 30 * 31 * @hide 32 */ 33 @SystemApi 34 @TestApi 35 public class ApexEnvironment { 36 37 private static final String APEX_DATA = "apexdata"; 38 39 /** 40 * Returns an ApexEnvironment instance for the APEX with the provided {@code apexModuleName}. 41 * 42 * <p>To preserve the safety and integrity of APEX modules, you must only obtain the 43 * ApexEnvironment for your specific APEX, and you <em>must never</em> attempt to obtain an 44 * ApexEnvironment for another APEX. Any coordination between APEXs must be performed through 45 * well-defined interfaces; attempting to directly read or write raw files belonging to another 46 * APEX will violate the hermetic storage requirements placed upon each module. 47 */ 48 @NonNull getApexEnvironment(@onNull String apexModuleName)49 public static ApexEnvironment getApexEnvironment(@NonNull String apexModuleName) { 50 Objects.requireNonNull(apexModuleName, "apexModuleName cannot be null"); 51 //TODO(b/141148175): Check that apexModuleName is an actual APEX name 52 return new ApexEnvironment(apexModuleName); 53 } 54 55 private final String mApexModuleName; 56 ApexEnvironment(String apexModuleName)57 private ApexEnvironment(String apexModuleName) { 58 mApexModuleName = apexModuleName; 59 } 60 61 /** 62 * Returns the data directory for the APEX in device-encrypted, non-user-specific storage. 63 * 64 * <p>This directory is automatically created by the system for installed APEXes, and its 65 * contents will be rolled back if the APEX is rolled back. 66 */ 67 @NonNull getDeviceProtectedDataDir()68 public File getDeviceProtectedDataDir() { 69 return Environment.buildPath( 70 Environment.getDataMiscDirectory(), APEX_DATA, mApexModuleName); 71 } 72 73 /** 74 * Returns the data directory for the APEX in device-encrypted, user-specific storage for the 75 * specified {@code user}. 76 * 77 * <p>This directory is automatically created by the system for each user and for each installed 78 * APEX, and its contents will be rolled back if the APEX is rolled back. 79 */ 80 @NonNull getDeviceProtectedDataDirForUser(@onNull UserHandle user)81 public File getDeviceProtectedDataDirForUser(@NonNull UserHandle user) { 82 return Environment.buildPath( 83 Environment.getDataMiscDeDirectory(user.getIdentifier()), APEX_DATA, 84 mApexModuleName); 85 } 86 87 /** 88 * Returns the data directory for the APEX in credential-encrypted, user-specific storage for 89 * the specified {@code user}. 90 * 91 * <p>This directory is automatically created by the system for each user and for each installed 92 * APEX, and its contents will be rolled back if the APEX is rolled back. 93 */ 94 @NonNull getCredentialProtectedDataDirForUser(@onNull UserHandle user)95 public File getCredentialProtectedDataDirForUser(@NonNull UserHandle user) { 96 return Environment.buildPath( 97 Environment.getDataMiscCeDirectory(user.getIdentifier()), APEX_DATA, 98 mApexModuleName); 99 } 100 } 101