1 /* 2 * Copyright (C) 2018 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 com.android.packageinstaller.role.model; 18 19 import android.content.Intent; 20 import android.content.IntentFilter; 21 import android.net.Uri; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 26 import java.util.List; 27 import java.util.Objects; 28 29 /** 30 * Specifies an {@code Intent} or an {@code IntentFilter} for matching application components. 31 */ 32 public class IntentFilterData { 33 34 /** 35 * The action of this {@code Intent} or {@code IntentFilter} specification. Exactly one action 36 * is required so that we can create a single {@code Intent} with it. 37 */ 38 @NonNull 39 private final String mAction; 40 41 /** 42 * The categories of the {@code Intent} or {@code IntentFilter} specification. Should not 43 * contain {@link Intent#CATEGORY_DEFAULT} as it should be automatically added when used for 44 * activities. 45 */ 46 @NonNull 47 private final List<String> mCategories; 48 49 /** 50 * Optional data scheme of the {@code Intent} or {@code IntentFilter} specification. At most one 51 * data scheme is supported so that we can create a single {@code Intent} with it. 52 */ 53 @Nullable 54 private final String mDataScheme; 55 56 /** 57 * Optional data type of the {@code Intent} or {@code IntentFilter} specification. At most one 58 * data type is supported so that we can create a single {@code Intent} with it. 59 */ 60 @Nullable 61 private final String mDataType; 62 IntentFilterData(@onNull String action, @NonNull List<String> categories, @Nullable String dataScheme, @Nullable String dataType)63 public IntentFilterData(@NonNull String action, @NonNull List<String> categories, 64 @Nullable String dataScheme, @Nullable String dataType) { 65 mAction = action; 66 mCategories = categories; 67 mDataScheme = dataScheme; 68 mDataType = dataType; 69 } 70 71 @NonNull getAction()72 public String getAction() { 73 return mAction; 74 } 75 76 @NonNull getCategories()77 public List<String> getCategories() { 78 return mCategories; 79 } 80 81 @Nullable getDataScheme()82 public String getDataScheme() { 83 return mDataScheme; 84 } 85 86 @Nullable getDataType()87 public String getDataType() { 88 return mDataType; 89 } 90 91 /** 92 * Create an {@code Intent} with this specification. 93 * 94 * @return the {@code Intent} created 95 */ 96 @NonNull createIntent()97 public Intent createIntent() { 98 Intent intent = new Intent(mAction); 99 Uri data = mDataScheme != null ? Uri.fromParts(mDataScheme, "", null) : null; 100 int categoriesSize = mCategories.size(); 101 for (int i = 0; i < categoriesSize; i++) { 102 String category = mCategories.get(i); 103 intent.addCategory(category); 104 } 105 intent.setDataAndType(data, mDataType); 106 return intent; 107 } 108 109 /** 110 * Create an {@code IntentFilter} with this specification. 111 * 112 * @return the {@code IntentFilter} created 113 */ 114 @NonNull createIntentFilter()115 public IntentFilter createIntentFilter() { 116 IntentFilter intentFilter = new IntentFilter(mAction); 117 int categoriesSize = mCategories.size(); 118 for (int i = 0; i < categoriesSize; i++) { 119 String category = mCategories.get(i); 120 intentFilter.addCategory(category); 121 } 122 if (mDataScheme != null) { 123 intentFilter.addDataScheme(mDataScheme); 124 } 125 if (mDataType != null) { 126 try { 127 intentFilter.addDataType(mDataType); 128 } catch (IntentFilter.MalformedMimeTypeException e) { 129 // Should have been validated when parsing roles. 130 throw new IllegalStateException(e); 131 } 132 } 133 return intentFilter; 134 } 135 136 @Override toString()137 public String toString() { 138 return "IntentFilterData{" 139 + "mAction='" + mAction + '\'' 140 + ", mCategories='" + mCategories + '\'' 141 + ", mDataScheme='" + mDataScheme + '\'' 142 + ", mDataType='" + mDataType + '\'' 143 + '}'; 144 } 145 146 @Override equals(Object object)147 public boolean equals(Object object) { 148 if (this == object) { 149 return true; 150 } 151 if (object == null || getClass() != object.getClass()) { 152 return false; 153 } 154 IntentFilterData that = (IntentFilterData) object; 155 return Objects.equals(mAction, that.mAction) 156 && Objects.equals(mCategories, that.mCategories) 157 && Objects.equals(mDataScheme, that.mDataScheme) 158 && Objects.equals(mDataType, that.mDataType); 159 } 160 161 @Override hashCode()162 public int hashCode() { 163 return Objects.hash(mAction, mCategories, mDataScheme, mDataType); 164 } 165 } 166