1 /*
2  * Copyright (C) 2021 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.graphics.fonts;
18 
19 import android.annotation.NonNull;
20 import android.graphics.fonts.FontUpdateRequest;
21 import android.text.TextUtils;
22 import android.util.ArraySet;
23 import android.util.Slog;
24 import android.util.Xml;
25 
26 import com.android.modules.utils.TypedXmlPullParser;
27 import com.android.modules.utils.TypedXmlSerializer;
28 
29 import org.xmlpull.v1.XmlPullParser;
30 import org.xmlpull.v1.XmlPullParserException;
31 
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Set;
38 
39 /* package */ class PersistentSystemFontConfig {
40     private static final String TAG = "PersistentSystemFontConfig";
41 
42     private static final String TAG_ROOT = "fontConfig";
43     private static final String TAG_LAST_MODIFIED_DATE = "lastModifiedDate";
44     private static final String TAG_UPDATED_FONT_DIR = "updatedFontDir";
45     private static final String TAG_FAMILY = "family";
46     private static final String ATTR_VALUE = "value";
47 
48     /* package */ static class Config {
49         public long lastModifiedMillis;
50         public final Set<String> updatedFontDirs = new ArraySet<>();
51         public final List<FontUpdateRequest.Family> fontFamilies = new ArrayList<>();
52     }
53 
54     /**
55      * Read config XML and write to out argument.
56      */
loadFromXml(@onNull InputStream is, @NonNull Config out)57     public static void loadFromXml(@NonNull InputStream is, @NonNull Config out)
58             throws XmlPullParserException, IOException {
59         TypedXmlPullParser parser = Xml.resolvePullParser(is);
60 
61         int type;
62         while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
63             if (type != XmlPullParser.START_TAG) {
64                 continue;
65             }
66             final int depth = parser.getDepth();
67             final String tag = parser.getName();
68             if (depth == 1) {
69                 if (!TAG_ROOT.equals(tag)) {
70                     Slog.e(TAG, "Invalid root tag: " + tag);
71                     return;
72                 }
73             } else if (depth == 2) {
74                 switch (tag) {
75                     case TAG_LAST_MODIFIED_DATE:
76                         out.lastModifiedMillis = parseLongAttribute(parser, ATTR_VALUE, 0);
77                         break;
78                     case TAG_UPDATED_FONT_DIR:
79                         out.updatedFontDirs.add(getAttribute(parser, ATTR_VALUE));
80                         break;
81                     case TAG_FAMILY:
82                         // updatableFontMap is not ready here. We get the base file names by passing
83                         // empty fontDir, and resolve font paths later.
84                         out.fontFamilies.add(FontUpdateRequest.Family.readFromXml(parser));
85                         break;
86                     default:
87                         Slog.w(TAG, "Skipping unknown tag: " + tag);
88                 }
89             }
90         }
91 
92     }
93 
94     /**
95      * Write config to OutputStream as XML file.
96      */
writeToXml(@onNull OutputStream os, @NonNull Config config)97     public static void writeToXml(@NonNull OutputStream os, @NonNull Config config)
98             throws IOException {
99         TypedXmlSerializer out = Xml.resolveSerializer(os);
100         out.startDocument(null /* encoding */, true /* standalone */);
101 
102         out.startTag(null, TAG_ROOT);
103         out.startTag(null, TAG_LAST_MODIFIED_DATE);
104         out.attribute(null, ATTR_VALUE, Long.toString(config.lastModifiedMillis));
105         out.endTag(null, TAG_LAST_MODIFIED_DATE);
106         for (String dir : config.updatedFontDirs) {
107             out.startTag(null, TAG_UPDATED_FONT_DIR);
108             out.attribute(null, ATTR_VALUE, dir);
109             out.endTag(null, TAG_UPDATED_FONT_DIR);
110         }
111         List<FontUpdateRequest.Family> fontFamilies = config.fontFamilies;
112         for (int i = 0; i < fontFamilies.size(); i++) {
113             FontUpdateRequest.Family fontFamily = fontFamilies.get(i);
114             out.startTag(null, TAG_FAMILY);
115             FontUpdateRequest.Family.writeFamilyToXml(out, fontFamily);
116             out.endTag(null, TAG_FAMILY);
117         }
118         out.endTag(null, TAG_ROOT);
119 
120         out.endDocument();
121     }
122 
parseLongAttribute(TypedXmlPullParser parser, String attr, long defValue)123     private static long parseLongAttribute(TypedXmlPullParser parser, String attr, long defValue) {
124         final String value = parser.getAttributeValue(null /* namespace */, attr);
125         if (TextUtils.isEmpty(value)) {
126             return defValue;
127         }
128         try {
129             return Long.parseLong(value);
130         } catch (NumberFormatException e) {
131             return defValue;
132         }
133     }
134 
135     @NonNull
getAttribute(TypedXmlPullParser parser, String attr)136     private static String getAttribute(TypedXmlPullParser parser, String attr) {
137         final String value = parser.getAttributeValue(null /* namespace */, attr);
138         return value == null ? "" : value;
139     }
140 }
141