1 /* 2 * Copyright (C) 2023 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 package com.android.intentresolver.validation.types 17 18 import android.content.Intent 19 import android.net.Uri 20 import com.android.intentresolver.validation.Importance 21 import com.android.intentresolver.validation.Invalid 22 import com.android.intentresolver.validation.NoValue 23 import com.android.intentresolver.validation.Valid 24 import com.android.intentresolver.validation.ValidationResult 25 import com.android.intentresolver.validation.Validator 26 import com.android.intentresolver.validation.ValueIsWrongType 27 28 class IntentOrUri(override val key: String) : Validator<Intent> { 29 validatenull30 override fun validate( 31 source: (String) -> Any?, 32 importance: Importance 33 ): ValidationResult<Intent> { 34 return when (val value = source(key)) { 35 // An intent, return it. 36 is Intent -> Valid(value) 37 38 // A Uri was supplied. 39 // Unfortunately, converting Uri -> Intent requires a toString(). 40 is Uri -> Valid(Intent.parseUri(value.toString(), Intent.URI_INTENT_SCHEME)) 41 42 // No value present. 43 null -> 44 when (importance) { 45 Importance.WARNING -> Invalid() // No warnings if optional, but missing 46 Importance.CRITICAL -> Invalid(NoValue(key, importance, Intent::class)) 47 } 48 49 // Some other type. 50 else -> { 51 return Invalid( 52 ValueIsWrongType( 53 key, 54 importance, 55 actualType = value::class, 56 allowedTypes = listOf(Intent::class, Uri::class) 57 ) 58 ) 59 } 60 } 61 } 62 } 63