16f482447b05492906755d91ac4c29ec505a8a544Ido Ofir/* 26f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * Copyright (C) 2014 The Android Open Source Project 36f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * 46f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * Licensed under the Apache License, Version 2.0 (the "License"); 56f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * you may not use this file except in compliance with the License. 66f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * You may obtain a copy of the License at 76f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * 86f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * http://www.apache.org/licenses/LICENSE-2.0 96f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * 106f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * Unless required by applicable law or agreed to in writing, software 116f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * distributed under the License is distributed on an "AS IS" BASIS, 126f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * See the License for the specific language governing permissions and 146f482447b05492906755d91ac4c29ec505a8a544Ido Ofir * limitations under the License 156f482447b05492906755d91ac4c29ec505a8a544Ido Ofir */ 166f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 176f482447b05492906755d91ac4c29ec505a8a544Ido Ofirpackage com.android.settings; 186f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 196f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.content.ComponentName; 206f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.content.pm.PackageManager; 216f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.content.pm.ResolveInfo; 226f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.content.res.Resources; 236f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.content.res.TypedArray; 246f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.content.res.XmlResourceParser; 256f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.service.trust.TrustAgentService; 266f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.util.AttributeSet; 276f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.util.Slog; 286f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport android.util.Xml; 296f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 306f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport org.xmlpull.v1.XmlPullParser; 316f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport org.xmlpull.v1.XmlPullParserException; 326f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 336f482447b05492906755d91ac4c29ec505a8a544Ido Ofirimport java.io.IOException; 346f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 35ec1052dbadbc472c0afff943a9b61e6d95ab8089Sudheer Shankaimport static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 36ec1052dbadbc472c0afff943a9b61e6d95ab8089Sudheer Shanka 37ccae73f228f98bc8412c435a273525514d78843aZachary Iqbal// TODO(b/34461256): Refactor TrustAgentUtils into TrustAgentManager. 386f482447b05492906755d91ac4c29ec505a8a544Ido Ofirpublic class TrustAgentUtils { 396f482447b05492906755d91ac4c29ec505a8a544Ido Ofir static final String TAG = "TrustAgentUtils"; 406f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 416f482447b05492906755d91ac4c29ec505a8a544Ido Ofir private static final String TRUST_AGENT_META_DATA = TrustAgentService.TRUST_AGENT_META_DATA; 426f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 436f482447b05492906755d91ac4c29ec505a8a544Ido Ofir public static class TrustAgentComponentInfo { 446f482447b05492906755d91ac4c29ec505a8a544Ido Ofir ComponentName componentName; 456f482447b05492906755d91ac4c29ec505a8a544Ido Ofir String title; 466f482447b05492906755d91ac4c29ec505a8a544Ido Ofir String summary; 47ec1052dbadbc472c0afff943a9b61e6d95ab8089Sudheer Shanka EnforcedAdmin admin = null; 486f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 496f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 506f482447b05492906755d91ac4c29ec505a8a544Ido Ofir public static ComponentName getComponentName(ResolveInfo resolveInfo) { 516f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (resolveInfo == null || resolveInfo.serviceInfo == null) return null; 526f482447b05492906755d91ac4c29ec505a8a544Ido Ofir return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); 536f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 546f482447b05492906755d91ac4c29ec505a8a544Ido Ofir 556f482447b05492906755d91ac4c29ec505a8a544Ido Ofir public static TrustAgentComponentInfo getSettingsComponent( 566f482447b05492906755d91ac4c29ec505a8a544Ido Ofir PackageManager pm, ResolveInfo resolveInfo) { 576f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (resolveInfo == null || resolveInfo.serviceInfo == null 586f482447b05492906755d91ac4c29ec505a8a544Ido Ofir || resolveInfo.serviceInfo.metaData == null) return null; 596f482447b05492906755d91ac4c29ec505a8a544Ido Ofir String cn = null; 606f482447b05492906755d91ac4c29ec505a8a544Ido Ofir TrustAgentComponentInfo trustAgentComponentInfo = new TrustAgentComponentInfo(); 616f482447b05492906755d91ac4c29ec505a8a544Ido Ofir XmlResourceParser parser = null; 626f482447b05492906755d91ac4c29ec505a8a544Ido Ofir Exception caughtException = null; 636f482447b05492906755d91ac4c29ec505a8a544Ido Ofir try { 646f482447b05492906755d91ac4c29ec505a8a544Ido Ofir parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA); 656f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (parser == null) { 666f482447b05492906755d91ac4c29ec505a8a544Ido Ofir Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data"); 676f482447b05492906755d91ac4c29ec505a8a544Ido Ofir return null; 686f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 696f482447b05492906755d91ac4c29ec505a8a544Ido Ofir Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo); 706f482447b05492906755d91ac4c29ec505a8a544Ido Ofir AttributeSet attrs = Xml.asAttributeSet(parser); 716f482447b05492906755d91ac4c29ec505a8a544Ido Ofir int type; 726f482447b05492906755d91ac4c29ec505a8a544Ido Ofir while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 736f482447b05492906755d91ac4c29ec505a8a544Ido Ofir && type != XmlPullParser.START_TAG) { 746f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 756f482447b05492906755d91ac4c29ec505a8a544Ido Ofir String nodeName = parser.getName(); 766f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (!"trust-agent".equals(nodeName)) { 776f482447b05492906755d91ac4c29ec505a8a544Ido Ofir Slog.w(TAG, "Meta-data does not start with trust-agent tag"); 786f482447b05492906755d91ac4c29ec505a8a544Ido Ofir return null; 796f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 806f482447b05492906755d91ac4c29ec505a8a544Ido Ofir TypedArray sa = 816f482447b05492906755d91ac4c29ec505a8a544Ido Ofir res.obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent); 826f482447b05492906755d91ac4c29ec505a8a544Ido Ofir trustAgentComponentInfo.summary = 836f482447b05492906755d91ac4c29ec505a8a544Ido Ofir sa.getString(com.android.internal.R.styleable.TrustAgent_summary); 846f482447b05492906755d91ac4c29ec505a8a544Ido Ofir trustAgentComponentInfo.title = 856f482447b05492906755d91ac4c29ec505a8a544Ido Ofir sa.getString(com.android.internal.R.styleable.TrustAgent_title); 866f482447b05492906755d91ac4c29ec505a8a544Ido Ofir cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity); 876f482447b05492906755d91ac4c29ec505a8a544Ido Ofir sa.recycle(); 886f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } catch (PackageManager.NameNotFoundException e) { 896f482447b05492906755d91ac4c29ec505a8a544Ido Ofir caughtException = e; 906f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } catch (IOException e) { 916f482447b05492906755d91ac4c29ec505a8a544Ido Ofir caughtException = e; 926f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } catch (XmlPullParserException e) { 936f482447b05492906755d91ac4c29ec505a8a544Ido Ofir caughtException = e; 946f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } finally { 956f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (parser != null) parser.close(); 966f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 976f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (caughtException != null) { 986f482447b05492906755d91ac4c29ec505a8a544Ido Ofir Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException); 996f482447b05492906755d91ac4c29ec505a8a544Ido Ofir return null; 1006f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 1016f482447b05492906755d91ac4c29ec505a8a544Ido Ofir if (cn != null && cn.indexOf('/') < 0) { 1026f482447b05492906755d91ac4c29ec505a8a544Ido Ofir cn = resolveInfo.serviceInfo.packageName + "/" + cn; 1036f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 1046f482447b05492906755d91ac4c29ec505a8a544Ido Ofir trustAgentComponentInfo.componentName = (cn == null) ? null : ComponentName.unflattenFromString(cn); 1056f482447b05492906755d91ac4c29ec505a8a544Ido Ofir return trustAgentComponentInfo; 1066f482447b05492906755d91ac4c29ec505a8a544Ido Ofir } 1076f482447b05492906755d91ac4c29ec505a8a544Ido Ofir} 108