blob: 20aafc2194f012982263cb2cdd396ecdd59401c5 [file] [log] [blame]
Jake Hamby5e9bd862013-04-09 15:48:25 -07001/*
2 * Copyright (C) 2013 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
17package com.android.settings;
18
19import android.app.Activity;
Jake Hamby5e9bd862013-04-09 15:48:25 -070020import android.content.DialogInterface;
21import android.content.res.Resources;
Fan Zhang1bc7ac32017-06-20 14:53:49 -070022import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
Jake Hamby5e9bd862013-04-09 15:48:25 -070024import android.graphics.drawable.Drawable;
25import android.os.Bundle;
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070026import android.os.SystemProperties;
27import android.text.TextUtils;
Jake Hamby5e9bd862013-04-09 15:48:25 -070028import android.view.Gravity;
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070029import android.view.View;
30import android.widget.ImageView;
Jake Hamby5e9bd862013-04-09 15:48:25 -070031import android.widget.TextView;
32
Fan Zhangc7162cd2018-06-18 15:21:41 -070033import androidx.annotation.VisibleForTesting;
tmfang41ab6b42018-07-17 13:53:04 +080034import androidx.appcompat.app.AlertDialog;
Fan Zhangc7162cd2018-06-18 15:21:41 -070035
Fan Zhang23f8d592018-08-28 15:11:40 -070036import java.util.Locale;
37
Jake Hamby5e9bd862013-04-09 15:48:25 -070038/**
39 * {@link Activity} that displays regulatory information for the "Regulatory information"
40 * preference item, and when "*#07#" is dialed on the Phone keypad. To enable this feature,
41 * set the "config_show_regulatory_info" boolean to true in a device overlay resource, and in the
42 * same overlay, either add a drawable named "regulatory_info.png" containing a graphical version
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070043 * of the required regulatory info (If ro.bootloader.hardware.sku property is set use
44 * "regulatory_info_<sku>.png where sku is ro.bootloader.hardware.sku property value in lowercase"),
45 * or add a string resource named "regulatory_info_text" with an HTML version of the required
46 * information (text will be centered in the dialog).
Jake Hamby5e9bd862013-04-09 15:48:25 -070047 */
48public class RegulatoryInfoDisplayActivity extends Activity implements
49 DialogInterface.OnDismissListener {
Fan Zhang1bc7ac32017-06-20 14:53:49 -070050
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070051 private final String REGULATORY_INFO_RESOURCE = "regulatory_info";
Fan Zhang1bc7ac32017-06-20 14:53:49 -070052 private static final String DEFAULT_REGULATORY_INFO_FILEPATH =
53 "/data/misc/elabel/regulatory_info.png";
54 private static final String REGULATORY_INFO_FILEPATH_TEMPLATE =
55 "/data/misc/elabel/regulatory_info_%s.png";
Jake Hamby5e9bd862013-04-09 15:48:25 -070056
57 /**
58 * Display the regulatory info graphic in a dialog window.
59 */
60 @Override
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
Jake Hamby5e9bd862013-04-09 15:48:25 -070063 AlertDialog.Builder builder = new AlertDialog.Builder(this)
Andrew Sapperstein1fad9af2016-05-31 10:34:01 -070064 .setTitle(R.string.regulatory_labels)
Jake Hamby5e9bd862013-04-09 15:48:25 -070065 .setOnDismissListener(this);
66
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070067 boolean regulatoryInfoDrawableExists = false;
Fan Zhang1bc7ac32017-06-20 14:53:49 -070068
69 final String regulatoryInfoFile = getRegulatoryInfoImageFileName();
70 final Bitmap regulatoryInfoBitmap = BitmapFactory.decodeFile(regulatoryInfoFile);
71
72 if (regulatoryInfoBitmap != null) {
73 regulatoryInfoDrawableExists = true;
74 }
75
76 int resId = 0;
77 if (!regulatoryInfoDrawableExists) {
78 resId = getResourceId();
79 }
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070080 if (resId != 0) {
81 try {
Alan Viverette0ba89bd2014-10-10 10:58:58 -070082 Drawable d = getDrawable(resId);
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070083 // set to false if the width or height is <= 2
84 // (missing PNG can return an empty 2x2 pixel Drawable)
85 regulatoryInfoDrawableExists = (d.getIntrinsicWidth() > 2
86 && d.getIntrinsicHeight() > 2);
87 } catch (Resources.NotFoundException ignored) {
88 regulatoryInfoDrawableExists = false;
89 }
Jake Hamby5e9bd862013-04-09 15:48:25 -070090 }
91
Ryan Mitchell4de772f2019-10-08 15:58:22 -070092 CharSequence regulatoryText = getResources()
93 .getText(R.string.regulatory_info_text);
Jake Hamby5e9bd862013-04-09 15:48:25 -070094
95 if (regulatoryInfoDrawableExists) {
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070096 View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
Fan Zhang1bc7ac32017-06-20 14:53:49 -070097 ImageView image = view.findViewById(R.id.regulatoryInfo);
98 if (regulatoryInfoBitmap != null) {
99 image.setImageBitmap(regulatoryInfoBitmap);
100 } else {
101 image.setImageResource(resId);
102 }
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700103 builder.setView(view);
Jake Hamby5e9bd862013-04-09 15:48:25 -0700104 builder.show();
105 } else if (regulatoryText.length() > 0) {
106 builder.setMessage(regulatoryText);
107 AlertDialog dialog = builder.show();
108 // we have to show the dialog first, or the setGravity() call will throw a NPE
109 TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
110 messageText.setGravity(Gravity.CENTER);
111 } else {
112 // neither drawable nor text resource exists, finish activity
113 finish();
114 }
115 }
116
Yanting Yang49b1bc12019-07-19 01:46:36 +0800117 @VisibleForTesting
118 int getResourceId() {
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700119 // Use regulatory_info by default.
120 int resId = getResources().getIdentifier(
121 REGULATORY_INFO_RESOURCE, "drawable", getPackageName());
122
123 // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700124 final String sku = getSku();
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700125 if (!TextUtils.isEmpty(sku)) {
126 String regulatory_info_res = REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase();
127 int id = getResources().getIdentifier(
128 regulatory_info_res, "drawable", getPackageName());
129 if (id != 0) {
130 resId = id;
131 }
132 }
Yanting Yang49b1bc12019-07-19 01:46:36 +0800133
134 // When hardware coo property exists, use regulatory_info_<sku>_<coo> resource if valid.
135 final String coo = getCoo();
136 if (!TextUtils.isEmpty(coo) && !TextUtils.isEmpty(sku)) {
137 final String regulatory_info_coo_res =
138 REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase() + "_" + coo.toLowerCase();
139 final int id = getResources().getIdentifier(
140 regulatory_info_coo_res, "drawable", getPackageName());
141 if (id != 0) {
142 resId = id;
143 }
144 }
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700145 return resId;
146 }
147
Jake Hamby5e9bd862013-04-09 15:48:25 -0700148 @Override
149 public void onDismiss(DialogInterface dialog) {
150 finish(); // close the activity
151 }
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700152
Yanting Yang49b1bc12019-07-19 01:46:36 +0800153 private String getCoo() {
154 return SystemProperties.get("ro.boot.hardware.coo", "");
155 }
156
157 private String getSku() {
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700158 return SystemProperties.get("ro.boot.hardware.sku", "");
159 }
160
Yanting Yang49b1bc12019-07-19 01:46:36 +0800161 private String getRegulatoryInfoImageFileName() {
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700162 final String sku = getSku();
163 if (TextUtils.isEmpty(sku)) {
164 return DEFAULT_REGULATORY_INFO_FILEPATH;
165 } else {
166 return String.format(Locale.US, REGULATORY_INFO_FILEPATH_TEMPLATE,
167 sku.toLowerCase());
168 }
169 }
Jake Hamby5e9bd862013-04-09 15:48:25 -0700170}