blob: ff4049bf2441362422518bdb67c5b53b6451226c [file] [log] [blame]
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001/*
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/**
Nicolas Prevot81948992014-05-16 18:25:26 +010029 * 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.
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000032 */
Nicolas Prevot81948992014-05-16 18:25:26 +010033class CrossProfileIntentFilter extends IntentFilter {
34 private static final String ATTR_TARGET_USER_ID = "targetUserId";
Nicolas Prevot63798c52014-05-27 13:22:38 +010035 private static final String ATTR_FLAGS = "flags";
Nicolas Prevot3f7777f2014-07-24 15:58:39 +010036 private static final String ATTR_OWNER_PACKAGE = "ownerPackage";
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000037 private static final String ATTR_FILTER = "filter";
38
Nicolas Prevot81948992014-05-16 18:25:26 +010039 private static final String TAG = "CrossProfileIntentFilter";
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000040
41 // If the intent matches the IntentFilter, then it can be forwarded to this userId.
Nicolas Prevot81948992014-05-16 18:25:26 +010042 final int mTargetUserId;
Nicolas Prevot3f7777f2014-07-24 15:58:39 +010043 final String mOwnerPackage; // packageName of the app.
Nicolas Prevot63798c52014-05-27 13:22:38 +010044 final int mFlags;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000045
Nicolas Prevot4b8d5822015-03-05 15:20:49 +000046 CrossProfileIntentFilter(IntentFilter filter, String ownerPackage, int targetUserId,
47 int flags) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000048 super(filter);
Nicolas Prevot81948992014-05-16 18:25:26 +010049 mTargetUserId = targetUserId;
Nicolas Prevot3f7777f2014-07-24 15:58:39 +010050 mOwnerPackage = ownerPackage;
Nicolas Prevot63798c52014-05-27 13:22:38 +010051 mFlags = flags;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000052 }
53
Nicolas Prevot81948992014-05-16 18:25:26 +010054 public int getTargetUserId() {
55 return mTargetUserId;
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000056 }
57
Nicolas Prevot63798c52014-05-27 13:22:38 +010058 public int getFlags() {
59 return mFlags;
Nicolas Prevot6fee7d42014-05-01 16:16:46 +010060 }
61
Nicolas Prevot3f7777f2014-07-24 15:58:39 +010062 public String getOwnerPackage() {
63 return mOwnerPackage;
64 }
65
Nicolas Prevot81948992014-05-16 18:25:26 +010066 CrossProfileIntentFilter(XmlPullParser parser) throws XmlPullParserException, IOException {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +010067 mTargetUserId = getIntFromXml(parser, ATTR_TARGET_USER_ID, UserHandle.USER_NULL);
Nicolas Prevot3f7777f2014-07-24 15:58:39 +010068 mOwnerPackage = getStringFromXml(parser, ATTR_OWNER_PACKAGE, "");
69 mFlags = getIntFromXml(parser, ATTR_FLAGS, 0);
70
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000071 int outerDepth = parser.getDepth();
72 String tagName = parser.getName();
73 int type;
74 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
75 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
76 tagName = parser.getName();
77 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
78 continue;
79 } else if (type == XmlPullParser.START_TAG) {
80 if (tagName.equals(ATTR_FILTER)) {
81 break;
82 } else {
Nicolas Prevot29762c32014-07-29 18:51:32 +010083 String msg = "Unknown element under "
84 + Settings.TAG_CROSS_PROFILE_INTENT_FILTERS + ": " + tagName + " at "
85 + parser.getPositionDescription();
Nicolas Prevot10fa67c2014-03-24 13:44:38 +000086 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
87 XmlUtils.skipCurrentTag(parser);
88 }
89 }
90 }
91 if (tagName.equals(ATTR_FILTER)) {
92 readFromXml(parser);
93 } else {
94 String msg = "Missing element under " + TAG + ": " + ATTR_FILTER +
95 " at " + parser.getPositionDescription();
96 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
97 XmlUtils.skipCurrentTag(parser);
98 }
99 }
100
Nicolas Prevot3f7777f2014-07-24 15:58:39 +0100101 String getStringFromXml(XmlPullParser parser, String attribute, String defaultValue) {
102 String value = parser.getAttributeValue(null, attribute);
103 if (value == null) {
104 String msg = "Missing element under " + TAG +": " + attribute + " at " +
105 parser.getPositionDescription();
106 PackageManagerService.reportSettingsProblem(Log.WARN, msg);
107 return defaultValue;
108 } else {
109 return value;
110 }
111 }
112
113 int getIntFromXml(XmlPullParser parser, String attribute, int defaultValue) {
114 String stringValue = getStringFromXml(parser, attribute, null);
115 if (stringValue != null) {
116 return Integer.parseInt(stringValue);
117 }
118 return defaultValue;
119 }
120
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000121 public void writeToXml(XmlSerializer serializer) throws IOException {
Nicolas Prevot81948992014-05-16 18:25:26 +0100122 serializer.attribute(null, ATTR_TARGET_USER_ID, Integer.toString(mTargetUserId));
Nicolas Prevot63798c52014-05-27 13:22:38 +0100123 serializer.attribute(null, ATTR_FLAGS, Integer.toString(mFlags));
Nicolas Prevot3f7777f2014-07-24 15:58:39 +0100124 serializer.attribute(null, ATTR_OWNER_PACKAGE, mOwnerPackage);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000125 serializer.startTag(null, ATTR_FILTER);
126 super.writeToXml(serializer);
127 serializer.endTag(null, ATTR_FILTER);
128 }
129
130 @Override
131 public String toString() {
Nicolas Prevot81948992014-05-16 18:25:26 +0100132 return "CrossProfileIntentFilter{0x" + Integer.toHexString(System.identityHashCode(this))
133 + " " + Integer.toString(mTargetUserId) + "}";
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000134 }
Nicolas Prevot3a1a0bb2014-10-22 16:24:00 +0100135
136 boolean equalsIgnoreFilter(CrossProfileIntentFilter other) {
137 return mTargetUserId == other.mTargetUserId
Nicolas Prevot3a1a0bb2014-10-22 16:24:00 +0100138 && mOwnerPackage.equals(other.mOwnerPackage)
139 && mFlags == other.mFlags;
140 }
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000141}