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.server.healthconnect.exportimport;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.annotation.NonNull;
22 import android.content.Context;
23 import android.content.ContextWrapper;
24 import android.os.UserHandle;
25 
26 import com.android.server.healthconnect.utils.FilesUtil;
27 
28 import java.io.File;
29 
30 /**
31  * {@link Context} for the staged health connect db.
32  *
33  * @hide
34  */
35 public final class DatabaseContext extends ContextWrapper {
36 
37     private final String mDatabaseDirName;
38 
39     private File mDatabaseDir;
40 
41     @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
DatabaseContext( @onNull Context context, String databaseDirName, UserHandle userHandle)42     private DatabaseContext(
43             @NonNull Context context, String databaseDirName, UserHandle userHandle) {
44         super(context);
45         requireNonNull(context);
46         mDatabaseDirName = databaseDirName;
47         setupForUser(userHandle);
48     }
49 
50     /** Updates the DB directory */
setupForUser(UserHandle userHandle)51     public void setupForUser(UserHandle userHandle) {
52         File hcDirectory = FilesUtil.getDataSystemCeHCDirectoryForUser(userHandle.getIdentifier());
53         mDatabaseDir = new File(hcDirectory, mDatabaseDirName);
54         mDatabaseDir.mkdirs();
55     }
56 
57     /** Returns the directory of the staged database */
getDatabaseDir()58     public File getDatabaseDir() {
59         return mDatabaseDir;
60     }
61 
62     /** Returns the file of the staged database with the given name */
63     @Override
getDatabasePath(String name)64     public File getDatabasePath(String name) {
65         return new File(mDatabaseDir, name);
66     }
67 
68     /** Factory method */
create( @onNull Context context, String databaseDirName, UserHandle userHandle)69     public static DatabaseContext create(
70             @NonNull Context context, String databaseDirName, UserHandle userHandle) {
71         return new DatabaseContext(context, databaseDirName, userHandle);
72     }
73 }
74