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
17package com.android.settings;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.DialogInterface;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.graphics.drawable.Drawable;
26import android.os.Bundle;
27import android.os.SystemProperties;
28import android.support.annotation.VisibleForTesting;
29import android.text.TextUtils;
30import android.view.Gravity;
31import android.view.View;
32import android.widget.ImageView;
33import android.widget.TextView;
34
35import java.util.Locale;
36
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
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).
46 */
47public class RegulatoryInfoDisplayActivity extends Activity implements
48        DialogInterface.OnDismissListener {
49
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";
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)
69                .setTitle(R.string.regulatory_labels)
70                .setOnDismissListener(this);
71
72        boolean regulatoryInfoDrawableExists = false;
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        }
85        if (resId != 0) {
86            try {
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;
94            }
95        }
96
97        CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
98
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);
104            } else {
105                image.setImageResource(resId);
106            }
107            builder.setView(view);
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
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.
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());
132            if (id != 0) {
133                resId = id;
134            }
135        }
136        return resId;
137    }
138
139    @Override
140    public void onDismiss(DialogInterface dialog) {
141        finish();   // close the activity
142    }
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    }
159}
160