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