• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ondevicepersonalization.services.manifest;
18 
19 import android.content.res.XmlResourceParser;
20 import android.util.Log;
21 
22 import org.xmlpull.v1.XmlPullParser;
23 import org.xmlpull.v1.XmlPullParserException;
24 
25 import java.io.IOException;
26 
27 /** Parser and validator for OnDevicePersonalization app manifest configs. */
28 public class AppManifestConfigParser {
29     private static final String TAG = "AppManifestConfigParser";
30     private static final String TAG_ON_DEVICE_PERSONALIZATION_CONFIG = "on-device-personalization";
31     private static final String TAG_DOWNLOAD_SETTINGS = "download-settings";
32     private static final String TAG_SERVICE = "service";
33     private static final String ATTR_DOWNLOAD_URL = "url";
34     private static final String ATTR_NAME = "name";
35 
AppManifestConfigParser()36     private AppManifestConfigParser() {
37     }
38 
39     /**
40      * Parses and validates given XML resource
41      *
42      * @param parser the XmlParser representing the OnDevicePersonalization app manifest config
43      */
getConfig(XmlResourceParser parser)44     public static AppManifestConfig getConfig(XmlResourceParser parser) throws IOException,
45             XmlPullParserException {
46         String downloadUrl = null;
47         String serviceName = null;
48 
49         // The first next goes to START_DOCUMENT, so we need another next to go to START_TAG.
50         parser.next();
51         parser.next();
52         parser.require(XmlPullParser.START_TAG, null, TAG_ON_DEVICE_PERSONALIZATION_CONFIG);
53         parser.next();
54 
55         // Walk through the config to parse required values.
56         while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
57             if (parser.getEventType() != XmlResourceParser.START_TAG) {
58                 parser.next();
59                 continue;
60             }
61             switch (parser.getName()) {
62                 case TAG_DOWNLOAD_SETTINGS:
63                     downloadUrl = parser.getAttributeValue(null, ATTR_DOWNLOAD_URL);
64                     break;
65                 case TAG_SERVICE:
66                     serviceName = parser.getAttributeValue(null, ATTR_NAME);
67                     break;
68                 default:
69                     Log.i(TAG, "Unknown tag: " + parser.getName());
70             }
71             parser.next();
72         }
73 
74         return new AppManifestConfig(downloadUrl, serviceName);
75     }
76 }
77