CrossProfileIntentFilter.java revision 8194899071e0a84c95ef10614bd1b9485b48f589
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_USER_ID_DEST = "userIdDest";//Old name. Kept for compatibility.
36    private static final String ATTR_REMOVABLE = "removable";
37    private static final String ATTR_FILTER = "filter";
38
39    private static final String TAG = "CrossProfileIntentFilter";
40
41    // If the intent matches the IntentFilter, then it can be forwarded to this userId.
42    final int mTargetUserId;
43    boolean mRemovable;
44
45    CrossProfileIntentFilter(IntentFilter filter, boolean removable, int targetUserId) {
46        super(filter);
47        mTargetUserId = targetUserId;
48        mRemovable = removable;
49    }
50
51    public int getTargetUserId() {
52        return mTargetUserId;
53    }
54
55    public boolean isRemovable() {
56        return mRemovable;
57    }
58
59    CrossProfileIntentFilter(XmlPullParser parser) throws XmlPullParserException, IOException {
60        String targetUserIdString = parser.getAttributeValue(null, ATTR_TARGET_USER_ID);
61        if (targetUserIdString == null) {
62            targetUserIdString = parser.getAttributeValue(null, ATTR_USER_ID_DEST);
63        }
64        if (targetUserIdString == null) {
65            String msg = "Missing element under " + TAG +": " + ATTR_TARGET_USER_ID + " at " +
66                    parser.getPositionDescription();
67            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
68            mTargetUserId = UserHandle.USER_NULL;
69        } else {
70            mTargetUserId = Integer.parseInt(targetUserIdString);
71        }
72        String removableString = parser.getAttributeValue(null, ATTR_REMOVABLE);
73        if (removableString != null) {
74            mRemovable = Boolean.parseBoolean(removableString);
75        }
76        int outerDepth = parser.getDepth();
77        String tagName = parser.getName();
78        int type;
79        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
80                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
81            tagName = parser.getName();
82            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
83                continue;
84            } else if (type == XmlPullParser.START_TAG) {
85                if (tagName.equals(ATTR_FILTER)) {
86                    break;
87                } else {
88                    String msg = "Unknown element under " + Settings.TAG_FORWARDING_INTENT_FILTERS
89                            + ": " + tagName + " at " + parser.getPositionDescription();
90                    PackageManagerService.reportSettingsProblem(Log.WARN, msg);
91                    XmlUtils.skipCurrentTag(parser);
92                }
93            }
94        }
95        if (tagName.equals(ATTR_FILTER)) {
96            readFromXml(parser);
97        } else {
98            String msg = "Missing element under " + TAG + ": " + ATTR_FILTER +
99                    " at " + parser.getPositionDescription();
100            PackageManagerService.reportSettingsProblem(Log.WARN, msg);
101            XmlUtils.skipCurrentTag(parser);
102        }
103    }
104
105    public void writeToXml(XmlSerializer serializer) throws IOException {
106        serializer.attribute(null, ATTR_TARGET_USER_ID, Integer.toString(mTargetUserId));
107        serializer.attribute(null, ATTR_REMOVABLE, Boolean.toString(mRemovable));
108        serializer.startTag(null, ATTR_FILTER);
109            super.writeToXml(serializer);
110        serializer.endTag(null, ATTR_FILTER);
111    }
112
113    @Override
114    public String toString() {
115        return "CrossProfileIntentFilter{0x" + Integer.toHexString(System.identityHashCode(this))
116                + " " + Integer.toString(mTargetUserId) + "}";
117    }
118}
119