CrossProfileIntentFilter.java revision 3f7777fa4f1d392e18bad39edcd4539880c52ff9
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_OWNER_USER_ID = "ownerUserId";
37    private static final String ATTR_OWNER_PACKAGE = "ownerPackage";
38    private static final String ATTR_FILTER = "filter";
39
40    private static final String TAG = "CrossProfileIntentFilter";
41
42    // If the intent matches the IntentFilter, then it can be forwarded to this userId.
43    final int mTargetUserId;
44    final int mOwnerUserId; // userId of the app which has set this CrossProfileIntentFilter.
45    final String mOwnerPackage; // packageName of the app.
46    final int mFlags;
47
48    CrossProfileIntentFilter(IntentFilter filter, String ownerPackage, int ownerUserId,
49            int targetUserId, int flags) {
50        super(filter);
51        mTargetUserId = targetUserId;
52        mOwnerUserId = ownerUserId;
53        mOwnerPackage = ownerPackage;
54        mFlags = flags;
55    }
56
57    public int getTargetUserId() {
58        return mTargetUserId;
59    }
60
61    public int getFlags() {
62        return mFlags;
63    }
64
65    public int getOwnerUserId() {
66        return mOwnerUserId;
67    }
68
69    public String getOwnerPackage() {
70        return mOwnerPackage;
71    }
72
73    CrossProfileIntentFilter(XmlPullParser parser) throws XmlPullParserException, IOException {
74        mTargetUserId = getIntFromXml(parser, ATTR_TARGET_USER_ID, UserHandle.USER_NULL);
75        mOwnerUserId = getIntFromXml(parser, ATTR_OWNER_USER_ID, UserHandle.USER_NULL);
76        mOwnerPackage = getStringFromXml(parser, ATTR_OWNER_PACKAGE, "");
77        mFlags = getIntFromXml(parser, ATTR_FLAGS, 0);
78
79        int outerDepth = parser.getDepth();
80        String tagName = parser.getName();
81        int type;
82        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
83                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
84            tagName = parser.getName();
85            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
86                continue;
87            } else if (type == XmlPullParser.START_TAG) {
88                if (tagName.equals(ATTR_FILTER)) {
89                    break;
90                } else {
91                    String msg = "Unknown element under " + Settings.TAG_FORWARDING_INTENT_FILTERS
92                            + ": " + tagName + " at " + parser.getPositionDescription();
93                    PackageManagerService.reportSettingsProblem(Log.WARN, msg);
94                    XmlUtils.skipCurrentTag(parser);
95                }
96            }
97        }
98        if (tagName.equals(ATTR_FILTER)) {
99            readFromXml(parser);
100        } else {
101            String msg = "Missing element under " + TAG + ": " + ATTR_FILTER +
102                    " at " + parser.getPositionDescription();
103            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
104            XmlUtils.skipCurrentTag(parser);
105        }
106    }
107
108    String getStringFromXml(XmlPullParser parser, String attribute, String defaultValue) {
109        String value = parser.getAttributeValue(null, attribute);
110        if (value == null) {
111            String msg = "Missing element under " + TAG +": " + attribute + " at " +
112                    parser.getPositionDescription();
113            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
114            return defaultValue;
115        } else {
116            return value;
117        }
118    }
119
120    int getIntFromXml(XmlPullParser parser, String attribute, int defaultValue) {
121        String stringValue = getStringFromXml(parser, attribute, null);
122        if (stringValue != null) {
123            return Integer.parseInt(stringValue);
124        }
125        return defaultValue;
126    }
127
128    public void writeToXml(XmlSerializer serializer) throws IOException {
129        serializer.attribute(null, ATTR_TARGET_USER_ID, Integer.toString(mTargetUserId));
130        serializer.attribute(null, ATTR_FLAGS, Integer.toString(mFlags));
131        serializer.attribute(null, ATTR_OWNER_USER_ID, Integer.toString(mOwnerUserId));
132        serializer.attribute(null, ATTR_OWNER_PACKAGE, mOwnerPackage);
133        serializer.startTag(null, ATTR_FILTER);
134            super.writeToXml(serializer);
135        serializer.endTag(null, ATTR_FILTER);
136    }
137
138    @Override
139    public String toString() {
140        return "CrossProfileIntentFilter{0x" + Integer.toHexString(System.identityHashCode(this))
141                + " " + Integer.toString(mTargetUserId) + "}";
142    }
143}
144