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.internal.pm.pkg.component;
18 
19 import android.R;
20 import android.annotation.NonNull;
21 import android.content.pm.parsing.result.ParseInput;
22 import android.content.pm.parsing.result.ParseResult;
23 import android.content.res.Resources;
24 import android.content.res.TypedArray;
25 import android.content.res.XmlResourceParser;
26 import android.text.TextUtils;
27 
28 import org.xmlpull.v1.XmlPullParserException;
29 
30 import java.io.IOException;
31 
32 /** @hide */
33 public class ParsedApexSystemServiceUtils {
34 
35     @NonNull
parseApexSystemService( Resources res, XmlResourceParser parser, ParseInput input)36     public static ParseResult<ParsedApexSystemService> parseApexSystemService(
37             Resources res, XmlResourceParser parser, ParseInput input)
38             throws XmlPullParserException, IOException {
39         final ParsedApexSystemServiceImpl systemService =
40                 new ParsedApexSystemServiceImpl();
41         TypedArray sa = res.obtainAttributes(parser,
42                 R.styleable.AndroidManifestApexSystemService);
43         try {
44             String className = sa.getString(
45                     R.styleable.AndroidManifestApexSystemService_name);
46             if (TextUtils.isEmpty(className)) {
47                 return input.error("<apex-system-service> does not have name attribute");
48             }
49 
50             String jarPath = sa.getString(
51                     R.styleable.AndroidManifestApexSystemService_path);
52             String minSdkVersion = sa.getString(
53                     R.styleable.AndroidManifestApexSystemService_minSdkVersion);
54             String maxSdkVersion = sa.getString(
55                     R.styleable.AndroidManifestApexSystemService_maxSdkVersion);
56             int initOrder = sa.getInt(R.styleable.AndroidManifestApexSystemService_initOrder, 0);
57 
58             systemService.setName(className)
59                     .setMinSdkVersion(minSdkVersion)
60                     .setMaxSdkVersion(maxSdkVersion)
61                     .setInitOrder(initOrder);
62 
63             if (!TextUtils.isEmpty(jarPath)) {
64                 systemService.setJarPath(jarPath);
65             }
66 
67             return input.success(systemService);
68         } finally {
69             sa.recycle();
70         }
71     }
72 }
73