1/*
2 * Copyright (C) 2017 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
17package com.android.server.locksettings;
18
19import android.content.Context;
20
21import com.android.server.PersistentDataBlockManagerInternal;
22
23import java.io.File;
24
25public class LockSettingsStorageTestable extends LockSettingsStorage {
26
27    public File mStorageDir;
28    public PersistentDataBlockManagerInternal mPersistentDataBlock;
29
30    public LockSettingsStorageTestable(Context context, File storageDir) {
31        super(context);
32        mStorageDir = storageDir;
33    }
34
35    @Override
36    String getLockPatternFilename(int userId) {
37        return makeDirs(mStorageDir,
38                super.getLockPatternFilename(userId)).getAbsolutePath();
39    }
40
41    @Override
42    String getLockPasswordFilename(int userId) {
43        return makeDirs(mStorageDir,
44                super.getLockPasswordFilename(userId)).getAbsolutePath();
45    }
46
47    @Override
48    String getChildProfileLockFile(int userId) {
49        return makeDirs(mStorageDir,
50                super.getChildProfileLockFile(userId)).getAbsolutePath();
51    }
52
53    @Override
54    protected File getSyntheticPasswordDirectoryForUser(int userId) {
55        return makeDirs(mStorageDir, super.getSyntheticPasswordDirectoryForUser(
56                userId).getAbsolutePath());
57    }
58
59    @Override
60    public PersistentDataBlockManagerInternal getPersistentDataBlock() {
61        return mPersistentDataBlock;
62    }
63
64    private File makeDirs(File baseDir, String filePath) {
65        File path = new File(filePath);
66        if (path.getParent() == null) {
67            return new File(baseDir, filePath);
68        } else {
69            File mappedDir = new File(baseDir, path.getParent());
70            mappedDir.mkdirs();
71            return new File(mappedDir, path.getName());
72        }
73    }
74}
75