1 /*
2  * Copyright 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 platform.test.screenshot
17 
18 import android.content.Context
19 import android.content.res.loader.ResourcesLoader
20 import android.content.res.loader.ResourcesProvider
21 import android.os.ParcelFileDescriptor
22 import android.system.Os
23 import android.util.SparseIntArray
24 import android.widget.RemoteViews
25 import com.google.common.io.ByteStreams
26 import java.io.File
27 import java.io.FileDescriptor
28 import java.io.FileOutputStream
29 import java.io.InputStream
30 import java.io.OutputStream
31 
32 private const val FIRST_RESOURCE_COLOR_ID = android.R.color.system_neutral1_0
33 private const val LAST_RESOURCE_COLOR_ID = android.R.color.system_accent3_1000
34 private const val ARSC_ENTRY_SIZE = 16
35 private const val PLACEHOLDER_GOOGLE_SANS = "plh-go-sans"
36 private const val PLACEHOLDER_GOOGLE_SANS_TEXT = "plh-go-sans-text"
37 private const val FONT_GOOGLE_SANS = "google-sans"
38 private const val FONT_GOOGLE_SANS_TEXT = "google-sans-text"
39 
40 /** Creates and applies a resources provider for Google fonts to a context. */
createAndApplyResourcesProvidernull41 fun createAndApplyResourcesProvider(context: Context) {
42     try {
43         val arscPath = System.getProperty("arsc.file.path")
44         val oldContents = File(arscPath).readBytes()
45         val contentBytes = compiledResourcesContentForGoogleFonts(oldContents)
46         if (contentBytes == null) {
47             return
48         }
49 
50         var arscFile: FileDescriptor? = null
51         var pfd: ParcelFileDescriptor? = null
52         try {
53             arscFile = Os.memfd_create("font_resources.arsc", /* flags= */ 0)
54             // Note: This must not be closed through the OutputStream.
55             val pipeWriter = FileOutputStream(arscFile)
56             pipeWriter.write(contentBytes);
57 
58             pfd = ParcelFileDescriptor.dup(arscFile)
59             val colorsLoader = ResourcesLoader()
60             colorsLoader.addProvider(
61                 ResourcesProvider.loadFromTable(pfd, /* assetsProvider= */ null))
62             context.resources.addLoaders(colorsLoader)
63         } finally {
64             if (arscFile != null) {
65                 Os.close(arscFile)
66             }
67             if (pfd != null && pfd.getFileDescriptor() != null && pfd.getFileDescriptor().valid()) {
68                 pfd.close()
69             }
70         }
71     } catch (ex: Exception) {
72         println("Failed to setup the context for fonts: ${ex}")
73     }
74 }
75 
76 /** Manipulates the compiled resources content manipulated for Google fonts. */
compiledResourcesContentForGoogleFontsnull77 private fun compiledResourcesContentForGoogleFonts(oldContent: ByteArray?): ByteArray? {
78     if (oldContent == null) {
79         return null
80     }
81     val newContent = oldContent.copyOf()
82     stringReplaceOnce(newContent, PLACEHOLDER_GOOGLE_SANS_TEXT, FONT_GOOGLE_SANS_TEXT)
83     stringReplaceOnce(newContent, PLACEHOLDER_GOOGLE_SANS, FONT_GOOGLE_SANS)
84     return newContent
85 }
86 
87 /** Replaces the first occurrence of `fromValue` to `toValue`. */
stringReplaceOncenull88 private fun stringReplaceOnce(contents: ByteArray, fromValue: String, toValue: String) {
89     if (fromValue.length != toValue.length) { return }
90     for (i in 0..<contents.size) {
91         if (i + fromValue.length > contents.size) { return }
92         var isEqual: Boolean = true
93         for (j in 0..<fromValue.length) {
94             if (contents[i + j] != fromValue.get(j).toByte()) {
95                 isEqual = false
96                 break
97             }
98         }
99         if (isEqual) {
100             for (j in 0..<toValue.length) {
101                 contents[i + j] = toValue.get(j).toByte();
102             }
103             break
104         }
105     }
106 }
107