Skip to content

Commit fe38ff9

Browse files
wangchaohuiAndroid (Google) Code Review
authored and
Android (Google) Code Review
committed
Merge "[Regulatory Labels] Load labels from overlays" into udc-d1-dev
2 parents 717ce2b + 590b4e7 commit fe38ff9

File tree

13 files changed

+312
-254
lines changed

13 files changed

+312
-254
lines changed

AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3759,6 +3759,7 @@
37593759
<!-- Show regulatory info (from settings item or dialing "*#07#") -->
37603760
<activity
37613761
android:name="RegulatoryInfoDisplayActivity"
3762+
android:theme="@style/Theme.AlertDialog"
37623763
android:label="@string/regulatory_labels"
37633764
android:exported="true"
37643765
android:enabled="@bool/config_show_regulatory_info">

res/values/config.xml

+3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@
345345
<bool name="config_show_manual">false</bool>
346346
<!-- Whether to show a preference item for regulatory information in About phone -->
347347
<bool name="config_show_regulatory_info">false</bool>
348+
<!-- Package name of regulatory information overlay which provides mapping and contents.
349+
Fetch resource from overlay package directly if this is set. -->
350+
<string name="config_regulatory_info_overlay_package_name" translatable="false" />
348351

349352
<!-- Whether to show a preference item for mobile plan -->
350353
<bool name="config_show_mobile_plan">true</bool>

src/com/android/settings/RegulatoryInfoDisplayActivity.java

-171
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.settings
18+
19+
import android.app.Activity
20+
import android.os.Bundle
21+
import android.view.Gravity
22+
import android.widget.ImageView
23+
import android.widget.TextView
24+
import androidx.appcompat.app.AlertDialog
25+
import com.android.settings.deviceinfo.regulatory.RegulatoryInfo.getRegulatoryInfo
26+
27+
/**
28+
* [Activity] that displays regulatory information for the "Regulatory information"
29+
* preference item, and when "*#07#" is dialed on the Phone keypad. To enable this feature,
30+
* set the "config_show_regulatory_info" boolean to true in a device overlay resource, and in the
31+
* same overlay, either add a drawable named "regulatory_info.png" containing a graphical version
32+
* of the required regulatory info (If ro.bootloader.hardware.sku property is set use
33+
* "regulatory_info_<sku>.png where sku is ro.bootloader.hardware.sku property value in lowercase"),
34+
* or add a string resource named "regulatory_info_text" with an HTML version of the required
35+
* information (text will be centered in the dialog).
36+
*/
37+
class RegulatoryInfoDisplayActivity : Activity() {
38+
39+
/** Display the regulatory info graphic in a dialog window. */
40+
override fun onCreate(savedInstanceState: Bundle?) {
41+
super.onCreate(savedInstanceState)
42+
val builder = AlertDialog.Builder(this)
43+
.setTitle(R.string.regulatory_labels)
44+
.setOnDismissListener { finish() } // close the activity
45+
.setPositiveButton(android.R.string.ok, null)
46+
47+
getRegulatoryInfo()?.let {
48+
val view = layoutInflater.inflate(R.layout.regulatory_info, null)
49+
val image = view.findViewById<ImageView>(R.id.regulatoryInfo)
50+
image.setImageDrawable(it)
51+
builder.setView(view)
52+
builder.show()
53+
return
54+
}
55+
56+
val regulatoryText = resources.getText(R.string.regulatory_info_text)
57+
if (regulatoryText.isNotEmpty()) {
58+
builder.setMessage(regulatoryText)
59+
val dialog = builder.show()
60+
// we have to show the dialog first, or the setGravity() call will throw a NPE
61+
dialog.findViewById<TextView>(android.R.id.message)?.gravity = Gravity.CENTER
62+
} else {
63+
// neither drawable nor text resource exists, finish activity
64+
finish()
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.settings.deviceinfo.regulatory
18+
19+
import android.content.Context
20+
import android.content.res.Resources
21+
import android.graphics.drawable.Drawable
22+
import android.os.SystemProperties
23+
import androidx.annotation.DrawableRes
24+
import androidx.annotation.VisibleForTesting
25+
import com.android.settings.R
26+
27+
28+
29+
/** To load Regulatory Info from device. */
30+
object RegulatoryInfo {
31+
private const val REGULATORY_INFO_RESOURCE = "regulatory_info"
32+
33+
@VisibleForTesting
34+
const val KEY_COO = "ro.boot.hardware.coo"
35+
36+
@VisibleForTesting
37+
const val KEY_SKU = "ro.boot.hardware.sku"
38+
39+
/** Gets the regulatory drawable. */
40+
fun Context.getRegulatoryInfo(): Drawable? {
41+
val sku = getSku()
42+
if (sku.isNotBlank()) {
43+
// When hardware coo property exists, use regulatory_info_<sku>_<coo> resource if valid.
44+
val coo = getCoo()
45+
if (coo.isNotBlank()) {
46+
getRegulatoryInfo("${REGULATORY_INFO_RESOURCE}_${sku}_$coo")?.let { return it }
47+
}
48+
// Use regulatory_info_<sku> resource if valid.
49+
getRegulatoryInfo("${REGULATORY_INFO_RESOURCE}_$sku")?.let { return it }
50+
}
51+
return getRegulatoryInfo(REGULATORY_INFO_RESOURCE)
52+
}
53+
54+
private fun getCoo(): String = SystemProperties.get(KEY_COO).lowercase()
55+
56+
private fun getSku(): String = SystemProperties.get(KEY_SKU).lowercase()
57+
58+
private fun Context.getRegulatoryInfo(fileName: String): Drawable? {
59+
val overlayPackageName =
60+
resources.getString(R.string.config_regulatory_info_overlay_package_name)
61+
.ifBlank { packageName }
62+
val resources = packageManager.getResourcesForApplication(overlayPackageName)
63+
val id = resources.getIdentifier(fileName, "drawable", overlayPackageName)
64+
return if (id > 0) resources.getRegulatoryInfo(id) else null
65+
}
66+
67+
private fun Resources.getRegulatoryInfo(@DrawableRes resId: Int): Drawable? = try {
68+
getDrawable(resId, null).takeIf {
69+
// Ignore the placeholder image
70+
it.intrinsicWidth > 10 && it.intrinsicHeight > 10
71+
}
72+
} catch (_: Resources.NotFoundException) {
73+
null
74+
}
75+
}
-159 Bytes
Binary file not shown.
-159 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)