• 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 
17 package com.android.intentresolver
18 
19 import android.content.ContentProvider
20 import android.content.ContentValues
21 import android.database.Cursor
22 import android.net.Uri
23 
24 class TestContentProvider : ContentProvider() {
querynull25     override fun query(
26         uri: Uri,
27         projection: Array<out String>?,
28         selection: String?,
29         selectionArgs: Array<out String>?,
30         sortOrder: String?,
31     ): Cursor? = null
32 
33     override fun getType(uri: Uri): String? =
34         runCatching { uri.getQueryParameter(PARAM_MIME_TYPE) }.getOrNull()
35 
getStreamTypesnull36     override fun getStreamTypes(uri: Uri, mimeTypeFilter: String): Array<String>? {
37         val delay =
38             runCatching { uri.getQueryParameter(PARAM_STREAM_TYPE_TIMEOUT)?.toLong() ?: 0L }
39                 .getOrDefault(0L)
40         if (delay > 0) {
41             try {
42                 Thread.sleep(delay)
43             } catch (e: InterruptedException) {
44                 Thread.currentThread().interrupt()
45             }
46         }
47         return runCatching { uri.getQueryParameter(PARAM_STREAM_TYPE)?.split(",")?.toTypedArray() }
48             .getOrNull()
49     }
50 
insertnull51     override fun insert(uri: Uri, values: ContentValues?): Uri? = null
52 
53     override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
54 
55     override fun update(
56         uri: Uri,
57         values: ContentValues?,
58         selection: String?,
59         selectionArgs: Array<out String>?,
60     ): Int = 0
61 
62     override fun onCreate(): Boolean = true
63 
64     companion object {
65         const val PARAM_MIME_TYPE = "mimeType"
66         const val PARAM_STREAM_TYPE = "streamType"
67         const val PARAM_STREAM_TYPE_TIMEOUT = "streamTypeTo"
68 
69         @JvmStatic
70         @JvmOverloads
71         fun makeItemUri(
72             name: String,
73             mimeType: String?,
74             streamTypes: Array<String> = emptyArray(),
75             timeout: Long = 0L,
76         ): Uri =
77             Uri.parse("content://com.android.intentresolver.tests/$name")
78                 .buildUpon()
79                 .appendQueryParameter(PARAM_MIME_TYPE, mimeType)
80                 .apply {
81                     mimeType?.let { appendQueryParameter(PARAM_MIME_TYPE, it) }
82                     if (streamTypes.isNotEmpty()) {
83                         appendQueryParameter(PARAM_STREAM_TYPE, streamTypes.joinToString(","))
84                     }
85                     if (timeout > 0) {
86                         appendQueryParameter(PARAM_STREAM_TYPE_TIMEOUT, timeout.toString())
87                     }
88                 }
89                 .build()
90     }
91 }
92