Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 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; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 20 | import android.content.DialogInterface; |
| 21 | import android.content.res.Resources; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.BitmapFactory; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.os.Bundle; |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 26 | import android.os.SystemProperties; |
| 27 | import android.text.TextUtils; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 28 | import android.view.Gravity; |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.widget.ImageView; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 31 | import android.widget.TextView; |
| 32 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 33 | import androidx.annotation.VisibleForTesting; |
tmfang | 41ab6b4 | 2018-07-17 13:53:04 +0800 | [diff] [blame] | 34 | import androidx.appcompat.app.AlertDialog; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 35 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 36 | import java.util.Locale; |
| 37 | |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 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 |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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). |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 47 | */ |
| 48 | public class RegulatoryInfoDisplayActivity extends Activity implements |
| 49 | DialogInterface.OnDismissListener { |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 50 | |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 51 | private final String REGULATORY_INFO_RESOURCE = "regulatory_info"; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 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"; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 56 | |
| 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 Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 63 | AlertDialog.Builder builder = new AlertDialog.Builder(this) |
Andrew Sapperstein | 1fad9af | 2016-05-31 10:34:01 -0700 | [diff] [blame] | 64 | .setTitle(R.string.regulatory_labels) |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 65 | .setOnDismissListener(this); |
| 66 | |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 67 | boolean regulatoryInfoDrawableExists = false; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 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 | } |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 80 | if (resId != 0) { |
| 81 | try { |
Alan Viverette | 0ba89bd | 2014-10-10 10:58:58 -0700 | [diff] [blame] | 82 | Drawable d = getDrawable(resId); |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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 | } |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ryan Mitchell | 4de772f | 2019-10-08 15:58:22 -0700 | [diff] [blame] | 92 | CharSequence regulatoryText = getResources() |
| 93 | .getText(R.string.regulatory_info_text); |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 94 | |
| 95 | if (regulatoryInfoDrawableExists) { |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 96 | View view = getLayoutInflater().inflate(R.layout.regulatory_info, null); |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 97 | ImageView image = view.findViewById(R.id.regulatoryInfo); |
| 98 | if (regulatoryInfoBitmap != null) { |
| 99 | image.setImageBitmap(regulatoryInfoBitmap); |
| 100 | } else { |
| 101 | image.setImageResource(resId); |
| 102 | } |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 103 | builder.setView(view); |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 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 | |
Yanting Yang | 49b1bc1 | 2019-07-19 01:46:36 +0800 | [diff] [blame] | 117 | @VisibleForTesting |
| 118 | int getResourceId() { |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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. |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 124 | final String sku = getSku(); |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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 | } |
Yanting Yang | 49b1bc1 | 2019-07-19 01:46:36 +0800 | [diff] [blame] | 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 | } |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 145 | return resId; |
| 146 | } |
| 147 | |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public void onDismiss(DialogInterface dialog) { |
| 150 | finish(); // close the activity |
| 151 | } |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 152 | |
Yanting Yang | 49b1bc1 | 2019-07-19 01:46:36 +0800 | [diff] [blame] | 153 | private String getCoo() { |
| 154 | return SystemProperties.get("ro.boot.hardware.coo", ""); |
| 155 | } |
| 156 | |
| 157 | private String getSku() { |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 158 | return SystemProperties.get("ro.boot.hardware.sku", ""); |
| 159 | } |
| 160 | |
Yanting Yang | 49b1bc1 | 2019-07-19 01:46:36 +0800 | [diff] [blame] | 161 | private String getRegulatoryInfoImageFileName() { |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 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 | } |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 170 | } |