1 /*
<lambda>null2  * Copyright (C) 2017 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.managedprovisioning.testcommon
17 
18 import android.content.ContentResolver
19 import android.content.Context
20 import android.net.Uri
21 import com.android.managedprovisioning.common.StoreUtils
22 import java.io.ByteArrayOutputStream
23 import java.io.File
24 import java.io.IOException
25 
26 object TestUtils {
27     @JvmStatic
28     fun resourceToUri(context: Context, resID: Int): Uri {
29         return Uri.parse(
30             ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
31                     + context.resources.getResourcePackageName(resID) + '/'
32                     + context.resources.getResourceTypeName(resID) + '/'
33                     + context.resources.getResourceEntryName(resID)
34         )
35     }
36 
37     @JvmStatic
38     @Throws(IOException::class)
39     fun stringFromUri(cr: ContentResolver, uri: Uri): String =
40         cr.openInputStream(uri).use { iStream ->
41             ByteArrayOutputStream().use { oStream ->
42                 StoreUtils.copyStream(iStream, oStream)
43                 oStream.toString()
44             }
45         }
46 
47     @JvmStatic
48     fun deleteRecursive(fileOrDirectory: File) {
49         fileOrDirectory.deleteRecursively()
50     }
51 }
52