blob: b3d560f612ee978e7267bee8623e64af9e1265e0 [file] [log] [blame]
Ido Ofir6f482442014-05-09 14:47:15 -07001/*
2 * Copyright (C) 2014 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.settings;
18
19import android.content.ComponentName;
20import android.content.pm.PackageManager;
21import android.content.pm.ResolveInfo;
22import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.content.res.XmlResourceParser;
25import android.service.trust.TrustAgentService;
26import android.util.AttributeSet;
27import android.util.Slog;
28import android.util.Xml;
29
30import org.xmlpull.v1.XmlPullParser;
31import org.xmlpull.v1.XmlPullParserException;
32
33import java.io.IOException;
34
Sudheer Shankaec1052d2016-02-03 00:24:52 +000035import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
36
Zachary Iqbalccae73f2017-01-17 14:55:49 -080037// TODO(b/34461256): Refactor TrustAgentUtils into TrustAgentManager.
Ido Ofir6f482442014-05-09 14:47:15 -070038public class TrustAgentUtils {
39 static final String TAG = "TrustAgentUtils";
40
41 private static final String TRUST_AGENT_META_DATA = TrustAgentService.TRUST_AGENT_META_DATA;
Ido Ofir6f482442014-05-09 14:47:15 -070042
43 public static class TrustAgentComponentInfo {
44 ComponentName componentName;
45 String title;
46 String summary;
Sudheer Shankaec1052d2016-02-03 00:24:52 +000047 EnforcedAdmin admin = null;
Ido Ofir6f482442014-05-09 14:47:15 -070048 }
49
50 public static ComponentName getComponentName(ResolveInfo resolveInfo) {
51 if (resolveInfo == null || resolveInfo.serviceInfo == null) return null;
52 return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
53 }
54
55 public static TrustAgentComponentInfo getSettingsComponent(
56 PackageManager pm, ResolveInfo resolveInfo) {
57 if (resolveInfo == null || resolveInfo.serviceInfo == null
58 || resolveInfo.serviceInfo.metaData == null) return null;
59 String cn = null;
60 TrustAgentComponentInfo trustAgentComponentInfo = new TrustAgentComponentInfo();
61 XmlResourceParser parser = null;
62 Exception caughtException = null;
63 try {
64 parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA);
65 if (parser == null) {
66 Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data");
67 return null;
68 }
69 Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo);
70 AttributeSet attrs = Xml.asAttributeSet(parser);
71 int type;
72 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
73 && type != XmlPullParser.START_TAG) {
74 }
75 String nodeName = parser.getName();
76 if (!"trust-agent".equals(nodeName)) {
77 Slog.w(TAG, "Meta-data does not start with trust-agent tag");
78 return null;
79 }
80 TypedArray sa =
81 res.obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent);
82 trustAgentComponentInfo.summary =
83 sa.getString(com.android.internal.R.styleable.TrustAgent_summary);
84 trustAgentComponentInfo.title =
85 sa.getString(com.android.internal.R.styleable.TrustAgent_title);
86 cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity);
87 sa.recycle();
88 } catch (PackageManager.NameNotFoundException e) {
89 caughtException = e;
90 } catch (IOException e) {
91 caughtException = e;
92 } catch (XmlPullParserException e) {
93 caughtException = e;
94 } finally {
95 if (parser != null) parser.close();
96 }
97 if (caughtException != null) {
98 Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException);
99 return null;
100 }
101 if (cn != null && cn.indexOf('/') < 0) {
102 cn = resolveInfo.serviceInfo.packageName + "/" + cn;
103 }
104 trustAgentComponentInfo.componentName = (cn == null) ? null : ComponentName.unflattenFromString(cn);
105 return trustAgentComponentInfo;
106 }
107}