2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.DialogInterface;
22 import android.content.res.Resources;
23 import android.graphics.Bitmap;
24 import android.graphics.BitmapFactory;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.os.SystemProperties;
28 import android.support.annotation.VisibleForTesting;
29 import android.text.TextUtils;
30 import android.view.Gravity;
31 import android.view.View;
32 import android.widget.ImageView;
33 import android.widget.TextView;
35 import java.util.Locale;
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
42 * 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).
47 public class RegulatoryInfoDisplayActivity extends Activity implements
48 DialogInterface.OnDismissListener {
50 private final String REGULATORY_INFO_RESOURCE = "regulatory_info";
51 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";
57 * Display the regulatory info graphic in a dialog window.
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 Resources resources = getResources();
64 if (!resources.getBoolean(R.bool.config_show_regulatory_info)) {
65 finish(); // no regulatory info to display for this device
68 AlertDialog.Builder builder = new AlertDialog.Builder(this)
69 .setTitle(R.string.regulatory_labels)
70 .setOnDismissListener(this);
72 boolean regulatoryInfoDrawableExists = false;
74 final String regulatoryInfoFile = getRegulatoryInfoImageFileName();
75 final Bitmap regulatoryInfoBitmap = BitmapFactory.decodeFile(regulatoryInfoFile);
77 if (regulatoryInfoBitmap != null) {
78 regulatoryInfoDrawableExists = true;
82 if (!regulatoryInfoDrawableExists) {
83 resId = getResourceId();
87 Drawable d = getDrawable(resId);
88 // 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;
97 CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
99 if (regulatoryInfoDrawableExists) {
100 View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
101 ImageView image = view.findViewById(R.id.regulatoryInfo);
102 if (regulatoryInfoBitmap != null) {
103 image.setImageBitmap(regulatoryInfoBitmap);
105 image.setImageResource(resId);
107 builder.setView(view);
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);
116 // neither drawable nor text resource exists, finish activity
121 private int getResourceId() {
122 // Use regulatory_info by default.
123 int resId = getResources().getIdentifier(
124 REGULATORY_INFO_RESOURCE, "drawable", getPackageName());
126 // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
127 final String sku = getSku();
128 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());
140 public void onDismiss(DialogInterface dialog) {
141 finish(); // close the activity
145 public static String getSku() {
146 return SystemProperties.get("ro.boot.hardware.sku", "");
150 public static String getRegulatoryInfoImageFileName() {
151 final String sku = getSku();
152 if (TextUtils.isEmpty(sku)) {
153 return DEFAULT_REGULATORY_INFO_FILEPATH;
155 return String.format(Locale.US, REGULATORY_INFO_FILEPATH_TEMPLATE,