1/*
2 * Copyright (C) 2014 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
17package com.android.server.pm;
18
19import com.android.internal.util.XmlUtils;
20
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23import org.xmlpull.v1.XmlSerializer;
24
25import android.content.ComponentName;
26import android.content.IntentFilter;
27import android.util.Log;
28
29import java.io.IOException;
30
31class PersistentPreferredActivity extends IntentFilter {
32    private static final String ATTR_NAME = "name"; // component name
33    private static final String ATTR_FILTER = "filter"; // filter
34
35    private static final String TAG = "PersistentPreferredActivity";
36
37    private static final boolean DEBUG_FILTERS = false;
38
39    final ComponentName mComponent;
40
41    PersistentPreferredActivity(IntentFilter filter, ComponentName activity) {
42        super(filter);
43        mComponent = activity;
44    }
45
46    PersistentPreferredActivity(XmlPullParser parser) throws XmlPullParserException, IOException {
47        String shortComponent = parser.getAttributeValue(null, ATTR_NAME);
48        mComponent = ComponentName.unflattenFromString(shortComponent);
49        if (mComponent == null) {
50            PackageManagerService.reportSettingsProblem(Log.WARN,
51                    "Error in package manager settings: " +
52                            "Bad activity name " + shortComponent +
53                            " at " + parser.getPositionDescription());
54        }
55        int outerDepth = parser.getDepth();
56        String tagName = parser.getName();
57        int type;
58        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
59                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
60            tagName = parser.getName();
61            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
62                continue;
63            } else if (type == XmlPullParser.START_TAG) {
64                if (tagName.equals(ATTR_FILTER)) {
65                    break;
66                } else {
67                    PackageManagerService.reportSettingsProblem(Log.WARN,
68                            "Unknown element: " + tagName +
69                            " at " + parser.getPositionDescription());
70                    XmlUtils.skipCurrentTag(parser);
71                }
72            }
73        }
74        if (tagName.equals(ATTR_FILTER)) {
75            readFromXml(parser);
76        } else {
77            PackageManagerService.reportSettingsProblem(Log.WARN,
78                    "Missing element filter at " +
79                    parser.getPositionDescription());
80            XmlUtils.skipCurrentTag(parser);
81        }
82    }
83
84    public void writeToXml(XmlSerializer serializer) throws IOException {
85        serializer.attribute(null, ATTR_NAME, mComponent.flattenToShortString());
86        serializer.startTag(null, ATTR_FILTER);
87            super.writeToXml(serializer);
88        serializer.endTag(null, ATTR_FILTER);
89    }
90
91    @Override
92    public String toString() {
93        return "PersistentPreferredActivity{0x" + Integer.toHexString(System.identityHashCode(this))
94                + " " + mComponent.flattenToShortString() + "}";
95    }
96}
97