1 /* 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 17 package com.android.settings; 18 19 import android.app.Activity; 20 import android.content.DialogInterface; 21 import android.content.res.Resources; 22 import android.graphics.Bitmap; 23 import android.graphics.BitmapFactory; 24 import android.graphics.drawable.Drawable; 25 import android.os.Bundle; 26 import android.os.SystemProperties; 27 import android.text.TextUtils; 28 import android.view.Gravity; 29 import android.view.View; 30 import android.widget.ImageView; 31 import android.widget.TextView; 32 33 import androidx.annotation.VisibleForTesting; 34 import androidx.appcompat.app.AlertDialog; 35 36 import java.util.Locale; 37 38 /** 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 43 * 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). 47 */ 48 public class RegulatoryInfoDisplayActivity extends Activity implements 49 DialogInterface.OnDismissListener { 50 51 private final String REGULATORY_INFO_RESOURCE = "regulatory_info"; 52 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"; 56 57 /** 58 * Display the regulatory info graphic in a dialog window. 59 */ 60 @Override onCreate(Bundle savedInstanceState)61 protected void onCreate(Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 AlertDialog.Builder builder = new AlertDialog.Builder(this) 64 .setTitle(R.string.regulatory_labels) 65 .setOnDismissListener(this); 66 67 boolean regulatoryInfoDrawableExists = false; 68 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 } 80 if (resId != 0) { 81 try { 82 Drawable d = getDrawable(resId); 83 // 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 } 90 } 91 92 CharSequence regulatoryText = getResources() 93 .getText(R.string.regulatory_info_text); 94 95 if (regulatoryInfoDrawableExists) { 96 View view = getLayoutInflater().inflate(R.layout.regulatory_info, null); 97 ImageView image = view.findViewById(R.id.regulatoryInfo); 98 if (regulatoryInfoBitmap != null) { 99 image.setImageBitmap(regulatoryInfoBitmap); 100 } else { 101 image.setImageResource(resId); 102 } 103 builder.setView(view); 104 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 117 @VisibleForTesting getResourceId()118 int getResourceId() { 119 // 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. 124 final String sku = getSku(); 125 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 } 133 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 } 145 return resId; 146 } 147 148 @Override onDismiss(DialogInterface dialog)149 public void onDismiss(DialogInterface dialog) { 150 finish(); // close the activity 151 } 152 getCoo()153 private String getCoo() { 154 return SystemProperties.get("ro.boot.hardware.coo", ""); 155 } 156 getSku()157 private String getSku() { 158 return SystemProperties.get("ro.boot.hardware.sku", ""); 159 } 160 getRegulatoryInfoImageFileName()161 private String getRegulatoryInfoImageFileName() { 162 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 } 170 } 171