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.app.KeyguardManager;
20import android.app.NotificationManager;
21import android.app.admin.DevicePolicyManager;
22import android.app.trust.TrustManager;
23import android.content.Context;
24import android.content.ContextWrapper;
25import android.content.pm.PackageManager;
26import android.os.UserManager;
27import android.os.storage.StorageManager;
28
29public class MockLockSettingsContext extends ContextWrapper {
30
31    private UserManager mUserManager;
32    private NotificationManager mNotificationManager;
33    private DevicePolicyManager mDevicePolicyManager;
34    private StorageManager mStorageManager;
35    private TrustManager mTrustManager;
36    private KeyguardManager mKeyguardManager;
37
38    public MockLockSettingsContext(Context base, UserManager userManager,
39            NotificationManager notificationManager, DevicePolicyManager devicePolicyManager,
40            StorageManager storageManager, TrustManager trustManager,
41            KeyguardManager keyguardManager) {
42        super(base);
43        mUserManager = userManager;
44        mNotificationManager = notificationManager;
45        mDevicePolicyManager = devicePolicyManager;
46        mStorageManager = storageManager;
47        mTrustManager = trustManager;
48        mKeyguardManager = keyguardManager;
49    }
50
51    @Override
52    public Object getSystemService(String name) {
53        if (USER_SERVICE.equals(name)) {
54            return mUserManager;
55        } else if (NOTIFICATION_SERVICE.equals(name)) {
56            return mNotificationManager;
57        } else if (DEVICE_POLICY_SERVICE.equals(name)) {
58            return mDevicePolicyManager;
59        } else if (STORAGE_SERVICE.equals(name)) {
60            return mStorageManager;
61        } else if (TRUST_SERVICE.equals(name)) {
62            return mTrustManager;
63        } else if (KEYGUARD_SERVICE.equals(name)) {
64            return mKeyguardManager;
65        } else {
66            throw new RuntimeException("System service not mocked: " + name);
67        }
68    }
69
70    @Override
71    public void enforceCallingOrSelfPermission(String permission, String message) {
72        // Skip permission checks for unit tests.
73    }
74
75    @Override
76    public int checkCallingOrSelfPermission(String permission) {
77        return PackageManager.PERMISSION_GRANTED;
78    }
79}
80