1 /* <lambda>null2 * 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.launcher3.util 18 19 import android.content.Context 20 import android.content.res.TypedArray 21 import android.util.AttributeSet 22 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation 23 import com.android.launcher3.R 24 import kotlin.IntArray 25 26 class TestResourceHelper(private val context: Context, specsFileId: Int) : 27 ResourceHelper(context, specsFileId) { 28 29 val responsiveStyleables = listOf( 30 R.styleable.SizeSpec, 31 R.styleable.WorkspaceSpec, 32 R.styleable.FolderSpec, 33 R.styleable.AllAppsSpec, 34 R.styleable.ResponsiveSpecGroup 35 ) 36 37 override fun obtainStyledAttributes(attrs: AttributeSet, styleId: IntArray): TypedArray { 38 val clone = 39 if (responsiveStyleables.any { styleId.contentEquals(it) }) { 40 convertStyleId(styleId) 41 } else { 42 styleId.clone() 43 } 44 45 return context.obtainStyledAttributes(attrs, clone) 46 } 47 48 private fun convertStyleId(styleableArr: IntArray): IntArray { 49 val targetContextRes = getInstrumentation().targetContext.resources 50 val context = getInstrumentation().context 51 return styleableArr 52 .map { attrId -> targetContextRes.getResourceName(attrId).split(":").last() } 53 .map { attrName -> 54 // Get required attr from context instead of targetContext 55 context.resources.getIdentifier(attrName, null, context.packageName) 56 } 57 .toIntArray() 58 } 59 } 60