15e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby/*
25e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * Copyright (C) 2013 The Android Open Source Project
35e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby *
45e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * Licensed under the Apache License, Version 2.0 (the "License");
55e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * you may not use this file except in compliance with the License.
65e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * You may obtain a copy of the License at
75e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby *
85e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby *      http://www.apache.org/licenses/LICENSE-2.0
95e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby *
105e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * Unless required by applicable law or agreed to in writing, software
115e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * distributed under the License is distributed on an "AS IS" BASIS,
125e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * See the License for the specific language governing permissions and
145e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * limitations under the License.
155e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby */
165e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
175e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambypackage com.android.settings;
185e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
195e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.app.Activity;
205e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.app.AlertDialog;
215e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.content.DialogInterface;
225e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.content.res.Resources;
235e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.graphics.drawable.Drawable;
245e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.os.Bundle;
25c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastavaimport android.os.SystemProperties;
26c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastavaimport android.text.TextUtils;
275e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.view.Gravity;
28c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastavaimport android.view.View;
29c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastavaimport android.widget.ImageView;
305e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambyimport android.widget.TextView;
315e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
325e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby/**
335e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * {@link Activity} that displays regulatory information for the "Regulatory information"
345e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * preference item, and when "*#07#" is dialed on the Phone keypad. To enable this feature,
355e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * set the "config_show_regulatory_info" boolean to true in a device overlay resource, and in the
365e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby * same overlay, either add a drawable named "regulatory_info.png" containing a graphical version
37c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava * of the required regulatory info (If ro.bootloader.hardware.sku property is set use
38c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava * "regulatory_info_<sku>.png where sku is ro.bootloader.hardware.sku property value in lowercase"),
39c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava * or add a string resource named "regulatory_info_text" with an HTML version of the required
40c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava * information (text will be centered in the dialog).
415e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby */
425e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hambypublic class RegulatoryInfoDisplayActivity extends Activity implements
435e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        DialogInterface.OnDismissListener {
44c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava    private final String REGULATORY_INFO_RESOURCE = "regulatory_info";
455e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
465e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    /**
475e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby     * Display the regulatory info graphic in a dialog window.
485e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby     */
495e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    @Override
505e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    protected void onCreate(Bundle savedInstanceState) {
515e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        super.onCreate(savedInstanceState);
525e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        Resources resources = getResources();
535e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
545e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        if (!resources.getBoolean(R.bool.config_show_regulatory_info)) {
555e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            finish();   // no regulatory info to display for this device
565e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        }
575e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
585e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        AlertDialog.Builder builder = new AlertDialog.Builder(this)
595e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby                .setTitle(R.string.regulatory_information)
605e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby                .setOnDismissListener(this);
615e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
62c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        boolean regulatoryInfoDrawableExists = false;
63c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        int resId = getResourceId();
64c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        if (resId != 0) {
65c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            try {
66c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                Drawable d = resources.getDrawable(resId);
67c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                // set to false if the width or height is <= 2
68c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                // (missing PNG can return an empty 2x2 pixel Drawable)
69c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                regulatoryInfoDrawableExists = (d.getIntrinsicWidth() > 2
70c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                        && d.getIntrinsicHeight() > 2);
71c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            } catch (Resources.NotFoundException ignored) {
72c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                regulatoryInfoDrawableExists = false;
73c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            }
745e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        }
755e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
765e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        CharSequence regulatoryText = resources.getText(R.string.regulatory_info_text);
775e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
785e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        if (regulatoryInfoDrawableExists) {
79c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            View view = getLayoutInflater().inflate(R.layout.regulatory_info, null);
80c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            ImageView image = (ImageView) view.findViewById(R.id.regulatoryInfo);
81c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            image.setImageResource(resId);
82c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            builder.setView(view);
835e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            builder.show();
845e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        } else if (regulatoryText.length() > 0) {
855e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            builder.setMessage(regulatoryText);
865e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            AlertDialog dialog = builder.show();
875e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            // we have to show the dialog first, or the setGravity() call will throw a NPE
885e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            TextView messageText = (TextView) dialog.findViewById(android.R.id.message);
895e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            messageText.setGravity(Gravity.CENTER);
905e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        } else {
915e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            // neither drawable nor text resource exists, finish activity
925e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby            finish();
935e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        }
945e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    }
955e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby
96c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava    private int getResourceId() {
97c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        // Use regulatory_info by default.
98c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        int resId = getResources().getIdentifier(
99c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                REGULATORY_INFO_RESOURCE, "drawable", getPackageName());
100c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava
101c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        // When hardware sku property exists, use regulatory_info_<sku> resource if valid.
102c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        String sku = SystemProperties.get("ro.boot.hardware.sku", "");
103c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        if (!TextUtils.isEmpty(sku)) {
104c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            String regulatory_info_res = REGULATORY_INFO_RESOURCE + "_" + sku.toLowerCase();
105c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            int id = getResources().getIdentifier(
106c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                    regulatory_info_res, "drawable", getPackageName());
107c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            if (id != 0) {
108c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava                resId = id;
109c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava            }
110c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        }
111c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava        return resId;
112c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava    }
113c26807f20b604b73c913cca74f2c07e119f96bb9Vineeta Srivastava
1145e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    @Override
1155e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    public void onDismiss(DialogInterface dialog) {
1165e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby        finish();   // close the activity
1175e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby    }
1185e9bd86bc6f89b1a743b04d69e793638ffb8cb41Jake Hamby}
119