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.drawable.Drawable;
24import android.os.Bundle;
25import android.os.SystemProperties;
26import android.text.TextUtils;
27import android.view.Gravity;
28import android.view.View;
29import android.widget.ImageView;
30import android.widget.TextView;
31
32/**
33 * {@link Activity} that displays regulatory information for the "Regulatory information"
34 * preference item, and when "*#07#" is dialed on the Phone keypad. To enable this feature,
35 * set the "config_show_regulatory_info" boolean to true in a device overlay resource, and in the
36 * same overlay, either add a drawable named "regulatory_info.png" containing a graphical version
37 * of the required regulatory info (If ro.bootloader.hardware.sku property is set use
38 * "regulatory_info_<sku>.png where sku is ro.bootloader.hardware.sku property value in lowercase"),
39 * or add a string resource named "regulatory_info_text" with an HTML version of the required
40 * information (text will be centered in the dialog).
41 */
42public class RegulatoryInfoDisplayActivity extends Activity implements
43        DialogInterface.OnDismissListener {
44    private final String REGULATORY_INFO_RESOURCE = "regulatory_info";
45
46    /**
47     * Display the regulatory info graphic in a dialog window.
48     */
49    @Override
50    protected void onCreate(Bundle savedInstanceState) {
51        super.onCreate(savedInstanceState);
52        Resources resources = getResources();
53
54        if (!resources.getBoolean(R.bool.config_show_regulatory_info)) {
55            finish();   // no regulatory info to display for this device
56        }
57
58        AlertDialog.Builder builder = new AlertDialog.Builder(this)
59                .setTitle(R.string.regulatory_information)
60                .setOnDismissListener(this);
61
62        boolean regulatoryInfoDrawableExists = false;
63        int resId = getResourceId();
64        if (resId != 0) {
65            try {
66                Drawable d = getDrawable(resId);
67                // set to false if the width or height is <= 2
68                // (missing PNG can return an empty 2x2 pixel Drawable)
69                regulatoryInfoDrawableExists = (d.getIntrinsicWidth() > 2
70                        && d.getIntrinsicHeight() > 2);
71            } catch (Resources.NotFoundException ignored) {
72                regulatoryInfoDrawableExists = false;
73            }
74        }
75
76        CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
77
78        if (regulatoryInfoDrawableExists) {
79            View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
80            ImageView image = (ImageView) view.findViewById(R.id.regulatoryInfo);
81            image.setImageResource(resId);
82            builder.setView(view);
83            builder.show();
84        } else if (regulatoryText.length() > 0) {
85            builder.setMessage(regulatoryText);
86            AlertDialog dialog = builder.show();
87            // we have to show the dialog first, or the setGravity() call will throw a NPE
88            TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
89            messageText.setGravity(Gravity.CENTER);
90        } else {
91            // neither drawable nor text resource exists, finish activity
92            finish();
93        }
94    }
95
96    private int getResourceId() {
97        // Use regulatory_info by default.
98        int resId = getResources().getIdentifier(
99                REGULATORY_INFO_RESOURCE, "drawable", getPackageName());
100
101        // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
102        String sku = SystemProperties.get("ro.boot.hardware.sku", "");
103        if (!TextUtils.isEmpty(sku)) {
104            String regulatory_info_res = REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase();
105            int id = getResources().getIdentifier(
106                    regulatory_info_res, "drawable", getPackageName());
107            if (id != 0) {
108                resId = id;
109            }
110        }
111        return resId;
112    }
113
114    @Override
115    public void onDismiss(DialogInterface dialog) {
116        finish();   // close the activity
117    }
118}
119