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.devicepolicy;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.admin.IntegerPolicyValue;
22 import android.app.admin.PolicyKey;
23 import android.util.Log;
24 
25 import com.android.modules.utils.TypedXmlPullParser;
26 import com.android.modules.utils.TypedXmlSerializer;
27 
28 import org.xmlpull.v1.XmlPullParserException;
29 
30 import java.io.IOException;
31 import java.util.Objects;
32 
33 final class IntegerPolicySerializer extends PolicySerializer<Integer> {
34 
35     private static final String TAG = "IntegerPolicySerializer";
36 
37     private static final String ATTR_VALUE = "value";
38 
39     @Override
saveToXml(PolicyKey policyKey, TypedXmlSerializer serializer, @NonNull Integer value)40     void saveToXml(PolicyKey policyKey, TypedXmlSerializer serializer,
41             @NonNull Integer value) throws IOException {
42         Objects.requireNonNull(value);
43         serializer.attributeInt(/* namespace= */ null, ATTR_VALUE, value);
44     }
45 
46     @Nullable
47     @Override
readFromXml(TypedXmlPullParser parser)48     IntegerPolicyValue readFromXml(TypedXmlPullParser parser) {
49         try {
50             return new IntegerPolicyValue(
51                     parser.getAttributeInt(/* namespace= */ null, ATTR_VALUE));
52         } catch (XmlPullParserException e) {
53             Log.e(TAG, "Error parsing Integer policy value", e);
54             return null;
55         }
56     }
57 }
58