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; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.DialogInterface; |
| 22 | import android.content.res.Resources; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.BitmapFactory; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.os.Bundle; |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 27 | import android.os.SystemProperties; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 28 | import android.support.annotation.VisibleForTesting; |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 30 | import android.view.Gravity; |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.widget.ImageView; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 35 | import java.util.Locale; |
| 36 | |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 37 | /** |
| 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 Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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). |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 46 | */ |
| 47 | public class RegulatoryInfoDisplayActivity extends Activity implements |
| 48 | DialogInterface.OnDismissListener { |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 49 | |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 50 | private final String REGULATORY_INFO_RESOURCE = "regulatory_info"; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 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"; |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 55 | |
| 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 Sapperstein | 1fad9af | 2016-05-31 10:34:01 -0700 | [diff] [blame] | 69 | .setTitle(R.string.regulatory_labels) |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 70 | .setOnDismissListener(this); |
| 71 | |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 72 | boolean regulatoryInfoDrawableExists = false; |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 73 | |
| 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 Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 85 | if (resId != 0) { |
| 86 | try { |
Alan Viverette | 0ba89bd | 2014-10-10 10:58:58 -0700 | [diff] [blame] | 87 | Drawable d = getDrawable(resId); |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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; |
| 94 | } |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text); |
| 98 | |
| 99 | if (regulatoryInfoDrawableExists) { |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 100 | View view = getLayoutInflater().inflate(R.layout.regulatory_info, null); |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 101 | ImageView image = view.findViewById(R.id.regulatoryInfo); |
| 102 | if (regulatoryInfoBitmap != null) { |
| 103 | image.setImageBitmap(regulatoryInfoBitmap); |
| 104 | } else { |
| 105 | image.setImageResource(resId); |
| 106 | } |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 107 | builder.setView(view); |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 108 | 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 Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 121 | 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 Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 127 | final String sku = getSku(); |
Vineeta Srivastava | c26807f | 2013-08-28 17:17:57 -0700 | [diff] [blame] | 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()); |
| 132 | if (id != 0) { |
| 133 | resId = id; |
| 134 | } |
| 135 | } |
| 136 | return resId; |
| 137 | } |
| 138 | |
Jake Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public void onDismiss(DialogInterface dialog) { |
| 141 | finish(); // close the activity |
| 142 | } |
Fan Zhang | 1bc7ac3 | 2017-06-20 14:53:49 -0700 | [diff] [blame] | 143 | |
| 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 Hamby | 5e9bd86 | 2013-04-09 15:48:25 -0700 | [diff] [blame] | 159 | } |