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 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.Log; 28import android.util.Slog; 29import android.util.Xml; 30 31import org.xmlpull.v1.XmlPullParser; 32import org.xmlpull.v1.XmlPullParserException; 33 34import java.io.IOException; 35 36public 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 */ 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 boolean disabledByAdministrator; 61 } 62 63 public static ComponentName getComponentName(ResolveInfo resolveInfo) { 64 if (resolveInfo == null || resolveInfo.serviceInfo == null) return null; 65 return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); 66 } 67 68 public static TrustAgentComponentInfo getSettingsComponent( 69 PackageManager pm, ResolveInfo resolveInfo) { 70 if (resolveInfo == null || resolveInfo.serviceInfo == null 71 || resolveInfo.serviceInfo.metaData == null) return null; 72 String cn = null; 73 TrustAgentComponentInfo trustAgentComponentInfo = new TrustAgentComponentInfo(); 74 XmlResourceParser parser = null; 75 Exception caughtException = null; 76 try { 77 parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA); 78 if (parser == null) { 79 Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data"); 80 return null; 81 } 82 Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo); 83 AttributeSet attrs = Xml.asAttributeSet(parser); 84 int type; 85 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 86 && type != XmlPullParser.START_TAG) { 87 } 88 String nodeName = parser.getName(); 89 if (!"trust-agent".equals(nodeName)) { 90 Slog.w(TAG, "Meta-data does not start with trust-agent tag"); 91 return null; 92 } 93 TypedArray sa = 94 res.obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent); 95 trustAgentComponentInfo.summary = 96 sa.getString(com.android.internal.R.styleable.TrustAgent_summary); 97 trustAgentComponentInfo.title = 98 sa.getString(com.android.internal.R.styleable.TrustAgent_title); 99 cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity); 100 sa.recycle(); 101 } catch (PackageManager.NameNotFoundException e) { 102 caughtException = e; 103 } catch (IOException e) { 104 caughtException = e; 105 } catch (XmlPullParserException e) { 106 caughtException = e; 107 } finally { 108 if (parser != null) parser.close(); 109 } 110 if (caughtException != null) { 111 Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException); 112 return null; 113 } 114 if (cn != null && cn.indexOf('/') < 0) { 115 cn = resolveInfo.serviceInfo.packageName + "/" + cn; 116 } 117 trustAgentComponentInfo.componentName = (cn == null) ? null : ComponentName.unflattenFromString(cn); 118 return trustAgentComponentInfo; 119 } 120} 121