• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         Resources resources = getResources();
64 
65         if (!resources.getBoolean(R.bool.config_show_regulatory_info)) {
66             finish();   // no regulatory info to display for this device
67         }
68 
69         AlertDialog.Builder builder = new AlertDialog.Builder(this)
70                 .setTitle(R.string.regulatory_labels)
71                 .setOnDismissListener(this);
72 
73         boolean regulatoryInfoDrawableExists = false;
74 
75         final String regulatoryInfoFile = getRegulatoryInfoImageFileName();
76         final Bitmap regulatoryInfoBitmap = BitmapFactory.decodeFile(regulatoryInfoFile);
77 
78         if (regulatoryInfoBitmap != null) {
79             regulatoryInfoDrawableExists = true;
80         }
81 
82         int resId = 0;
83         if (!regulatoryInfoDrawableExists) {
84             resId = getResourceId();
85         }
86         if (resId != 0) {
87             try {
88                 Drawable d = getDrawable(resId);
89                 // set to false if the width or height is <= 2
90                 // (missing PNG can return an empty 2x2 pixel Drawable)
91                 regulatoryInfoDrawableExists = (d.getIntrinsicWidth() > 2
92                         && d.getIntrinsicHeight() > 2);
93             } catch (Resources.NotFoundException ignored) {
94                 regulatoryInfoDrawableExists = false;
95             }
96         }
97 
98         CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
99 
100         if (regulatoryInfoDrawableExists) {
101             View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
102             ImageView image = view.findViewById(R.id.regulatoryInfo);
103             if (regulatoryInfoBitmap != null) {
104                 image.setImageBitmap(regulatoryInfoBitmap);
105             } else {
106                 image.setImageResource(resId);
107             }
108             builder.setView(view);
109             builder.show();
110         } else if (regulatoryText.length() > 0) {
111             builder.setMessage(regulatoryText);
112             AlertDialog dialog = builder.show();
113             // we have to show the dialog first, or the setGravity() call will throw a NPE
114             TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
115             messageText.setGravity(Gravity.CENTER);
116         } else {
117             // neither drawable nor text resource exists, finish activity
118             finish();
119         }
120     }
121 
122     @VisibleForTesting
getResourceId()123     int getResourceId() {
124         // Use regulatory_info by default.
125         int resId = getResources().getIdentifier(
126                 REGULATORY_INFO_RESOURCE, "drawable", getPackageName());
127 
128         // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
129         final String sku = getSku();
130         if (!TextUtils.isEmpty(sku)) {
131             String regulatory_info_res = REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase();
132             int id = getResources().getIdentifier(
133                     regulatory_info_res, "drawable", getPackageName());
134             if (id != 0) {
135                 resId = id;
136             }
137         }
138 
139         // When hardware coo property exists, use regulatory_info_<sku>_<coo> resource if valid.
140         final String coo = getCoo();
141         if (!TextUtils.isEmpty(coo) && !TextUtils.isEmpty(sku)) {
142             final String regulatory_info_coo_res =
143                     REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase() + "_" + coo.toLowerCase();
144             final int id = getResources().getIdentifier(
145                     regulatory_info_coo_res, "drawable", getPackageName());
146             if (id != 0) {
147                 resId = id;
148             }
149         }
150         return resId;
151     }
152 
153     @Override
onDismiss(DialogInterface dialog)154     public void onDismiss(DialogInterface dialog) {
155         finish();   // close the activity
156     }
157 
getCoo()158     private String getCoo() {
159         return SystemProperties.get("ro.boot.hardware.coo", "");
160     }
161 
getSku()162     private String getSku() {
163         return SystemProperties.get("ro.boot.hardware.sku", "");
164     }
165 
getRegulatoryInfoImageFileName()166     private String getRegulatoryInfoImageFileName() {
167         final String sku = getSku();
168         if (TextUtils.isEmpty(sku)) {
169             return DEFAULT_REGULATORY_INFO_FILEPATH;
170         } else {
171             return String.format(Locale.US, REGULATORY_INFO_FILEPATH_TEMPLATE,
172                     sku.toLowerCase());
173         }
174     }
175 }
176