1 /*
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 
17 package com.android.settings;
18 
19 import android.content.ComponentName;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.content.res.Resources;
23 import android.content.res.TypedArray;
24 import android.content.res.XmlResourceParser;
25 import android.service.trust.TrustAgentService;
26 import android.util.AttributeSet;
27 import android.util.Log;
28 import android.util.Slog;
29 import android.util.Xml;
30 
31 import org.xmlpull.v1.XmlPullParser;
32 import org.xmlpull.v1.XmlPullParserException;
33 
34 import java.io.IOException;
35 
36 public class TrustAgentUtils {
37     static final String TAG = "TrustAgentUtils";
38 
39     private static final String TRUST_AGENT_META_DATA = TrustAgentService.TRUST_AGENT_META_DATA;
40     private static final String PERMISSION_PROVIDE_AGENT = android.Manifest.permission.PROVIDE_TRUST_AGENT;
41 
42     /**
43      * @return true, if the service in resolveInfo has the permission to provide a trust agent.
44      */
checkProvidePermission(ResolveInfo resolveInfo, PackageManager pm)45     public static boolean checkProvidePermission(ResolveInfo resolveInfo, PackageManager pm) {
46         String packageName = resolveInfo.serviceInfo.packageName;
47         if (pm.checkPermission(PERMISSION_PROVIDE_AGENT, packageName)
48                 != PackageManager.PERMISSION_GRANTED) {
49             Log.w(TAG, "Skipping agent because package " + packageName
50                     + " does not have permission " + PERMISSION_PROVIDE_AGENT + ".");
51             return false;
52         }
53         return true;
54     }
55 
56     public static class TrustAgentComponentInfo {
57         ComponentName componentName;
58         String title;
59         String summary;
60     }
61 
getComponentName(ResolveInfo resolveInfo)62     public static ComponentName getComponentName(ResolveInfo resolveInfo) {
63         if (resolveInfo == null || resolveInfo.serviceInfo == null) return null;
64         return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
65     }
66 
getSettingsComponent( PackageManager pm, ResolveInfo resolveInfo)67     public static TrustAgentComponentInfo getSettingsComponent(
68             PackageManager pm, ResolveInfo resolveInfo) {
69         if (resolveInfo == null || resolveInfo.serviceInfo == null
70                 || resolveInfo.serviceInfo.metaData == null) return null;
71         String cn = null;
72         TrustAgentComponentInfo trustAgentComponentInfo = new TrustAgentComponentInfo();
73         XmlResourceParser parser = null;
74         Exception caughtException = null;
75         try {
76             parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA);
77             if (parser == null) {
78                 Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data");
79                 return null;
80             }
81             Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo);
82             AttributeSet attrs = Xml.asAttributeSet(parser);
83             int type;
84             while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
85                     && type != XmlPullParser.START_TAG) {
86             }
87             String nodeName = parser.getName();
88             if (!"trust-agent".equals(nodeName)) {
89                 Slog.w(TAG, "Meta-data does not start with trust-agent tag");
90                 return null;
91             }
92             TypedArray sa =
93                     res.obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent);
94             trustAgentComponentInfo.summary =
95                     sa.getString(com.android.internal.R.styleable.TrustAgent_summary);
96             trustAgentComponentInfo.title =
97                     sa.getString(com.android.internal.R.styleable.TrustAgent_title);
98             cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity);
99             sa.recycle();
100         } catch (PackageManager.NameNotFoundException e) {
101             caughtException = e;
102         } catch (IOException e) {
103             caughtException = e;
104         } catch (XmlPullParserException e) {
105             caughtException = e;
106         } finally {
107             if (parser != null) parser.close();
108         }
109         if (caughtException != null) {
110             Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException);
111             return null;
112         }
113         if (cn != null && cn.indexOf('/') < 0) {
114             cn = resolveInfo.serviceInfo.packageName + "/" + cn;
115         }
116         trustAgentComponentInfo.componentName = (cn == null) ? null : ComponentName.unflattenFromString(cn);
117         return trustAgentComponentInfo;
118     }
119 }
120