CrossProfileIntentFilter.java revision 63798c596dc757135950313eb4bb44ca58696c68
1/*
2 * Copyright 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;
20import org.xmlpull.v1.XmlPullParser;
21import org.xmlpull.v1.XmlPullParserException;
22import org.xmlpull.v1.XmlSerializer;
23import android.content.IntentFilter;
24import android.util.Log;
25import java.io.IOException;
26import android.os.UserHandle;
27
28/**
29 * The {@link PackageManagerService} maintains some {@link CrossProfileIntentFilter}s for each user.
30 * If an {@link Intent} matches the {@link CrossProfileIntentFilter}, then activities in the user
31 * {@link #mTargetUserId} can access it.
32 */
33class CrossProfileIntentFilter extends IntentFilter {
34    private static final String ATTR_TARGET_USER_ID = "targetUserId";
35    private static final String ATTR_FLAGS = "flags";
36    private static final String ATTR_FILTER = "filter";
37
38    private static final String TAG = "CrossProfileIntentFilter";
39
40    // If the intent matches the IntentFilter, then it can be forwarded to this userId.
41    final int mTargetUserId;
42    final int mFlags;
43
44    CrossProfileIntentFilter(IntentFilter filter, int targetUserId, int flags) {
45        super(filter);
46        mTargetUserId = targetUserId;
47        mFlags = flags;
48    }
49
50    public int getTargetUserId() {
51        return mTargetUserId;
52    }
53
54    public int getFlags() {
55        return mFlags;
56    }
57
58    CrossProfileIntentFilter(XmlPullParser parser) throws XmlPullParserException, IOException {
59        String targetUserIdString = parser.getAttributeValue(null, ATTR_TARGET_USER_ID);
60        if (targetUserIdString == null) {
61            String msg = "Missing element under " + TAG +": " + ATTR_TARGET_USER_ID + " at " +
62                    parser.getPositionDescription();
63            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
64            mTargetUserId = UserHandle.USER_NULL;
65        } else {
66            mTargetUserId = Integer.parseInt(targetUserIdString);
67        }
68        String flagsString = parser.getAttributeValue(null, ATTR_FLAGS);
69        if (flagsString == null) {
70            String msg = "Missing element under " + TAG +": " + ATTR_FLAGS + " at " +
71                    parser.getPositionDescription();
72            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
73            mFlags = 0;
74        } else {
75            mFlags = Integer.parseInt(flagsString);
76        }
77        int outerDepth = parser.getDepth();
78        String tagName = parser.getName();
79        int type;
80        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
81                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
82            tagName = parser.getName();
83            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
84                continue;
85            } else if (type == XmlPullParser.START_TAG) {
86                if (tagName.equals(ATTR_FILTER)) {
87                    break;
88                } else {
89                    String msg = "Unknown element under " + Settings.TAG_FORWARDING_INTENT_FILTERS
90                            + ": " + tagName + " at " + parser.getPositionDescription();
91                    PackageManagerService.reportSettingsProblem(Log.WARN, msg);
92                    XmlUtils.skipCurrentTag(parser);
93                }
94            }
95        }
96        if (tagName.equals(ATTR_FILTER)) {
97            readFromXml(parser);
98        } else {
99            String msg = "Missing element under " + TAG + ": " + ATTR_FILTER +
100                    " at " + parser.getPositionDescription();
101            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
102            XmlUtils.skipCurrentTag(parser);
103        }
104    }
105
106    public void writeToXml(XmlSerializer serializer) throws IOException {
107        serializer.attribute(null, ATTR_TARGET_USER_ID, Integer.toString(mTargetUserId));
108        serializer.attribute(null, ATTR_FLAGS, Integer.toString(mFlags));
109        serializer.startTag(null, ATTR_FILTER);
110            super.writeToXml(serializer);
111        serializer.endTag(null, ATTR_FILTER);
112    }
113
114    @Override
115    public String toString() {
116        return "CrossProfileIntentFilter{0x" + Integer.toHexString(System.identityHashCode(this))
117                + " " + Integer.toString(mTargetUserId) + "}";
118    }
119}
120