blob: af188a95c92904c0cebb40dbff644ff0a87f3ede [file] [log] [blame]
Alex Klyubindcdaf872015-05-13 15:57:09 -07001/*
2 * Copyright (C) 2015 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
Alex Klyubin10a9f172015-04-16 13:41:19 -070017package android.security;
18
Artur Satayev53fe9662019-12-10 17:47:55 +000019import android.compat.annotation.UnsupportedAppUsage;
Amith Yamasanid04aaa32016-06-13 12:09:36 -070020import android.content.Context;
Alex Klyubin10a9f172015-04-16 13:41:19 -070021import android.os.RemoteException;
22import android.os.ServiceManager;
23import android.os.UserHandle;
24import android.service.gatekeeper.IGateKeeperService;
25
26/**
27 * Convenience class for accessing the gatekeeper service.
28 *
29 * @hide
30 */
31public abstract class GateKeeper {
32
Rubin Xu59ced282017-01-30 23:39:12 +000033 public static final long INVALID_SECURE_USER_ID = 0;
34
Alex Klyubin10a9f172015-04-16 13:41:19 -070035 private GateKeeper() {}
36
37 public static IGateKeeperService getService() {
Alex Klyubin708fc9402015-04-28 18:58:47 -070038 IGateKeeperService service = IGateKeeperService.Stub.asInterface(
Amith Yamasanid04aaa32016-06-13 12:09:36 -070039 ServiceManager.getService(Context.GATEKEEPER_SERVICE));
Alex Klyubin708fc9402015-04-28 18:58:47 -070040 if (service == null) {
41 throw new IllegalStateException("Gatekeeper service not available");
42 }
43 return service;
Alex Klyubin10a9f172015-04-16 13:41:19 -070044 }
45
Mathew Inwoode420f8b2018-08-16 18:40:47 +010046 @UnsupportedAppUsage
Alex Klyubin10a9f172015-04-16 13:41:19 -070047 public static long getSecureUserId() throws IllegalStateException {
48 try {
Alex Klyubin708fc9402015-04-28 18:58:47 -070049 return getService().getSecureUserId(UserHandle.myUserId());
Alex Klyubin10a9f172015-04-16 13:41:19 -070050 } catch (RemoteException e) {
51 throw new IllegalStateException(
52 "Failed to obtain secure user ID from gatekeeper", e);
53 }
54 }
55}