1 /*
2  * Copyright (C) 2022 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 import android.app.admin.DeviceAdminInfo;
18 import android.content.Context;
19 import android.content.pm.ActivityInfo;
20 
21 import org.xmlpull.v1.XmlPullParserException;
22 
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 public class DeviceAdminInfoCompat {
28     public DeviceAdminInfo mDeviceAdminInfo;
29 
DeviceAdminInfoCompat(DeviceAdminInfo deviceAdminInfo)30     public DeviceAdminInfoCompat(DeviceAdminInfo deviceAdminInfo) {
31         mDeviceAdminInfo = deviceAdminInfo;
32     }
33 
DeviceAdminInfoCompat(Context context, ActivityInfo activityInfo)34     public DeviceAdminInfoCompat(Context context, ActivityInfo activityInfo)
35             throws XmlPullParserException, IOException {
36         mDeviceAdminInfo = new DeviceAdminInfo(context, activityInfo);
37     }
38 
getUsedPolicies()39     public List<PolicyInfo> getUsedPolicies() {
40         List<DeviceAdminInfo.PolicyInfo> obj = mDeviceAdminInfo.getUsedPolicies();
41         List<PolicyInfo> result = new ArrayList<>();
42         for (Object o : obj) {
43             result.add(new PolicyInfo(o));
44         }
45         return result;
46     }
47 
48     public static class PolicyInfo {
49         private final Object mPolicyInfo;
50 
51         public final int mIdent;
52         public final int mLabel;
53         public final int mDescription;
54         public final int mLabelForSecondaryUsers;
55         public final int mDescriptionForSecondaryUsers;
56 
PolicyInfo(Object policyInfo)57         public PolicyInfo(Object policyInfo) {
58             mPolicyInfo = policyInfo;
59             mIdent = ((DeviceAdminInfo.PolicyInfo) policyInfo).ident;
60             mLabel = ((DeviceAdminInfo.PolicyInfo) policyInfo).label;
61             mDescription = ((DeviceAdminInfo.PolicyInfo) policyInfo).description;
62             mLabelForSecondaryUsers =
63                     ((DeviceAdminInfo.PolicyInfo) policyInfo).labelForSecondaryUsers;
64             mDescriptionForSecondaryUsers =
65                     ((DeviceAdminInfo.PolicyInfo) policyInfo).descriptionForSecondaryUsers;
66         }
67     }
68 }
69