TrustAgentUtils.java revision 6f482447b05492906755d91ac4c29ec505a8a544
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
356f482447b05492906755d91ac4c29ec505a8a544Ido Ofirpublic class TrustAgentUtils {
366f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    static final String TAG = "TrustAgentUtils";
376f482447b05492906755d91ac4c29ec505a8a544Ido Ofir
386f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    private static final String TRUST_AGENT_META_DATA = TrustAgentService.TRUST_AGENT_META_DATA;
396f482447b05492906755d91ac4c29ec505a8a544Ido Ofir
406f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    public static class TrustAgentComponentInfo {
416f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        ComponentName componentName;
426f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        String title;
436f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        String summary;
446f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    }
456f482447b05492906755d91ac4c29ec505a8a544Ido Ofir
466f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    public static ComponentName getComponentName(ResolveInfo resolveInfo) {
476f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        if (resolveInfo == null || resolveInfo.serviceInfo == null) return null;
486f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
496f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    }
506f482447b05492906755d91ac4c29ec505a8a544Ido Ofir
516f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    public static TrustAgentComponentInfo getSettingsComponent(
526f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            PackageManager pm, ResolveInfo resolveInfo) {
536f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        if (resolveInfo == null || resolveInfo.serviceInfo == null
546f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                || resolveInfo.serviceInfo.metaData == null) return null;
556f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        String cn = null;
566f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        TrustAgentComponentInfo trustAgentComponentInfo = new TrustAgentComponentInfo();
576f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        XmlResourceParser parser = null;
586f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        Exception caughtException = null;
596f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        try {
606f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, TRUST_AGENT_META_DATA);
616f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            if (parser == null) {
626f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                Slog.w(TAG, "Can't find " + TRUST_AGENT_META_DATA + " meta-data");
636f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                return null;
646f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            }
656f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo);
666f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            AttributeSet attrs = Xml.asAttributeSet(parser);
676f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            int type;
686f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
696f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                    && type != XmlPullParser.START_TAG) {
706f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            }
716f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            String nodeName = parser.getName();
726f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            if (!"trust-agent".equals(nodeName)) {
736f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                Slog.w(TAG, "Meta-data does not start with trust-agent tag");
746f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                return null;
756f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            }
766f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            TypedArray sa =
776f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                    res.obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent);
786f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            trustAgentComponentInfo.summary =
796f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                    sa.getString(com.android.internal.R.styleable.TrustAgent_summary);
806f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            trustAgentComponentInfo.title =
816f482447b05492906755d91ac4c29ec505a8a544Ido Ofir                    sa.getString(com.android.internal.R.styleable.TrustAgent_title);
826f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity);
836f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            sa.recycle();
846f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        } catch (PackageManager.NameNotFoundException e) {
856f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            caughtException = e;
866f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        } catch (IOException e) {
876f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            caughtException = e;
886f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        } catch (XmlPullParserException e) {
896f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            caughtException = e;
906f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        } finally {
916f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            if (parser != null) parser.close();
926f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        }
936f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        if (caughtException != null) {
946f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException);
956f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            return null;
966f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        }
976f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        if (cn != null && cn.indexOf('/') < 0) {
986f482447b05492906755d91ac4c29ec505a8a544Ido Ofir            cn = resolveInfo.serviceInfo.packageName + "/" + cn;
996f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        }
1006f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        trustAgentComponentInfo.componentName = (cn == null) ? null : ComponentName.unflattenFromString(cn);
1016f482447b05492906755d91ac4c29ec505a8a544Ido Ofir        return trustAgentComponentInfo;
1026f482447b05492906755d91ac4c29ec505a8a544Ido Ofir    }
1036f482447b05492906755d91ac4c29ec505a8a544Ido Ofir}
104