• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.ext
17 
18 import android.content.Intent
19 import java.util.function.Predicate
20 
21 /** Applies an operation on this Intent if matches the given filter. */
ifMatchnull22 inline fun Intent.ifMatch(
23     predicate: Predicate<Intent>,
24     crossinline block: Intent.() -> Unit
25 ): Intent {
26     if (predicate.test(this)) {
27         apply(block)
28     }
29     return this
30 }
31 
32 /** True if the Intent has one of the specified actions. */
hasActionnull33 fun Intent.hasAction(vararg actions: String): Boolean = action in actions
34 
35 /** True if the Intent has a specific component target */
36 fun Intent.hasComponent(): Boolean = (component != null)
37 
38 /** True if the Intent has a single matching category. */
39 fun Intent.hasSingleCategory(category: String) = categories.singleOrNull() == category
40 
41 /** True if the Intent is a SEND or SEND_MULTIPLE action. */
42 fun Intent.hasSendAction() = hasAction(Intent.ACTION_SEND, Intent.ACTION_SEND_MULTIPLE)
43 
44 /** True if the Intent resolves to the special Home (Launcher) component */
45 fun Intent.isHomeIntent() = hasAction(Intent.ACTION_MAIN) && hasSingleCategory(Intent.CATEGORY_HOME)
46