1 /*
2  * Copyright (C) 2022 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.server.pm.pkg.component;
18 
19 import static com.android.server.pm.pkg.parsing.ParsingUtils.NOT_SET;
20 
21 import android.annotation.NonNull;
22 import android.content.pm.parsing.result.ParseInput;
23 import android.content.pm.parsing.result.ParseResult;
24 import android.content.res.Resources;
25 import android.content.res.TypedArray;
26 import android.content.res.XmlResourceParser;
27 
28 import com.android.internal.R;
29 import com.android.server.pm.pkg.parsing.ParsingPackage;
30 
31 import org.xmlpull.v1.XmlPullParserException;
32 
33 import java.io.IOException;
34 
35 /**
36  * @hide
37  */
38 public class ParsedInstrumentationUtils {
39 
40     @NonNull
parseInstrumentation(ParsingPackage pkg, Resources res, XmlResourceParser parser, boolean useRoundIcon, ParseInput input)41     public static ParseResult<ParsedInstrumentation> parseInstrumentation(ParsingPackage pkg,
42             Resources res, XmlResourceParser parser, boolean useRoundIcon,
43             ParseInput input) throws IOException, XmlPullParserException {
44         ParsedInstrumentationImpl
45                 instrumentation = new ParsedInstrumentationImpl();
46         String tag = "<" + parser.getName() + ">";
47 
48         TypedArray sa = res.obtainAttributes(parser, R.styleable.AndroidManifestInstrumentation);
49         try {
50             ParseResult<ParsedInstrumentationImpl> result = ParsedComponentUtils.parseComponent(
51                     instrumentation, tag, pkg, sa, useRoundIcon, input,
52                     R.styleable.AndroidManifestInstrumentation_banner,
53                     NOT_SET /*descriptionAttr*/,
54                     R.styleable.AndroidManifestInstrumentation_icon,
55                     R.styleable.AndroidManifestInstrumentation_label,
56                     R.styleable.AndroidManifestInstrumentation_logo,
57                     R.styleable.AndroidManifestInstrumentation_name,
58                     R.styleable.AndroidManifestInstrumentation_roundIcon);
59             if (result.isError()) {
60                 return input.error(result);
61             }
62 
63             // @formatter:off
64             // Note: don't allow this value to be a reference to a resource
65             // that may change.
66             instrumentation.setTargetPackage(sa.getNonResourceString(R.styleable.AndroidManifestInstrumentation_targetPackage))
67                     .setTargetProcesses(sa.getNonResourceString(R.styleable.AndroidManifestInstrumentation_targetProcesses))
68                     .setHandleProfiling(sa.getBoolean(R.styleable.AndroidManifestInstrumentation_handleProfiling, false))
69                     .setFunctionalTest(sa.getBoolean(R.styleable.AndroidManifestInstrumentation_functionalTest, false));
70             // @formatter:on
71         } finally {
72             sa.recycle();
73         }
74 
75         ParseResult<ParsedInstrumentationImpl> result =
76                 ComponentParseUtils.parseAllMetaData(pkg, res, parser, tag, instrumentation, input);
77 
78         if (result.isError()) {
79             return input.error(result);
80         }
81 
82         return input.success(result.getResult());
83     }
84 }
85