blob: 043071c9edbd9b392e316ae2dbf47a8e0cfe44da [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;
20import android.app.AlertDialog;
21import android.content.DialogInterface;
22import android.content.res.Resources;
Fan Zhang1bc7ac32017-06-20 14:53:49 -070023import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
Jake Hamby5e9bd862013-04-09 15:48:25 -070025import android.graphics.drawable.Drawable;
26import android.os.Bundle;
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070027import android.os.SystemProperties;
Fan Zhang1bc7ac32017-06-20 14:53:49 -070028import android.support.annotation.VisibleForTesting;
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070029import android.text.TextUtils;
Jake Hamby5e9bd862013-04-09 15:48:25 -070030import android.view.Gravity;
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070031import android.view.View;
32import android.widget.ImageView;
Jake Hamby5e9bd862013-04-09 15:48:25 -070033import android.widget.TextView;
34
Fan Zhang1bc7ac32017-06-20 14:53:49 -070035import java.util.Locale;
36
Jake Hamby5e9bd862013-04-09 15:48:25 -070037/**
38 * {@link Activity} that displays regulatory information for the "Regulatory information"
39 * preference item, and when "*#07#" is dialed on the Phone keypad. To enable this feature,
40 * set the "config_show_regulatory_info" boolean to true in a device overlay resource, and in the
41 * same overlay, either add a drawable named "regulatory_info.png" containing a graphical version
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070042 * of the required regulatory info (If ro.bootloader.hardware.sku property is set use
43 * "regulatory_info_<sku>.png where sku is ro.bootloader.hardware.sku property value in lowercase"),
44 * or add a string resource named "regulatory_info_text" with an HTML version of the required
45 * information (text will be centered in the dialog).
Jake Hamby5e9bd862013-04-09 15:48:25 -070046 */
47public class RegulatoryInfoDisplayActivity extends Activity implements
48 DialogInterface.OnDismissListener {
Fan Zhang1bc7ac32017-06-20 14:53:49 -070049
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070050 private final String REGULATORY_INFO_RESOURCE = "regulatory_info";
Fan Zhang1bc7ac32017-06-20 14:53:49 -070051 private static final String DEFAULT_REGULATORY_INFO_FILEPATH =
52 "/data/misc/elabel/regulatory_info.png";
53 private static final String REGULATORY_INFO_FILEPATH_TEMPLATE =
54 "/data/misc/elabel/regulatory_info_%s.png";
Jake Hamby5e9bd862013-04-09 15:48:25 -070055
56 /**
57 * Display the regulatory info graphic in a dialog window.
58 */
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 Resources resources = getResources();
63
64 if (!resources.getBoolean(R.bool.config_show_regulatory_info)) {
65 finish(); // no regulatory info to display for this device
66 }
67
68 AlertDialog.Builder builder = new AlertDialog.Builder(this)
Andrew Sapperstein1fad9af2016-05-31 10:34:01 -070069 .setTitle(R.string.regulatory_labels)
Jake Hamby5e9bd862013-04-09 15:48:25 -070070 .setOnDismissListener(this);
71
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070072 boolean regulatoryInfoDrawableExists = false;
Fan Zhang1bc7ac32017-06-20 14:53:49 -070073
74 final String regulatoryInfoFile = getRegulatoryInfoImageFileName();
75 final Bitmap regulatoryInfoBitmap = BitmapFactory.decodeFile(regulatoryInfoFile);
76
77 if (regulatoryInfoBitmap != null) {
78 regulatoryInfoDrawableExists = true;
79 }
80
81 int resId = 0;
82 if (!regulatoryInfoDrawableExists) {
83 resId = getResourceId();
84 }
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070085 if (resId != 0) {
86 try {
Alan Viverette0ba89bd2014-10-10 10:58:58 -070087 Drawable d = getDrawable(resId);
Vineeta Srivastavac26807f2013-08-28 17:17:57 -070088 // set to false if the width or height is <= 2
89 // (missing PNG can return an empty 2x2 pixel Drawable)
90 regulatoryInfoDrawableExists = (d.getIntrinsicWidth() > 2
91 && d.getIntrinsicHeight() > 2);
92 } catch (Resources.NotFoundException ignored) {
93 regulatoryInfoDrawableExists = false;
94 }
Jake Hamby5e9bd862013-04-09 15:48:25 -070095 }
96
97 CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
98
99 if (regulatoryInfoDrawableExists) {
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700100 View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700101 ImageView image = view.findViewById(R.id.regulatoryInfo);
102 if (regulatoryInfoBitmap != null) {
103 image.setImageBitmap(regulatoryInfoBitmap);
104 } else {
105 image.setImageResource(resId);
106 }
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700107 builder.setView(view);
Jake Hamby5e9bd862013-04-09 15:48:25 -0700108 builder.show();
109 } else if (regulatoryText.length() > 0) {
110 builder.setMessage(regulatoryText);
111 AlertDialog dialog = builder.show();
112 // we have to show the dialog first, or the setGravity() call will throw a NPE
113 TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
114 messageText.setGravity(Gravity.CENTER);
115 } else {
116 // neither drawable nor text resource exists, finish activity
117 finish();
118 }
119 }
120
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700121 private int getResourceId() {
122 // Use regulatory_info by default.
123 int resId = getResources().getIdentifier(
124 REGULATORY_INFO_RESOURCE, "drawable", getPackageName());
125
126 // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700127 final String sku = getSku();
Vineeta Srivastavac26807f2013-08-28 17:17:57 -0700128 if (!TextUtils.isEmpty(sku)) {
129 String regulatory_info_res = REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase();
130 int id = getResources().getIdentifier(
131 regulatory_info_res, "drawable", getPackageName());
132 if (id != 0) {
133 resId = id;
134 }
135 }
136 return resId;
137 }
138
Jake Hamby5e9bd862013-04-09 15:48:25 -0700139 @Override
140 public void onDismiss(DialogInterface dialog) {
141 finish(); // close the activity
142 }
Fan Zhang1bc7ac32017-06-20 14:53:49 -0700143
144 @VisibleForTesting
145 public static String getSku() {
146 return SystemProperties.get("ro.boot.hardware.sku", "");
147 }
148
149 @VisibleForTesting
150 public static String getRegulatoryInfoImageFileName() {
151 final String sku = getSku();
152 if (TextUtils.isEmpty(sku)) {
153 return DEFAULT_REGULATORY_INFO_FILEPATH;
154 } else {
155 return String.format(Locale.US, REGULATORY_INFO_FILEPATH_TEMPLATE,
156 sku.toLowerCase());
157 }
158 }
Jake Hamby5e9bd862013-04-09 15:48:25 -0700159}