• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @file:JvmName("UriFilters")
17 
18 package com.android.intentresolver.util
19 
20 import android.content.ContentProvider.getUserIdFromUri
21 import android.content.ContentResolver.SCHEME_CONTENT
22 import android.graphics.drawable.Icon
23 import android.graphics.drawable.Icon.TYPE_URI
24 import android.graphics.drawable.Icon.TYPE_URI_ADAPTIVE_BITMAP
25 import android.net.Uri
26 import android.os.UserHandle
27 import android.service.chooser.ChooserAction
28 
29 /**
30  * Checks if the [Uri] is a `content://` uri which references the current user (from process uid).
31  *
32  * MediaStore interprets the user field of a content:// URI as a UserId and applies it if the caller
33  * holds INTERACT_ACROSS_USERS permission. (Example: `content://10@media/images/1234`)
34  *
35  * No URI content should be loaded unless it passes this check since the caller would not have
36  * permission to read it.
37  *
38  * @return false if this is a content:// [Uri] which references another user
39  */
40 val Uri?.ownedByCurrentUser: Boolean
41     @JvmName("isOwnedByCurrentUser")
42     get() =
<lambda>null43         this?.let {
44             when (getUserIdFromUri(this, UserHandle.USER_CURRENT)) {
45                 UserHandle.USER_CURRENT,
46                 UserHandle.myUserId() -> true
47                 else -> false
48             }
49         } == true
50 
51 /** Does the [Uri] reference a content provider ('content://')? */
52 internal val Uri.contentScheme: Boolean
53     get() = scheme == SCHEME_CONTENT
54 
55 /**
56  * Checks if the Icon of a [ChooserAction] backed by content:// [Uri] is safe for display.
57  *
58  * @param action the chooser action
59  * @see [Uri.ownedByCurrentUser]
60  */
hasValidIconnull61 fun hasValidIcon(action: ChooserAction) = hasValidIcon(action.icon)
62 
63 /**
64  * Checks if the Icon backed by content:// [Uri] is safe for display.
65  *
66  * @see [Uri.ownedByCurrentUser]
67  */
68 fun hasValidIcon(icon: Icon) =
69     with(icon) {
70         when (type) {
71             TYPE_URI,
72             TYPE_URI_ADAPTIVE_BITMAP -> !uri.contentScheme || uri.ownedByCurrentUser
73             else -> true
74         }
75     }
76