Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package android.content.pm; |
| 18 | |
| 19 | import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED; |
| 20 | import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK; |
| 21 | import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS; |
Christopher Tate | 56f0ff3 | 2015-08-13 16:29:33 -0700 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER; |
| 24 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
| 28 | import android.text.TextUtils; |
Fabrice Di Meglio | 0788595 | 2015-04-06 19:41:28 -0700 | [diff] [blame] | 29 | import android.util.ArraySet; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 30 | import android.util.Log; |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 31 | |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 32 | import com.android.internal.util.XmlUtils; |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 33 | |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 34 | import org.xmlpull.v1.XmlPullParser; |
| 35 | import org.xmlpull.v1.XmlPullParserException; |
| 36 | import org.xmlpull.v1.XmlSerializer; |
| 37 | |
| 38 | import java.io.IOException; |
| 39 | import java.util.ArrayList; |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 40 | import java.util.Set; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * The {@link com.android.server.pm.PackageManagerService} maintains some |
Fabrice Di Meglio | 7d014ce | 2015-04-08 16:17:46 -0700 | [diff] [blame] | 44 | * {@link IntentFilterVerificationInfo}s for each domain / package name. |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 45 | * |
| 46 | * @hide |
| 47 | */ |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 48 | @SystemApi |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 49 | public final class IntentFilterVerificationInfo implements Parcelable { |
| 50 | private static final String TAG = IntentFilterVerificationInfo.class.getName(); |
| 51 | |
| 52 | private static final String TAG_DOMAIN = "domain"; |
| 53 | private static final String ATTR_DOMAIN_NAME = "name"; |
| 54 | private static final String ATTR_PACKAGE_NAME = "packageName"; |
| 55 | private static final String ATTR_STATUS = "status"; |
| 56 | |
Fabrice Di Meglio | 1de3f0d | 2015-04-29 19:42:41 -0700 | [diff] [blame] | 57 | private ArraySet<String> mDomains = new ArraySet<>(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 58 | private String mPackageName; |
| 59 | private int mMainStatus; |
| 60 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 61 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 62 | public IntentFilterVerificationInfo() { |
| 63 | mPackageName = null; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 64 | mMainStatus = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED; |
| 65 | } |
| 66 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 67 | /** @hide */ |
Todd Kennedy | 788c842 | 2016-08-10 10:52:34 -0700 | [diff] [blame] | 68 | public IntentFilterVerificationInfo(String packageName, ArraySet<String> domains) { |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 69 | mPackageName = packageName; |
Todd Kennedy | 788c842 | 2016-08-10 10:52:34 -0700 | [diff] [blame] | 70 | mDomains = domains; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 71 | mMainStatus = INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED; |
| 72 | } |
| 73 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 74 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 75 | public IntentFilterVerificationInfo(XmlPullParser parser) |
| 76 | throws IOException, XmlPullParserException { |
| 77 | readFromXml(parser); |
| 78 | } |
| 79 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 80 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 81 | public IntentFilterVerificationInfo(Parcel source) { |
| 82 | readFromParcel(source); |
| 83 | } |
| 84 | |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 85 | public String getPackageName() { |
| 86 | return mPackageName; |
| 87 | } |
| 88 | |
| 89 | public int getStatus() { |
| 90 | return mMainStatus; |
| 91 | } |
| 92 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 93 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 94 | public void setStatus(int s) { |
| 95 | if (s >= INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED && |
| 96 | s <= INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER) { |
| 97 | mMainStatus = s; |
| 98 | } else { |
| 99 | Log.w(TAG, "Trying to set a non supported status: " + s); |
| 100 | } |
| 101 | } |
| 102 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 103 | public Set<String> getDomains() { |
Fabrice Di Meglio | 1de3f0d | 2015-04-29 19:42:41 -0700 | [diff] [blame] | 104 | return mDomains; |
| 105 | } |
| 106 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 107 | /** @hide */ |
Todd Kennedy | 788c842 | 2016-08-10 10:52:34 -0700 | [diff] [blame] | 108 | public void setDomains(ArraySet<String> list) { |
| 109 | mDomains = list; |
Fabrice Di Meglio | 1de3f0d | 2015-04-29 19:42:41 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 112 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 113 | public String getDomainsString() { |
| 114 | StringBuilder sb = new StringBuilder(); |
| 115 | for (String str : mDomains) { |
| 116 | if (sb.length() > 0) { |
| 117 | sb.append(" "); |
| 118 | } |
| 119 | sb.append(str); |
| 120 | } |
| 121 | return sb.toString(); |
| 122 | } |
| 123 | |
| 124 | String getStringFromXml(XmlPullParser parser, String attribute, String defaultValue) { |
| 125 | String value = parser.getAttributeValue(null, attribute); |
| 126 | if (value == null) { |
| 127 | String msg = "Missing element under " + TAG +": " + attribute + " at " + |
| 128 | parser.getPositionDescription(); |
| 129 | Log.w(TAG, msg); |
| 130 | return defaultValue; |
| 131 | } else { |
| 132 | return value; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | int getIntFromXml(XmlPullParser parser, String attribute, int defaultValue) { |
| 137 | String value = parser.getAttributeValue(null, attribute); |
| 138 | if (TextUtils.isEmpty(value)) { |
| 139 | String msg = "Missing element under " + TAG +": " + attribute + " at " + |
| 140 | parser.getPositionDescription(); |
| 141 | Log.w(TAG, msg); |
| 142 | return defaultValue; |
| 143 | } else { |
| 144 | return Integer.parseInt(value); |
| 145 | } |
| 146 | } |
| 147 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 148 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 149 | public void readFromXml(XmlPullParser parser) throws XmlPullParserException, |
| 150 | IOException { |
| 151 | mPackageName = getStringFromXml(parser, ATTR_PACKAGE_NAME, null); |
| 152 | if (mPackageName == null) { |
| 153 | Log.e(TAG, "Package name cannot be null!"); |
| 154 | } |
| 155 | int status = getIntFromXml(parser, ATTR_STATUS, -1); |
| 156 | if (status == -1) { |
| 157 | Log.e(TAG, "Unknown status value: " + status); |
| 158 | } |
| 159 | mMainStatus = status; |
| 160 | |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 161 | int outerDepth = parser.getDepth(); |
| 162 | int type; |
| 163 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 164 | && (type != XmlPullParser.END_TAG |
| 165 | || parser.getDepth() > outerDepth)) { |
| 166 | if (type == XmlPullParser.END_TAG |
| 167 | || type == XmlPullParser.TEXT) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | String tagName = parser.getName(); |
| 172 | if (tagName.equals(TAG_DOMAIN)) { |
| 173 | String name = getStringFromXml(parser, ATTR_DOMAIN_NAME, null); |
| 174 | if (!TextUtils.isEmpty(name)) { |
Fabrice Di Meglio | 0788595 | 2015-04-06 19:41:28 -0700 | [diff] [blame] | 175 | mDomains.add(name); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 176 | } |
| 177 | } else { |
| 178 | Log.w(TAG, "Unknown tag parsing IntentFilter: " + tagName); |
| 179 | } |
| 180 | XmlUtils.skipCurrentTag(parser); |
| 181 | } |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 184 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 185 | public void writeToXml(XmlSerializer serializer) throws IOException { |
| 186 | serializer.attribute(null, ATTR_PACKAGE_NAME, mPackageName); |
| 187 | serializer.attribute(null, ATTR_STATUS, String.valueOf(mMainStatus)); |
| 188 | for (String str : mDomains) { |
| 189 | serializer.startTag(null, TAG_DOMAIN); |
| 190 | serializer.attribute(null, ATTR_DOMAIN_NAME, str); |
| 191 | serializer.endTag(null, TAG_DOMAIN); |
| 192 | } |
| 193 | } |
| 194 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 195 | /** @hide */ |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 196 | public String getStatusString() { |
Christopher Tate | 3decab6 | 2016-12-15 14:18:15 -0800 | [diff] [blame] | 197 | return getStatusStringFromValue(((long)mMainStatus) << 32); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Jeff Schumacher | 6fd90ed | 2017-01-20 13:58:34 -0800 | [diff] [blame] | 200 | /** @hide */ |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 201 | public static String getStatusStringFromValue(long val) { |
| 202 | StringBuilder sb = new StringBuilder(); |
| 203 | switch ((int)(val >> 32)) { |
| 204 | case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS: |
| 205 | sb.append("always : "); |
| 206 | sb.append(Long.toHexString(val & 0x00000000FFFFFFFF)); |
| 207 | break; |
| 208 | |
| 209 | case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK: |
| 210 | sb.append("ask"); |
| 211 | break; |
| 212 | |
| 213 | case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER: |
| 214 | sb.append("never"); |
| 215 | break; |
| 216 | |
Christopher Tate | 56f0ff3 | 2015-08-13 16:29:33 -0700 | [diff] [blame] | 217 | case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS_ASK: |
| 218 | sb.append("always-ask"); |
| 219 | break; |
| 220 | |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 221 | case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED: |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 222 | default: |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 223 | sb.append("undefined"); |
| 224 | break; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 225 | } |
Christopher Tate | f0d6cb3 | 2015-07-10 17:44:53 -0700 | [diff] [blame] | 226 | return sb.toString(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public int describeContents() { |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | private void readFromParcel(Parcel source) { |
| 235 | mPackageName = source.readString(); |
| 236 | mMainStatus = source.readInt(); |
Fabrice Di Meglio | 1de3f0d | 2015-04-29 19:42:41 -0700 | [diff] [blame] | 237 | ArrayList<String> list = new ArrayList<>(); |
| 238 | source.readStringList(list); |
| 239 | mDomains.addAll(list); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void writeToParcel(Parcel dest, int flags) { |
| 244 | dest.writeString(mPackageName); |
| 245 | dest.writeInt(mMainStatus); |
Fabrice Di Meglio | 1de3f0d | 2015-04-29 19:42:41 -0700 | [diff] [blame] | 246 | dest.writeStringList(new ArrayList<>(mDomains)); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 249 | public static final @android.annotation.NonNull Creator<IntentFilterVerificationInfo> CREATOR = |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 250 | new Creator<IntentFilterVerificationInfo>() { |
| 251 | public IntentFilterVerificationInfo createFromParcel(Parcel source) { |
| 252 | return new IntentFilterVerificationInfo(source); |
| 253 | } |
| 254 | public IntentFilterVerificationInfo[] newArray(int size) { |
| 255 | return new IntentFilterVerificationInfo[size]; |
| 256 | } |
| 257 | }; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 258 | } |